5 require_once 'plc_functions.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 PlcForm ($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=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 $this->method=$method;
34 function start () { print $this->start_html(); }
35 function start_html () {
36 $html="<form method=$this->method action='$this->url' enctype='multipart/form-data'>";
38 foreach ($this->values as $key=>$value)
39 $html .= $this->hidden_html($key,$value);
43 function end() { print $this->end_html(); }
44 function end_html() { return "</form>"; }
46 static function attributes ($options) {
48 $names=array('id','size','selected', 'checked',
49 'onfocus','onselect', 'onchange',
50 'onkeyup', 'onmouseup', 'onclick', 'onsubmit');
51 if ($options['selected']) $options['selected']='selected';
52 if ($options['checked']) $options['checked']='checked';
53 if ($options) foreach ($options as $key=>$value) {
54 if (in_array(strtolower($key),$names))
55 $html .= " $key='$value'";
61 // (*) width to set the text size
62 // (*) callbacks, e.g. onFocus=>'your javascript code'
63 static function input_html ($type,$name,$value,$options=NULL) {
64 if ( ! $options) $options=array();
66 $html="<input type='$type' name='$name' value='$value'";
67 $html .= PlcForm::attributes ($options);
72 static function text_html ($name,$value, $options=NULL) { return PlcForm::input_html('text', $name, $value, $options); }
73 static function hidden_html ($name,$value, $options=NULL) { return PlcForm::input_html('hidden', $name, $value, $options); }
74 static function checkbox_html ($name,$value,$options=NULL) { return PlcForm::input_html('checkbox', $name, $value, $options); }
75 static function submit_html ($name,$value,$options=NULL) { return PlcForm::input_html('submit', $name, $value, $options); }
76 static function button_html ($name,$value,$options=NULL) { return PlcForm::input_html('button', $name, $value, $options); }
77 static function radio_html ($name,$value,$options=NULL) { return PlcForm::input_html('radio', $name, $value, $options); }
78 static function file_html ($name,$value,$options=NULL) { return PlcForm::input_html('file', $name, $value, $options); }
80 static function label_html ($name,$display) {
81 return "<label for=$name>$display</label>";
83 static function textarea_html ($name,$value,$cols,$rows) {
84 return "<textarea name='$name' cols=$cols rows=$rows>$value</textarea>";
87 // selectors is an array of hashes with the following keys
89 // (*) value : the value that the 'name' variable will be assigned
90 // (*) optional 'selected': the entry selected initially
91 // (*) optional 'disabled': the entry is displayed but not selectable
94 // (*) label : displayed as the first option, with no value attached
95 // (*) autosubmit : equivalent to onChange=>'submit()'
96 // (*) standard callbacks
98 static function select_html ($name,$selectors,$options=NULL) {
99 if ( ! $options) $options=array();
100 if ( $options ['autosubmit'] ) $options['onChange']='submit()';
102 $html.="<select name='$name'";
103 if ($options['id']) $html .= " id='" . $options['id'] . "'";
104 $cbs=array('onFocus','onSelect','onChange');
105 foreach ($cbs as $cb) {
107 $html .= " $cb='" . $options[$cb] . "'";
110 if ($options['label']) {
111 $encoded=htmlentities($options['label'],ENT_QUOTES);
112 $html.="<option selected=selected value=''>$encoded</option>";
114 foreach ($selectors as $selector) {
115 $display=htmlentities($selector['display'],ENT_QUOTES);
116 $value=$selector['value'];
117 $html .= "<option value='$value'";
118 if ($selector['selected']) $html .= " selected=selected";
119 if ($selector['disabled']) $html .= " disabled=disabled";
120 $html .= ">$display</option>\n";
122 $html .= "</select>";
126 // helper function to handle role-oriented selectors
127 // because GetRoles does not correctly support filters, it's really painful to do this
128 static public function role_selectors($api,$role_ids=NULL,$current=NULL) {
129 function role_selector ($role) { return array('display'=>$role['name'],"value"=>$role['role_id']); }
130 function role_id ($role) { return $role['role_id']; }
132 $all_roles=$api->GetRoles();
134 $role_ids=array_map("role_id",$all_roles);
137 // preserve input order
138 foreach ($role_ids as $role_id) {
139 foreach ($all_roles as $all_role) {
140 if ($all_role['role_id'] == $role_id) {
141 $selector=role_selector($all_role);
142 if ($role_id == $current)
143 $selector['selected']=true;
144 $selectors []= $selector;
151 static public function role_selectors_excluding ($api,$exclude_role_ids=NULL,$current=NULL) {
152 if ( ! $exclude_role_ids ) $exclude_role_ids = array();
153 $all_roles=$api->GetRoles();
155 foreach ($all_roles as $role) {
156 if ( ! in_array ($role['role_id'],$exclude_role_ids)) {
157 $role_ids [] = $role['role_id'];
160 return PlcForm::role_selectors($api,$role_ids,$current);
164 // a form with a single button
165 class PlcFormButton extends PlcForm {
170 function PlcFormButton ($full_url, $button_id, $button_text, $method="POST") {
171 $this->PlcForm($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).