Example #1
0
//Returns an array ($dependentFields) with the names of the fields
//that depends of fields passed through AJAX ($_GET/$_POST)
$dependentFields = array();
for ($r = 0; $r < sizeof($newValues); $r++) {
    $newValues[$r] = (array) $newValues[$r];
    $G_FORM->setValues($newValues[$r]);
    //Search dependent fields
    foreach ($newValues[$r] as $k => $v) {
        $myDependentFields = explode(',', $G_FORM->fields[$k]->dependentFields);
        $dependentFields = array_merge($dependentFields, $myDependentFields);
    }
}
$dependentFields = array_unique($dependentFields);
//Parse and update the new content
$template = PATH_CORE . 'templates/xmlform.html';
$newContent = $G_FORM->getFields($template);
//Returns the dependentFields's content
$sendContent = array();
$r = 0;
foreach ($dependentFields as $d) {
    $sendContent[$r]->name = $d;
    $sendContent[$r]->content = NULL;
    foreach ($G_FORM->fields[$d] as $attribute => $value) {
        switch ($attribute) {
            case 'type':
                $sendContent[$r]->content->{$attribute} = $value;
                break;
            case 'options':
                $sendContent[$r]->content->{$attribute} = toJSArray($value);
                break;
        }
 /**
  * Create FormAPI instance from XML file
  *
  * @param string $xml XML filename and location
  * @param string $action URL of target php file, that receive the request
  *
  * @return FormAPI instance of control object
  * @todo Implements Model creation from XML file
  */
 public function create($xml, $action, $model, $layout = Form::vertLayout, $mode = "post", $id = 1)
 {
     // form object
     $form = new form($id);
     $form->setMode($mode);
     $form->setLayout($layout);
     $form->setTarget($action);
     // parsing fields tag
     $xmlobj = new SimpleXMLElement($xml, 0, true);
     $form->setName((string) $xmlobj->fields[0]["name"]);
     $form->setTitle(intval((string) $xmlobj->fields[0]["title"]));
     // process fields
     foreach ($xmlobj->fields[0]->field as $f) {
         $fields =& $form->getFields();
         $id = intval((string) $f["id"]);
         $name = (string) $f["name"];
         $requested = (string) $f["requested"] == "yes" ? true : false;
         $w = trim((string) $f);
         $default = strlen($w) ? intval($w) : NULL;
         $regexp = (string) $f["regexp"];
         $w = trim((string) $f["help"]);
         $help = strlen($w) ? intval($w) : NULL;
         //			unset($options);
         $options = NULL;
         // create text field
         // TODO change to switch
         if ($f["type"] == "text") {
             $label = intval((string) $f["label"]);
             $length = intval((string) $f["length"]);
             $maxlength = intval((string) $f["maxlength"]);
             $fields[$id] = new TextField($id, $name, $label, $length, $maxlength, $requested, $default, $regexp, $help);
         }
         if ($f["type"] == "textarea") {
             $label = intval((string) $f["label"]);
             $length = intval((string) $f["length"]);
             $rows = intval((string) $f["rows"]);
             $fields[$id] = new TextareaField($id, $name, $label, $length, $rows, $requested, $default, $regexp, $help);
         }
         // create check field
         if ($f["type"] == "check") {
             $label = intval((string) $f["label"]);
             $length = intval((string) $f["length"]);
             if (isset($f->option)) {
                 foreach ($f->option as $o) {
                     $w = intval((string) $o);
                     if ($w > 0) {
                         $options[] = intval((string) $o);
                     } else {
                         $options[] = (string) $o;
                     }
                 }
             }
             $fields[$id] = new CheckField($id, $name, $label, $options, $length, $requested, $default, $help);
         }
         // create radio field
         if ($f["type"] == "radio") {
             $label = intval((string) $f["label"]);
             $length = intval((string) $f["length"]);
             if (isset($f->option)) {
                 foreach ($f->option as $o) {
                     $w = intval((string) $o);
                     if ($w > 0) {
                         $options[] = intval((string) $o);
                     } else {
                         $options[] = (string) $o;
                     }
                 }
             }
             $fields[$id] = new RadioField($id, $name, $label, $options, $length, $requested, $default, NULL, $help);
         }
         // create list field
         if ($f["type"] == "list") {
             $label = intval((string) $f["label"]);
             $length = intval((string) $f["length"]);
             if (isset($f->option)) {
                 foreach ($f->option as $o) {
                     $options[] = intval((string) $o);
                 }
             }
             $fields[$id] = new ListField($id, $name, $label, $options, $length, $requested, $default, $help);
         }
         // create filelist field
         if ($f["type"] == "filelist") {
             $label = intval((string) $f["label"]);
             $length = intval((string) $f["length"]);
             $dir = (string) $f["dir"];
             $recursdirs = isset($f["recursdirs"]) ? (string) $f["recursdirs"] : false;
             $fields[$id] = new FileListField($id, $name, $label, $regexp, $dir, $recursdirs, $length, $requested, $help);
         }
         // create file field
         if ($f["type"] == "file") {
             $label = intval((string) $f["label"]);
             $length = intval((string) $f["length"]);
             $maxlength = intval((string) $f["maxlength"]);
             if (isset($f->option)) {
                 foreach ($f->option as $o) {
                     $options[] = intval((string) $o);
                 }
             }
             $fields[$id] = new FileField($id, $name, $label, $length, $maxlength, $requested, $help);
         }
         // create submit field
         if ($f["type"] == "submit") {
             $label = intval((string) $f["label"]);
             $fields[$id] = new SubmitField($id, $name, $label, $help);
         }
         // create reset field
         if ($f["type"] == "reset") {
             $label = intval((string) $f["label"]);
             $fields[$id] = new ResetField($id, $name, $label, $help);
         }
         //TODO: Add XML process logic for further fields
     }
     // process messages and inject into Form object
     foreach ($xmlobj->messages[0]->message as $m) {
         $messages =& $form->getMessages();
         $messages[(string) $m["id"]][(string) $m["language"]] = (string) $m;
     }
     //TODO Implement Model creation from model
     return new FormAPI($form, $model);
 }