From e3225b71ba763cee4f813361a3a3e0a8710c65cf Mon Sep 17 00:00:00 2001 From: k1LoW Date: Fri, 21 Jun 2013 18:55:41 +0900 Subject: [PATCH] Add RoutineComponent --- Controller/Component/RoutineComponent.php | 225 ++++++++++++++++++++++ Controller/RoutineController.php | 182 +---------------- Test/Case/Model/RoutineModelTest.php | 29 ++- 3 files changed, 263 insertions(+), 173 deletions(-) create mode 100644 Controller/Component/RoutineComponent.php diff --git a/Controller/Component/RoutineComponent.php b/Controller/Component/RoutineComponent.php new file mode 100644 index 0000000..cbd7298 --- /dev/null +++ b/Controller/Component/RoutineComponent.php @@ -0,0 +1,225 @@ +Controller = $collection->getController(); + + $defaults = array( + 'redirect' => array( + 'add' => array(array($this, 'redirect'), array(array('action' => 'index'))), + 'edit' => array(array($this, 'redirect'), array(array('action' => 'index'))), + 'delete' => array(array($this, 'redirect'), array(array('action' => 'index'))), + ) + ); + + $settings = Hash::merge($defaults, $settings); + + // configure. + $this->_set($settings); + + parent::__construct($collection, $settings); + + } + + public function startup(Controller $Controller) { + // not load + return true; + } + + /** + * index + * + */ + public function index(){ + $this->Controller->set( + Inflector::pluralize(Inflector::variable($this->Controller->modelClass)), + $this->Controller->paginate() + ); + } + + /** + * search + * + */ + public function search(){ + $this->Controller->Prg->commonProcess(); + $conditions = $this->Controller->{$this->modelClass}->parseCriteria($this->Controller->passedArgs); + $this->Controller->set( + Inflector::pluralize(Inflector::variable($this->Controller->modelClass)), + $this->Controller->Paginator->paginate($conditions) + ); + } + + /** + * view + * + */ + public function view($id = null){ + $this->Controller->set( + Inflector::variable($this->Controller->modelClass), + $this->Controller->{$this->Controller->modelClass}->view($id) + ); + } + + /** + * add + * + */ + public function add(){ + $this->addRedirect(); + try { + $result = $this->Controller->{$this->Controller->modelClass}->add($this->Controller->request->data); + if ($result === true) { + $this->Controller->Session->setFlash( + __('The %s has been created', __($this->Controller->modelClass)), + $this->Controller->setFlashElement['success'], + $this->Controller->setFlashParams['success']); + $this->addRedirect(); + } + } catch (ValidationException $e) { + $this->Controller->Session->setFlash($e->getMessage(), + $this->Controller->setFlashElement['error'], + $this->Controller->setFlashParams['error']); + } catch (OutOfBoundsException $e) { + $this->Controller->Session->setFlash($e->getMessage(), + $this->Controller->setFlashElement['error'], + $this->Controller->setFlashParams['error']); + } + if (!empty($this->Controller->{$this->Controller->modelClass}->belongsTo)) { + foreach ($this->Controller->{$this->Controller->modelClass}->belongsTo as $modelClass => $value) { + $this->Controller->set(Inflector::pluralize(Inflector::variable($modelClass)), $this->Controller->{$this->Controller->modelClass}->{$modelClass}->find('list')); + } + } + } + + /** + * addRedirect + * + */ + protected function addRedirect(){ + $func = $this->redirect['add'][0]; + $param = $this->redirect['add'][1]; + call_user_func_array($func, $param); + } + + /** + * edit + * + */ + public function edit($id = null){ + try { + $result = $this->Controller->{$this->Controller->modelClass}->edit($id, $this->Controller->request->data); + if ($result === true) { + $this->Controller->Session->setFlash( + __('The %s has been modified', __(Inflector::humanize($this->Controller->modelClass))), + $this->Controller->setFlashElement['success'], + $this->Controller->setFlashParams['success']); + $this->editRedirect($id); + } else { + $this->Controller->request->data = $result; + } + } catch (ValidationException $e) { + $this->Controller->Session->setFlash($e->getMessage(), + $this->Controller->setFlashElement['error'], + $this->Controller->setFlashParams['error']); + } catch (OutOfBoundsException $e) { + $this->Controller->Session->setFlash($e->getMessage(), + $this->Controller->setFlashElement['error'], + $this->Controller->setFlashParams['error']); + } + if (!empty($this->Controller->{$this->Controller->modelClass}->belongsTo)) { + foreach ($this->Controller->{$this->Controller->modelClass}->belongsTo as $modelClass => $value) { + $this->Controller->set(Inflector::pluralize(Inflector::variable($modelClass)), $this->Controller->{$this->Controller->modelClass}->{$modelClass}->find('list')); + } + } + } + + /** + * editRedirect + * + */ + protected function editRedirect($id = null){ + $func = $this->redirect['edit'][0]; + $param = $this->redirect['edit'][1]; + $param[] = $id; + call_user_func_array($func, $param); + } + + /** + * delete + * + */ + public function delete($id = null){ + if (!$this->Controller->request->is('post')) { + throw new OutOfBoundsException(__('Invalid Access')); + } + try { + if($this->Controller->{$this->Controller->modelClass}->drop($id)) { + $this->Controller->Session->setFlash( + __('The %s has been deleted', __($this->Controller->modelClass)), + $this->Controller->setFlashElement['success'], + $this->Controller->setFlashParams['success']); + $this->deleteRedirect(); + } + } catch (ValidationException $e) { + $this->Controller->Session->setFlash($e->getMessage(), + $this->Controller->setFlashElement['error'], + $this->Controller->setFlashParams['error']); + $this->deleteRedirect(); + } catch (OutOfBoundsException $e) { + $this->Controller->Session->setFlash($e->getMessage(), + $this->setFlashElement['error'], + $this->setFlashParams['error']); + $this->deleteRedirect(); + } + } + + /** + * deleteRedirect + * + */ + protected function deleteRedirect(){ + $func = $this->redirect['delete'][0]; + $param = $this->redirect['delete'][1]; + call_user_func_array($func, $param); + } + + /** + * login + * + */ + public function login(){ + if ($this->Controller->request->is('post')) { + if ($this->Controller->Auth->login()) { + $this->Controller->redirect($this->Controller->Auth->redirect(), null, true); + } else { + $this->Controller->Session->setFlash( + __('Username or password is incorrect'), + $this->Controller->setFlashElement['error'], + $this->Controller->setFlashParams['error'], + 'auth'); + } + } + } + + /** + * logout + * + */ + public function logout(){ + $this->Controller->redirect($this->Controller->Auth->logout()); + } + + /** + * redirect + * + * @param $url + */ + private function redirect($url){ + $this->Controller->redirect($url); + } +} + diff --git a/Controller/RoutineController.php b/Controller/RoutineController.php index 1e33ac7..f16e548 100644 --- a/Controller/RoutineController.php +++ b/Controller/RoutineController.php @@ -15,25 +15,8 @@ class RoutineController extends AppController { */ public function index(){ $this->{$this->modelClass}->recursive = 0; - $this->_index(); - } - - /** - * _index - * - */ - protected function _index(){ - $this->set(Inflector::pluralize(Inflector::variable($this->modelClass)), $this->paginate()); - } - - /** - * _search - * - */ - protected function _search(){ - $this->Prg->commonProcess(); - $conditions = $this->{$this->modelClass}->parseCriteria($this->passedArgs); - $this->set(Inflector::pluralize(Inflector::variable($this->modelClass)), $this->Paginator->paginate($conditions)); + $this->Routine = $this->Components->load('Routine.Routine'); + $this->Routine->index(); } /** @@ -41,15 +24,8 @@ protected function _search(){ * */ public function view($id = null) { - $this->_view($id); - } - - /** - * _view - * - */ - protected function _view($id = null){ - $this->set(Inflector::variable($this->modelClass), $this->{$this->modelClass}->view($id)); + $this->Routine = $this->Components->load('Routine.Routine'); + $this->Routine->view($id); } /** @@ -57,45 +33,8 @@ protected function _view($id = null){ * */ public function add() { - $this->_add(); - } - - /** - * _add - * - */ - protected function _add(){ - try { - $result = $this->{$this->modelClass}->add($this->request->data); - if ($result === true) { - $this->Session->setFlash( - __('The %s has been created', __($this->modelClass)), - $this->setFlashElement['success'], - $this->setFlashParams['success']); - $this->_addRedirect(); - } - } catch (ValidationException $e) { - $this->Session->setFlash($e->getMessage(), - $this->setFlashElement['error'], - $this->setFlashParams['error']); - } catch (OutOfBoundsException $e) { - $this->Session->setFlash($e->getMessage(), - $this->setFlashElement['error'], - $this->setFlashParams['error']); - } - if (!empty($this->{$this->modelClass}->belongsTo)) { - foreach ($this->{$this->modelClass}->belongsTo as $modelClass => $value) { - $this->set(Inflector::pluralize(Inflector::variable($modelClass)), $this->{$this->modelClass}->{$modelClass}->find('list')); - } - } - } - - /** - * _addRedirect - * - */ - protected function _addRedirect(){ - $this->redirect(array('action' => 'index')); + $this->Routine = $this->Components->load('Routine.Routine'); + $this->Routine->add(); } /** @@ -103,47 +42,8 @@ protected function _addRedirect(){ * */ public function edit($id = null) { - $this->_edit($id); - } - - /** - * _edit - * - */ - protected function _edit($id = null){ - try { - $result = $this->{$this->modelClass}->edit($id, $this->request->data); - if ($result === true) { - $this->Session->setFlash( - __('The %s has been modified', __(Inflector::humanize($this->modelClass))), - $this->setFlashElement['success'], - $this->setFlashParams['success']); - $this->_editRedirect($id); - } else { - $this->request->data = $result; - } - } catch (ValidationException $e) { - $this->Session->setFlash($e->getMessage(), - $this->setFlashElement['error'], - $this->setFlashParams['error']); - } catch (OutOfBoundsException $e) { - $this->Session->setFlash($e->getMessage(), - $this->setFlashElement['error'], - $this->setFlashParams['error']); - } - if (!empty($this->{$this->modelClass}->belongsTo)) { - foreach ($this->{$this->modelClass}->belongsTo as $modelClass => $value) { - $this->set(Inflector::pluralize(Inflector::variable($modelClass)), $this->{$this->modelClass}->{$modelClass}->find('list')); - } - } - } - - /** - * _editRedirect - * - */ - protected function _editRedirect($id = null){ - $this->_addRedirect(); + $this->Routine = $this->Components->load('Routine.Routine'); + $this->Routine->edit($id); } /** @@ -151,69 +51,7 @@ protected function _editRedirect($id = null){ * */ public function delete($id = null){ - $this->_delete($id); - } - - /** - * _delete - * - */ - protected function _delete($id = null){ - if (!$this->request->is('post')) { - throw new OutOfBoundsException(__('Invalid Access')); - } - try { - if($this->{$this->modelClass}->drop($id)) { - $this->Session->setFlash( - __('The %s has been deleted', __($this->modelClass)), - $this->setFlashElement['success'], - $this->setFlashParams['success']); - $this->_deleteRedirect(); - } - } catch (ValidationException $e) { - $this->Session->setFlash($e->getMessage(), - $this->setFlashElement['error'], - $this->setFlashParams['error']); - $this->_deleteRedirect(); - } catch (OutOfBoundsException $e) { - $this->Session->setFlash($e->getMessage(), - $this->setFlashElement['error'], - $this->setFlashParams['error']); - $this->_deleteRedirect(); - } - } - - /** - * _deleteRedirect - * - */ - protected function _deleteRedirect(){ - $this->_addRedirect(); - } - - /** - * _login - * - */ - protected function _login(){ - if ($this->request->is('post')) { - if ($this->Auth->login()) { - $this->redirect($this->Auth->redirect(), null, true); - } else { - $this->Session->setFlash( - __('Username or password is incorrect'), - $this->setFlashElement['error'], - $this->setFlashParams['error'], - 'auth'); - } - } - } - - /** - * _logout - * - */ - protected function _logout(){ - $this->redirect($this->Auth->logout()); + $this->Routine = $this->Components->load('Routine.Routine'); + $this->Routine->delete($id); } } diff --git a/Test/Case/Model/RoutineModelTest.php b/Test/Case/Model/RoutineModelTest.php index 25321cf..92572c9 100644 --- a/Test/Case/Model/RoutineModelTest.php +++ b/Test/Case/Model/RoutineModelTest.php @@ -11,6 +11,33 @@ class RoutinePost extends RoutineModel { 'rule' => 'notEmpty', 'required' => true )); + + /** + * __construct + * + * @param + */ + public function __construct($id = false, $table = null, $ds = null) { + $this->actsAs = false; + parent::__construct($id, $table, $ds); + } + + /** + * beforeValidate + * + */ + public function beforeValidate($options = array()){ + return true; + } + + /** + * beforeSave + * + */ + public function beforeSave($options = array()){ + return true; + } + } class RoutineModelTestCase extends CakeTestCase{ @@ -18,7 +45,7 @@ class RoutineModelTestCase extends CakeTestCase{ public $fixtures = array('plugin.Routine.routine_post'); public function setUp() { - $this->RoutinePost = new RoutinePost(); + $this->RoutinePost = ClassRegistry::init('RoutinePost'); $this->RoutinePostFixture = ClassRegistry::init('RoutinePostFixture'); }