From d218079628f1ab5b70463c89fe53d9caf6cea3ea Mon Sep 17 00:00:00 2001 From: Eudyptula Date: Tue, 11 Aug 2009 19:17:12 -0400 Subject: Make 'wizard_step' class a subclass of new generic 'form' class; Use ACCEPT_PROPERTIES=-interactive --- backend/modules/gentoo_portage/setup.php | 1 + frontend/classes/form.php | 53 ++++++ frontend/classes/form_elements.php | 280 +++++++++++++++++++++++++++++++ frontend/classes/forms.php | 280 ------------------------------- frontend/classes/wizard.php | 69 +++----- 5 files changed, 353 insertions(+), 330 deletions(-) create mode 100644 frontend/classes/form.php create mode 100644 frontend/classes/form_elements.php delete mode 100644 frontend/classes/forms.php diff --git a/backend/modules/gentoo_portage/setup.php b/backend/modules/gentoo_portage/setup.php index 93dbad8..b6accfb 100644 --- a/backend/modules/gentoo_portage/setup.php +++ b/backend/modules/gentoo_portage/setup.php @@ -6,6 +6,7 @@ $makeconf=array( 'pkgdir' => $S['conf']['pkgdir_root'].'/'.$profile->pkgdir, 'chost' => $headers['chost'], 'accept_keywords' => $headers['accept_keywords'], + 'accept_properties' => '-interactive', 'root' => $imagedir, 'port_logdir' => "$workdir/log", 'emerge_log_dir' => "$workdir/log", diff --git a/frontend/classes/form.php b/frontend/classes/form.php new file mode 100644 index 0000000..51f548f --- /dev/null +++ b/frontend/classes/form.php @@ -0,0 +1,53 @@ +elements as $name => &$el) { + if (is_object($el)) { + if (!$el->status) + echo print_warning('Please complete this field:'); + $el->output($rw, isset($vals[$name])?$vals[$name]:false); + } else { + echo $el; + } + } + } + function process() { + $vals=array(); + foreach ($this->elements as $name => &$el) { + if (!is_object($el)) continue; + $vals[$name]=$el->process(); + $el->status=$vals[$name] !== false; + } + return $vals; + } + function verify($vals) { + foreach ($this->elements as $name => &$el) { + if (!is_object($el)) continue; + if (!isset($vals[$name])) + return null; + elseif (!($el->status=$el->verify($vals[$name]))) + return false; + } + return true; + } + public function text($text) { + $this->elements[]=$text; + } + public function text_input($optname, $htmlname, $label) { + $this->elements[$optname]=new text_input($htmlname, $label); + } + public function select($optname, $htmlname, $label, $options) { + $this->elements[$optname]=new select($htmlname, $label, $options); + } + public function radio_array($optname, $htmlname, $label, $options) { + $this->elements[$optname]=new radio_array($htmlname, $label, $options); + } + public function checkbox_array($optname, $htmlname, $label, $array, $delim=' ') { + $this->elements[$optname]=new checkbox_array($htmlname, $label, $array, $delim=' '); + } + public function layered_checkbox_array($optname, $htmlname, $label, &$array, $delim=' ', $metadata) { + $this->elements[$optname]=new layered_checkbox_array($htmlname, $label, $array, $delim, $metadata); + } +} +?> 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 @@ +htmlname=htmlentities($htmlname); + $this->label=htmlentities($label); + } + public function output($rw=true, $val=false) { + echo "$this->label: "; + } + 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?"htmlname\"".($val===false?'':'value="'.htmlentities($val).'"').' />':($val===false?'':htmlentities($val)); + echo "
\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 ''; + echo "
\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:
\n"; + $i=0; + foreach ($this->options as $value => $label) { + echo "\thtmlname-$i\" name=\"$this->htmlname\" value=\"".$i."\"".($value == $val?' checked="checked"':'')."\" />
\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 "$this->label:
\n"; + $i=0; + foreach ($this->array as $value => $label) { + $label=htmlentities($label); + if ($rw) + echo "\thtmlname-$i\" name=\"$this->htmlname[$i]\"".($this->val_has($value)?' checked="checked"':'')." />
\n"; + elseif ($this->val_has($value)) + echo "$label
\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 '

'.htmlentities($this->label).'

'; + } + 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; $imetadata); $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: Clear Show checked
'."\n"; + } + echo '
'."\n"; + foreach ($array as $name => &$val) { + $this->r_output($val, $depth+1, $name, $name); + $uid++; + } + echo '

No results

'; + echo "\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 '±'; + } + } + if (isset($meta['checkbox'])) { + $enc=self::b36($ucid++); + echo 'val_has($this->format_label($array, $meta['checkbox'], $path, $name))?' checked="checked"':'').' />'."\n"; + } elseif (isset($meta['label'])) { + echo ''.$this->format_label($array, $meta['label'], $path, $name)."\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 '\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)."
\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); + } +} +?> diff --git a/frontend/classes/forms.php b/frontend/classes/forms.php deleted file mode 100644 index 6e5b43e..0000000 --- a/frontend/classes/forms.php +++ /dev/null @@ -1,280 +0,0 @@ -htmlname=htmlentities($htmlname); - $this->label=htmlentities($label); - } - public function output($rw=true, $val=false) { - echo "$this->label: "; - } - 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?"htmlname\"".($val===false?'':'value="'.htmlentities($val).'"').' />':($val===false?'':htmlentities($val)); - echo "
\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 ''; - echo "
\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:
\n"; - $i=0; - foreach ($this->options as $value => $label) { - echo "\thtmlname-$i\" name=\"$this->htmlname\" value=\"".$i."\"".($value == $val?' checked="checked"':'')."\" />
\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 "$this->label:
\n"; - $i=0; - foreach ($this->array as $value => $label) { - $label=htmlentities($label); - if ($rw) - echo "\thtmlname-$i\" name=\"$this->htmlname[$i]\"".($this->val_has($value)?' checked="checked"':'')." />
\n"; - elseif ($this->val_has($value)) - echo "$label
\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 '

'.htmlentities($this->label).'

'; - } - 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; $imetadata); $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: Clear Show checked
'."\n"; - } - echo '
'."\n"; - foreach ($array as $name => &$val) { - $this->r_output($val, $depth+1, $name, $name); - $uid++; - } - echo '

No results

'; - echo "\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 '±'; - } - } - if (isset($meta['checkbox'])) { - $enc=self::b36($ucid++); - echo 'val_has($this->format_label($array, $meta['checkbox'], $path, $name))?' checked="checked"':'').' />'."\n"; - } elseif (isset($meta['label'])) { - echo ''.$this->format_label($array, $meta['label'], $path, $name)."\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 '\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)."
\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); - } -} -?> diff --git a/frontend/classes/wizard.php b/frontend/classes/wizard.php index de37e72..31d02e9 100644 --- a/frontend/classes/wizard.php +++ b/frontend/classes/wizard.php @@ -1,6 +1,6 @@ configuration=&$c; $this->module=new module($c->module); @@ -26,16 +26,7 @@ class wizard_step { echo '
'."\n"; $this->echo_buttons(); } - foreach ($this->data as $obj) { - if (is_array($obj)) { - if (!$obj[0]->status) { - echo print_warning('Please complete this field:'); - } - $obj[0]->output($rw, $this->get_opt($obj[1])); - } else { - echo $obj; - } - } + parent::output($rw, $this->get_opts()); if ($rw) { echo '
'; $this->echo_buttons(); @@ -43,52 +34,30 @@ class wizard_step { echo ''."\n"; } public function process() { - if (!isset($_REQUEST['wizard_submit'][$this->step])) { + if (!isset($_REQUEST['wizard_submit'][$this->step])) return $this->step; - } $result=$this->next; - foreach ($this->data as $obj) { - if (is_array($obj)) { - $value=$obj[0]->process(); - $obj[0]->status=($value !== false); - if ($obj[0]->status) { - $this->set_opt($obj[1], $value); - } else { - $result=$this->step; - debug('wizard', htmlentities("{$obj[1]} incomplete ($value)")); - } + $vals=parent::process(); + foreach ($vals as $name => $value) { + if ($this->elements[$name]->status) { + $this->set_opt($name, $value); + } else { + $result=$this->step; + debug('wizard', htmlentities("$name incomplete ($value)")); } } return $result; } public function verify() { - foreach ($this->data as $obj) { - if (!is_array($obj)) continue; - if (($val=$this->get_opt($obj[1])) === false) { - return null; - } elseif (!($obj[0]->status=$obj[0]->verify($val))) { - return false; - } - } - return true; + return parent::verify($this->get_opts()); } - private function text($text) { - $this->data[]=$text; - } - private function text_input($optname, $htmlname, $label) { - $this->data[]=array(new text_input($htmlname, $label), $optname); - } - private function select($optname, $htmlname, $label, $options) { - $this->data[]=array(new select($htmlname, $label, $options), $optname); - } - private function radio_array($optname, $htmlname, $label, $options) { - $this->data[]=array(new radio_array($htmlname, $label, $options), $optname); - } - private function checkbox_array($optname, $htmlname, $label, $array, $delim=' ') { - $this->data[]=array(new checkbox_array($htmlname, $label, $array, $delim=' '), $optname); - } - private function layered_checkbox_array($optname, $htmlname, $label, &$array, $delim=' ', $metadata) { - $this->data[]=array(new layered_checkbox_array($htmlname, $label, $array, $delim, $metadata), $optname); + private function get_opts() { + $vals=array(); + foreach ($this->elements as $name => &$el) { + if (!is_object($el)) continue; + $vals[$name]=$this->get_opt($name); + } + return $vals; } private function set_opt($opt, $val) { return $this->configuration->set_opt($opt, $val); -- cgit v1.2.3-65-gdbad