interface form
[plewww.git] / planetlab / includes / plc_forms.php
1 <?php
2
3 // $Id$
4
5 require_once 'plc_functions.php';
6
7 // the rationale behind having function names with _html is that
8 // the first functions that we had were actually printing the stuff instead of returning it
9 // so basically the foo (...) function should just do ``print (foo_html(...))''
10
11 class PlcForm {
12   // mandatory
13   var $url;
14   var $values; // a hash var=>value - default is empty array
15   var $method; // default is POST
16
17   function PlcForm ($full_url, $values, $method="POST") {
18     // so we can use the various l_* functions:
19     // we parse the url to extract var-values pairs, 
20     // and add them to the 'values' argument if any
21
22     // extract var=value settings from url if any
23     $split=split_url($full_url);
24     $this->url=$split['url'];
25     
26     $url_values=$split['values'];
27     if ( ! $values ) $values = array();
28     if ( $url_values ) $values=array_merge($values,$url_values);
29     $this->values=$values;
30
31     $this->method=$method;
32   }
33
34   function start () { print $this->start_html(); }
35   function start_html () {
36     $html="<form method=$this->method action='$this->url' enctype='multipart/form-data'>";
37     if ($this->values) 
38       foreach ($this->values as $key=>$value) 
39         $html .= $this->hidden_html($key,$value);
40     return $html;
41   }
42
43   function end() { print $this->end_html(); }
44   function end_html() { return "</form>"; }
45
46   static function hidden_html ($key,$value) {
47     return "<input type=hidden name='$key' value='$value'/>";  
48   }
49   static function checkbox_html ($name,$value,$selected=false) {
50     if ($selected) $xtra=" selected=selected";
51     return "<input type=checkbox name='$name' value='$value'$xtra/>";
52   }
53   static function submit_html ($name,$display) {
54     return "<input type=submit name='$name' value='$display'/>";
55   }
56   static function file_html ($name,$size) {
57     return "<input type=file name='$name' size=$size/>";
58   }
59   static function label_html ($name,$display) {
60     return "<label for=$name>$display </label>";
61   }
62   static function radio_html  ($id, $name, $value, $checked) {
63     $html="<input type='radio' id='$id' name='$name' value='$value'";
64     if ($checked) $html .= " checked='checked'";
65     $html .="/>";
66     return $html;
67   }
68   // options
69   // (*) width to set the text size
70   // (*) callbacks, e.g. onFocus=>'your javascript code'
71   static function text_html ($name,$value,$options=NULL) {
72     $default_options = array('width'=>20);
73     if ( ! $options) $options=array();
74     $options = array_merge($default_options,$options);
75     $html="<input type=text name='$name' value='$value'";
76     $html .= " size=" . $options['width'];
77     $cbs=array('onFocus','onSelect', 'onChange');
78     foreach ($cbs as $cb) {
79       if ($options[$cb])
80         $html .= " $cb='" . $options[$cb] . "'";
81     }
82     $html .= "/>";
83     return $html;
84   }
85   static function textarea_html ($name,$value,$cols,$rows) {
86     return "<textarea name='$name' cols=$cols rows=$rows>$value</textarea>";
87   }
88  
89   // selectors is an array of hashes with the following keys
90   // (*) display 
91   // (*) value : the value that the 'name' variable will be assigned
92   // (*) optional 'selected': the entry selected initially
93   // (*) optional 'disabled': the entry is displayed but not selectable
94   // options
95   // (*) id
96   // (*) label : displayed as the first option, with no value attached
97   // (*) autosubmit : equivalent to onChange=>'submit()'
98   // (*) standard callbacks
99
100   static function select_html ($name,$selectors,$options) {
101     if ( ! $options) $options=array();
102     if ( $options ['autosubmit'] ) $options['onChange']='submit()';
103     $html="";
104     $html.="<select name='$name'";
105     if ($options['id']) $html .= " id='" . $options['id'] . "'";
106     $cbs=array('onFocus','onSelect','onChange');
107     foreach ($cbs as $cb) {
108       if ($options[$cb])
109         $html .= " $cb='" . $options[$cb] . "'";
110     }
111     $html .= ">";
112     if ($options['label']) {
113       $encoded=htmlentities($options['label'],ENT_QUOTES);
114       $html.="<option selected=selected value=''>$encoded</option>";
115     }
116     foreach ($selectors as $selector) {
117       $display=htmlentities($selector['display'],ENT_QUOTES);
118       $value=$selector['value'];
119       $html .= "<option value='$value'";
120       if ($selector['selected']) $html .= " selected=selected";
121       if ($selector['disabled']) $html .= " disabled=disabled";
122       $html .= ">$display</option>\n";
123     }
124     $html .= "</select>";
125     return $html;
126   }
127
128   // helper function to handle role-oriented selectors
129   // because GetRoles does not correctly support filters, it's really painful to do this
130   static public function role_selectors($api,$role_ids=NULL,$current=NULL) {
131     function role_selector ($role) { return array('display'=>$role['name'],"value"=>$role['role_id']); }
132     function role_id ($role) { return $role['role_id']; }
133
134     $all_roles=$api->GetRoles();
135     if ( ! $role_ids)
136       $role_ids=array_map("role_id",$all_roles);
137
138     $selectors=array();
139     // preserve input order
140     foreach ($role_ids as $role_id) {
141       foreach ($all_roles as $all_role) {
142         if ($all_role['role_id'] == $role_id) {
143           $selector=role_selector($all_role);
144           if ($role_id == $current) 
145             $selector['selected']=true;
146           $selectors []= $selector;
147         }
148       }
149     }
150     return $selectors;
151   }
152
153   static public function role_selectors_excluding ($api,$exclude_role_ids=NULL,$current=NULL) {
154     if ( ! $exclude_role_ids ) $exclude_role_ids = array();
155     $all_roles=$api->GetRoles();
156     $role_ids = array();
157     foreach ($all_roles as $role) {
158       if ( ! in_array ($role['role_id'],$exclude_role_ids)) {
159         $role_ids [] = $role['role_id'];
160       }
161     }
162     return PlcForm::role_selectors($api,$role_ids,$current);    
163   }
164 }
165
166 // a form with a single button
167 class PlcFormButton extends PlcForm {
168   
169   var $button_id;
170   var $button_text;
171
172   function PlcFormButton ($full_url, $button_id, $button_text, $method="POST") {
173     $this->PlcForm($full_url,array(),$method);
174     $this->button_id=$button_id;
175     $this->button_text=$button_text;
176   }
177
178   function html () {
179     return 
180       $this->start_html() . 
181       $this->submit_html($this->button_id,$this->button_text).
182       $this->end_html();
183   }
184 }
185
186 ?>