88cc490e22a849219994e1b34a35bcd09066f08c
[plewww.git] / planetlab / sites / site_form.php
1 <?php
2 // $Id$
3
4 // Drupalish, but does not use Drupal itself to generate the form
5
6 require_once 'plc_functions.php';
7
8 function build_site_form ($register_mode) {
9   $form = array();
10   $form['site:name'] = array('title' => 'Site name', 'required' => TRUE,
11                              'maxlength' => 40, 'size' => 20,
12                              'comment' => '<span class="bold">Site Information</span>');
13   $form['site:login_base'] = array('title' => 'Login base', 'required' => TRUE,
14                                    'maxlength' => 16, 'size' => 10);
15   $form['site:abbreviated_name'] = array('title' => 'Abbreviated name', 'required' => TRUE,
16                                          'maxlength' => 40, 'size' => 20);
17   $form['site:url'] = array('title' => 'URL', 'required' => TRUE,
18                             'maxlength' => 128, 'size' => 30);
19   $form['site:latitude'] = array('title' => 'Latitude', 'required' => TRUE,
20                                  'maxlength' => 10, 'size' => 10, 'type' => 'double');
21   $form['site:longitude'] = array('title' => 'Longitude', 'required' => TRUE,
22                                   'maxlength' => 10, 'size' => 10, 'type' => 'double');
23
24   $form['pi:first_name'] = array('title' => 'PI First Name', 'required' => TRUE,
25                                  'maxlength' => 20, 'size' => 20,
26                                  'comment' => '<span class="bold">Principal Investigator Information</span>');
27   $form['pi:last_name'] = array('title' => 'PI Last Name', 'required' => TRUE,
28                                 'maxlength' => 20, 'size' => 20);
29   $form['pi:title'] = array('title' => 'PI Title', 'required' => FALSE,
30                             'maxlength' => 6, 'size' => 6);
31   $form['pi:phone'] = array('title' => 'PI Phone', 'required' => TRUE,
32                             'maxlength' => 20, 'size' => 20);
33   $form['pi:email'] = array('title' => 'PI email', 'required' => TRUE,
34                             'maxlength' => 40, 'size' => 20);
35   if ($register_mode) {
36     $form['pi:password'] = array('title' => 'PI password', 'required' => TRUE,
37                                  'maxlength' => 20, 'size' => 20, 'type' => 'password');
38   }
39
40
41   if ($register_mode) {
42     // required for the following code
43     drupal_set_html_head('<script type="text/javascript" src="/planetlab/sites/site_form.js"></script>');
44
45     $fill_from_pi_button = <<< EOF
46 <input type="button" value="Same as PI" onclick='copyValue("edit-pi:first_name","edit-tech:first_name");
47 copyValue("edit-pi:last_name","edit-tech:last_name");
48 copyValue("edit-pi:title","edit-tech:title");
49 copyValue("edit-pi:phone","edit-tech:phone");
50 copyValue("edit-pi:email","edit-tech:email");
51 copyValue("edit-pi:password","edit-tech:password")' 
52 </input> 
53 EOF;
54   }
55
56
57   $form['tech:first_name'] = array('title' => 'Tech First Name', 'required' => TRUE,
58                                    'maxlength' => 20, 'size' => 20,
59                                    'comment' => '<span class="bold">Technical Contact Information</span>' . $fill_from_pi_button);
60   $form['tech:last_name'] = array('title' => 'Tech Last Name', 'required' => TRUE,
61                                   'maxlength' => 20, 'size' => 20);
62   $form['tech:title'] = array('title' => 'Tech Title', 'required' => FALSE,
63                               'maxlength' => 6, 'size' => 6);
64   $form['tech:phone'] = array('title' => 'Tech Phone', 'required' => TRUE,
65                             'maxlength' => 20, 'size' => 20);
66   $form['tech:email'] = array('title' => 'Tech email', 'required' => TRUE,
67                               'maxlength' => 40, 'size' => 20);
68   if ($register_mode) {
69     $form['tech:password'] = array('title' => 'Tech password', 'required' => TRUE,
70                                    'maxlength' => 20, 'size' => 20, 'type' => 'password');
71     $form['tech:user-role'] = array('type' => 'boolean', 'title' => 'Need user role', 'default' => TRUE);
72   }
73
74   $form['address:line1'] = array('title' => 'Address', 'required' => FALSE,
75                                  'maxlength' => 40, 'size' => 30,
76                                  'comment' => '<span class="bold">Postal address</span>');
77   $form['address:line2'] = array('title' => 'Address (2)', 'required' => FALSE,
78                                  'maxlength' => 40, 'size' => 30);
79   $form['address:line3'] = array('title' => 'Address (3)', 'required' => FALSE,
80                                  'maxlength' => 40, 'size' => 30);
81   $form['address:city'] = array('title' => 'City', 'required' => FALSE,
82                                 'maxlength' => 20, 'size' => 20);
83   $form['address:postalcode'] = array('title' => 'Postal Code', 'required' => FALSE,
84                                       'maxlength' => 10, 'size' => 10);
85 # would have liked it *not* required but it is mandatory in the DB - sigh
86   $form['address:state'] = array('title' => 'State', 'required' => FALSE,
87                                  'maxlength' => 20, 'size' => 20);
88   $form['address:country'] = array('title' => 'Country', 'required' => FALSE,
89                                    'maxlength' => 20, 'size' => 20);
90
91   return $form;
92 }
93
94 // input : 
95 // $form : the form as defined above
96 // $request : usually $_REQUEST
97 // $input : a dict ('site'=>$site ..)
98 // takes the values from the request and fills $input accordingly
99 // output
100 // $input : the modified dict, with as many keys as form categories,
101 // + the 'is_empty' key that returns a boolean, FALSE if any field was set in the request
102
103 function parse_form ($form, $request, $input = NULL) {
104   if (empty ($input)) {
105     $input = array();
106   }
107   $empty_form = TRUE;
108
109   // fill with values form the form
110   foreach ($form as $fullname => $item) {
111     list($objname,$field) = explode(":",$fullname);
112     $raw_input=$request[$fullname];
113     if (!empty($raw_input)) {
114       $empty_form = FALSE;
115       // implement type conversion
116       switch ($item['type']) {
117       case 'double':
118         $input[$objname][$field] = doubleval(trim($raw_input));
119         break;
120       case 'int':
121         $input[$objname][$field] = intval(trim($raw_input));
122         break;
123       case 'boolean':
124         $input[$objname][$field] = ($raw_input=="yes");
125         break;
126       case 'password':
127       case 'raw':
128         $input[$objname][$field] = $raw_input;
129         break;
130       default:
131         $input[$objname][$field] = trim($raw_input);
132         break;
133       }
134     } else {
135       switch ($item['type']) {
136       case 'double':
137         $input[$objname][$field] = 0.0;
138         break;
139       case 'int':
140         $input[$objname][$field] = 0;
141         break;
142       case 'boolean':
143         if (array_key_exists($field,$request)) {
144           $input[$objname][$field]=FALSE;
145         }
146         break;
147       default:
148         $input[$objname][$field] = '';
149         break;
150       }
151     }
152   }
153
154   $input['is_empty'] = $empty_form;
155
156   return $input;
157 }
158
159 // checks all required fields are filled
160 // returns a - possibly empty - html error string
161 function form_check_required ($form, $input) {
162   $missing = array();
163   foreach ($form as $fullname => $item) {
164     list($objname,$field) = explode(":",$fullname);
165     if ($item['required'] && empty($input[$objname][$field])) {
166       $missing[] = $item['title'];
167     }
168   }
169   if (empty($missing)) 
170     return "";
171   $error = "<ul>";
172   foreach ($missing as $field) {
173     $error .= "<li>Field '$field' is required.</li>";
174   }
175   $error .= "</ul>";
176   return $error;
177 }
178
179 // displays the actual form, with values from $input
180 // if $outline_missing is set, missing required fields are outlined
181 // fields typed as 'password' are displayed differently
182 // expected to be embedded in a table with 2 columns
183 function form_render_details ($details, $site_form, $input, $outline_missing) {
184
185   foreach ($site_form as $fullname => $item) {
186     
187     list($objname,$field) = explode(":",$fullname);
188     
189     // render the comment field
190     if ( ! empty($item['comment'])) {
191       $details->space();
192       $details->tr ($item['comment'] . ":");
193     }
194
195     // compute line attributes
196     $title = $item['title'];
197     $required = $item['required'] ? '<span class="form-required" title="This field is required.">*</span>' : "";
198     $class = $item['required'] ? "required" : "";
199     if ($outline_missing && $item['required'] && empty($input[$objname][$field])) {
200       $class .= " error";
201     }
202
203     // Label part
204     $left_part = "<label class='$class' for='edit-$fullname'>$title: $required</label>";
205
206     // input part
207     if ($item['type'] == 'boolean') {
208       // compute boolean default : whether we have this in the request
209       if (array_key_exists($field,$input[$objname])) {
210         $default = $input[$objname][$field] ;
211       } else { // or not - in which case we use the form default
212         $default = ($item['default'] == TRUE);
213       }
214       if ($default) {
215         $checkedyes = "checked='checked'";
216         $checkedno = "";
217       } else {
218         $checkedyes = "";
219         $checkedno = "checked='checked'";
220       }
221       $right_part = <<<EOF
222 <input type='radio' id="check-$fullname" name="$fullname" value="yes" $checkedyes> Yes
223 <input type='radio' id="check-$fullname" name="$fullname" value="no"  $checkedno> No
224 EOF;
225     } else {
226       $type = ($item['type'] == 'password') ? "password" : "text";
227       $value = !empty($input[$objname][$field]) ? $input[$objname][$field] : "";
228       $maxlength = $item['maxlength'];
229       $size = $item['size'];
230       $right_part= <<<EOF
231 <input type="$type" id="edit-$fullname" name="$fullname" value="$value" 
232 size="$size" maxlength="$maxlength" 
233 class="form-text $class" /> 
234 EOF;
235     }
236
237     $details->th_td($left_part,$right_part);
238   }
239 }
240
241 ?>