f12d53e98d7a089fdd4e21d1ea588d3a268dfd2e
[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, can be changed with options
16   var $onSubmit; // can be set with options
17   var $onReset; // can be set with options
18
19   function PlekitForm ($full_url, $values, $options=NULL) {
20     // so we can use the various l_* functions:
21     // we parse the url to extract var-values pairs, 
22     // and add them to the 'values' argument if any
23
24     // extract var=value settings from url if any
25     $split=plekit_split_url($full_url);
26     $this->url=$split['url'];
27     
28     $url_values=$split['values'];
29     if ( ! $values ) $values = array();
30     if ( $url_values ) $values=array_merge($values,$url_values);
31     $this->values=$values;
32
33     // make strict xhtml happy
34     $this->method="post";       if ($options['method']) $this->method=strtolower($options['method']);
35     $this->onSubmit=NULL;       if ($options['onSubmit']) $this->onSubmit=$options['onSubmit'];
36     $this->onReset=NULL;        if ($options['onReset']) $this->onReset=$options['onReset'];
37   }
38
39   function start () { print $this->start_html(); }
40   function start_html () {
41     $html="<form method='$this->method' action='$this->url' enctype='multipart/form-data'";
42     if ($this->onSubmit) $html .= " onSubmit='$this->onSubmit'";
43     if ($this->onReset) $html .= " onReset='$this->onReset'";
44     $html .= ">";
45     if ($this->values) 
46       foreach ($this->values as $key=>$value) 
47         $html .= $this->hidden_html($key,$value);
48     return $html;
49   }
50
51   function end() { print $this->end_html(); }
52   function end_html() { return "</form>"; }
53
54   static function attributes ($options) {
55     $html="";
56     $names=array('id','size','selected', 'checked',
57                  'onfocus','onselect', 'onchange', 
58                  'onkeyup', 'onmouseup', 'onclick', 'onsubmit');
59     if ($options['selected']) $options['selected']='selected';
60     if ($options['checked']) $options['checked']='checked';
61     if ($options) foreach ($options as $key=>$value) {
62         if (in_array(strtolower($key),$names)) 
63           $html .= " $key='$value'";
64       }
65     return $html;
66   }
67
68   // options
69   // (*) width to set the text size
70   // (*) callbacks, e.g. onFocus=>'your javascript code'
71   static function input_html ($type,$name,$value,$options=NULL) {
72     if ( ! $options) $options=array();
73     $html="<input";
74     $html .= " type='$type' name='$name' value='$value'";
75     $html .= PlekitForm::attributes ($options);
76     $html .= "/>";
77     return $html;
78   }
79
80   static function text_html ($name,$value, $options=NULL) {     return PlekitForm::input_html('text', $name, $value, $options); }
81   static function hidden_html ($name,$value, $options=NULL) {   return PlekitForm::input_html('hidden', $name, $value, $options); }
82   static function checkbox_html ($name,$value,$options=NULL) {  return PlekitForm::input_html('checkbox', $name, $value, $options); }
83   static function submit_html ($name,$value,$options=NULL) {    return PlekitForm::input_html('submit', $name, $value, $options); }
84   static function button_html ($name,$value,$options=NULL) {    return PlekitForm::input_html('button', $name, $value, $options); }
85   static function radio_html ($name,$value,$options=NULL) {     return PlekitForm::input_html('radio', $name, $value, $options); }
86   static function file_html ($name,$value,$options=NULL) {      return PlekitForm::input_html('file', $name, $value, $options); }
87
88   static function label_html ($name,$display) {
89     return "<label for=$name>$display</label>";
90   }
91   static function textarea_html ($name,$value,$cols,$rows) {
92     return "<textarea name='$name' cols='$cols' rows='$rows'>$value</textarea>";
93   }
94  
95   // selectors is an array of hashes with the following keys
96   // (*) display 
97   // (*) value : the value that the 'name' variable will be assigned
98   // (*) optional 'selected': the entry selected initially
99   // (*) optional 'disabled': the entry is displayed but not selectable
100   // options
101   // (*) id
102   // (*) label : displayed as the first option, with no value attached
103   // (*) autosubmit : equivalent to onChange=>'submit()'
104   // (*) standard callbacks
105
106   static function select_html ($name,$selectors,$options=NULL) {
107     if ( ! $options) $options=array();
108     if ( $options ['autosubmit'] ) $options['onChange']='submit()';
109     $html="";
110     $html.="<select name='$name'";
111     if ($options['id']) $html .= " id='" . $options['id'] . "'";
112     $cbs=array('onFocus','onSelect','onChange');
113     foreach ($cbs as $cb) {
114       if ($options[$cb])
115         $html .= " $cb='" . $options[$cb] . "'";
116     }
117     $html .= ">";
118     if ($options['label']) {
119       $encoded=htmlentities($options['label'],ENT_QUOTES);
120       $html.="<option selected=selected value=''>$encoded</option>";
121     }
122     if ($selectors) {
123       foreach ($selectors as $selector) {
124         $display=htmlentities($selector['display'],ENT_QUOTES);
125         $value=$selector['value'];
126         $html .= "<option value='$value'";
127         if ($selector['selected']) $html .= " selected=selected";
128         if ($selector['disabled']) $html .= " disabled=disabled";
129         $html .= ">$display</option>\n";
130       }
131     }
132     $html .= "</select>";
133     return $html;
134   }
135
136   // helper function to handle role-oriented selectors
137   // because GetRoles does not correctly support filters, it's really painful to do this
138   static public function role_selectors($roles,$current_id=NULL) {
139     function role_selector ($role) { return array('display'=>$role['name'],"value"=>$role['role_id']); }
140     $selectors=array();
141     // preserve input order
142     if ( ! $roles) {
143       drupal_set_message('WARNING: empty roles in role_selectors');
144     } else {
145       foreach ($roles as $role) {
146         $selector=role_selector($role);
147         if ($role['role_id'] == $current_id) 
148             $selector['selected']=true;
149         $selectors []= $selector;
150       }
151     }
152     return $selectors;
153   }
154
155 }
156
157 // a form with a single button
158 class PlekitFormButton extends PlekitForm {
159   
160   var $button_id;
161   var $button_text;
162
163   function PlekitFormButton ($full_url, $button_id, $button_text, $method="POST") {
164     $this->PlekitForm($full_url,array(),$method);
165     $this->button_id=$button_id;
166     $this->button_text=$button_text;
167   }
168
169   function html () {
170     return 
171       $this->start_html() . 
172       $this->submit_html($this->button_id,$this->button_text).
173       $this->end_html();
174   }
175 }
176
177 ?>