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 __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
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 (get_array($options, 'selected'))
60 $options['selected'] = 'selected';
61 if (get_array($options, 'checked'))
62 $options['checked'] = 'checked';
64 foreach ($options as $key=>$value) {
65 if (in_array(strtolower($key), $names))
66 $html .= " $key='$value'";
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();
77 $html .= " type='$type' name='$name' value='$value'";
78 $html .= PlekitForm::attributes ($options);
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); }
91 static function label_html ($name,$display) {
92 return "<label for=$name>$display</label>";
94 static function textarea_html ($name,$value,$cols,$rows) {
95 return "<textarea name='$name' cols='$cols' rows='$rows'>$value</textarea>";
98 // selectors is an array of hashes with the following keys
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
105 // (*) label : displayed as the first option, with no value attached
106 // (*) autosubmit : equivalent to onChange=>'submit()'
107 // (*) standard callbacks
109 static function select_html ($name,$selectors,$options=NULL) {
110 if ( ! $options) $options=array();
111 if (get_array($options, 'autosubmit'))
112 $options['onChange']='submit()';
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] . "'";
123 if (get_array($options, 'label')) {
124 $encoded=htmlentities($options['label'],ENT_QUOTES);
125 $html.="<option selected=selected value=''>$encoded</option>";
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";
139 $html .= "</select>";
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']); }
148 // preserve input order
150 drupal_set_message('WARNING: empty roles in role_selectors');
152 foreach ($roles as $role) {
153 $selector=role_selector($role);
154 if ($role['role_id'] == $current_id)
155 $selector['selected']=true;
156 $selectors []= $selector;
164 // a form with a single button
165 class PlekitFormButton extends PlekitForm {
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;
178 $this->start_html() .
179 $this->submit_html($this->button_id,$this->button_text).