/** * Render the output of the widget and add it to the DOM * * @param One_Model $model * @param One_Dom $d */ protected function _render($model, One_Dom $dom) { $src = $this->getCfg('src'); if (trim($src) == '') { throw new One_Exception("A field of type 'nscript' should have a 'src'-attribute defining the nanoScript file to parse."); } One_Script_Factory::saveSearchPath(); One_Script_Factory::clearSearchPath(); $useLang = $this->getCfg('language'); if ('' == trim($useLang)) { $useLang = strtolower(One_Config::get('app.language')); } die('deprecated stuff found in ' . __FILE__ . ':' . __LINE); $cps = One_Config::getInstance()->getCustomPaths(); foreach ($cps as $cp) { One_Script_Factory::addSearchPath($cp . '/views/' . One_Config::get('app.name') . '/' . $model->getScheme()->getName() . '/language/' . $useLang . '/'); } foreach ($cps as $cp) { One_Script_Factory::addSearchPath($cp . '/views/' . One_Config::get('app.name') . '/' . $model->getScheme()->getName() . '/'); } $ns = new One_Script(); $ns->load($src); if (!$ns->isError()) { if ($this->getID()) { $ns->set('id', $this->getID()); } if ($this->getName()) { $ns->set('name', $this->getName()); } if ($this->getLabel()) { $ns->set('label', $this->getLabel()); } if ($this->getValue($model)) { $ns->set('value', $this->getValue($model)); } $ns->set('model', $model); $dom->add($ns->execute()); } else { throw new One_Exception($ns->error); } $dom->add($this->value); One_Script_Factory::restoreSearchPath(); }
/** * 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; }
/** * Parse the output of the widget with nanoscript * * @param One_Model $model * @param array $data */ protected function parse($model, $data = array()) { // general Dataset $data['excludeError'] = in_array($this->getCfg('excludeError'), array('true', 'yes', '1', 'exclude', 'excludeError')) ? 1 : 0; // determine if we need to look for the template-file in a subfolder of widget $widgetClass = preg_match('/One_Form_Widget_Default_/', get_class($this)) ? get_parent_class($this) : get_class($this); $current = str_replace('One_Form_Widget_', '', $widgetClass); $parts = preg_split('/_/', strtolower($current)); // array_pop($parts); $wtype = implode(DIRECTORY_SEPARATOR, $parts); $formChrome = One_Config::get('form.chrome', ''); $pattern = "%ROOT%/views/" . "{%APP%,default}/" . "oneform/" . ($formChrome ? "{" . $formChrome . '/,}' : '') . "widget/" . ($wtype ? "{" . $wtype . "/,}" : '') . "{%LANG%/,}"; One_Script_Factory::pushSearchPath($pattern); $script = new One_Script(); // $script->load($this->_type . '.html'); $script->load($wtype . '.html'); if ($script->error) { One_Script_Factory::popSearchPath(); throw new One_Exception('Error loading template for widget ' . $this->_type . ' : ' . $script->error); } $dom = One_Repository::createDom(); $dom->add($script->execute($data)); return $dom; }