9789bae583c3a238fd8d574302299b226d5fabe9
[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   static function text_html ($name,$value,$options=NULL) {
69     $default_options = array('width'=>20);
70     if ( ! $options) $options=array();
71     $options = array_merge($default_options,$options);
72     $html="<input type=text name='$name' value='$value'";
73     $html .= " size=" . $options['width'];
74     $cbs=array('onFocus','onSelect');
75     foreach ($cbs as $cb) {
76       if ($options[$cb])
77         $html .= " $cb='" . $options[$cb] . "'";
78     }
79     $html .= "/>";
80     return $html;
81   }
82   static function textarea_html ($name,$value,$cols,$rows) {
83     return "<textarea name='$name' cols=$cols rows=$rows>$value</textarea>";
84   }
85  
86   // selectors is an array of hashes with the following keys
87   // (*) display 
88   // (*) value : the value that the 'name' variable will be assigned
89   // (*) optional 'selected': the entry selected initially
90   // (*) optional 'disabled': the entry is displayed but not selectable
91   // optional label is inserted as the first option, with no value attached
92   // autosubmit: onchange=submit()
93   static function select_html ($name,$selectors,$label=NULL,$autosubmit=false) {
94     $html="";
95     $html.="<select name='$name'";
96     if ($autosubmit) $html .= " onChange='submit();'";
97     $html .= ">";
98     if ($label) {
99       $encoded=htmlentities($label,ENT_QUOTES);
100       $html.="<option selected=selected value=''>$encoded</option>";
101     }
102     foreach ($selectors as $selector) {
103       $display=htmlentities($selector['display'],ENT_QUOTES);
104       $value=$selector['value'];
105       $html .= "<option value='$value'";
106       if ($selector['selected']) $html .= " selected=selected";
107       if ($selector['disabled']) $html .= " disabled=disabled";
108       $html .= ">$display</option>\n";
109     }
110     $html .= "</select>";
111     return $html;
112   }
113
114   // helper function to handle role-oriented selectors
115   // because GetRoles does not correctly support filters, it's really painful to do this
116   static public function role_selectors($api,$role_ids=NULL,$current=NULL) {
117     function role_selector ($role) { return array('display'=>$role['name'],"value"=>$role['role_id']); }
118     function role_id ($role) { return $role['role_id']; }
119
120     $all_roles=$api->GetRoles();
121     if ( ! $role_ids)
122       $role_ids=array_map("role_id",$all_roles);
123
124     $selectors=array();
125     // preserve input order
126     foreach ($role_ids as $role_id) {
127       foreach ($all_roles as $all_role) {
128         if ($all_role['role_id'] == $role_id) {
129           $selector=role_selector($all_role);
130           if ($role_id == $current) 
131             $selector['selected']=true;
132           $selectors []= $selector;
133         }
134       }
135     }
136     return $selectors;
137   }
138
139   static public function role_selectors_excluding ($api,$exclude_role_ids=NULL,$current=NULL) {
140     if ( ! $exclude_role_ids ) $exclude_role_ids = array();
141     $all_roles=$api->GetRoles();
142     $role_ids = array();
143     foreach ($all_roles as $role) {
144       if ( ! in_array ($role['role_id'],$exclude_role_ids)) {
145         $role_ids [] = $role['role_id'];
146       }
147     }
148     return PlcForm::role_selectors($api,$role_ids,$current);    
149   }
150 }
151
152 // a form with a single button
153 class PlcFormButton extends PlcForm {
154   
155   var $button_id;
156   var $button_text;
157
158   function PlcFormButton ($full_url, $button_id, $button_text, $method="POST") {
159     $this->PlcForm($full_url,array(),$method);
160     $this->button_id=$button_id;
161     $this->button_text=$button_text;
162   }
163
164   function html () {
165     return 
166       $this->start_html() . 
167       $this->submit_html($this->button_id,$this->button_text).
168       $this->end_html();
169   }
170 }
171
172 ?>