<?php
-// $Id: plc_details.php 11645 2009-01-21 23:09:49Z thierry $
+// $Id$
require_once 'plc_functions.php';
-// the rationale behind having function names with _text is that
+// the rationale behind having function names with _html is that
// the first functions that we had were actually printing the stuff instead of returning it
-// so basically the foo (...) function should do ``print (foo_text(...))''
+// so basically the foo (...) function should just do ``print (foo_html(...))''
-function plc_form_start ($url, $values, $options=array()) {
- $method = array_key_exists('method',$options) ? $options['method'] : 'POST';
- print "<form method=$method action='$url' enctype='multipart/form-data'>";
- foreach ($values as $key=>$value) {
- print "<input type=hidden name='$key' value='$value'>";
+class PlcForm {
+ // mandatory
+ var $url;
+ var $values; // a hash var=>value - default is empty array
+ var $method; // default is POST
+
+ function PlcForm ($full_url, $values, $method="POST") {
+ // so we can use the various l_* functions:
+ // we parse the url to extract var-values pairs,
+ // and add them to the 'values' argument if any
+
+ // extract var=value settings from url if any
+ $split=split_url($full_url);
+ $this->url=$split['url'];
+
+ $url_values=$split['values'];
+ if ( ! $values ) $values = array();
+ if ( $url_values ) $values=array_merge($values,$url_values);
+ $this->values=$values;
+
+ $this->method=$method;
}
-}
-function plc_form_end($options=array()) {
- print "</form>";
-}
+ function start () { print $this->start_html(); }
+ function start_html () {
+ $html="<form method=$this->method action='$this->url' enctype='multipart/form-data'>";
+ if ($this->values)
+ foreach ($this->values as $key=>$value)
+ $html .= $this->hidden_html($key,$value);
+ return $html;
+ }
-function plc_form_checkbox_text ($name,$value,$selected=false) {
- if ($selected) $xtra=" selected=selected";
- return "<input type=checkbox name='$name' value='$value'$xtra>";
-}
+ function end() { print $this->end_html(); }
+ function end_html() { return "</form>"; }
-function plc_form_submit_text ($name,$display) {
- return "<input type=submit name='$name' value='$display'>";
+ static function hidden_html ($key,$value) {
+ return "<input type=hidden name='$key' value='$value'/>";
+ }
+ static function checkbox_html ($name,$value,$selected=false) {
+ if ($selected) $xtra=" selected=selected";
+ return "<input type=checkbox name='$name' value='$value'$xtra/>";
+ }
+ static function submit_html ($name,$display) {
+ return "<input type=submit name='$name' value='$display'/>";
+ }
+ static function file_html ($name,$size) {
+ return "<input type=file name='$name' size=$size/>";
+ }
+ static function label_html ($name,$display) {
+ return "<label for=$name>$display </label>";
+ }
+ static function text_html ($name,$value,$size) {
+ return "<input type=text name='$name' size=$size value='$value'>";
+ }
+ static function textarea_html ($name,$value,$cols,$rows) {
+ return "<textarea name='$name' cols=$cols rows=$rows>$value</textarea>";
+ }
+
+ // selectors is an array of hashes with the following keys
+ // (*) display
+ // (*) value : the value that the 'name' variable will be assigned
+ // (*) optional 'selected': the entry selected initially
+ // (*) optional 'disabled': the entry is displayed but not selectable
+ // optional label is inserted as the first option, with no value attached
+ // autosubmit: onchange=submit()
+ static function select_html ($name,$selectors,$label=NULL,$autosubmit=false) {
+ $html="";
+ $html.="<select name='$name'";
+ if ($autosubmit) $html .= " onChange='submit();'";
+ $html .= ">";
+ if ($label) {
+ $encoded=htmlentities($label,ENT_QUOTES);
+ $html.="<option selected=selected value=''>$encoded</option>";
+ }
+ foreach ($selectors as $selector) {
+ $display=htmlentities($selector['display'],ENT_QUOTES);
+ $value=$selector['value'];
+ $html .= "<option value='$value'";
+ if ($selector['selected']) $html .= " selected=selected";
+ if ($selector['disabled']) $html .= " disabled=disabled";
+ $html .= ">$display</option>\n";
+ }
+ $html .= "</select>";
+ return $html;
+ }
+
+ // helper function to handle role-oriented selectors
+ // because GetRoles does not correctly support filters, it's really painful to do this
+ static public function role_selectors($api,$role_ids=NULL,$current=NULL) {
+ function role_selector ($role) { return array('display'=>$role['name'],"value"=>$role['role_id']); }
+ function role_id ($role) { return $role['role_id']; }
+
+ $all_roles=$api->GetRoles();
+ if ( ! $role_ids)
+ $role_ids=array_map("role_id",$all_roles);
+
+ $selectors=array();
+ // preserve input order
+ foreach ($role_ids as $role_id) {
+ foreach ($all_roles as $all_role) {
+ if ($all_role['role_id'] == $role_id) {
+ $selector=role_selector($all_role);
+ if ($role_id == $current)
+ $selector['selected']=true;
+ $selectors []= $selector;
+ }
+ }
+ }
+ return $selectors;
+ }
+
+ static public function role_selectors_excluding ($api,$exclude_role_ids=NULL,$current=NULL) {
+ if ( ! $exclude_role_ids ) $exclude_role_ids = array();
+ $all_roles=$api->GetRoles();
+ $role_ids = array();
+ foreach ($all_roles as $role) {
+ if ( ! in_array ($role['role_id'],$exclude_role_ids)) {
+ $role_ids [] = $role['role_id'];
+ }
+ }
+ return PlcForm::role_selectors($api,$role_ids,$current);
+ }
}
+
+// a form with a single button
+class PlcFormButton extends PlcForm {
-function plc_form_file_text ($name,$size) {
- return "<input type=file name='$name' size=$size>";
-}
+ var $button_id;
+ var $button_text;
-function plc_form_label_text ($name,$display) {
- return "<label for=$name>$display </label>";
-}
-
-function plc_form_select_text ($name,$values,$label="") {
- $selector="";
- $selector.="<select name='$name'>";
- if ($label)
- $selector.="<option value=''>$label</option>";
- foreach ($values as $chunk) {
- $display=$chunk['display'];
- $value=$chunk['value'];
- $selector .= "<option value='$value'>$display</option>\n";
- }
- $selector .= "</select>";
- return $selector;
+ function PlcFormButton ($full_url, $button_id, $button_text, $method="POST") {
+ $this->PlcForm($full_url,array(),$method);
+ $this->button_id=$button_id;
+ $this->button_text=$button_text;
+ }
+
+ function html () {
+ return
+ $this->start_html() .
+ $this->submit_html($this->button_id,$this->button_text).
+ $this->end_html();
+ }
}
?>