Ejemplo n.º 1
0
 /**
  * Create a new OneFormContainerForm
  *
  * @param One_Scheme $scheme
  * @param string $formName
  * @param string $action
  * @param string $formFile location of the file, admin/form.xml by default
  * @return OneFormContainerForm
  */
 public static function createForm(One_Scheme $scheme, $formFile = NULL, $language = NULL, $formName = 'oneForm', $action = '', $method = 'post')
 {
     if (is_null($language)) {
         $language = One_Config::get('app.language');
     }
     // search for appropriate form.xml
     $pattern = "%ROOT%/views/" . "{" . ($scheme->getName() != '' ? "%APP%/{$scheme->getName()}," : "") . "%APP%,default}" . DIRECTORY_SEPARATOR . "{%LANG%" . DIRECTORY_SEPARATOR . ",}";
     $filepath = One_Locator::locateUsing($formFile . '.xml', $pattern);
     try {
         if ($filepath !== null) {
             $form = One_Form_Reader_Xml::load($filepath, $scheme, $language, $formName, $action, $method);
             //          echo '<pre>';
             //          print_r($form);
             //          echo '</pre>';
             //          echo '<hr>';
             //          print_r(self::createDefaultForm($scheme, $formFile, $language, $formName, $action, $method));
         } else {
             $form = self::createDefaultForm($scheme, $formFile, $language, $formName, $action, $method);
         }
     } catch (Exception $e) {
         $form = self::createDefaultForm($scheme, $formFile, $language, $formName, $action, $method);
     }
     self::addObligatedWidgets($form, $scheme);
     return $form;
 }
Ejemplo n.º 2
0
 /**
  * Loads a form definition
  *
  * @param One_Scheme $scheme
  * @param $formFile
  * @return One_Form_Container_Form
  */
 public static function load($filepath, $scheme, $language = NULL, $formName = 'oneForm', $action = '', $method = 'post')
 {
     $templater = One_Repository::getTemplater(NULL, false);
     // -----------------
     // TODO: this section of code is absolute horror
     //    $filepath = One::getInstance()->locate('meta'.DIRECTORY_SEPARATOR.'scheme'.DIRECTORY_SEPARATOR.$fileName.'.xml');
     //    $oldSearchpaths = $templater->getSearchpath();
     //    $templater->clearSearchpath();
     //    $pattern = "%ROOT%/views/"
     //      . "{" . ($scheme->getName() != '' ? "%APP%/{$scheme->getName()}," : "") . "%APP%,default}" . DIRECTORY_SEPARATOR
     //      . "{%LANG%" . DIRECTORY_SEPARATOR . ",}";
     //
     //    $templater->addSearchPath($pattern);
     //    $templater->setFile('form.xml');
     //    if ($templater->hasError()) {
     //      return self::createDefaultForm($scheme, $formFile, $language, $formName, $action, $method);
     ////      throw new One_Exception($templater->getError());
     //    }
     //    $templater->setSearchpath($oldSearchpaths);
     $script = new One_Script();
     $script->load($filepath);
     $parsedContent = $script->execute();
     if ($templater->hasError()) {
         throw new One_Exception($templater->getError());
     }
     // -----------------
     self::$_dom = new DOMDocument('1.0', 'utf-8');
     $validFile = self::$_dom->loadXML($parsedContent);
     if ($validFile !== false) {
         // load rules if any
         self::loadConditions();
         // load redirects if any
         $redirects = self::loadRedirects();
         // first element in the xml file should be a container of the type 'form'
         if (strtolower(self::$_dom->firstChild->localName) == 'form') {
             $formEle = self::$_dom->firstChild;
             $formName = trim($formEle->getAttribute('id')) != '' ? trim($formEle->getAttribute('id')) : $formName;
             $action = trim($formEle->getAttribute('action')) != '' ? trim($formEle->getAttribute('action')) : $action;
             $attributes = array();
             $rawAttributes = $formEle->attributes;
             for ($i = 0; $i < $rawAttributes->length; $i++) {
                 $attribute = $rawAttributes->item($i);
                 $attributes[$attribute->localName] = $attribute->value;
             }
             if (isset($attributes['type']) && trim(strtolower($attributes['type'])) == 'search') {
                 self::$_defaultWidget = 'search';
             } else {
                 self::$_defaultWidget = 'scalar';
             }
             if (count($redirects) > 0) {
                 $attributes['redirects'] = $redirects;
             }
             $form = new One_Form_Container_Form($formName, $action, $method, $attributes);
             foreach ($formEle->childNodes as $child) {
                 if ($child instanceof DOMElement) {
                     self::_parseToForm($child, $form);
                 }
             }
         } else {
             throw new One_Exception('Form definition "' . $fileName . '" found, but no form defined');
         }
     } else {
         return self::createDefaultForm($scheme, $formFile, $language, $formName, $action, $method);
     }
     //    $templater->clearSearchpath();
     //    $templater->setSearchpath($oldSearchpaths);
     //      print_r($form);
     return $form;
 }