cleanup for plc_sorts.php removal.
[plewww.git] / planetlab / sites / register.php
1 <?php
2 // $Id$
3 //
4 // Site registration and verification form. 
5 //
6 // Thierry Parmentelat -- INRIA 
7 // based on persons/register.php 
8 //
9
10 // Get session and API handles
11 require_once 'plc_session.php';
12 global $plc, $api, $adm;
13
14 // Print header
15 require_once 'plc_drupal.php';
16 drupal_set_title('New Site Registration');
17 include 'plc_header.php';
18
19 include 'site_form.php';
20
21 $verbose = FALSE;
22
23 // initialize objects
24 $objectnames = array ('site','pi','tech','address');
25
26 $site_form = build_site_form(TRUE);
27 $input = parse_form ($site_form, $_REQUEST, $input);
28 $empty_form = $input['is_empty'];
29
30 $site=$input['site'];
31 $pi=$input['pi'];
32 $tech=$input['tech'];
33 $address=$input['address'];
34
35 if (! $empty_form ) {
36   // Look for missing/blank entries
37   $error = form_check_required ($site_form, $input);
38   $messages= array();
39   $verboses= array();
40
41   if (empty($error)) {
42
43     // I considered using begin/commit
44     // but first it was incorrectly reporting errors - which I fixed
45     // second: you need site_id to perform AddSiteAddress
46     // but you cannot write
47     // begin()
48     // site_id=AddSite()
49     // AddSiteAddress(site_id,..)
50     // because site_id would not be evaluated at the time you use it
51     //
52     // and you cannot either write
53     // begin
54     // AddSite(..)
55     // AddSiteAddress('login_base',...)
56     // because if the first call fails because login_base is already taken, 
57     // then you're going to add the new address to the wrong site
58     // 
59     // the bottom line is, there's no advantage in using begin/commit at all
60     
61     // creating the site
62     $site['enabled']=FALSE;
63     $site_id=$adm->AddSite($site);
64     $api_error .= $adm->error();
65     if (empty($api_error)) {
66       $verboses [] = "Site created as disabled";
67     } else {
68       $error .= $api_error;
69     }
70   }
71   
72   if (empty($error)) {
73
74     // Thierry on august 22 2007
75     // use NotifySupport to send this message, and get rid of the fake account
76
77     $subject = "Site registration form : new site " . $site['name'];
78     // should use GetMessages and stuff, but I do not trust the way 
79     // templates are created in db-config, for upgrades notably
80     $template = <<< EOF
81 We received a site registration form for site name %s
82
83 To review, please visit https://%s:%d/db/sites/join_request.php?review=t&site_id=%d.
84 EOF;
85     $body=sprintf($template,$site['name'],PLC_WWW_HOST,PLC_WWW_SSL_PORT,$site_id);
86     $adm->NotifySupport($subject,$body);
87       
88     $messages [] = "Your registration request has been received.";
89     $messages [] = "A mail was sent to the operations team, your application should be processed shortly.";
90     $messages [] = "Upon approval, the PI will receive an information e-mail";
91     $messages [] = "Please send a message to " . PLC_MAIL_SUPPORT_ADDRESS . " if this request is not instructed within a few days.";
92
93   // creating address
94     $adm->AddSiteAddress($site_id,$address);
95     $api_error = $adm->error();
96     if (empty($api_error)) {
97       $verboses [] = "Address created";
98     } else {
99       $error .= $api_error;
100     }
101
102     // creating PI
103     // Thierry 23 august 2007
104     // avoid using a pre-existing federated account
105     $known_pi = $adm->GetPersons(array("email"=>$pi['email'],
106                                        "peer_id"=>NULL),array("person_id"));
107     if ($known_pi) {
108       $messages [] = " Note: PI was already known";
109       $pi_id=$known_pi[0]['person_id'];
110     } else {
111       $pi['enabled']=FALSE;
112       $pi_id=$adm->AddPerson($pi);
113       $api_error = $adm->error();
114       if (empty($api_error)) {
115         $verboses [] = "PI created as disabled</p>";
116       } else {
117         $error .= $api_error;
118       }
119     }
120     if ($adm->AddPersonToSite($pi_id,$site_id)) {
121       $verboses [] = "PI attached to new site";
122     }
123     if ($adm->AddRoleToPerson('pi',$pi_id)) {
124       $verboses [] = $pi['email'] . " granted PI role</p>";
125     }
126     
127     if ($pi['email'] == $tech['email']) {
128       // need to assign tech role so the registration request gets filled properly
129       if ($adm->AddRoleToPerson('tech',$pi_id)) {
130         $verboses [] = $pi['email'] . " granted Tech role</p>";
131       }
132     } else {
133       // creating TECH
134       $known_tech = $adm->GetPersons(array("email"=>$tech['email'],
135                                            "peer_id"=>NULL),array("person_id"));
136       if ($known_tech) {
137         $messages [] = " Note: Tech was already known";
138         $tech_id=$known_tech[0]['person_id'];
139       } else {
140         $tech['enabled']=FALSE;
141         $tech_id=$adm->AddPerson($tech);
142         $api_error = $adm->error();
143         if (empty($api_error)) {
144           $verboses [] = "Tech created as disabled</p>";
145         } else {
146           $error .= $api_error;
147         }
148       }
149       if ($adm->AddPersonToSite($tech_id,$site_id)) {
150         $verboses [] = "Tech attached to new site";
151       }
152       if ($adm->AddRoleToPerson('tech',$tech_id)) {
153         $verboses [] = $tech['email'] . " granted Tech role";
154       }
155       if ( ($tech['user-role']) && $adm->AddRoleToPerson('user',$tech_id) ) {
156         $verboses [] = $tech['email'] . " granted User role";
157       }
158     }
159   }
160     
161
162   // Show messages
163   if (!empty($messages)) {
164     print '<div class="messages status"><ul>';
165     foreach ($messages as $line) {
166       print "<li> $line";
167     }
168     print "</ul></div>";
169   }
170         
171   if ($verbose && !empty($verboses)) {
172     print '<div class="messages status"><ul>';
173     print "<p> Verbose messages</p>";
174     foreach ($verboses as $line) {
175       print "<li> $line";
176     }
177     print "</ul></div>";
178   }
179         
180   if (!empty($error)) {
181     print '<div class="messages error">' . $error . '</div>';
182   } else {
183     // to prevent resubmit
184     $site['site_id']="XXX";
185   }
186 }
187
188 $self = $_SERVER['PHP_SELF'];
189 if (!empty($_SERVER['QUERY_STRING'])) {
190   $self .= "?" . $_SERVER['QUERY_STRING'];
191 }
192
193 print <<<EOF
194 <div class="content">
195
196 <form action="$self" method="post">
197
198 <table border="0" width="100%" cellspacing="0" cellpadding="3">
199 EOF;
200
201 // Do not allow resubmits
202 if (empty($site['site_id'])) {
203   print '<tr><td colspan=2 align=center><input type="submit" name="op" value="Register"  class="form-submit" /></td></tr>';
204 }
205
206 form_render_table2 ($site_form, $input, ! $empty_form);
207
208 // Do not allow resubmits
209 if (empty($site['site_id'])) {
210   print '<tr><td colspan=2> &nbsp; </td></tr>';
211   print '<tr><td colspan=2 align=center><input type="submit" name="op" value="Register"  class="form-submit" /></td></tr>';
212 }
213
214 print "</table></form></div>";
215
216 include 'plc_footer.php';
217
218 ?>