📄 controller.php.svn-base
字号:
* Validates a FORM according to the rules set up in the Model. * * @return int Number of errors * @access public */ function validateErrors() { $objects = func_get_args(); if (!count($objects)) { return false; } $errors = array(); foreach($objects as $object) { $errors = array_merge($errors, $this->{$object->name}->invalidFields($object->data)); } return $this->validationErrors = (count($errors) ? $errors : false); }/** * Gets an instance of the view object and prepares it for rendering the output, then * asks the view to actualy do the job. * * @param string $action * @param string $layout * @param string $file * @return controllers related views * @access public */ function render($action = null, $layout = null, $file = null) { $viewClass = $this->view; if ($this->view != 'View') { $viewClass = $this->view . 'View'; loadView($this->view); } $this->beforeRender(); $this->__viewClass =& new $viewClass($this); if (!empty($this->modelNames)) { foreach($this->modelNames as $model) { if (!empty($this->{$model}->validationErrors)) { $this->__viewClass->validationErrors[$model] = &$this->{$model}->validationErrors; } } } $this->autoRender = false; return $this->__viewClass->render($action, $layout, $file); }/** * Gets the referring URL of this request * * @param string $default Default URL to use if HTTP_REFERER cannot be read from headers * @param boolean $local If true, restrict referring URLs to local server * @access public */ function referer($default = null, $local = false) { $ref = env('HTTP_REFERER'); $base = FULL_BASE_URL . $this->webroot; if ($ref != null && (defined(FULL_BASE_URL) || FULL_BASE_URL)) { if (strpos($ref, $base) === 0) { return substr($ref, strlen($base) - 1); } elseif(!$local) { return $ref; } } if ($default != null) { return $default; } else { return '/'; } }/** * Sets data for this view. Will set title if the key "title" is in given $data array. * * @param array $data Array of * @access protected */ function _setArray($data) { foreach($data as $name => $value) { if ($name == 'title') { $this->_setTitle($value); } else { $this->viewVars[$name] = $value; } } }/** * Set the title element of the page. * * @param string $pageTitle Text for the title * @access private */ function _setTitle($pageTitle) { $this->pageTitle = $pageTitle; }/** * Shows a message to the user $time seconds, then redirects to $url * Uses flash.thtml as a layout for the messages * * @param string $message Message to display to the user * @param string $url Relative URL to redirect to after the time expires * @param int $time seconds to show the message * @access public */ function flash($message, $url, $pause = 1) { $this->autoRender = false; $this->autoLayout = false; $this->set('url', $this->base . $url); $this->set('message', $message); $this->set('pause', $pause); $this->set('page_title', $message); if (file_exists(VIEWS . 'layouts' . DS . 'flash.thtml')) { $flash = VIEWS . 'layouts' . DS . 'flash.thtml'; } elseif ($flash = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . "layouts" . DS . 'flash.thtml')) { } $this->render(null, false, $flash); }/** * Replaced with Controller::flash(); * @deprecated will not be avialable after 1.1.x.x */ function flashOut($message, $url, $pause = 1) { trigger_error('(Controller::flashOut()) Deprecated: Use Controller::flash() instead', E_USER_WARNING); $this->autoRender = false; $this->autoLayout = false; $this->set('url', $url); $this->set('message', $message); $this->set('pause', $pause); $this->set('page_title', $message); if (file_exists(VIEWS . 'layouts' . DS . 'flash.thtml')) { $flash = VIEWS . 'layouts' . DS . 'flash.thtml'; } elseif($flash = fileExistsInPath(LIBS . 'view' . DS . 'templates' . DS . "layouts" . DS . 'flash.thtml')) { } $this->render(null, false, $flash); }/** * This function creates a $fieldNames array for the view to use. * * @param array $data * @param boolean $doCreateOptions * @return field name arrays for the view * @access public */ function generateFieldNames($data = null, $doCreateOptions = true) { $fieldNames = array(); $model = $this->modelClass; $modelKey = $this->modelKey; $table = $this->{$model}->table; $objRegistryModel =& ClassRegistry::getObject($modelKey); foreach($objRegistryModel->_tableInfo as $tables) { foreach($tables as $tabl) { if ($objRegistryModel->isForeignKey($tabl['name'])) { if(false !== strpos($tabl['name'], "_id")) { $niceName = substr($tabl['name'], 0, strpos($tabl['name'], "_id" )); } else { $niceName = $niceName = $tabl['name']; } $fkNames = $this->{$model}->keyToTable[$tabl['name']]; $fieldNames[$tabl['name']]['table'] = $fkNames[0]; $fieldNames[$tabl['name']]['prompt'] = Inflector::humanize($niceName); $fieldNames[$tabl['name']]['model'] = $fkNames[1]; $fieldNames[$tabl['name']]['modelKey'] = $this->{$model}->tableToModel[$fieldNames[$tabl['name']]['table']]; $fieldNames[$tabl['name']]['controller'] = Inflector::pluralize($this->{$model}->tableToModel[$fkNames[0]]); $fieldNames[$tabl['name']]['foreignKey'] = true; } else if('created' != $tabl['name'] && 'updated' != $tabl['name']) { $fieldNames[$tabl['name']]['prompt'] = Inflector::humanize($tabl['name']); } else if('created' == $tabl['name']) { $fieldNames[$tabl['name']]['prompt'] = 'Created'; } else if('updated' == $tabl['name']) { $fieldNames[$tabl['name']]['prompt'] = 'Modified'; } $fieldNames[$tabl['name']]['tagName'] = $model . '/' . $tabl['name']; $validationFields = $objRegistryModel->validate; if (isset($validationFields[$tabl['name']])) { if (VALID_NOT_EMPTY == $validationFields[$tabl['name']]) { $fieldNames[$tabl['name']]['required'] = true; $fieldNames[$tabl['name']]['errorMsg'] = "Required Field"; } } $lParenPos = strpos($tabl['type'], '('); $rParenPos = strpos($tabl['type'], ')'); if (false != $lParenPos) { $type = substr($tabl['type'], 0, $lParenPos); $fieldLength = substr($tabl['type'], $lParenPos + 1, $rParenPos - $lParenPos - 1); } else { $type = $tabl['type']; } switch($type) { case "text": $fieldNames[$tabl['name']]['type'] = 'area'; break; case "string": if (isset($fieldNames[$tabl['name']]['foreignKey'])) { $fieldNames[$tabl['name']]['type'] = 'select'; $fieldNames[$tabl['name']]['options'] = array(); $otherModel =& ClassRegistry::getObject(Inflector::underscore($fieldNames[$tabl['name']]['modelKey'])); if (is_object($otherModel)) { if ($doCreateOptions) { $otherDisplayField = $otherModel->getDisplayField(); $otherModel->recursive = 0; $rec = $otherModel->findAll(); foreach($rec as $pass) { foreach($pass as $key => $value) { if ($key == $this->{$model}->tableToModel[$fieldNames[$tabl['name']]['table']] && isset($value[$otherModel->primaryKey]) && isset($value[$otherDisplayField])) { $fieldNames[$tabl['name']]['options'][$value[$otherModel->primaryKey]] = $value[$otherDisplayField]; } } } } $fieldNames[$tabl['name']]['selected'] = $data[$model][$tabl['name']]; } } else { $fieldNames[$tabl['name']]['type'] = 'input'; } break; case "boolean": $fieldNames[$tabl['name']]['type'] = 'checkbox'; break; case "integer": case "float": if (strcmp($tabl['name'], $this->$model->primaryKey) == 0) { $fieldNames[$tabl['name']]['type'] = 'hidden'; } else if(isset($fieldNames[$tabl['name']]['foreignKey'])) { $fieldNames[$tabl['name']]['type'] = 'select'; $fieldNames[$tabl['name']]['options'] = array(); $otherModel =& ClassRegistry::getObject(Inflector::underscore($fieldNames[$tabl['name']]['modelKey'])); if (is_object($otherModel)) { if ($doCreateOptions) { $otherDisplayField = $otherModel->getDisplayField(); $otherModel->recursive = 0; $rec = $otherModel->findAll(); foreach($rec as $pass) { foreach($pass as $key => $value) { if ($key == $this->{$model}->tableToModel[$fieldNames[$tabl['name']]['table']] && isset($value[$otherModel->primaryKey]) && isset($value[$otherDisplayField])) { $fieldNames[$tabl['name']]['options'][$value[$otherModel->primaryKey]] = $value[$otherDisplayField]; } } } } $fieldNames[$tabl['name']]['selected'] = $data[$model][$tabl['name']]; } } else { $fieldNames[$tabl['name']]['type'] = 'input'; } break; case "enum": $fieldNames[$tabl['name']]['type'] = 'select'; $fieldNames[$tabl['name']]['options'] = array(); $enumValues = split(',', $fieldLength); foreach($enumValues as $enum) { $enum = trim($enum, "'"); $fieldNames[$tabl['name']]['options'][$enum] = $enum; } $fieldNames[$tabl['name']]['selected'] = $data[$model][$tabl['name']]; break; case "date": case "datetime": case "time": case "year": if (0 != strncmp("created", $tabl['name'], 7) && 0 != strncmp("modified", $tabl['name'], 8)) { $fieldNames[$tabl['name']]['type'] = $type; } if (isset($data[$model][$tabl['name']])) { $fieldNames[$tabl['name']]['selected'] = $data[$model][$tabl['name']]; } else { $fieldNames[$tabl['name']]['selected'] = null; } break; default: break; } } foreach($objRegistryModel->hasAndBelongsToMany as $relation => $relData) { $modelName = $relData['className']; $manyAssociation = $relation; $modelKeyM = Inflector::underscore($modelName); $modelObject =& new $modelName(); if ($doCreateOptions) { $otherDisplayField = $modelObject->getDisplayField(); $fieldNames[$modelKeyM]['model'] = $modelName; $fieldNames[$modelKeyM]['prompt'] = "Related " . Inflector::humanize(Inflector::pluralize($modelName)); $fieldNames[$modelKeyM]['type'] = "selectMultiple"; $fieldNames[$modelKeyM]['tagName'] = $manyAssociation . '/' . $manyAssociation; $modelObject->recursive = 0; $rec = $modelObject->findAll(); foreach($rec as $pass) { foreach($pass as $key => $value) { if ($key == $modelName && isset($value[$modelObject->primaryKey]) && isset($value[$otherDisplayField])) { $fieldNames[$modelKeyM]['options'][$value[$modelObject->primaryKey]] = $value[$otherDisplayField]; } } } if (isset($data[$manyAssociation])) { foreach($data[$manyAssociation] as $key => $row) { $fieldNames[$modelKeyM]['selected'][$row[$modelObject->primaryKey]] = $row[$modelObject->primaryKey]; } } } } } return $fieldNames; }/** * Converts POST'ed model data to a model conditions array, suitable for a find or findAll Model query * * @param array $data POST'ed data organized by model and field * @return array An array of model conditions * @access public */ function postConditions($data) { if (!is_array($data) || empty($data)) { return null; } $conditions = array(); foreach($data as $model => $fields) { foreach($fields as $field => $value) { $conditions[$model . '.' . $field] = $value; } } return $conditions; }/** * Cleans up the date fields of current Model. * * @param string $modelName * @access public */ function cleanUpFields($modelName = null) { if ($modelName == null) { $modelName = $this->modelClass; } foreach($this->{$modelName}->_tableInfo as $table) { foreach($table as $field) { if ('date' == $field['type'] && isset($this->params['data'][$modelName][$field['name'] . '_year'])) { $newDate = $this->params['data'][$modelName][$field['name'] . '_year'] . '-'; $newDate .= $this->params['data'][$modelName][$field['name'] . '_month'] . '-'; $newDate .= $this->params['data'][$modelName][$field['name'] . '_day']; unset($this->params['data'][$modelName][$field['name'] . '_year']); unset($this->params['data'][$modelName][$field['name'] . '_month']); unset($this->params['data'][$modelName][$field['name'] . '_day']); unset($this->params['data'][$modelName][$field['name'] . '_hour']); unset($this->params['data'][$modelName][$field['name'] . '_min']); unset($this->params['data'][$modelName][$field['name'] . '_meridian']); $this->params['data'][$modelName][$field['name']] = $newDate; $this->data[$modelName][$field['name']] = $newDate; } elseif('datetime' == $field['type'] && isset($this->params['data'][$modelName][$field['name'] . '_year'])) { $hour = $this->params['data'][$modelName][$field['name'] . '_hour']; if ($hour != 12 && (isset($this->params['data'][$modelName][$field['name'] . '_meridian']) && 'pm' == $this->params['data'][$modelName][$field['name'] . '_meridian'])) { $hour = $hour + 12; } $newDate = $this->params['data'][$modelName][$field['name'] . '_year'] . '-'; $newDate .= $this->params['data'][$modelName][$field['name'] . '_month'] . '-'; $newDate .= $this->params['data'][$modelName][$field['name'] . '_day'] . ' '; $newDate .= $hour . ':' . $this->params['data'][$modelName][$field['name'] . '_min'] . ':00'; unset($this->params['data'][$modelName][$field['name'] . '_year']); unset($this->params['data'][$modelName][$field['name'] . '_month']); unset($this->params['data'][$modelName][$field['name'] . '_day']); unset($this->params['data'][$modelName][$field['name'] . '_hour']); unset($this->params['data'][$modelName][$field['name'] . '_min']); unset($this->params['data'][$modelName][$field['name'] . '_meridian']); $this->params['data'][$modelName][$field['name']] = $newDate; $this->data[$modelName][$field['name']] = $newDate; } elseif('time' == $field['type'] && isset($this->params['data'][$modelName][$field['name'] . '_hour'])) { $hour = $this->params['data'][$modelName][$field['name'] . '_hour']; if ($hour != 12 && (isset($this->params['data'][$modelName][$field['name'] . '_meridian']) && 'pm' == $this->params['data'][$modelName][$field['name'] . '_meridian'])) { $hour = $hour + 12; } $newDate = $hour . ':' . $this->params['data'][$modelName][$field['name'] . '_min'] . ':00'; unset($this->params['data'][$modelName][$field['name'] . '_hour']); unset($this->params['data'][$modelName][$field['name'] . '_min']); unset($this->params['data'][$modelName][$field['name'] . '_meridian']); $this->params['data'][$modelName][$field['name']] = $newDate; $this->data[$modelName][$field['name']] = $newDate; } } } }/** * Called before the controller action. Overridden in subclasses. * * @access public */ function beforeFilter() { }/** * Called after the controller action is run, but before the view is rendered. Overridden in subclasses. * * @access public */ function beforeRender() { }/** * Called after the controller action is run and rendered. Overridden in subclasses. * * @access public */ function afterFilter() { }/** * This method should be overridden in child classes. * * @param string $method name of method called example index, edit, etc. * @return boolean * @access protected */ function _beforeScaffold($method) { return true; }/** * This method should be overridden in child classes. * * @param string $method name of method called either edit or update. * @return boolean * @access protected */ function _afterScaffoldSave($method) { return true; }/** * This method should be overridden in child classes. * * @param string $method name of method called either edit or update. * @return boolean * @access protected */ function _afterScaffoldSaveError($method) { return true; }/** * This method should be overridden in child classes. * If not it will render a scaffold error. * Method MUST return true in child classes * * @param string $method name of method called example index, edit, etc. * @return boolean * @access protected */ function _scaffoldError($method) { return false; }/** * Used to convert HABTM data into an array for selectTag * * @param array $data * @param string $key * @return array * @access protected */ function _selectedArray($data, $key = 'id') { $array = array(); if(!empty($data)) { foreach($data as $var) { $array[$var[$key]] = $var[$key]; } } return $array; }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -