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