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