diff options
Diffstat (limited to 'frontend/classes/form_elements.php')
-rw-r--r-- | frontend/classes/form_elements.php | 280 |
1 files changed, 280 insertions, 0 deletions
diff --git a/frontend/classes/form_elements.php b/frontend/classes/form_elements.php new file mode 100644 index 0000000..6e5b43e --- /dev/null +++ b/frontend/classes/form_elements.php @@ -0,0 +1,280 @@ +<?php +abstract class form_element { + protected $htmlname, $label; + public $status=true; + function __construct($htmlname, $label) { + $this->htmlname=htmlentities($htmlname); + $this->label=htmlentities($label); + } + public function output($rw=true, $val=false) { + echo "<b>$this->label:</b> "; + } + public function process() { + return isset($_REQUEST[$this->htmlname])?$_REQUEST[$this->htmlname]:false; + } + public function verify($val) { + return $val !== false; + } +} +class text_input extends form_element { + public function output($rw=true, $val=false) { + parent::output($rw, $val); + echo $rw?"<input name=\"$this->htmlname\"".($val===false?'':'value="'.htmlentities($val).'"').' />':($val===false?'':htmlentities($val)); + echo "<br/>\n"; + } +} +class select extends form_element { + private $options; + function __construct($htmlname, $label, $options) { + parent::__construct($htmlname, $label); + $this->options=$options; + } + public function output($rw=true, $val=false) { + parent::output($rw, $val); + if ($rw) { + echo '<select name="'.$this->htmlname.'">'."\n"; + $i=0; + } + foreach ($this->options as $value => $label) { + if ($rw) + echo "\t".'<option value="'.$i++.'"'.($value == $val?' selected="selected"':'').'>'.htmlentities($label).'</option>'."\n"; + elseif ($value == $val) + echo htmlentities($label); + } + if ($rw) + echo '</select>'; + echo "<br/>\n"; + } + public function process() { + $vals=array_keys($this->options); + if (isset($_REQUEST[$this->htmlname]) && is_numeric($_REQUEST[$this->htmlname]) && isset($vals[$_REQUEST[$this->htmlname]])) { + return $vals[$_REQUEST[$this->htmlname]]; + } else return false; + } + public function verify($val) { + return isset($this->options[$val]); + } +} +class radio_array extends select { + public function output($rw=true, $val=false) { + if (!$rw) return parent::output($rw, $val); + echo "$this->label:<br/>\n"; + $i=0; + foreach ($this->options as $value => $label) { + echo "\t<input type=\"radio\" id=\"$this->htmlname-$i\" name=\"$this->htmlname\" value=\"".$i."\"".($value == $val?' checked="checked"':'')."\" /><label for=\"$this->htmlname-$i\">".htmlentities($label)."</label><br/>\n"; + $i++; + } + } +} +class checkbox_array extends form_element { + protected $array; + function __construct($htmlname, $label, $array, $delim=' ') { + parent::__construct($htmlname, $label); + $this->array=$array; + $this->delim=$delim; + } + public function output($rw=true, $val=false) { + $this->set_val($val); + if (strlen($this->label)) + echo "<b>$this->label:</b><br/>\n"; + $i=0; + foreach ($this->array as $value => $label) { + $label=htmlentities($label); + if ($rw) + echo "\t<input type=\"checkbox\" id=\"$this->htmlname-$i\" name=\"$this->htmlname[$i]\"".($this->val_has($value)?' checked="checked"':'')." /><label for=\"$this->htmlname-$i\">$label</label><br/>\n"; + elseif ($this->val_has($value)) + echo "$label<br/>\n"; + $i++; + } + } + public function process() { + $val=array(); + if (isset($_REQUEST[$this->htmlname])) { + $vals=array_keys($this->array); + foreach ($_REQUEST[$this->htmlname] as $i => $null) { + $val[]=$vals[$i]; + } + } + return implode($this->delim, $val); + } + public function verify($val) { + if ($val === false) return false; + if (strlen($val) == 0) return true; + $val=explode($this->delim, $val); + foreach ($val as $i => $value) { + if (isset($this->array[$value])) { + unset($val[$i]); + } + } + return count($val) == 0; + } + private $vals; + protected function set_val($val) { + $this->vals=explode($this->delim, $val); + } + protected function val_has($needle) { + return in_array($needle, $this->vals); + } +} +class layered_checkbox_array extends checkbox_array { + private $depth=0, $path_delims=array('', '/', '-'); + function __construct($htmlname, $label, &$array, $delim=' ', $metadata) { + parent::__construct($htmlname, $label, $array, $delim); + $this->metadata=$metadata; + for ($i=current($array); is_array($i); $i=current($i)) $this->depth++; + global $S; + if (!in_array('lca', $S['scripts'])) { + $S['scripts'][]='lca'; + } + } + public function output($rw=true, $val=false) { + $this->set_val($val); + if ($this->label) { + echo '<h4>'.htmlentities($this->label).'</h4>'; + } + if ($rw) + $this->r_output($this->array); + else + $this->r_ro_output($this->array); + } + public function process() { + return implode($this->delim, $this->r_process($this->array)); + } + public function verify($val) { + if ($val === false) return false; + if (strlen($val) == 0) return true; + $val=explode($this->delim, $val); + $r=$this->r_verify($val, $this->array); + debug('lca', 'verify leftovers: '.implode(' ',$r)); + return count($r) == 0; + } + private function r_output(&$array, $depth=0, $path=null, $name=null) { + static $uid=0, $ucid=0; + $S['conf']=&$this->metadata[0]; + if ($depth == 0) { + $search=$autosize=0; + for ($i=1; $i<count($this->metadata); $i++) { + $m=&$this->metadata[$i]; + if (isset($m['tag'])) { + $autosize++; + } + if (isset($m['search'])) { + $search++; + } + } + if ($search) { + if (!isset($S['conf']['id'])) { + $S['conf']['id']=self::b36($uid++); + } + echo 'Search: <input id="'.$S['conf']['id'].'-q" onkeyup="lca_search(this.value, document.getElementById(\''.$S['conf']['id'].'\'), 0, '.$this->depth.')" /> <a href="javascript:q=document.getElementById(\''.$S['conf']['id'].'-q\'); q.value=\'\'; q.onkeyup()">Clear</a> <a href="javascript:lca_show_checked(document.getElementById(\''.$S['conf']['id'].'\'), 0, '.$this->depth.'); undefined">Show checked</a><br/>'."\n"; + } + echo '<div class="lca'.(isset($S['conf']['autosize'])?' autosize" style="font-size: '.pow(1.15, $autosize)*100.0.'%':'').'" id="'.$S['conf']['id'].'">'."\n"; + foreach ($array as $name => &$val) { + $this->r_output($val, $depth+1, $name, $name); + $uid++; + } + echo '<h3 style="display: none">No results</h3></div>'; + echo "<script type=\"text/javascript\">\n<!--\nif (lca_show_checked(document.getElementById('{$S['conf']['id']}'), 0, $this->depth) == 0) lca_search(document.getElementById('{$S['conf']['id']}-q').value, document.getElementById('{$S['conf']['id']}'), 0, $this->depth);\n-->\n</script>\n"; + } else { + $meta=$this->metadata[$depth]; + if (isset($meta['tag'])) { + echo '<'.$meta['tag'].' class="lcae'.(isset($meta['search'])?' lcas':'').(isset($meta['collapsed'])?' lca'.($meta['collapsed']?'c':'C'):'').(isset($meta['class'])?' '.$meta['class']:'').'" id="'.self::b36($uid).'"'.($depth > 1 && isset($this->metadata[$depth-1]['collapsed']) && $this->metadata[$depth-1]['collapsed'] && false?' style="display: none"':'').'>'; + if (isset($meta['collapsed']) && $depth < $this->depth) { + echo '<a href="javascript:lcat(\''.self::b36($uid).'\')">±</a>'; + } + } + if (isset($meta['checkbox'])) { + $enc=self::b36($ucid++); + echo '<input type="checkbox" id="-'.$enc.'" name="'.$this->htmlname.'['.$enc.']"'.($this->val_has($this->format_label($array, $meta['checkbox'], $path, $name))?' checked="checked"':'').' /><label for="-'.$enc.'">'.$this->format_label($array, $meta['label'], $path, $name).'</label>'."\n"; + } elseif (isset($meta['label'])) { + echo '<span class="lcal">'.$this->format_label($array, $meta['label'], $path, $name)."</span>\n"; + } + if ($depth < $this->depth) { + foreach ($array as $name => &$val) { + $uid++; + $this->r_output($val, $depth+1, $path.$meta['delim'].$name, $name); + } + } + if (isset($meta['tag'])) { + echo '</'.$meta['tag'].">\n"; + } + } + } + private function r_process(&$array, $depth=0, $path=null, $name=null) { + static $ucid=0, $r; + if ($depth == 0) { + $r=array(); + foreach ($array as $name => &$val) { + $this->r_process($val, $depth+1, $name, $name); + } + return $r; + } else { + $meta=$this->metadata[$depth]; + if (isset($meta['checkbox'])) { + if (isset($_REQUEST[$this->htmlname][self::b36($ucid)])) { + $r[]=$this->format_label($array, $meta['checkbox'], $path, $name); + } + $ucid++; + } + if ($depth < $this->depth) { + foreach ($array as $name => &$val) + $this->r_process($val, $depth+1, $path.$meta['delim'].$name, $name); + } + } + } + private function &r_verify(&$vals, &$array, $depth=0, $path=null, $name=null) { + if ($depth == 0) { + foreach($array as $name => &$val) { + $this->r_verify($vals, $val, $depth+1, $name, $name); + } + return $vals; + } else { + $meta=$this->metadata[$depth]; + if (isset($meta['checkbox'])) { + $label=$this->format_label($array, $meta['checkbox'], $path, $name); + if (($i=array_search($label, $vals)) !== false) { + unset($vals[$i]); + } + } + if ($depth < $this->depth) { + foreach ($array as $name => &$val) + $this->r_verify($vals, $val, $depth+1, $path.$meta['delim'].$name, $name); + } + return $vals; + } + } + private function r_ro_output(&$array, $depth=0, $path=null, $name=null) { + if ($depth == 0) { + foreach ($array as $name => &$val) { + $this->r_ro_output($val, $depth+1, $name, $name); + } + } else { + $meta=$this->metadata[$depth]; + if (isset($meta['checkbox'])) { + $val=$this->format_label($array, $meta['checkbox'], $path, $name); + if ($this->val_has($val)) { + echo $this->format_label($array, $meta['label'], $path, $name)."<br/>\n"; + } + } + if ($depth < $this->depth) { + foreach ($array as $name => &$val) + $this->r_ro_output($val, $depth+1, $path.$meta['delim'].$name, $name); + } + } + } + private function format_label(&$array, $label='%p', $path, $name) { + $arg=$array; + $out=str_replace(array('%p', '%n'), array($path, $name), $label); + if (strpos($label, '$')) { + while (is_array(current($arg))) { + $arg=current($arg); + } + $out=eval("extract(\$arg, EXTR_PREFIX_INVALID, 'var_');\n".(strpos($label, 'return')===0?$out:"return <<<_XQ1\n$out\n_XQ1").";\n"); + } + return strpos($label, 'return')===0?$out:htmlentities($out); + } + private static function b36($n) { + return base_convert($n, 10, 36); + } +} +?> |