5 require_once 'plekit-utils.php';
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(...))''
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
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
24 // extract var=value settings from url if any
25 $split=plekit_split_url($full_url);
26 $this->url=$split['url'];
28 $url_values=$split['values'];
29 if ( ! $values ) $values = array();
30 if ( $url_values ) $values=array_merge($values,$url_values);
31 $this->values=$values;
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'];
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'";
46 foreach ($this->values as $key=>$value)
47 $html .= $this->hidden_html($key,$value);
51 function end() { print $this->end_html(); }
52 function end_html() { return "</form>"; }
54 static function attributes ($options) {
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'";
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();
74 $html .= " type='$type' name='$name' value='$value'";
75 $html .= PlekitForm::attributes ($options);
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); }
88 static function label_html ($name,$display) {
89 return "<label for=$name>$display</label>";
91 static function textarea_html ($name,$value,$cols,$rows) {
92 return "<textarea name='$name' cols='$cols' rows='$rows'>$value</textarea>";
95 // selectors is an array of hashes with the following keys
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
102 // (*) label : displayed as the first option, with no value attached
103 // (*) autosubmit : equivalent to onChange=>'submit()'
104 // (*) standard callbacks
106 static function select_html ($name,$selectors,$options=NULL) {
107 if ( ! $options) $options=array();
108 if ( $options ['autosubmit'] ) $options['onChange']='submit()';
110 $html.="<select name='$name'";
111 if ($options['id']) $html .= " id='" . $options['id'] . "'";
112 $cbs=array('onFocus','onSelect','onChange');
113 foreach ($cbs as $cb) {
115 $html .= " $cb='" . $options[$cb] . "'";
118 if ($options['label']) {
119 $encoded=htmlentities($options['label'],ENT_QUOTES);
120 $html.="<option selected=selected value=''>$encoded</option>";
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";
132 $html .= "</select>";
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']); }
141 // preserve input order
143 drupal_set_message('WARNING: empty roles in role_selectors');
145 foreach ($roles as $role) {
146 $selector=role_selector($role);
147 if ($role['role_id'] == $current_id)
148 $selector['selected']=true;
149 $selectors []= $selector;
157 // a form with a single button
158 class PlekitFormButton extends PlekitForm {
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;
171 $this->start_html() .
172 $this->submit_html($this->button_id,$this->button_text).