Class Common_Controller extends Zend_Controller_Action { /** * . * @var string; */ protected $modelClass = ''; /** * . * @var string; */ protected $editFormClass = ''; /** * JS , , . * @var string; */ protected $jsModelFile = ''; /** * , * $id - , - . * * @param mixed $id; * @return Model_Record $model; */ protected function modelFactory( $id = null ) { $modelClass = $this->modelClass; if ( $id ) { $model = $modelClass::find( $id ); } else { $model = $modelClass::create(); } return $model; } /** * . * * @param Model_Record $model; * @return Zend_form $form; */ protected function formFactory( Model_Record $model ) { $formClass = $this->editFormClass; $form = new $formClass(); $form->setDefaults( $model->toArray(1) ); return $form; } /** * . * * @param Model_record $model; * @param Zend_Form $form; */ protected function save( Model_Record $model , Zend_Form $form ) { $model->fromArray($form->getValues(), false); $model->save(); } /** * , * editAction() . */ protected function _editActionHelper() { $id = $this->_request->getParam('id'); if ( !$id ) { throw new Zend_Controller_Action_Exception(' ' , 404); } // $model = $this->modelFactory($id); if ( !$model ) { throw new Zend_Controller_Action_Exception(' ' , 404); } $this->view->model = $model; // $this->view->PageTitle = $model->getFullTitle(); // $form = $this->formFactory( $model ); $this->view->form = $form; // get- - ( ) , if ( $this->_request->isGet() ) { $form->redirect->setValue( $_SERVER['HTTP_REFERER'] ); } // , // ... // $model->lock(); // js if ( $this->jsModelFile ) { $this->view->headScript()->appendFile( '/js/models/' . $this->jsModelFile ); } // if ( isset($_POST['save']) || isset($_POST['saveExit']) ) { // if ( $form->isValid( $this->_request->getPost() ) ) { // try { Model::connection()->beginTransaction(); $this->save( $model, $form ); Model::connection()->commit(); $model->releaseLock(); $this->view->Flash()->addSuccess( 'Success !' ); // , // $redirect = '/' . $this->_request->getControllerName() . '/edit/id/' . $model->ID . '?redirect=' . $this->_request->getParam('redirect', '/'); // if ( isset($_POST['saveExit']) ) { $redirect = $this->_request->getParam('redirect', '/'); } $this->_redirect( $redirect ); } catch (Exception $e) { Model::connection()->rollback(); $this->view->Flash()->addError( $e->getMessage() ); } } else { $this->view->Flash()->addError(" "); } } } }
public function editAction() { $this->_editActionHelper(); }
Class Video extends Common_Controller { protected $modelClass = 'Video'; protected $editFormClass = 'Form_Video'; protected function save( Model_Record $model , Zend_Form $form ) { parent::save( $model , $form ); $model->setChannels( $form->channels->getValue() ); } public function editAction() { $this->_editActionHelper(); } public function addAction() { $this->_addActionHelper(); } public function indexAction() { $this->_indexActionHelper(); } }
Source: https://habr.com/ru/post/213971/
All Articles