controller = $controller; $count = count($this->__passedVars); for ($j = 0; $j < $count; $j++) { $var = $this->__passedVars[$j]; $this->{$var} = $controller->{$var}; } $this->redirect = array('action' => 'index'); $this->modelClass = $controller->modelClass; $this->modelKey = $controller->modelKey; if (!is_object($this->controller->{$this->modelClass})) { throw new MissingModelException($this->modelClass); } $this->ScaffoldModel = $this->controller->{$this->modelClass}; $this->scaffoldTitle = Inflector::humanize(Inflector::underscore($this->viewPath)); $this->scaffoldActions = $controller->scaffold; $title_for_layout = __d('cake', 'Scaffold :: ') . Inflector::humanize($request->action) . ' :: ' . $this->scaffoldTitle; $modelClass = $this->controller->modelClass; $primaryKey = $this->ScaffoldModel->primaryKey; $displayField = $this->ScaffoldModel->displayField; $singularVar = Inflector::variable($modelClass); $pluralVar = Inflector::variable($this->controller->name); $singularHumanName = Inflector::humanize(Inflector::underscore($modelClass)); $pluralHumanName = Inflector::humanize(Inflector::underscore($this->controller->name)); $scaffoldFields = array_keys($this->ScaffoldModel->schema()); $associations = $this->_associations(); $this->controller->set(compact( 'title_for_layout', 'modelClass', 'primaryKey', 'displayField', 'singularVar', 'pluralVar', 'singularHumanName', 'pluralHumanName', 'scaffoldFields', 'associations' )); if ($this->controller->viewClass) { $this->controller->viewClass = 'Scaffold'; } $this->_validSession = ( isset($this->controller->Session) && $this->controller->Session->valid() != false ); $this->_scaffold($request); } /** * Outputs the content of a scaffold method passing it through the Controller::afterFilter() * * @return void */ protected function _output() { $this->controller->afterFilter(); $this->controller->response->send(); } /** * Renders a view action of scaffolded model. * * @param CakeRequest $request Request Object for scaffolding * @return mixed A rendered view of a row from Models database table */ protected function _scaffoldView(CakeRequest $request) { if ($this->controller->_beforeScaffold('view')) { if (isset($request->params['pass'][0])) { $this->ScaffoldModel->id = $request->params['pass'][0]; } if (!$this->ScaffoldModel->exists()) { throw new NotFoundException(__d('cake', 'Invalid %s', Inflector::humanize($this->modelKey))); } $this->ScaffoldModel->recursive = 1; $this->controller->request->data = $this->ScaffoldModel->read(); $this->controller->set( Inflector::variable($this->controller->modelClass), $this->request->data ); $this->controller->render($this->request['action'], $this->layout); $this->_output(); } elseif ($this->controller->_scaffoldError('view') === false) { return $this->_scaffoldError(); } } /** * Renders index action of scaffolded model. * * @param array $params Parameters for scaffolding * @return mixed A rendered view listing rows from Models database table */ protected function _scaffoldIndex($params) { if ($this->controller->_beforeScaffold('index')) { $this->ScaffoldModel->recursive = 0; $this->controller->set( Inflector::variable($this->controller->name), $this->controller->paginate() ); $this->controller->render($this->request['action'], $this->layout); $this->_output(); } elseif ($this->controller->_scaffoldError('index') === false) { return $this->_scaffoldError(); } } /** * Renders an add or edit action for scaffolded model. * * @param string $action Action (add or edit) * @return mixed A rendered view with a form to edit or add a record in the Models database table */ protected function _scaffoldForm($action = 'edit') { $this->controller->viewVars['scaffoldFields'] = array_merge( $this->controller->viewVars['scaffoldFields'], array_keys($this->ScaffoldModel->hasAndBelongsToMany) ); $this->controller->render($action, $this->layout); $this->_output(); } /** * Saves or updates the scaffolded model. * * @param CakeRequest $request Request Object for scaffolding * @param string $action add or edt * @return mixed Success on save/update, add/edit form if data is empty or error if save or update fails */ protected function _scaffoldSave(CakeRequest $request, $action = 'edit') { $formAction = 'edit'; $success = __d('cake', 'updated'); if ($action === 'add') { $formAction = 'add'; $success = __d('cake', 'saved'); } if ($this->controller->_beforeScaffold($action)) { if ($action == 'edit') { if (isset($request->params['pass'][0])) { $this->ScaffoldModel->id = $request['pass'][0]; } if (!$this->ScaffoldModel->exists()) { throw new NotFoundException(__d('cake', 'Invalid %s', Inflector::humanize($this->modelKey))); } } if (!empty($request->data)) { if ($action == 'create') { $this->ScaffoldModel->create(); } if ($this->ScaffoldModel->save($request->data)) { if ($this->controller->_afterScaffoldSave($action)) { $message = __d('cake', 'The %1$s has been %2$s', Inflector::humanize($this->modelKey), $success ); return $this->_sendMessage($message); } else { return $this->controller->_afterScaffoldSaveError($action); } } else { if ($this->_validSession) { $this->controller->Session->setFlash(__d('cake', 'Please correct errors below.')); } } } if (empty($request->data)) { if ($this->ScaffoldModel->id) { $this->controller->data = $request->data = $this->ScaffoldModel->read(); } else { $this->controller->data = $request->data = $this->ScaffoldModel->create(); } } foreach ($this->ScaffoldModel->belongsTo as $assocName => $assocData) { $varName = Inflector::variable(Inflector::pluralize( preg_replace('/(?:_id)$/', '', $assocData['foreignKey']) )); $this->controller->set($varName, $this->ScaffoldModel->{$assocName}->find('list')); } foreach ($this->ScaffoldModel->hasAndBelongsToMany as $assocName => $assocData) { $varName = Inflector::variable(Inflector::pluralize($assocName)); $this->controller->set($varName, $this->ScaffoldModel->{$assocName}->find('list')); } return $this->_scaffoldForm($formAction); } elseif ($this->controller->_scaffoldError($action) === false) { return $this->_scaffoldError(); } } /** * Performs a delete on given scaffolded Model. * * @param array $params Parameters for scaffolding * @return mixed Success on delete, error if delete fails */ protected function _scaffoldDelete(CakeRequest $request) { if ($this->controller->_beforeScaffold('delete')) { if (!$request->is('post')) { throw new MethodNotAllowedException(); } $id = false; if (isset($request->params['pass'][0])) { $id = $request->params['pass'][0]; } $this->ScaffoldModel->id = $id; if (!$this->ScaffoldModel->exists()) { throw new NotFoundException(__d('cake', 'Invalid %s', Inflector::humanize($this->modelClass))); } if ($this->ScaffoldModel->delete()) { $message = __d('cake', 'The %1$s with id: %2$d has been deleted.', Inflector::humanize($this->modelClass), $id); return $this->_sendMessage($message); } else { $message = __d('cake', 'There was an error deleting the %1$s with id: %2$d', Inflector::humanize($this->modelClass), $id ); return $this->_sendMessage($message); } } elseif ($this->controller->_scaffoldError('delete') === false) { return $this->_scaffoldError(); } } /** * Sends a message to the user. Either uses Sessions or flash messages depending * on the availability of a session * * @param string $message Message to display * @return void */ protected function _sendMessage($message) { if ($this->_validSession) { $this->controller->Session->setFlash($message); $this->controller->redirect($this->redirect); } else { $this->controller->flash($message, $this->redirect); $this->_output(); } } /** * Show a scaffold error * * @return mixed A rendered view showing the error */ protected function _scaffoldError() { return $this->controller->render('error', $this->layout); $this->_output(); } /** * When methods are now present in a controller * scaffoldView is used to call default Scaffold methods if: * `public $scaffold;` is placed in the controller's class definition. * * @param CakeRequest $request Request object for scaffolding * @return mixed A rendered view of scaffold action, or showing the error */ protected function _scaffold(CakeRequest $request) { $db = ConnectionManager::getDataSource($this->ScaffoldModel->useDbConfig); $prefixes = Configure::read('Routing.prefixes'); $scaffoldPrefix = $this->scaffoldActions; if (isset($db)) { if (empty($this->scaffoldActions)) { $this->scaffoldActions = array( 'index', 'list', 'view', 'add', 'create', 'edit', 'update', 'delete' ); } elseif (!empty($prefixes) && in_array($scaffoldPrefix, $prefixes)) { $this->scaffoldActions = array( $scaffoldPrefix . '_index', $scaffoldPrefix . '_list', $scaffoldPrefix . '_view', $scaffoldPrefix . '_add', $scaffoldPrefix . '_create', $scaffoldPrefix . '_edit', $scaffoldPrefix . '_update', $scaffoldPrefix . '_delete' ); } if (in_array($request->params['action'], $this->scaffoldActions)) { if (!empty($prefixes)) { $request->params['action'] = str_replace($scaffoldPrefix . '_', '', $request->params['action']); } switch ($request->params['action']) { case 'index': case 'list': $this->_scaffoldIndex($request); break; case 'view': $this->_scaffoldView($request); break; case 'add': case 'create': $this->_scaffoldSave($request, 'add'); break; case 'edit': case 'update': $this->_scaffoldSave($request, 'edit'); break; case 'delete': $this->_scaffoldDelete($request); break; } } else { throw new MissingActionException(array( 'controller' => $this->controller->name, 'action' => $request->action )); } } else { throw new MissingDatabaseException(array('connection' => $this->ScaffoldModel->useDbConfig)); } } /** * Returns associations for controllers models. * * @return array Associations for model */ protected function _associations() { $keys = array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'); $associations = array(); foreach ($keys as $key => $type) { foreach ($this->ScaffoldModel->{$type} as $assocKey => $assocData) { $associations[$type][$assocKey]['primaryKey'] = $this->ScaffoldModel->{$assocKey}->primaryKey; $associations[$type][$assocKey]['displayField'] = $this->ScaffoldModel->{$assocKey}->displayField; $associations[$type][$assocKey]['foreignKey'] = $assocData['foreignKey']; $associations[$type][$assocKey]['controller'] = Inflector::pluralize(Inflector::underscore($assocData['className'])); if ($type == 'hasAndBelongsToMany') { $associations[$type][$assocKey]['with'] = $assocData['with']; } } } return $associations; } }