// the first functions that we had were actually printing the stuff instead of returning it
// so basically the foo (...) function should just do ``print (foo_html(...))''
-class PlcForm {
+class PlekitForm {
// mandatory
var $url;
var $values; // a hash var=>value - default is empty array
- var $method; // default is POST
+ var $method; // default is POST, can be changed with options
+ var $onSubmit; // can be set with options
+ var $onReset; // can be set with options
- function PlcForm ($full_url, $values, $method="POST") {
+ function PlekitForm ($full_url, $values, $options=NULL) {
// 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
if ( $url_values ) $values=array_merge($values,$url_values);
$this->values=$values;
- $this->method=$method;
+ // make strict xhtml happy
+ $this->method="post"; if ($options['method']) $this->method=strtolower($options['method']);
+ $this->onSubmit=NULL; if ($options['onSubmit']) $this->onSubmit=$options['onSubmit'];
+ $this->onReset=NULL; if ($options['onReset']) $this->onReset=$options['onReset'];
}
function start () { print $this->start_html(); }
function start_html () {
- $html="<form method=$this->method action='$this->url' enctype='multipart/form-data'>";
+ $html="<form method='$this->method' action='$this->url' enctype='multipart/form-data'";
+ if ($this->onSubmit) $html .= " onSubmit='$this->onSubmit'";
+ if ($this->onReset) $html .= " onReset='$this->onReset'";
+ $html .= ">";
if ($this->values)
foreach ($this->values as $key=>$value)
$html .= $this->hidden_html($key,$value);
static function input_html ($type,$name,$value,$options=NULL) {
if ( ! $options) $options=array();
$html="<input";
- $html="<input type='$type' name='$name' value='$value'";
- $html .= PlcForm::attributes ($options);
+ $html .= " type='$type' name='$name' value='$value'";
+ $html .= PlekitForm::attributes ($options);
$html .= "/>";
return $html;
}
- static function text_html ($name,$value, $options=NULL) { return PlcForm::input_html('text', $name, $value, $options); }
- static function hidden_html ($name,$value, $options=NULL) { return PlcForm::input_html('hidden', $name, $value, $options); }
- static function checkbox_html ($name,$value,$options=NULL) { return PlcForm::input_html('checkbox', $name, $value, $options); }
- static function submit_html ($name,$value,$options=NULL) { return PlcForm::input_html('submit', $name, $value, $options); }
- static function button_html ($name,$value,$options=NULL) { return PlcForm::input_html('button', $name, $value, $options); }
- static function radio_html ($name,$value,$options=NULL) { return PlcForm::input_html('radio', $name, $value, $options); }
- static function file_html ($name,$value,$options=NULL) { return PlcForm::input_html('file', $name, $value, $options); }
+ static function text_html ($name,$value, $options=NULL) { return PlekitForm::input_html('text', $name, $value, $options); }
+ static function hidden_html ($name,$value, $options=NULL) { return PlekitForm::input_html('hidden', $name, $value, $options); }
+ static function checkbox_html ($name,$value,$options=NULL) { return PlekitForm::input_html('checkbox', $name, $value, $options); }
+ static function submit_html ($name,$value,$options=NULL) { return PlekitForm::input_html('submit', $name, $value, $options); }
+ static function button_html ($name,$value,$options=NULL) { return PlekitForm::input_html('button', $name, $value, $options); }
+ static function radio_html ($name,$value,$options=NULL) { return PlekitForm::input_html('radio', $name, $value, $options); }
+ static function file_html ($name,$value,$options=NULL) { return PlekitForm::input_html('file', $name, $value, $options); }
static function label_html ($name,$display) {
return "<label for=$name>$display</label>";
}
static function textarea_html ($name,$value,$cols,$rows) {
- return "<textarea name='$name' cols=$cols rows=$rows>$value</textarea>";
+ return "<textarea name='$name' cols='$cols' rows='$rows'>$value</textarea>";
}
// selectors is an array of hashes with the following keys
$encoded=htmlentities($options['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";
+ if ($selectors) {
+ 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) {
+ static public function role_selectors($roles,$current_id=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)
+ if ( ! $roles) {
+ drupal_set_message('WARNING: empty roles in role_selectors');
+ } else {
+ foreach ($roles as $role) {
+ $selector=role_selector($role);
+ if ($role['role_id'] == $current_id)
$selector['selected']=true;
- $selectors []= $selector;
- }
+ $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 {
+class PlekitFormButton extends PlekitForm {
var $button_id;
var $button_text;
- function PlcFormButton ($full_url, $button_id, $button_text, $method="POST") {
- $this->PlcForm($full_url,array(),$method);
+ function PlekitFormButton ($full_url, $button_id, $button_text, $method="POST") {
+ $this->PlekitForm($full_url,array(),$method);
$this->button_id=$button_id;
$this->button_text=$button_text;
}