summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEudyptula <eitan@mosenkis.net>2009-07-07 17:19:21 -0400
committerEudyptula <eitan@mosenkis.net>2009-07-07 17:19:21 -0400
commit2b92850a6aaa28074cddba730b11ab606a3adcd9 (patch)
tree43c29ba3cf393cc7d541af1cb824388cea6def00 /frontend/classes/wizard_api.php
parentFix blip with favicon.ico (diff)
downloadingenue-2b92850a6aaa28074cddba730b11ab606a3adcd9.tar.gz
ingenue-2b92850a6aaa28074cddba730b11ab606a3adcd9.tar.bz2
ingenue-2b92850a6aaa28074cddba730b11ab606a3adcd9.zip
Major restructuring of frontend modules (package selection not done yet); created conf_build_common class for shared code among configurations and builds
Diffstat (limited to 'frontend/classes/wizard_api.php')
-rw-r--r--frontend/classes/wizard_api.php171
1 files changed, 171 insertions, 0 deletions
diff --git a/frontend/classes/wizard_api.php b/frontend/classes/wizard_api.php
new file mode 100644
index 0000000..95ed57a
--- /dev/null
+++ b/frontend/classes/wizard_api.php
@@ -0,0 +1,171 @@
+<?php
+class wizard_step {
+ var $module, $step, $title, $next, $data=array();
+ function __construct($mod, $step) {
+ $this->module=$mod;
+ $this->step=$step;
+ $file=FRONTEND."/modules/$mod/step$step.php";
+ if (!is_readable($file)) {
+ throw_exception("$mod step $step doesn't exist!");
+ }
+ require($file);
+ $this->title="Step $step".($title?" - $title":'');
+ $this->next=$next;
+ }
+ public function output() {
+ echo "<h3>$this->title</h3>\n";
+ echo '<form action="'.url('config/'.wizard::$configuration->id).'" method="post">';
+ foreach ($this->data as $obj) {
+ if (!$obj->status) {
+ echo print_warning('Please complete this field.');
+ }
+ $obj->output();
+ }
+ echo '<br/><input type="submit" name="wizard_submit['.$this->step.']" value="Continue" />'."\n";
+ }
+ public function process() {
+ global $request;
+ if (isset($request['wizard_submit_'.$this->step])) {
+ return $this->step;
+ }
+ $result=$this->next;
+ foreach ($this->data as $obj) {
+ if (!$obj->status=$obj->process()) {
+ $result=$this->step;
+ }
+ }
+ return $result;
+ }
+ private function text($text) {
+ $this->data[]=new wizard_text($text);
+ }
+ private function text_input($optname, $htmlname, $label) {
+ $this->data[]=new wizard_text_input($optname, $htmlname, $label);
+ }
+ private function select($optname, $htmlname, $label, $options) {
+ $this->data[]=new wizard_select($optname, $htmlname, $label, $options);
+ }
+ private function query($q) {
+ return $GLOBALS['S']['pdo']->query($q);
+ }
+}
+abstract class wizard {
+ public $status=true;
+ public static $configuration;
+ public static function set_configuration(&$c) {
+ self::$configuration=&$c;
+ }
+ abstract public function output();
+ abstract public function process();
+ protected static function get_opt($name) {
+ $opts=self::$configuration->get_opts();
+ return isset($opts[$name])?$opts[$name]:null;
+ }
+ protected static function set_opt($name, $val) {
+ if (substr($name, 0, 1) == ':') {
+ self::$configuration->$name=$val;
+ self::$configuration->write();
+ } else {
+ self::$configuration->set_opt($name, $val);
+ }
+ }
+ protected static function opt_is($name, $val) {
+ $opts=self::$configuration->get_opts();
+ if (isset($opts[$name]) && $opts[$name] === $val) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
+class wizard_text extends wizard {
+ protected $text;
+ function __construct($text) {
+ $this->text=$text;
+ }
+ public function output() {
+ echo $this->text;
+ }
+ public function process() {
+ return true;
+ }
+}
+abstract class wizard_input extends wizard {
+ protected $optname, $htmlname, $label;
+ function __construct($optname, $htmlname, $label) {
+ $this->optname=$optname;
+ $this->htmlname=htmlentities($htmlname);
+ $this->label=htmlentities($label);
+ }
+ public function output() {
+ echo "$this->label ";
+ }
+ public function process() {
+ global $request;
+ if (isset($request[$this->optname])) {
+ self::set_opt($this->optname, $request[$this->optname]);
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
+class wizard_text_input extends wizard_input {
+ public function output() {
+ parent::output();
+ }
+}
+class wizard_select extends wizard_input {
+ private $options;
+ function __construct($optname, $htmlname, $label, $options) {
+ parent::__construct($optname, $htmlname, $label);
+ $this->options=$options;
+ }
+ public function output() {
+ parent::output();
+ echo '<select name="'.$this->htmlname.'">'."\n";
+ foreach ($this->options as $val => $label) {
+ echo "\t".'<option value="'.htmlentities($val).'"'.(self::opt_is($this->optname, $val)?' selected="selected"':'').'>'.htmlentities($label).'</option>'."\n";
+ }
+ echo '</select>'."\n";
+ }
+}
+class wizard_radio extends wizard_select {
+ public function output() {
+ echo "$this->label:<br/>\n";
+ $i=0;
+ foreach ($this->options as $val => $label) {
+ echo "\t<input type=\"radio\" id=\"$this->htmlname-$i\" name=\"$this->htmlname\" value=\"".htmlentities($val)."\"".(self::opt_is($this->optname, $val)?' checked="checked"':'')."\" /><label for=\"$this->htmlname-$i\">".htmlentities($label)."</label>\n";
+ }
+ }
+}
+class wizard_checkbox_array extends wizard_input {
+ private $boxes;
+ function __construct($optname, $htmlname, $label, $boxes, $delim=' ') {
+ parent::__construct($optname, $htmlname, $label);
+ $this->boxes=$boxes;
+ $this->delim=$delim;
+ }
+ public function output() {
+ echo "$this->label:<br/>\n";
+ $i=0;
+ foreach ($this->boxes as $val => $label) {
+ echo "\t<input type=\"checkbox\" id=\"$this->htmlname-$i\" name=\"$this->htmlname[$i]\"".(self::opt_has($this->optname, $val, $this->delim)?' checked="checked"':'')." /><label for=\"$this->htmlname-$i\">$label</label><br/>\n";
+ $i++;
+ }
+ }
+ public function process() {
+ global $request;
+ $val=array();
+ // FIXME we're assuming that array_keys order is determinate and the same as the foreach in output()
+ $vals=array_keys($this->boxes);
+ foreach ($request[$this->htmlname] as $i) {
+ $val[]=$vals[$i];
+ }
+ self::set_opt($this->optname, implode($this->delim, $vals));
+ }
+ protected static function opt_has($name, $val, $delim=' ') {
+ return (array_search($val, explode($delim, self::get_opt($name))) !== false);
+ }
+}
+?>