abstract class TModel extends CActiveRecord { const STATUS_DEFAULT = 0; const STATUS_REMOVED = 1; public function defaultScope() { return array( 'condition' => 'status=' . self::STATUS_DEFAULT ); } public function removed() { $this->resetScope()->getDbCriteria()->mergeWith(array( 'condition' => 'status=' . self::STATUS_REMOVED )); return $this; } public function restore() { if($this->getIsNewRecord()) throw new CDbException(Yii::t('yii','The active record cannot be deleted because it is new.')); if($this->status != self::STATUS_REMOVED) return false; $this->status = self::STATUS_DEFAULT; $this->save(false, array('status')); return true; } public function beforeDelete() { if($this->status == self::STATUS_DEFAULT) { $this->status = self::STATUS_REMOVED; $this->save(false, array('status')); return false; } return parent::beforeDelete(); } }
class BackendController extends CController { public $defaultAction = 'list'; public function actions() { return array( 'list' => 'backend.actions.ListAction', 'update' => 'backend.actions.UpdateAction', 'delete' => 'backend.actions.DeleteAction', 'restore' => 'backend.actions.RestoreAction', ); } }
abstract class BackendAction extends CAction { private $_modelName; private $_view; /** * * - */ public function redirect($actionId = null) { if($actionId === null) $actionId = $this->controller->defaultAction; $this->controller->redirect(array($actionId)); } /** * . * - */ public function render($data, $return = false) { if($this->_view === null) $this->_view = $this->id; return $this->controller->render($this->_view, $data, $return); } /** * * , id */ public function getModel($scenario = 'insert') { if(($id = Yii::app()->request->getParam('id')) === null) $model = new $this->modelName($scenario); else if(($model = CActiveRecord::model($this->modelName)->resetScope()->findByPk($id)) === null) throw new CHttpException(404, Yii::t('base', 'The specified record cannot be found.')); return $model; } /** * , * - */ public function getModelName() { if($this->_modelName === null) $this->_modelName = ucfirst($this->controller->id); return $this->_modelName; } public function setView($value) { $this->_view = $value; } public function setModelName($value) { $this->_modelName = $value; } }
class ListAction extends BackendAction { public function run($showRemoved = null) { $model = $this->getModel('search'); if($showRemoved !== null) $model->removed(); if(isset($_GET[$this->modelName])) $model->attributes = $_GET[$this->modelName]; $this->render(array( 'model' => $model, 'showRemoved' => $showRemoved, )); } }
class UpdateAction extends BackendAction { public function run() { $model = $this->getModel(); if(isset($_POST[$this->modelName])) { $model->attributes = $_POST[$this->modelName]; if($model->save()) $this->redirect(); } $this->render(array('model' => $model)); } }
class DeleteAction extends BackendAction { public function run() { $this->getModel()->delete(); $this->redirect(); } }
class RestoreAction extends BackendAction { public function run() { $this->getModel()->restore(); $this->redirect(); } }
class Article extends TModel { public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return 'article'; } public function rules() { return array( array('name, content, createTime', 'required'), ); } } class ArticleController extends BackendController { // , }
Source: https://habr.com/ru/post/126576/
All Articles