- PIs can add/remove tech and user roles
[plewww.git] / plekit / php / form.php
1 <?php
2
3 // $Id$
4
5 require_once 'plekit-utils.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 PlekitForm {
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 PlekitForm ($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=plekit_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     // make strict xhtml happy
32     $this->method=strtolower($method);
33   }
34
35   function start () { print $this->start_html(); }
36   function start_html () {
37     $html="<form method='$this->method' action='$this->url' enctype='multipart/form-data'>";
38     if ($this->values) 
39       foreach ($this->values as $key=>$value) 
40         $html .= $this->hidden_html($key,$value);
41     return $html;
42   }
43
44   function end() { print $this->end_html(); }
45   function end_html() { return "</form>"; }
46
47   static function attributes ($options) {
48     $html="";
49     $names=array('id','size','selected', 'checked',
50                  'onfocus','onselect', 'onchange', 
51                  'onkeyup', 'onmouseup', 'onclick', 'onsubmit');
52     if ($options['selected']) $options['selected']='selected';
53     if ($options['checked']) $options['checked']='checked';
54     if ($options) foreach ($options as $key=>$value) {
55         if (in_array(strtolower($key),$names)) 
56           $html .= " $key='$value'";
57       }
58     return $html;
59   }
60
61   // options
62   // (*) width to set the text size
63   // (*) callbacks, e.g. onFocus=>'your javascript code'
64   static function input_html ($type,$name,$value,$options=NULL) {
65     if ( ! $options) $options=array();
66     $html="<input";
67     $html .= " type='$type' name='$name' value='$value'";
68     $html .= PlekitForm::attributes ($options);
69     $html .= "/>";
70     return $html;
71   }
72
73   static function text_html ($name,$value, $options=NULL) {     return PlekitForm::input_html('text', $name, $value, $options); }
74   static function hidden_html ($name,$value, $options=NULL) {   return PlekitForm::input_html('hidden', $name, $value, $options); }
75   static function checkbox_html ($name,$value,$options=NULL) {  return PlekitForm::input_html('checkbox', $name, $value, $options); }
76   static function submit_html ($name,$value,$options=NULL) {    return PlekitForm::input_html('submit', $name, $value, $options); }
77   static function button_html ($name,$value,$options=NULL) {    return PlekitForm::input_html('button', $name, $value, $options); }
78   static function radio_html ($name,$value,$options=NULL) {     return PlekitForm::input_html('radio', $name, $value, $options); }
79   static function file_html ($name,$value,$options=NULL) {      return PlekitForm::input_html('file', $name, $value, $options); }
80
81   static function label_html ($name,$display) {
82     return "<label for=$name>$display</label>";
83   }
84   static function textarea_html ($name,$value,$cols,$rows) {
85     return "<textarea name='$name' cols='$cols' rows='$rows'>$value</textarea>";
86   }
87  
88   // selectors is an array of hashes with the following keys
89   // (*) display 
90   // (*) value : the value that the 'name' variable will be assigned
91   // (*) optional 'selected': the entry selected initially
92   // (*) optional 'disabled': the entry is displayed but not selectable
93   // options
94   // (*) id
95   // (*) label : displayed as the first option, with no value attached
96   // (*) autosubmit : equivalent to onChange=>'submit()'
97   // (*) standard callbacks
98
99   static function select_html ($name,$selectors,$options=NULL) {
100     if ( ! $options) $options=array();
101     if ( $options ['autosubmit'] ) $options['onChange']='submit()';
102     $html="";
103     $html.="<select name='$name'";
104     if ($options['id']) $html .= " id='" . $options['id'] . "'";
105     $cbs=array('onFocus','onSelect','onChange');
106     foreach ($cbs as $cb) {
107       if ($options[$cb])
108         $html .= " $cb='" . $options[$cb] . "'";
109     }
110     $html .= ">";
111     if ($options['label']) {
112       $encoded=htmlentities($options['label'],ENT_QUOTES);
113       $html.="<option selected=selected value=''>$encoded</option>";
114     }
115     if ($selectors) {
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     }
125     $html .= "</select>";
126     return $html;
127   }
128
129   // helper function to handle role-oriented selectors
130   // because GetRoles does not correctly support filters, it's really painful to do this
131   static public function role_selectors($api,$role_ids=NULL,$current=NULL) {
132     function role_selector ($role) { return array('display'=>$role['name'],"value"=>$role['role_id']); }
133     function role_id ($role) { return $role['role_id']; }
134
135     $all_roles=$api->GetRoles();
136     if ( ! $role_ids)
137       $role_ids=array_map("role_id",$all_roles);
138
139     $selectors=array();
140     // preserve input order
141     foreach ($role_ids as $role_id) {
142       foreach ($all_roles as $all_role) {
143         if ($all_role['role_id'] == $role_id) {
144           $selector=role_selector($all_role);
145           if ($role_id == $current) 
146             $selector['selected']=true;
147           $selectors []= $selector;
148         }
149       }
150     }
151     return $selectors;
152   }
153
154   static public function role_selectors_excluding ($api,$exclude_role_ids=NULL,$current=NULL) {
155     if ( ! $exclude_role_ids ) $exclude_role_ids = array();
156     $all_roles=$api->GetRoles();
157     $role_ids = array();
158     foreach ($all_roles as $role) {
159       if ( ! in_array ($role['role_id'],$exclude_role_ids)) {
160         $role_ids [] = $role['role_id'];
161       }
162     }
163     return PlekitForm::role_selectors($api,$role_ids,$current);    
164   }
165 }
166
167 // a form with a single button
168 class PlekitFormButton extends PlekitForm {
169   
170   var $button_id;
171   var $button_text;
172
173   function PlekitFormButton ($full_url, $button_id, $button_text, $method="POST") {
174     $this->PlekitForm($full_url,array(),$method);
175     $this->button_id=$button_id;
176     $this->button_text=$button_text;
177   }
178
179   function html () {
180     return 
181       $this->start_html() . 
182       $this->submit_html($this->button_id,$this->button_text).
183       $this->end_html();
184   }
185 }
186
187 ?>