00001 <?php
00002
00011 class Form
00012 {
00013 protected $instanceList = array();
00014 protected $inputList = array();
00015
00016 protected $years = array();
00017 protected $months = array();
00018 protected $days = array();
00019
00020 protected $topSubmit = false;
00021
00022
00031 function __construct($numYears = "") {
00032 $this->setYears($numYears);
00033 $this->setMonths();
00034 $this->setDays();
00035 }
00036
00055 function addInput($inputType, $label, $ref="", $validType="", $required="",
00056 $value="", $items="", $appearance="", $description="") {
00057
00058 $input = new FormInput($inputType, $label, $ref, $validType, $required,
00059 $value, $items, $appearance, $description);
00060 $this->add($input);
00061 }
00062
00063
00070 function addText($label, $description="") {
00071
00072 $input = new FormInput('text', $label, '', '', '', '', '', '', $description);
00073 $this->add($input);
00074 }
00075
00081 function addTextarea($label, $ref="", $validType="", $required="",
00082 $value="", $items="", $appearance="", $description="") {
00083
00084 $input = new FormInput('textarea', $label, $ref, $validType, $required,
00085 $value, $items, $appearance, $description);
00086 $this->add($input);
00087 }
00088
00092 function add($input) {
00093 array_push($this->inputList, $input);
00094 }
00095
00096
00103 function addDateChooser($label, $ref="", $required="", $value="", $appearance="minimal", $description="") {
00104 $yearValue = "";
00105 $monthValue = "";
00106 $dayValue = "";
00107
00108 if ($value != "") {
00109 $date = new DateParser($value);
00110 $yearValue = $date->getYear();
00111 $monthValue = $date->getMonth();
00112 $dayValue = $date->getDay();
00113 }
00114
00115 $this->addInput("text", $label, "","","","","","", $description);
00116 $this->addMonthChooser("", $ref."Month", $required, $monthValue, $appearance, "-month-");
00117 $this->addDayChooser("", $ref."Day", $required, $dayValue, $appearance, "-day-");
00118 $this->addYearChooser("", $ref."Year", $required, $yearValue, $appearance, "-year-");
00119 }
00120
00126 function addYearChooser($label, $ref="", $required="", $value="", $appearance="", $description="") {
00127 $this->addInput("select1", $label, $ref, "numbers", $required, $value, $this->getYears(),
00128 $appearance, $description);
00129
00130 }
00131
00137 function addMonthChooser($label, $ref="", $required="", $value="", $appearance="", $description="") {
00138 $this->addInput("select1", $label, $ref, "numbers", $required, $value, $this->getMonths(),
00139 $appearance, $description);
00140
00141 }
00142
00148 function addDayChooser($label, $ref="", $required="", $value="", $appearance="", $description="") {
00149 $this->addInput("select1", $label, $ref, "numbers", $required, $value, $this->getDays(),
00150 $appearance, $description);
00151
00152 }
00153
00162 public function buildForm($submitLabel) {
00163 $form = "<xforms>\n";
00164 $form .= $this->beginModel();
00165
00166 foreach ($this->inputList as $input) {
00167 $form .= $input->buildInstance();
00168 }
00169
00170 $form.= $this->endModel();
00171
00172 if ($this->topSubmit)
00173 $form.= $this->addSubmit($submitLabel);
00174
00175 foreach ($this->inputList as $input) {
00176 $form .= $input->buildInput();
00177 }
00178
00179 $form.= $this->addSubmit($submitLabel);
00180
00181 $form.= "</xforms>\n";
00182 return $form;
00183 }
00184
00189 protected function beginModel() {
00190 return "<model>\n".
00191 "<instance>\n";
00192 }
00193
00194
00199 protected function endModel() {
00200 return"</instance>\n".
00201 "<submission id=\"form\"\n".
00202 "action=\"SELF\"\n".
00203 "method=\"POST\"/>\n".
00204 "</model>\n";
00205 }
00206
00207
00212 protected function addSubmit($label) {
00213 return '<submit><label>' . $label . "</label></submit>\n";
00214 }
00215
00223 function getYears() {
00224 return $this->years;
00225 }
00226
00234 function getMonths() {
00235 return $this->months;
00236 }
00237
00245 function getDays() {
00246 return $this->days;
00247 }
00248
00258 public function setYears($numYears = "", $startYear = "") {
00259 $this->years = array();
00260 if ($numYears == "")
00261 $numYears = 5;
00262 if ($startYear == "")
00263 $startYear = date("Y") - 1;
00264 for ($i = 0; $i <= $numYears; ++$i)
00265 {
00266 $yearInfo = array($startYear, $startYear);
00267 array_push($this->years, $yearInfo);
00268 ++$startYear;
00269 }
00270 }
00271
00276 protected function setMonths() {
00277 $this->months = array(
00278 array("January", "01"),
00279 array("February", "02"),
00280 array("March", "03"),
00281 array("April", "04"),
00282 array("May", "05"),
00283 array("June", "06"),
00284 array("July", "07"),
00285 array("August", "08"),
00286 array("September", "09"),
00287 array("October", "10"),
00288 array("November", "11"),
00289 array("December", "12"));
00290 }
00291
00292
00293
00301 protected function setDays() {
00302 for ($i = 1; $i <= 31; ++$i)
00303 {
00304 if ($i < 10)
00305 $dayInfo = array($i, "0$i");
00306 else
00307 $dayInfo = array($i, $i);
00308 array_push($this->days, $dayInfo);
00309 }
00310 }
00311
00317 public function setTopSubmit($topSubmit) {
00318 $this->topSubmit = $topSubmit;
00319 }
00320
00321
00322 }
00323
00324 ?>