editorClasses = $editorClasses; if(is_object($src)) { if($src instanceof iModel) $this->model = &$src; else throw new \InvalidArgumentException(get_class($src) . ' does not implement the iModel interface'); } elseif(is_string($src)) { if(!class_exists($src)) throw new \InvalidArgumentException($src . ' is a unknown class'); $this->model = new $src(); if(!($this->model instanceof iModel)) throw new \InvalidArgumentException(get_class($src) . ' does not implement the iModel interface'); } elseif ($src === false) { return; } else throw new \InvalidArgumentException('Class needs to be an object or a classname'); $fields = call_user_func(array(get_class($this->model),'fieldInfo')); foreach($fields as $name=>$info) $this->replaceEditorInfo($name, $info); } public function replaceEditorInfo($name, $newInfo) { $this->editors[$name] = $this->generateEditorFor($newInfo); $this->editors[$name]->setName($name); if (isset($this->model->$name)) $this->editors[$name]->loadFromObject($this->model); } public function setMethod($method) { $this->method = strtoupper((string) $method); } public function setEncoding($encoding) { $this->encoding = (string) $encoding; } public function setTargetLink($link) { if($this->started) throw new IllegalStateException('form generation has been started'); $this->targetLink = $link; } public function setTargetParams($params) { if($this->started) throw new IllegalStateException('form generation has been started'); $this->targetParams = $params; } public function genStart() { if($this->started) throw new IllegalStateException('form generation has been started'); $this->started = true; $st = 'id)) $st .= ' id="' . htmlentities($this->id, ENT_COMPAT, 'utf-8') . '"'; if(!empty($this->css)) $st .= ' style="' . htmlentities($this->css, ENT_COMPAT, 'utf-8') . '"'; if(!empty($this->cssClass)) $st .= ' style="' . htmlentities($this->cssClass, ENT_COMPAT, 'utf-8') . '"'; if(!empty($this->targetLink)) $st .= ' action="' . htmlentities($this->targetLink, ENT_COMPAT, 'utf-8') . '"'; if(!empty($this->encoding)) $st .= ' enctype="' . htmlentities($this->encoding, ENT_COMPAT, 'utf-8') . '"'; $st .= ' method="' . $this->method . '" accept-charset="UTF-8">'."\n"; foreach($this->targetParams as $key=>$value) $st .= ''."\n"; foreach ($this->addOns as $addOn) { if ($addOn instanceof iPlugInForm) $st .= $addOn->genFormAddon($this); } $jsimport = array(); foreach($this->editors as $name => $e) { $content = $e->generate($this); $jsimport = array_merge($jsimport, empty($content['js_import']) ? array() : $content['js_import']); } $jsimport = array_unique($jsimport); foreach($jsimport as $js) $st .= ''."\n"; return $st; } /** * Fetches the editor component * * @param string $name * the element name * @throws IllegalStateException the form creation has not been started * @throws \InvalidArgumentException if the editor is not defined in the form * @return iComEditor the editor component */ public function getElement($name) { if(!isset($this->editors[$name])) { if(!isset($this->extraEd[$name])) throw new \InvalidArgumentException('form has no editor with the name ' . $name); if(!empty($this->editorParams[$name])) { foreach ($this->editorParams[$name] as $key => $value) { if($key == self::EP_READONLY) $this->extraEd[$name]->setReadOnly((boolean) $value); elseif($key == self::EP_CSS_CLASS) $this->extraEd[$name]->setCSSClass((string) $value); elseif($key == self::EP_INLINE_CSS) $this->extraEd[$name]->setInlineCSS((string) $value); else $this->extraEd[$name]->setParam($key, $value); } } self::addCheckSettings($this->extraEd[$name], $this->checks, $name); return $this->extraEd[$name]; } if(!empty($this->editorParams[$name])) { foreach ($this->editorParams[$name] as $key => $value) { if($key == self::EP_READONLY) $this->editors[$name]->setReadOnly((boolean) $value); elseif($key == self::EP_CSS_CLASS) $this->editors[$name]->setCSSClass((string) $value); elseif($key == self::EP_INLINE_CSS) $this->editors[$name]->setInlineCSS((string) $value); else $this->editors[$name]->setParam($key, $value); } } self::addCheckSettings($this->editors[$name], $this->checks, $name); return $this->editors[$name]; } static protected function addCheckSettings(&$editor, array $checks, $name) { foreach ($checks as $check) { if ($check->field == $name) { switch ($check->condition) { case FormCheck::CON_REQUIRED: $editor->setRequired(true); break; default: throw new \Exception('Unknown condition'); } } } } /** * Creates the code of an element * * @param string $name * the element name * @throws IllegalStateException the form creation has not been started * @throws \InvalidArgumentException if the editor is not defined in the form * @return string the html code */ public function genElement($name) { if(!$this->started) throw new IllegalStateException('form generation has not been started'); $editor = $this->getElement($name); $content = $editor->generate($this); return $content['html']; } /** * Generate a submit button. * * @param string $text * text on the button * @param string $cssClasses * additional css classes * @throws IllegalStateException if called without calling genStart() first */ public function genSubmit($text = false, $cssClasses = false) { if(!$this->started) throw new IllegalStateException('form generation has not been started'); $st = $cssClasses ? ''; return $st; } /** * Generate a form reset button. * * @param string $text * text on the button * @param string $cssClasses * additional css classes * @throws IllegalStateException if called without calling genStart() first */ public function genReset($text = false, $cssClasses = false) { if(!$this->started) throw new IllegalStateException('form generation has not been started'); $st = $cssClasses ? ''; return $st; } /** * Generates the end tag and stuff of a form. * * This finalizes the form. To create another form, you need to call genStart() first after this. * * @throws IllegalStateException if called without calling genStart() first */ public function genEnd() { if(!$this->started) throw new IllegalStateException('form generation has not been started'); $endjs = ''; foreach($this->editors as $e) { $content = $e->generate($this); $endjs .= empty($content['js_end']) ? '' : $content['js_end']; } if(!empty($endjs)) $endjs = "\n\n"; $st = $endjs . ''; $this->started = false; return $st; } }