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 hidden_html ($key,$value) {
47 return "<input type=hidden name='$key' value='$value'/>";
49 static function checkbox_html ($name,$value,$selected=false) {
50 if ($selected) $xtra=" selected=selected";
51 return "<input type=checkbox name='$name' value='$value'$xtra/>";
53 static function submit_html ($name,$display) {
54 return "<input type=submit name='$name' value='$display'/>";
56 static function file_html ($name,$size) {
57 return "<input type=file name='$name' size=$size/>";
59 static function label_html ($name,$display) {
60 return "<label for=$name>$display </label>";
62 static function radio_html ($id, $name, $value, $checked) {
63 $html="<input type='radio' id='$id' name='$name' value='$value'";
64 if ($checked) $html .= " checked='checked'";
69 // (*) width to set the text size
70 // (*) callbacks, e.g. onFocus=>'your javascript code'
71 static function text_html ($name,$value,$options=NULL) {
72 $default_options = array('width'=>20);
73 if ( ! $options) $options=array();
74 $options = array_merge($default_options,$options);
75 $html="<input type=text name='$name' value='$value'";
76 $html .= " size=" . $options['width'];
77 $cbs=array('onFocus','onSelect', 'onChange');
78 foreach ($cbs as $cb) {
80 $html .= " $cb='" . $options[$cb] . "'";
85 static function textarea_html ($name,$value,$cols,$rows) {
86 return "<textarea name='$name' cols=$cols rows=$rows>$value</textarea>";
89 // selectors is an array of hashes with the following keys
91 // (*) value : the value that the 'name' variable will be assigned
92 // (*) optional 'selected': the entry selected initially
93 // (*) optional 'disabled': the entry is displayed but not selectable
96 // (*) label : displayed as the first option, with no value attached
97 // (*) autosubmit : equivalent to onChange=>'submit()'
98 // (*) standard callbacks
100 static function select_html ($name,$selectors,$options) {
101 if ( ! $options) $options=array();
102 if ( $options ['autosubmit'] ) $options['onChange']='submit()';
104 $html.="<select name='$name'";
105 if ($options['id']) $html .= " id='" . $options['id'] . "'";
106 $cbs=array('onFocus','onSelect','onChange');
107 foreach ($cbs as $cb) {
109 $html .= " $cb='" . $options[$cb] . "'";
112 if ($options['label']) {
113 $encoded=htmlentities($options['label'],ENT_QUOTES);
114 $html.="<option selected=selected value=''>$encoded</option>";
116 foreach ($selectors as $selector) {
117 $display=htmlentities($selector['display'],ENT_QUOTES);
118 $value=$selector['value'];
119 $html .= "<option value='$value'";
120 if ($selector['selected']) $html .= " selected=selected";
121 if ($selector['disabled']) $html .= " disabled=disabled";
122 $html .= ">$display</option>\n";
124 $html .= "</select>";
128 // helper function to handle role-oriented selectors
129 // because GetRoles does not correctly support filters, it's really painful to do this
130 static public function role_selectors($api,$role_ids=NULL,$current=NULL) {
131 function role_selector ($role) { return array('display'=>$role['name'],"value"=>$role['role_id']); }
132 function role_id ($role) { return $role['role_id']; }
134 $all_roles=$api->GetRoles();
136 $role_ids=array_map("role_id",$all_roles);
139 // preserve input order
140 foreach ($role_ids as $role_id) {
141 foreach ($all_roles as $all_role) {
142 if ($all_role['role_id'] == $role_id) {
143 $selector=role_selector($all_role);
144 if ($role_id == $current)
145 $selector['selected']=true;
146 $selectors []= $selector;
153 static public function role_selectors_excluding ($api,$exclude_role_ids=NULL,$current=NULL) {
154 if ( ! $exclude_role_ids ) $exclude_role_ids = array();
155 $all_roles=$api->GetRoles();
157 foreach ($all_roles as $role) {
158 if ( ! in_array ($role['role_id'],$exclude_role_ids)) {
159 $role_ids [] = $role['role_id'];
162 return PlcForm::role_selectors($api,$role_ids,$current);
166 // a form with a single button
167 class PlcFormButton extends PlcForm {
172 function PlcFormButton ($full_url, $button_id, $button_text, $method="POST") {
173 $this->PlcForm($full_url,array(),$method);
174 $this->button_id=$button_id;
175 $this->button_text=$button_text;
180 $this->start_html() .
181 $this->submit_html($this->button_id,$this->button_text).