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' => '<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');
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);
36 $form['pi:password'] = array('title' => 'PI password', 'required' => TRUE,
37 'maxlength' => 20, 'size' => 20, 'type' => 'password');
42 // required for the following code
43 drupal_set_html_head('<script type="text/javascript" src="/planetlab/sites/site_form.js"></script>');
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")'
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);
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);
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);
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) = explode(":",$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) = explode(":",$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) = explode(":",$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);