model = $type; else throw new \UnexpectedValueException(get_class($type) . ' does not implement the iModel interface'); } elseif(is_string($type)) { if(!class_exists($type)) throw new \InvalidArgumentException($src . ' is a unknown class'); $this->model = new $type(); if(!($this->model instanceof \MCMS\Model\iModel)) throw new \UnexpectedValueException(get_class($type) . ' does not implement the iModel interface'); } elseif(is_array($type)) { $this->asArray = true; $this->model = $type; } else throw new \InvalidArgumentException('Type needs to be an object, array or a classname'); } else $this->model = false; $this->editorClasses = $editorClasses; $this->required = $required; if($type !== false) { if(!$this->asArray) { $fields = call_user_func(array(get_class($this->model),'fieldInfo')); foreach($fields as $name=>$info) $this->editors[$name] = $this->generateEditorFor($info); } else { foreach($this->model as $name=>$info) $this->editors[$name] = $this->generateEditorFor($info); } } // foreach($this->editors[$name] as $name=>$editor) // $this->editors[$name] = $this->generateEditorFor($info); } public function replaceEditorInfo($name, $newInfo) { $this->editors[$name] = $this->generateEditorFor($newInfo); } public function setRequiredFields(array $required) { $this->required = $required; } /** * fetches the form data */ public function fetch($input = NULL) { if(!$this->model) throw new \BadMethodCallException('Could not fetch model with modelless form'); if(is_null($input)) $input = self::getInputArray(); if($input instanceof \MCMS\Request) $input = $input->getParam(); $missing = array(); foreach($this->required as $rField) { if(!isset($input[$rField]) || trim($input[$rField]) === '') $missing[] = $rField; } if(count($missing) > 0) throw new \MissingArgumentException($missing); if($this->asArray) { $result = array(); foreach($this->model as $key->value) { $result = $input[$key]; // TODO: datatype convertation } return $result; } else { $fields = call_user_func(array(get_class($this->model),'fieldInfo')); foreach($fields as $name=>$info) { if(isset($input[$name])) $this->model->$name = $this->editors[$name]->parseInput($input[$name], $this); } return $this->model; } } public function fetchExtraField($name, $input = NULL) { if(is_null($input)) $input = self::getInputArray(); if(!isset($this->extraEd[$name])) throw new \InvalidArgumentException('undefined field ' . $name); $value = $this->extraEd[$name]->parseInput($input[$name], $this); return $value; } public function fetchOnly(array $field2fetch, $input = NULL) { if(!$this->model) throw new \BadMethodCallException('Could not fetch model with modelless form'); if(is_null($input)) $input = self::getInputArray(); if($input instanceof MMRequest) $input = $input->getParam(); if($this->asArray) { $result = array(); foreach($this->model as $key->value) { if(in_array($key, $field2fetch)) $result = $input[$key]; // TODO: datatype convertation } return $result; } else { $fields = call_user_func(array(get_class($this->model),'fieldInfo')); foreach($fields as $name=>$info) { if(isset($input[$name]) && in_array($name, $field2fetch)) $this->model->$name = $this->editors[$name]->parseInput($input[$name]); } return $this->model; } } public function fetchFile($fieldname) { if(!isset($_FILES)) throw new \IllegalStateException('$_FILES array does not exist'); if(!isset($_FILES[$fieldname])) throw new \IllegalStateException('$_FILES array does not contain key ' . $fieldname); if(is_uploaded_file($_FILES[$fieldname]['tmp_name'])) if(!isset($_FILES[$fieldname]['tmp_name'])) throw new \IllegalStateException('tmpfile was not uploaded'); return new \MFFile($_FILES[$fieldname]['tmp_name']); } public function isFileUploaded($fieldname) { if(!isset($_FILES) || !isset($_FILES[$fieldname])) return false; if(is_uploaded_file($_FILES[$fieldname]['tmp_name'])) return true; return false; } public static function getInputArray() { switch(strtoupper($_SERVER['REQUEST_METHOD'])) { case 'GET': return $_GET; case 'POST': return $_POST; default: throw new \IllegalStateException('only get and put are allowed'); } } }