00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00030 class XFormsRenderModule extends SObject{
00031
00032
00033 protected $xml;
00034
00035
00036 protected $presets;
00037
00038
00039 protected $booleans;
00040 protected $options;
00041
00042
00043 protected $ib;
00044 protected $ip;
00045
00046
00047 protected $model;
00048 protected $xformsError;
00049 protected $itemCount;
00050
00051
00052 protected $state;
00053 protected $READY_TO_LOAD_XML = 0;
00054 protected $XML_LOADED_SUCCESSFULLY = 5;
00055
00063 public function __construct () {}
00064
00072 public function loadXML($xml)
00073 {
00074 $xml = utf8_encode($xml);
00075 $this->xml = @simplexml_load_string($xml);
00076 if ($this->xml === false){
00077 $this->setPrettyError('loadXML', 'Your XForms XML does not parse');
00078 return false;
00079 }
00080 $this->init();
00081 $this->countItems();
00082
00083 }
00084
00092 public function init() {
00093 # create new manager classes
00094 $this->ib = new XFormsInterfaceBuilder($this);
00095 $this->ip = new XFormsInputProcessor($this);
00096
00097 # check the created classes' validity
00098 if (!is_object($this->ib)){
00099 $this->setPrettyError('init', 'Instantiation of XFormsInterfaceBuilder failed');
00100 return false;
00101 }
00102 if (!is_object($this->ip)){
00103 $this->setPrettyError('init', 'Instantiation of XFormsInputProcessor failed');
00104 return false;
00105 }
00106
00107 # clear all the class variables
00108 $this->booleans = array();
00109
00110 $this->booleans['validated'] = false;
00111 $this->booleans['submitted'] = false;
00112
00113 $this->options = array();
00114 $this->options['pictcha'] = true;
00115 $this->options['token'] = true;
00116
00117 $this->model = "";
00118 $this->modelObj = null;
00119 $this->xformsError = array();
00120 $this->itemCount = array();
00121
00122 $this->presets = array();
00123 }
00124
00125
00126
00127
00128
00134 public function getOption ($key) {
00135 if (!isset($this->options[$key])) return "";
00136 return $this->options[$key];}
00137
00143 public function setOption ($key, $value) {
00144 $this->options[$key] = $value;
00145 if ($key == 'pictcha') {
00146 SConfig::setOption('xforms.pictcha', $value);
00147 }
00148 }
00149
00150
00151
00152
00163 public function setPreset($tag, $value) {
00164 if (!isset($this->presets["$tag"])){ $this->presets["$tag"] = array();}
00165 if (is_array($value)){
00166 $this->presets["$tag"] = array_merge($this->presets["$tag"], $value);
00167 } else {
00168 array_push($this->presets["$tag"], $value);
00169 }
00170 }
00171
00177 public function getPreset($tag) {
00178 if (!isset($this->presets["$tag"])){ return false; }
00179 return $this->presets["$tag"];
00180 }
00181
00182
00183
00184
00191 public function processInput () {
00192 # if there is no form, return false immediately
00193 if ($this->xml == null){
00194 $this->booleans['submitted'] = false;
00195 return false;
00196 }
00197
00198
00199 # if the form has been submitted
00200 if ( isset($_POST['submit']) ) {
00201
00202 $this->booleans['submitted'] = true;
00203
00204 $this->ip->processInput($this->xml);
00205
00206 $this->validate();
00207 } else{
00208 $this->booleans['submitted'] = false;
00209 }
00210 }
00211
00218 public function validate() {
00219 $this->booleans['validated'] = !($this->getErrorCount());
00220 }
00221
00222
00223
00224
00225
00226
00233 public function setModel ( $model ) {
00234 $this->model = $model;
00235 $this->modelObj = @simplexml_load_string($model);
00236 if (!$this->modelObj) {
00237 $this->setPrettyWarning('setModel', 'unable to parse model into a simplexml object.');
00238 }
00239 }
00240
00241
00248 public function getInstanceXML ()
00249 { return $this->model; }
00250
00251
00252
00258 public function getSubmittedValue ($tag)
00259 {
00260 $index = $this->ib->getNamePrefix() . $tag;
00261 if(isset($_POST[$index])) return $_POST[$index];
00262 return "";
00263 }
00264
00271 public function getInstanceValue($path) {
00272 if (!$this->modelObj) {
00273 $this->setPrettyWarning('getInstanceValue', 'Could not search for value at path ' . $path
00274 . '; XML does not parse.');
00275 return null;
00276 }
00277 $search = $this->modelObj->xpath($path);
00278 if (count($search) == 0){
00279 $this->setPrettyWarning('getInstanceValue', 'No node found for path ' . $path);
00280 return null;
00281 }
00282 if (count($search) == 1){
00283 return XFormsFunctions::contentFromParsedXML($search[0]);
00284 }
00285 else {
00286 $output = array();
00287 foreach ($search as $searchVal) {
00288 $output[] = XFormsFunctions::contentFromParsedXML((string)$searchVal);
00289 }
00290 return $output;
00291 }
00292 }
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00309 public function setErrorCode($index, $code)
00310 {
00311 if (!isset($this->xformsError["$index"]))
00312 { $this->xformsError["$index"] = array(); }
00313 array_push($this->xformsError["$index"], $code);
00314 }
00315
00316
00322 public function getErrorCode ($index) {
00323 if (isset($this->xformsError["$index"]))
00324 { return $this->xformsError["$index"][0]; }
00325 }
00326
00327
00332 public function getErrorCount() {
00333 return count($this->xformsError);
00334 }
00335
00336
00345 public function getValidator () {
00346 return $this->ib->getValidator();
00347 }
00348
00349
00350
00351
00352
00357 public function isComplete () {
00358 if (!isset($this->booleans['submitted']) || ($this->booleans['submitted'] == false)) {
00359 # if it's not set at all, don't have a warning, just get the heck out.
00360 if (!isset($this->booleans['submitted'])) {
00361 $this->setPrettyWarning('isComplete', 'Attempted to call isComplete() before calling processInput()');
00362 }
00363 return false;
00364 }
00365
00366 $this->validate();
00367
00368 return (
00369 $this->booleans['submitted'] &&
00370 $this->booleans['validated']
00371 );
00372 }
00373
00378 public function isSubmitted () {
00379 return $this->booleans['submitted'];
00380 }
00381
00393 public function toHtml () {
00394 $this->validate();
00395 if ($this->isComplete()) {
00396 return "Form Was Submitted";
00397 } else {
00398 return $this->formatXFormsRoot($this->xml);
00399 }
00400 }
00401
00407 protected function formatXFormsRoot ( $xforms )
00408 {
00409 $htmlOutput = "";
00410 if ($xforms === false) {
00411 return false;
00412 }
00413
00414 if (!is_object($this->ib)){
00415 $this->setPrettyError('formatXFormsRoot', 'XFormsInterfaceBuilder is not an object.');
00416 return false;
00417 }
00418
00419 $htmlOutput .= $this->ib->formatXFormsInterface($xforms);
00420 if ($this->ib->hasError()) {
00421 $this->setError('formatXFormsRoot', 'Interface Builder had an error');
00422 $this->getErrorFrom($this->ib);
00423 return false;
00424 }
00425 return $htmlOutput;
00426 }
00427
00428
00429
00430
00431
00432
00433
00443 public function getItemCount ($tag)
00444 {
00445 return (isset($this->itemCount["$tag"])) ? $this->itemCount["$tag"] : 0;
00446 }
00447
00455 protected function updateItemCount ($tag, $child) {
00456 $nodeCounter = 0;
00457 foreach($child->item as $node){
00458 $nodeCounter++;
00459 }
00460 $this->itemCount["$tag"] = $nodeCounter;
00461 }
00462
00473 protected function countItems () {
00474 foreach($this->xml->children() as $tag => $child) {
00475 if ( isset($child['ref']) ) {
00476 $ref = $child['ref'];
00477
00478
00479 switch($tag) {
00480
00481
00482 case 'select':
00483 if (isset($child['appearance']) && 'minimal' == $child['appearance'])
00484 $this->itemCount["$ref"] = 1;
00485 else
00486 $this->updateItemCount($ref, $child);
00487 break;
00488
00489 default:
00490 $this->itemCount["$ref"] = 1;
00491 break;
00492 }
00493 }
00494 }
00495 }
00496 }
00497 ?>