summaryrefslogtreecommitdiff
path: root/utils/forms.php
diff options
context:
space:
mode:
authordeva <deva>2009-04-23 07:41:28 +0000
committerdeva <deva>2009-04-23 07:41:28 +0000
commit3f1dda1e19e1f77e908df1d49d028d0932d0f1a1 (patch)
treed44586018a0594aa53cfbe172d7a576bbe5f593c /utils/forms.php
parent9fc8170f4492c3e5900156610061e59c84aa9d83 (diff)
A completely new forms class, to replace the old function based method. Also some work on a new config module, evenmtuelly to replace the old hardcoded values.
Diffstat (limited to 'utils/forms.php')
-rw-r--r--utils/forms.php361
1 files changed, 238 insertions, 123 deletions
diff --git a/utils/forms.php b/utils/forms.php
index d5216f2..dd0a768 100644
--- a/utils/forms.php
+++ b/utils/forms.php
@@ -1,147 +1,262 @@
<?php
+class LineEdit {
+ private $label, $name, $value;
+
+ function LineEdit($label, $name, $value = "")
+ {
+ $this->label = $label;
+ $this->name = $name;
+ $this->value = $value;
+ }
-function beginform($action, $hasfile = false)
-{
- global $m, $s;
-?>
-<form method="post"
-<?php if($hasfile) { ?> enctype="multipart/form-data"
-<?php } ?> action="?page=admin&amp;m=<?php echo $m; ?>&amp;s=<?php echo $s; ?>&amp;a=<?php echo $action; ?>">
-<?php
+ function render($indent = "")
+ {
+ $str = $indent . "<div class=\"input\">\n";
+ $str .= $indent . " <div class=\"label\">". $this->label ."</div>\n";
+ $str .= $indent . " <div class=\"widget\"><input name=\"vars[".$this->name."]\" value=\"".$this->value."\"/></div>\n";
+ $str .= $indent . "</div>\n";
+ return $str;
+ }
}
-function endform()
-{
-?>
-</form>
-<?php
-}
+class FileUpload {
+ private $label, $name, $accept;
-function button($label)
-{
-?>
- <div class="input">
- <div class="label"></div>
- <div class="widget"><button type="submit"><?php echo $label; ?></button></div>
- </div>
-<?php
+ public function FileUpload($label, $name, $accept = "*")
+ {
+ $this->label = $label;
+ $this->name = $name;
+ $this->accept = $accept;
+ }
+
+ public function render($indent = "")
+ {
+ $str = $indent . "<div class=\"input\">\n";
+ $str .= $indent . " <div class=\"label\">". $this->label . "</div>\n";
+ $str .= $indent . " <div class=\"widget\"><input type=\"file\" name=\""
+ . $this->name. "\" accept=\"". $this->accept ."\"/></div>\n";
+ $str .= $indent . "</div>\n";
+ return $str;
+ }
}
-function lineedit($label, $name, $value = "")
-{
-?>
- <div class="input">
- <div class="label"><?php echo $label; ?></div>
- <div class="widget"><input name="<?php echo "vars[".$name."]"; ?>" value="<?php echo $value; ?>"/></div>
- </div>
-<?php
+class Button {
+ private $label;
+
+ public function Button($label)
+ {
+ $this->label = $label;
+ }
+
+ public function render($indent = "")
+ {
+ $str = $indent . "<div class=\"input\">\n";
+ $str .= $indent . " <div class=\"label\"></div>\n";
+ $str .= $indent . " <div class=\"widget\"><button type=\"submit\">".
+ $this->label."</button></div>\n";
+ $str .= $indent . "</div>\n";
+ return $str;
+ }
}
-function fileupload($label, $name, $accept = "*")
-{
-?>
- <div class="input">
- <div class="label"><?php echo $label; ?></div>
- <div class="widget"><input type="file" name="<?php echo $name; ?>" accept="<?php echo $accept;?>"/></div>
- </div>
-<?php
+class CheckBox {
+ private $label, $name, $value;
+
+ public function CheckBox($label, $name, $value = "")
+ {
+ $this->label = $label;
+ $this->name = $name;
+ $this->value = $value;
+ }
+
+ public function render($indent = "")
+ {
+ $str = $indent . "<div class=\"input\">\n";
+ $str .= $indent . " <div class=\"label\">". $this->label ."</div>\n";
+ $str .= $indent . " <div class=\"widget\"><input type=\"checkbox\" name=\"vars[".$this->name."]\" value=\"".$this->value."\"/></div>\n";
+ $str .= $indent . "</div>\n";
+ return $str;
+ }
}
-function hidden($values)
-{
- foreach($values as $key => $value) {
-?>
- <input type="hidden" name="<?php echo "vars[".$key."]"; ?>" value="<?php echo $value; ?>"/>
-<?php
- }
+class ComboBox {
+ private $label, $name, $value, $values;
+
+ public function ComboBox($label, $name, $value, $values)
+ {
+ $this->label = $label;
+ $this->name = $name;
+ $this->value = $value;
+ $this->values = $values;
+ }
+
+ public function render($indent = "")
+ {
+ $str = $indent . "<div class=\"input\">\n";
+ $str .= $indent . " <div class=\"label\">".$this->label."</div>\n";
+ $str .= $indent . " <div class=\"widget\">\n";
+ $str .= $indent . " <select name=\"vars[".$this->name."]\">\n";
+ foreach($this->values as $k => $v) {
+ if($v != $this->value) $str .= $indent . " <option value=\"".$v."\">".$k."</option>\n";
+ else $str .= $indent . " <option value=\"".$v."\" selected>".$k."</option>\n";
+ }
+ $str .= $indent . " </select>\n";
+ $str .= $indent . " </div>\n";
+ $str .= $indent . "</div>\n";
+ return $str;
+ }
}
-function textedit($label, $name, $value = "")
-{
-?>
- <div class="input">
- <div class="label"><?php echo $label; ?></div>
- <div class="widget"><textarea class="textedit" name="<?php echo "vars[".$name."]"; ?>"><?php echo $value; ?></textarea></div>
- </div>
-<?php
+class Hidden {
+ private $values;
+
+ public function Hidden($values)
+ {
+ $this->values = $values;
+ }
+
+ public function render($indent = "")
+ {
+ $str = "";
+ foreach($this->values as $key => $value) {
+ $str .= $indent . "<input type=\"hidden\" name=\"vars[".$key."]\" value=\"".$value."\"/>\n";
+ }
+ return $str;
+ }
}
-function datetimeedit($label, $name, $timestamp = 0)
-{
- if($timestamp == 0) $timestamp = time();
+class TextEdit {
+ private $label, $name, $value;
+
+ function TextEdit($label, $name, $value = "")
+ {
+ $this->label = $label;
+ $this->name = $name;
+ $this->value = $value;
+ }
- $second = date('s',$timestamp);
- $minute = date('i',$timestamp);
- $hour = date('G',$timestamp);
- $day = date('d',$timestamp);
- $month = date('m',$timestamp);
- $year = date('Y',$timestamp);
-?>
- <div class="input">
- <div class="label"><?php echo $label; ?></div>
- <div class="widget">
- <input style="width: 40px;" name="<?php echo "vars[".$name."_year]"; ?>" value="<?php echo $year; ?>"/>/<input style="width: 20px;" name="<?php echo "vars[".$name."_month]"; ?>" value="<?php echo $month; ?>"/>/<input style="width: 20px;" name="<?php echo "vars[".$name."_day]"; ?>" value="<?php echo $day; ?>"/>
- -
- <input style="width: 20px;" name="<?php echo "vars[".$name."_hour]"; ?>" value="<?php echo $hour; ?>"/>:<input style="width: 20px;" name="<?php echo "vars[".$name."_minute]"; ?>" value="<?php echo $minute; ?>"/>:<input style="width: 20px;" name="<?php echo "vars[".$name."_second]"; ?>" value="<?php echo $second; ?>"/>
- </div>
- </div>
-<?php
+ function render($indent = "")
+ {
+ $str = $indent . "<div class=\"input\">\n";
+ $str .= $indent . " <div class=\"label\">". $this->label ."</div>\n";
+ $str .= $indent . " <div class=\"widget\"><textarea class=\"textedit\" name=\"vars[".$this->name."]\">".$this->value."</textarea></div>\n";
+ $str .= $indent . "</div>\n";
+ return $str;
+ }
}
-function totimestamp($t, $name)
-{
- $timestring = sprintf("%d", $t[$name."_year"]) ."/".
- sprintf("%d", $t[$name."_month"]) ."/".
- sprintf("%d", $t[$name."_day"]) ." ".
- sprintf("%d", $t[$name."_hour"]) .":".
- sprintf("%02d", $t[$name."_minute"]).":".
- sprintf("%02d", $t[$name."_second"]);
-
- //echo $timestring;
-
- return strtotime($timestring);
+
+class DateTimeEdit {
+ private $label, $name, $timestamp;
+
+ function DateTimeEdit($label, $name, $timestamp = 0)
+ {
+ $this->label = $label;
+ $this->name = $name;
+ $this->timestamp = $timestamp;
+ }
+
+ function render($indent = "")
+ {
+ if($this->timestamp == 0) $t = time();
+ else $t = $this->timestamp;
+
+ $second = date('s',$t);
+ $minute = date('i',$t);
+ $hour = date('G',$t);
+ $day = date('d',$t);
+ $month = date('m',$t);
+ $year = date('Y',$t);
+
+ $str = $indent . "<div class=\"input\">\n";
+ $str .= $indent . " <div class=\"label\">".$this->label."</div>\n";
+ $str .= $indent . " <div class=\"widget\">\n";
+ $str .= $indent . " <input style=\"width: 50px;\" name=\"vars[".$this->name."_year]\" value=\"".$year."\"/>";
+ $str .= "/<input style=\"width: 30px;\" name=\"vars[".$this->name."_month]\" value=\"".$month."\"/>";
+ $str .= "/<input style=\"width: 30px;\" name=\"vars[".$this->name."_day]\" value=\"".$day."\"/>";
+ $str .= " - ";
+ $str .= "<input style=\"width: 30px;\" name=\"vars[".$this->name."_hour]\" value=\"".$hour."\"/>";
+ $str .= ":<input style=\"width: 30px;\" name=\"vars[".$this->name."_minute]\" value=\"".$minute."\"/>";
+ $str .= ":<input style=\"width: 30px;\" name=\"vars[".$this->name."_second]\" value=\"".$second."\"/>\n";
+ $str .= $indent . " </div>\n";
+ $str .= $indent . "</div>\n";
+
+ return $str;
+ }
+
+ public function toTimestamp($t, $name)
+ {
+ $timestring = sprintf("%d", $t[$name."_year"]) ."/".
+ sprintf("%d", $t[$name."_month"]) ."/".
+ sprintf("%d", $t[$name."_day"]) ." ".
+ sprintf("%d", $t[$name."_hour"]) .":".
+ sprintf("%02d", $t[$name."_minute"]).":".
+ sprintf("%02d", $t[$name."_second"]);
+
+ return strtotime($timestring);
+ }
}
-function combobox($label, $name, $value, $values)
-{
-?>
- <div class="input">
- <div class="label"><?php echo $label; ?></div>
- <div class="widget">
- <select name="<?php echo "vars[".$name."]"; ?>">
-<?php
-
- foreach($values as $k => $v) {
- if($v != $value) echo " <option value=\"$v\">$k</option>\n";
- else echo " <option value=\"$v\" selected>$k</option>\n";
- }
-?>
- </select>
- </div>
- </div>
-<?php
+class ImageComboBox {
+ private $label, $name, $value, $values;
+
+ public function ImageComboBox($label, $name, $value, $values)
+ {
+ $this->label = $label;
+ $this->name = $name;
+ $this->value = $value;
+ $this->values = $values;
+ }
+
+ public function render($indent = "")
+ {
+ $str = $indent . "<div class=\"input\">\n";
+ $str .= $indent . " <div class=\"label\">".$this->label."</div>\n";
+ $str .= $indent . " <div class=\"widget\">\n";
+ $str .= $indent . " <select name=\"vars[".$this->name."]\">\n";
+
+ foreach($this->values as $k => $v) {
+ $str .= $indent . " <optgroup style=\"background-image: url(".$v."); height: 100px; width: 100px;\"/>\n";
+ $str .= $indent . " <option value=\"".$k."\"";
+ if($v == $value) $str .= " selected";
+ $str .=">".$k."</option>\n";
+ $str .= $indent . " </optgroup>\n";
+ }
+
+ $str .= $indent . " </select>\n";
+ $str .= $indent . " </div>\n";
+ $str .= $indent . "</div>\n";
+ return $str;
+ }
}
-function imagecombobox($label, $name, $value, $values)
-{
-?>
- <div class="input">
- <div class="label"><?php echo $label; ?></div>
- <div class="widget">
- <select name="<?php echo "vars[".$name."]"; ?>">
-<?php
-
- foreach($values as $k => $v) {
-?>
- <optgroup style="background-image: url(<?php echo $v;?>); height: 100px; width: 100px;"/>
- <option value="<?php echo $k; ?>"<?php if($v == $value) echo " selected";?>><?php echo $k;?></option>
- </optgroup>
-<?php
- }
-?>
- </select>
- </div>
- </div>
-<?php
+class Form {
+ private $widgets = array();
+ private $action;
+ private $hasFileUpload = false;
+
+ public function addWidget($widget)
+ {
+ if(get_class($widget) == "FileUpload") $this->hasFileUpload = true;
+ array_push($this->widgets, $widget);
+ }
+
+ public function render($indent = "")
+ {
+ global $m, $s;
+ $str = $indent . "<form method=\"post\"\n";
+ if($this->hasFileUpload) $str .= $indent . " enctype=\"multipart/form-data\"\n";
+ $str .= $indent . " action=\"?page=admin&amp;m=".$m."&amp;s=".$s."&amp;a=".$this->action."\">\n";
+ foreach($this->widgets as $widget) {
+ $str .= $widget->render($indent . " ");
+ }
+ $str .= $indent . "</form>\n";
+ echo $str;
+ }
+
+ public function Form($action)
+ {
+ $this->action = $action;
+ }
}
?>