iterating on new nodes index
[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' => '<strong>Site Information</strong>:');
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' => 20, 'size' => 12);
17   $form['site:url'] = array('title' => 'URL', 'required' => TRUE,
18                             'maxlength' => 30, 'size' => 30);
19   $form['site:latitude'] = array('title' => 'Latitude', 'required' => FALSE,
20                                  'maxlength' => 10, 'size' => 10, 'type' => 'double');
21   $form['site:longitude'] = array('title' => 'Longitude', 'required' => FALSE,
22                                   'maxlength' => 10, 'size' => 10, 'type' => 'double');
23
24   $form['address:line1'] = array('title' => 'Address', 'required' => TRUE,
25                                  'maxlength' => 40, 'size' => 30,
26                                  'comment' => '<strong>Postal address</strong>:');
27   $form['address:line2'] = array('title' => 'Address (2)', 'required' => FALSE,
28                                  'maxlength' => 40, 'size' => 30);
29   $form['address:line3'] = array('title' => 'Address (3)', 'required' => FALSE,
30                                  'maxlength' => 40, 'size' => 30);
31   $form['address:city'] = array('title' => 'City', 'required' => TRUE,
32                                 'maxlength' => 20, 'size' => 20);
33   $form['address:postalcode'] = array('title' => 'Postal Code', 'required' => TRUE,
34                                       'maxlength' => 10, 'size' => 10);
35 # would have liked it *not* required but it is mandatory in the DB - sigh
36   $form['address:state'] = array('title' => 'State', 'required' => TRUE,
37                                  'maxlength' => 20, 'size' => 20);
38   $form['address:country'] = array('title' => 'Country', 'required' => TRUE,
39                                    'maxlength' => 20, 'size' => 20);
40
41   $form['pi:first_name'] = array('title' => 'PI First Name', 'required' => TRUE,
42                                  'maxlength' => 20, 'size' => 20,
43                                  'comment' => '<strong>Principal Investigator Information</strong>:');
44   $form['pi:last_name'] = array('title' => 'PI Last Name', 'required' => TRUE,
45                                 'maxlength' => 20, 'size' => 20);
46   $form['pi:title'] = array('title' => 'PI Title', 'required' => FALSE,
47                             'maxlength' => 6, 'size' => 6);
48   $form['pi:phone'] = array('title' => 'PI Phone', 'required' => TRUE,
49                             'maxlength' => 20, 'size' => 20);
50   $form['pi:email'] = array('title' => 'PI email', 'required' => TRUE,
51                             'maxlength' => 40, 'size' => 20);
52   if ($register_mode) {
53     $form['pi:password'] = array('title' => 'PI password', 'required' => TRUE,
54                                  'maxlength' => 20, 'size' => 20, 'type' => 'password');
55   }
56
57
58   if ($register_mode) {
59     // required for the following code
60     drupal_set_html_head('<script type="text/javascript" src="/planetlab/js/plc_functions.js"></script>');
61
62     $fill_from_pi_button = <<< EOF
63 <input type="button" value="Same as PI" onclick='copyValue("edit-pi:first_name","edit-tech:first_name");
64 copyValue("edit-pi:last_name","edit-tech:last_name");
65 copyValue("edit-pi:title","edit-tech:title");
66 copyValue("edit-pi:phone","edit-tech:phone");
67 copyValue("edit-pi:email","edit-tech:email");
68 copyValue("edit-pi:password","edit-tech:password")' 
69 </input> 
70 EOF;
71   }
72
73
74   $form['tech:first_name'] = array('title' => 'Tech First Name', 'required' => TRUE,
75                                    'maxlength' => 20, 'size' => 20,
76                                    'comment' => '<strong>Technical Contact Information</strong>:' . $fill_from_pi_button);
77   $form['tech:last_name'] = array('title' => 'Tech Last Name', 'required' => TRUE,
78                                   'maxlength' => 20, 'size' => 20);
79   $form['tech:title'] = array('title' => 'Tech Title', 'required' => FALSE,
80                               'maxlength' => 6, 'size' => 6);
81   $form['tech:phone'] = array('title' => 'Tech Phone', 'required' => TRUE,
82                             'maxlength' => 20, 'size' => 20);
83   $form['tech:email'] = array('title' => 'Tech email', 'required' => TRUE,
84                               'maxlength' => 40, 'size' => 20);
85   if ($register_mode) {
86     $form['tech:password'] = array('title' => 'Tech password', 'required' => TRUE,
87                                    'maxlength' => 20, 'size' => 20, 'type' => 'password');
88     $form['tech:user-role'] = array('type' => 'boolean', 'title' => 'Need user role', 'default' => TRUE);
89   }
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) = split(":",$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) = split(":",$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_table2 ($form, $input, $outline_missing) {
184
185   foreach ($form as $fullname => $item) {
186     
187     list($objname,$field) = split(":",$fullname);
188     
189     // render the comment field
190     if ( ! empty($item['comment'])) {
191       $comment=$item['comment'];
192       print "<tr><td colspan=2> $comment </td></tr>";
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     print "<tr>";
205     print <<<EOF
206       <td> <label class="$class" for="edit-$fullname">$title: $required</label> </td>\n
207 EOF;
208
209     // input part
210     if ($item['type'] == 'boolean') {
211       // compute boolean default : whether we have this in the request
212       if (array_key_exists($field,$input[$objname])) {
213         $default = $input[$objname][$field] ;
214       } else { // or not - in which case we use the form default
215         $default = ($item['default'] == TRUE);
216       }
217       if ($default) {
218         $checkedyes = "checked='checked'";
219         $checkedno = "";
220       } else {
221         $checkedyes = "";
222         $checkedno = "checked='checked'";
223       }
224       print <<< EOF
225 <td>
226 <input type='radio' id="check-$fullname" name="$fullname" value="yes" $checkedyes> Yes
227 <input type='radio' id="check-$fullname" name="$fullname" value="no"  $checkedno> No
228 </td>\n
229 EOF;
230     } else {
231       $type = ($item['type'] == 'password') ? "password" : "text";
232       $value = !empty($input[$objname][$field]) ? $input[$objname][$field] : "";
233       $maxlength = $item['maxlength'];
234       $size = $item['size'];
235       print <<<EOF
236 <td><input type="$type" id="edit-$fullname" name="$fullname" value="$value" 
237 size="$size" maxlength="$maxlength" 
238 class="form-text $class" /> </td>\n
239 EOF;
240     }
241
242     print "</tr>\n";
243   }
244 }
245
246 ?>