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