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