4 // Drupalish, but does not use Drupal itself to generate the form
6 require_once 'plc_functions.php';
8 function build_site_form ($register_mode) {
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');
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);
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);
53 $form['pi:password'] = array('title' => 'PI password', 'required' => TRUE,
54 'maxlength' => 20, 'size' => 20, 'type' => 'password');
59 // required for the following code
60 drupal_set_html_head('<script type="text/javascript" src="/planetlab/sites/site_form.js"></script>');
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")'
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);
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);
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
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
103 function parse_form ($form, $request, $input = NULL) {
104 if (empty ($input)) {
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)) {
115 // implement type conversion
116 switch ($item['type']) {
118 $input[$objname][$field] = doubleval(trim($raw_input));
121 $input[$objname][$field] = intval(trim($raw_input));
124 $input[$objname][$field] = ($raw_input=="yes");
128 $input[$objname][$field] = $raw_input;
131 $input[$objname][$field] = trim($raw_input);
135 switch ($item['type']) {
137 $input[$objname][$field] = 0.0;
140 $input[$objname][$field] = 0;
143 if (array_key_exists($field,$request)) {
144 $input[$objname][$field]=FALSE;
148 $input[$objname][$field] = '';
154 $input['is_empty'] = $empty_form;
159 // checks all required fields are filled
160 // returns a - possibly empty - html error string
161 function form_check_required ($form, $input) {
163 foreach ($form as $fullname => $item) {
164 list($objname,$field) = split(":",$fullname);
165 if ($item['required'] && empty($input[$objname][$field])) {
166 $missing[] = $item['title'];
172 foreach ($missing as $field) {
173 $error .= "<li>Field '$field' is required.</li>";
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) {
185 foreach ($site_form as $fullname => $item) {
187 list($objname,$field) = split(":",$fullname);
189 // render the comment field
190 if ( ! empty($item['comment'])) {
192 $details->tr ($item['comment'] . ":");
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])) {
204 $left_part = "<label class='$class' for='edit-$fullname'>$title: $required</label>";
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);
215 $checkedyes = "checked='checked'";
219 $checkedno = "checked='checked'";
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
226 $type = ($item['type'] == 'password') ? "password" : "text";
227 $value = !empty($input[$objname][$field]) ? $input[$objname][$field] : "";
228 $maxlength = $item['maxlength'];
229 $size = $item['size'];
231 <input type="$type" id="edit-$fullname" name="$fullname" value="$value"
232 size="$size" maxlength="$maxlength"
233 class="form-text $class" />
237 $details->th_td($left_part,$right_part);