3 // Drupalish, but does not use Drupal itself to generate the form
5 require_once 'plc_functions.php';
7 function build_site_form ($register_mode) {
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');
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);
46 $form['pi:password'] = array(
47 'title' => 'PI password', 'required' => TRUE,
48 'maxlength' => 20, 'size' => 20, 'type' => 'password');
53 // required for the following code
54 drupal_set_html_head('<script type="text/javascript" src="/planetlab/sites/site_form.js"></script>');
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")'
68 $form['tech:first_name'] = array(
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);
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);
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);
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
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
129 function parse_form ($form, $request, $input = NULL) {
130 if (empty ($input)) {
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)) {
141 // implement type conversion
142 switch ($item['type']) {
144 $input[$objname][$field] = doubleval(trim($raw_input));
147 $input[$objname][$field] = intval(trim($raw_input));
150 $input[$objname][$field] = ($raw_input=="yes");
154 $input[$objname][$field] = $raw_input;
157 $input[$objname][$field] = trim($raw_input);
161 switch ($item['type']) {
163 $input[$objname][$field] = 0.0;
166 $input[$objname][$field] = 0;
169 if (array_key_exists($field,$request)) {
170 $input[$objname][$field]=FALSE;
174 $input[$objname][$field] = '';
180 $input['is_empty'] = $empty_form;
185 // checks all required fields are filled
186 // returns a - possibly empty - html error string
187 function form_check_required ($form, $input) {
189 foreach ($form as $fullname => $item) {
190 list($objname, $field) = explode(":", $fullname);
191 if ($item['required'] && empty($input[$objname][$field])) {
192 $missing[] = $item['title'];
198 foreach ($missing as $field) {
199 $error .= "<li>Field '$field' is required.</li>";
206 // some additional checks to stay clear of some robots triggering
207 // site registration storms
208 // would detect an error if any of the folowing fields are
209 // only numbers (the attack typically fills everything with '1')
210 // besides, need to dig a bit because
211 // the site registration that we get can't be Deleted
212 // because some fields are missing (PI Phone and Tech Phone for example)
213 function form_check_non_numeric($form, $input) {
214 $to_check = ["name", "login_base", "abbreviated_name", "url"];
215 $only_numeric = array();
217 foreach ($form as $fullname => $item) {
218 list($objname, $field) = explode(":", $fullname);
219 if (in_array($field, $to_check) && is_numeric($input[$objname][$field])) {
220 $only_numeric[] = $item['title'];
223 if (empty($only_numeric))
226 foreach ($only_numeric as $field) {
227 $error .= "<li> Field '$field' can not be only numeric.</li>";
233 // displays the actual form, with values from $input
234 // if $outline_missing is set, missing required fields are outlined
235 // fields typed as 'password' are displayed differently
236 // expected to be embedded in a table with 2 columns
237 function form_render_details ($details, $site_form, $input, $outline_missing) {
239 foreach ($site_form as $fullname => $item) {
241 list($objname,$field) = explode(":",$fullname);
243 // render the comment field
244 if ( ! empty($item['comment'])) {
246 $details->tr ($item['comment'] . ":");
249 // compute line attributes
250 $title = $item['title'];
251 $required = $item['required'] ? '<span class="form-required" title="This field is required.">*</span>' : "";
252 $class = $item['required'] ? "required" : "";
253 if ($outline_missing && $item['required'] && empty($input[$objname][$field])) {
258 $left_part = "<label class='$class' for='edit-$fullname'>$title: $required</label>";
261 if ($item['type'] == 'boolean') {
262 // compute boolean default : whether we have this in the request
263 if (array_key_exists($field,$input[$objname])) {
264 $default = $input[$objname][$field] ;
265 } else { // or not - in which case we use the form default
266 $default = ($item['default'] == TRUE);
269 $checkedyes = "checked='checked'";
273 $checkedno = "checked='checked'";
276 <input type='radio' id="check-$fullname" name="$fullname" value="yes" $checkedyes> Yes
277 <input type='radio' id="check-$fullname" name="$fullname" value="no" $checkedno> No
280 $type = ($item['type'] == 'password') ? "password" : "text";
281 $value = !empty($input[$objname][$field]) ? $input[$objname][$field] : "";
282 $maxlength = $item['maxlength'];
283 $size = $item['size'];
285 <input type="$type" id="edit-$fullname" name="$fullname" value="$value"
286 size="$size" maxlength="$maxlength"
287 class="form-text $class" />
291 $details->th_td($left_part,$right_part);