scaffold.php.svn-base

来自「j2me is based on j2mepolish, client & se」· SVN-BASE 代码 · 共 432 行 · 第 1/2 页

SVN-BASE
432
字号
		}	}/** * Saves or updates a model. * * @param array $params * @param string $type create or update * @return success on save/update, add/edit form if data is empty or error if save or update fails * @access private */	function __scaffoldSave($params = array(), $type) {		$thtml = 'edit';		$form = 'Edit';		$success = 'updated';		$formError = 'edit';		if ($this->controllerClass->_beforeScaffold($type)) {			if (empty($this->controllerClass->params['data'])) {				if ($type === 'create') {					$formError = 'add';				}				return $this->__scaffoldForm($params, $formError);			}			$this->controllerClass->set('fieldNames', $this->controllerClass->generateFieldNames());			$this->controllerClass->cleanUpFields();			if ($type == 'create') {				$this->controllerClass->{$this->modelKey}->create();				$thtml = 'add';				$form = 'Add';				$success = 'saved';			}			if ($this->controllerClass->{$this->modelKey}->save($this->controllerClass->params['data'])) {				if ($this->controllerClass->_afterScaffoldSave($type)) {					if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {						$this->controllerClass->Session->setFlash('The ' . Inflector::humanize($this->modelKey) . ' has been ' . $success . '.');						$this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));					} else {						return $this->controllerClass->flash('The ' . Inflector::humanize($this->modelKey) . ' has been ' . $success . '.',																			'/' . Inflector::underscore($this->controllerClass->viewPath));					}				} else {					return $this->controllerClass->_afterScaffoldSaveError($type);				}			} else {				if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {					$this->controllerClass->Session->setFlash('Please correct errors below.');				}				$this->controllerClass->set('data', $this->controllerClass->params['data']);				$this->controllerClass->set('fieldNames',														$this->controllerClass->generateFieldNames($this->__rebuild($this->controllerClass->params['data'])));				$this->controllerClass->validateErrors($this->controllerClass->{$this->modelKey});				$this->controllerClass->set('type', $form);				if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffolds' . DS . 'scaffold.' . $thtml . '.thtml')) {					return $this->controllerClass->render($this->actionView, '',																		APP . 'views' . DS . $this->viewPath . DS . 'scaffolds' . DS . 'scaffold.' . $thtml . '.thtml');				} elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml')) {					return $this->controllerClass->render($this->actionView, '',																		APP . 'views' . DS . 'scaffold' . DS . 'scaffold.' . $thtml . '.thtml');				} else {					return $this->controllerClass->render($this->actionView, '',																		LIBS . 'view' . DS . 'templates' . DS . 'scaffolds' . DS . 'edit.thtml');				}			}		} elseif ($this->controllerClass->_scaffoldError($type) === false) {			return $this->__scaffoldError();		}	}/** * Performs a delete on given scaffolded Model. * * @param array $params * @return success on delete error if delete fails * @access private */	function __scaffoldDelete($params = array()) {		if ($this->controllerClass->_beforeScaffold('delete')) {			$id = $params['pass'][0];			if ($this->controllerClass->{$this->modelKey}->del($id)) {				if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {					$this->controllerClass->Session->setFlash('The ' . Inflector::humanize($this->modelKey) . ' with id: ' . $id . ' has been deleted.');					$this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));				} else {					return $this->controllerClass->flash('The ' . Inflector::humanize($this->modelKey) . ' with id: ' . $id . ' has been deleted.',																		'/' . Inflector::underscore($this->controllerClass->viewPath));				}			} else {				if (isset($this->controllerClass->Session) && $this->controllerClass->Session->valid != false) {					$this->controllerClass->Session->setFlash('There was an error deleting the ' . Inflector::humanize($this->modelKey) . ' with the id ' . $id);					$this->controllerClass->redirect('/' . Inflector::underscore($this->controllerClass->viewPath));				} else {					return $this->controllerClass->flash('There was an error deleting the ' . Inflector::humanize($this->modelKey) . ' with the id ' . $id,																		'/' . Inflector::underscore($this->controllerClass->viewPath));				}			}		} elseif ($this->controllerClass->_scaffoldError('delete') === false) {			return $this->__scaffoldError();		}	}/** * Enter description here... * * @return unknown */	function __scaffoldError() {		if (file_exists(APP . 'views' . DS . $this->viewPath . DS . 'scaffolds' . DS . 'scaffold.error.thtml')) {			return $this->controllerClass->render($this->actionView, '',																APP . 'views' . DS . $this->viewPath	. DS . 'scaffolds' . DS . 'scaffold.error.thtml');		} elseif (file_exists(APP . 'views' . DS . 'scaffold' . DS . 'scaffold.error.thtml')) {			return $this->controllerClass->render($this->actionView, '',																APP . 'views' . DS . 'scaffold' . DS . 'scaffold.error.thtml');		} else {			return $this->controllerClass->render($this->actionView, '',																LIBS . 'view' . DS . 'templates' . DS . 'errors' . DS . 'scaffold_error.thtml');		}	}/** * When forms are submited the arrays need to be rebuilt if * an error occured, here the arrays are rebuilt to structure needed * * @param array $params data passed to forms * @return array rebuilds the association arrays to pass back to Controller::generateFieldNames() */	function __rebuild($params) {		foreach($params as $model => $field) {			if (!empty($field) && is_array($field)) {				$match = array_keys($field);				if ($model == $match[0]) {					$count = 0;					foreach($field[$model] as $value) {						$params[$model][$count][$this->controllerClass->{$this->modelKey}->{$model}->primaryKey] = $value;						$count++;					}					unset ($params[$model][$model]);				}			}		}		return $params;	}/** * When methods are now present in a controller * scaffoldView is used to call default Scaffold methods if: * <code> * var $scaffold; * </code> * is placed in the controller's class definition. * * @param string $url * @param string $controller_class * @param array $params * @since Cake v 0.10.0.172 * @access private */	function __scaffold($params) {		if (!in_array('Form', $this->controllerClass->helpers)) {			$this->controllerClass->helpers[] = 'Form';		}		if($this->controllerClass->constructClasses()){			$db =& ConnectionManager::getDataSource($this->controllerClass->{$this->modelKey}->useDbConfig);			if (isset($db)) {				if ($params['action'] === 'index' || $params['action'] === 'list' || $params['action'] === 'view'						|| $params['action'] === 'add' || $params['action'] === 'create'						|| $params['action'] === 'edit' || $params['action'] === 'update'						|| $params['action'] === 'delete') {					switch($params['action']) {						case 'index':							$this->__scaffoldIndex($params);						break;						case 'view':							$this->__scaffoldView($params);						break;						case 'list':							$this->__scaffoldIndex($params);						break;						case 'add':							$this->__scaffoldForm($params, 'add');						break;						case 'edit':							$this->__scaffoldForm($params, 'edit');						break;						case 'create':							$this->__scaffoldSave($params, 'create');						break;						case 'update':							$this->__scaffoldSave($params, 'update');						break;						case 'delete':							$this->__scaffoldDelete($params);						break;					}				} else {					return $this->cakeError('missingAction',													array(array('className' => Inflector::camelize($params['controller'] . "Controller"),																	'base' => $this->controllerClass->base,																	'action' => $params['action'],																	'webroot' => $this->controllerClass->webroot)));				}			} else {				return $this->cakeError('missingDatabase', array(array('webroot' => $this->controllerClass->webroot)));			}		} else {			return $this->cakeError('missingModel', array(array('className' => $this->modelKey, 'webroot' => '', 'base' => $this->base)));		}	}}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?