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'";
68 static function text_html ($name,$value,$options=NULL) {
69 $default_options = array('width'=>20);
70 if ( ! $options) $options=array();
71 $options = array_merge($default_options,$options);
72 $html="<input type=text name='$name' value='$value'";
73 $html .= " size=" . $options['width'];
74 $cbs=array('onFocus','onSelect');
75 foreach ($cbs as $cb) {
77 $html .= " $cb='" . $options[$cb] . "'";
82 static function textarea_html ($name,$value,$cols,$rows) {
83 return "<textarea name='$name' cols=$cols rows=$rows>$value</textarea>";
86 // selectors is an array of hashes with the following keys
88 // (*) value : the value that the 'name' variable will be assigned
89 // (*) optional 'selected': the entry selected initially
90 // (*) optional 'disabled': the entry is displayed but not selectable
91 // optional label is inserted as the first option, with no value attached
92 // autosubmit: onchange=submit()
93 static function select_html ($name,$selectors,$label=NULL,$autosubmit=false) {
95 $html.="<select name='$name'";
96 if ($autosubmit) $html .= " onChange='submit();'";
99 $encoded=htmlentities($label,ENT_QUOTES);
100 $html.="<option selected=selected value=''>$encoded</option>";
102 foreach ($selectors as $selector) {
103 $display=htmlentities($selector['display'],ENT_QUOTES);
104 $value=$selector['value'];
105 $html .= "<option value='$value'";
106 if ($selector['selected']) $html .= " selected=selected";
107 if ($selector['disabled']) $html .= " disabled=disabled";
108 $html .= ">$display</option>\n";
110 $html .= "</select>";
114 // helper function to handle role-oriented selectors
115 // because GetRoles does not correctly support filters, it's really painful to do this
116 static public function role_selectors($api,$role_ids=NULL,$current=NULL) {
117 function role_selector ($role) { return array('display'=>$role['name'],"value"=>$role['role_id']); }
118 function role_id ($role) { return $role['role_id']; }
120 $all_roles=$api->GetRoles();
122 $role_ids=array_map("role_id",$all_roles);
125 // preserve input order
126 foreach ($role_ids as $role_id) {
127 foreach ($all_roles as $all_role) {
128 if ($all_role['role_id'] == $role_id) {
129 $selector=role_selector($all_role);
130 if ($role_id == $current)
131 $selector['selected']=true;
132 $selectors []= $selector;
139 static public function role_selectors_excluding ($api,$exclude_role_ids=NULL,$current=NULL) {
140 if ( ! $exclude_role_ids ) $exclude_role_ids = array();
141 $all_roles=$api->GetRoles();
143 foreach ($all_roles as $role) {
144 if ( ! in_array ($role['role_id'],$exclude_role_ids)) {
145 $role_ids [] = $role['role_id'];
148 return PlcForm::role_selectors($api,$role_ids,$current);
152 // a form with a single button
153 class PlcFormButton extends PlcForm {
158 function PlcFormButton ($full_url, $button_id, $button_text, $method="POST") {
159 $this->PlcForm($full_url,array(),$method);
160 $this->button_id=$button_id;
161 $this->button_text=$button_text;
166 $this->start_html() .
167 $this->submit_html($this->button_id,$this->button_text).