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
17 function PlekitForm ($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
22 // extract var=value settings from url if any
23 $split=plekit_split_url($full_url);
24 $this->url=$split['url'];
26 $url_values=$split['values'];
27 if ( ! $values ) $values = array();
28 if ( $url_values ) $values=array_merge($values,$url_values);
29 $this->values=$values;
31 // make strict xhtml happy
32 $this->method=strtolower($method);
35 function start () { print $this->start_html(); }
36 function start_html () {
37 $html="<form method='$this->method' action='$this->url' enctype='multipart/form-data'>";
39 foreach ($this->values as $key=>$value)
40 $html .= $this->hidden_html($key,$value);
44 function end() { print $this->end_html(); }
45 function end_html() { return "</form>"; }
47 static function attributes ($options) {
49 $names=array('id','size','selected', 'checked',
50 'onfocus','onselect', 'onchange',
51 'onkeyup', 'onmouseup', 'onclick', 'onsubmit');
52 if ($options['selected']) $options['selected']='selected';
53 if ($options['checked']) $options['checked']='checked';
54 if ($options) foreach ($options as $key=>$value) {
55 if (in_array(strtolower($key),$names))
56 $html .= " $key='$value'";
62 // (*) width to set the text size
63 // (*) callbacks, e.g. onFocus=>'your javascript code'
64 static function input_html ($type,$name,$value,$options=NULL) {
65 if ( ! $options) $options=array();
67 $html .= " type='$type' name='$name' value='$value'";
68 $html .= PlekitForm::attributes ($options);
73 static function text_html ($name,$value, $options=NULL) { return PlekitForm::input_html('text', $name, $value, $options); }
74 static function hidden_html ($name,$value, $options=NULL) { return PlekitForm::input_html('hidden', $name, $value, $options); }
75 static function checkbox_html ($name,$value,$options=NULL) { return PlekitForm::input_html('checkbox', $name, $value, $options); }
76 static function submit_html ($name,$value,$options=NULL) { return PlekitForm::input_html('submit', $name, $value, $options); }
77 static function button_html ($name,$value,$options=NULL) { return PlekitForm::input_html('button', $name, $value, $options); }
78 static function radio_html ($name,$value,$options=NULL) { return PlekitForm::input_html('radio', $name, $value, $options); }
79 static function file_html ($name,$value,$options=NULL) { return PlekitForm::input_html('file', $name, $value, $options); }
81 static function label_html ($name,$display) {
82 return "<label for=$name>$display</label>";
84 static function textarea_html ($name,$value,$cols,$rows) {
85 return "<textarea name='$name' cols='$cols' rows='$rows'>$value</textarea>";
88 // selectors is an array of hashes with the following keys
90 // (*) value : the value that the 'name' variable will be assigned
91 // (*) optional 'selected': the entry selected initially
92 // (*) optional 'disabled': the entry is displayed but not selectable
95 // (*) label : displayed as the first option, with no value attached
96 // (*) autosubmit : equivalent to onChange=>'submit()'
97 // (*) standard callbacks
99 static function select_html ($name,$selectors,$options=NULL) {
100 if ( ! $options) $options=array();
101 if ( $options ['autosubmit'] ) $options['onChange']='submit()';
103 $html.="<select name='$name'";
104 if ($options['id']) $html .= " id='" . $options['id'] . "'";
105 $cbs=array('onFocus','onSelect','onChange');
106 foreach ($cbs as $cb) {
108 $html .= " $cb='" . $options[$cb] . "'";
111 if ($options['label']) {
112 $encoded=htmlentities($options['label'],ENT_QUOTES);
113 $html.="<option selected=selected value=''>$encoded</option>";
115 foreach ($selectors as $selector) {
116 $display=htmlentities($selector['display'],ENT_QUOTES);
117 $value=$selector['value'];
118 $html .= "<option value='$value'";
119 if ($selector['selected']) $html .= " selected=selected";
120 if ($selector['disabled']) $html .= " disabled=disabled";
121 $html .= ">$display</option>\n";
123 $html .= "</select>";
127 // helper function to handle role-oriented selectors
128 // because GetRoles does not correctly support filters, it's really painful to do this
129 static public function role_selectors($api,$role_ids=NULL,$current=NULL) {
130 function role_selector ($role) { return array('display'=>$role['name'],"value"=>$role['role_id']); }
131 function role_id ($role) { return $role['role_id']; }
133 $all_roles=$api->GetRoles();
135 $role_ids=array_map("role_id",$all_roles);
138 // preserve input order
139 foreach ($role_ids as $role_id) {
140 foreach ($all_roles as $all_role) {
141 if ($all_role['role_id'] == $role_id) {
142 $selector=role_selector($all_role);
143 if ($role_id == $current)
144 $selector['selected']=true;
145 $selectors []= $selector;
152 static public function role_selectors_excluding ($api,$exclude_role_ids=NULL,$current=NULL) {
153 if ( ! $exclude_role_ids ) $exclude_role_ids = array();
154 $all_roles=$api->GetRoles();
156 foreach ($all_roles as $role) {
157 if ( ! in_array ($role['role_id'],$exclude_role_ids)) {
158 $role_ids [] = $role['role_id'];
161 return PlekitForm::role_selectors($api,$role_ids,$current);
165 // a form with a single button
166 class PlekitFormButton extends PlekitForm {
171 function PlekitFormButton ($full_url, $button_id, $button_text, $method="POST") {
172 $this->PlekitForm($full_url,array(),$method);
173 $this->button_id=$button_id;
174 $this->button_text=$button_text;
179 $this->start_html() .
180 $this->submit_html($this->button_id,$this->button_text).