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->editors[$name] = $this->generateEditorFor($info); $this->editors[$name]->setName($name); $this->editors[$name]->loadFromObject($this->model); } } public function replaceEditorInfo($name, $newInfo) { $this->editors[$name] = $this->generateEditorFor($newInfo); $this->editors[$name]->setName($name); $this->editors[$name]->loadFromObject($this->model); } public function setRequiredFields(array $required) { $this->required = $required; } public function setMethod($method) { $this->method = strtoupper((string) $method); } public function setEncoding($encoding) { $this->encoding = (string) $encoding; } public function setEditorParams($editor, array $param) { $this->editorParams[$editor] = $param; } public function getEditorParams($editor) { return $this->editorParams[$editor]; } 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() { $this->started = true; $st = '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 . '">'; foreach($this->targetParams as $key=>$value) $st .= ''; $jsimport = array(); foreach($this->editors as $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 .= ''; return $st; } /** * Creates the code of an element * * @param string $name * the element name * @throws MFIllegalStateException 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'); 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])) { if(isset($this->editorParams[$name][self::EP_CSS_CLASS])) $this->extraEd[$name]->setCSSClass((string) $this->editorParams[$name][self::EP_CSS_CLASS]); if(isset($this->editorParams[$name][self::EP_INLINE_CSS])) $this->extraEd[$name]->setInlineCSS((string) $this->editorParams[$name][self::EP_INLINE_CSS]); if(isset($this->editorParams[$name][self::EP_EXTRA])) $this->extraEd[$name]->setParam(self::EP_EXTRA, $this->editorParams[$name][self::EP_EXTRA]); if(isset($this->editorParams[$name][self::EP_TEXT])) $this->extraEd[$name]->setParam(self::EP_TEXT, $this->editorParams[$name][self::EP_TEXT]); } $content = $this->extraEd[$name]->generate($this); return $content['html']; } if(!empty($this->editorParams[$name])) { if(isset($this->editorParams[$name][self::EP_CSS_CLASS])) $this->editors[$name]->setCSSClass((string) $this->editorParams[$name][self::EP_CSS_CLASS]); if(isset($this->editorParams[$name][self::EP_INLINE_CSS])) $this->editors[$name]->setInlineCSS((string) $this->editorParams[$name][self::EP_INLINE_CSS]); if(isset($this->editorParams[$name][self::EP_EXTRA])) $this->editors[$name]->setParam(self::EP_EXTRA, $this->editorParams[$name][self::EP_EXTRA]); if(isset($this->editorParams[$name][self::EP_TEXT])) $this->editors[$name]->setParam(self::EP_TEXT, $this->editorParams[$name][self::EP_TEXT]); } $content = $this->editors[$name]->generate($this); return $content['html']; } public function genSubmit($text = false) { if(!$this->started) throw new MFIllegalStateException('form generation has not been started'); $st = 'started) throw new MFIllegalStateException('form generation has not been started'); $st = 'started) throw new MFIllegalStateException('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($endjs != '') $endjs = "\n"; $st = $endjs . ''; return $st; } }