| @@ 18-168 (lines=151) @@ | ||
| 15 | /** |
|
| 16 | * HtmlController implements the CRUD actions for Html model. |
|
| 17 | */ |
|
| 18 | class HtmlController extends Controller |
|
| 19 | { |
|
| 20 | /** |
|
| 21 | * @var boolean whether to enable CSRF validation for the actions in this controller. |
|
| 22 | * CSRF validation is enabled only when both this property and [[Request::enableCsrfValidation]] are true. |
|
| 23 | */ |
|
| 24 | public $enableCsrfValidation = false; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * @inheritdoc |
|
| 28 | */ |
|
| 29 | public function behaviors() |
|
| 30 | { |
|
| 31 | return [ |
|
| 32 | 'access' => [ |
|
| 33 | 'class' => AccessControl::className(), |
|
| 34 | 'rules' => [ |
|
| 35 | [ |
|
| 36 | 'allow' => true, |
|
| 37 | 'matchCallback' => function ($rule, $action) { |
|
| 38 | return \Yii::$app->user->can( |
|
| 39 | $this->module->id.'_'.$this->id.'_'.$action->id, |
|
| 40 | ['route' => true] |
|
| 41 | ); |
|
| 42 | }, |
|
| 43 | ] |
|
| 44 | ] |
|
| 45 | ] |
|
| 46 | ]; |
|
| 47 | } |
|
| 48 | ||
| 49 | /** |
|
| 50 | * Lists all Html models. |
|
| 51 | * @return mixed |
|
| 52 | */ |
|
| 53 | public function actionIndex() |
|
| 54 | { |
|
| 55 | $searchModel = new HtmlSearch; |
|
| 56 | $dataProvider = $searchModel->search($_GET); |
|
| 57 | ||
| 58 | Tabs::clearLocalStorage(); |
|
| 59 | ||
| 60 | Url::remember(); |
|
| 61 | \Yii::$app->session['__crudReturnUrl'] = null; |
|
| 62 | ||
| 63 | return $this->render( |
|
| 64 | 'index', |
|
| 65 | [ |
|
| 66 | 'dataProvider' => $dataProvider, |
|
| 67 | 'searchModel' => $searchModel, |
|
| 68 | ] |
|
| 69 | ); |
|
| 70 | } |
|
| 71 | ||
| 72 | /** |
|
| 73 | * Displays a single Html model. |
|
| 74 | * |
|
| 75 | * @param integer $id |
|
| 76 | * |
|
| 77 | * @return mixed |
|
| 78 | */ |
|
| 79 | public function actionView($id) |
|
| 80 | { |
|
| 81 | \Yii::$app->session['__crudReturnUrl'] = Url::previous(); |
|
| 82 | Url::remember(); |
|
| 83 | Tabs::rememberActiveState(); |
|
| 84 | ||
| 85 | return $this->render( |
|
| 86 | 'view', |
|
| 87 | [ |
|
| 88 | 'model' => $this->findModel($id), |
|
| 89 | ] |
|
| 90 | ); |
|
| 91 | } |
|
| 92 | ||
| 93 | /** |
|
| 94 | * Creates a new Html model. |
|
| 95 | * If creation is successful, the browser will be redirected to the 'view' page. |
|
| 96 | * @return mixed |
|
| 97 | */ |
|
| 98 | public function actionCreate() |
|
| 99 | { |
|
| 100 | $model = new Html; |
|
| 101 | ||
| 102 | try { |
|
| 103 | if ($model->load($_POST) && $model->save()) { |
|
| 104 | return $this->redirect(Url::previous()); |
|
| 105 | } elseif (!\Yii::$app->request->isPost) { |
|
| 106 | $model->load($_GET); |
|
| 107 | } |
|
| 108 | } catch (\Exception $e) { |
|
| 109 | $msg = (isset($e->errorInfo[2])) ? $e->errorInfo[2] : $e->getMessage(); |
|
| 110 | $model->addError('_exception', $msg); |
|
| 111 | } |
|
| 112 | return $this->render('create', ['model' => $model]); |
|
| 113 | } |
|
| 114 | ||
| 115 | /** |
|
| 116 | * Updates an existing Html model. |
|
| 117 | * If update is successful, the browser will be redirected to the 'view' page. |
|
| 118 | * |
|
| 119 | * @param integer $id |
|
| 120 | * |
|
| 121 | * @return mixed |
|
| 122 | */ |
|
| 123 | public function actionUpdate($id) |
|
| 124 | { |
|
| 125 | $model = $this->findModel($id); |
|
| 126 | ||
| 127 | if ($model->load($_POST) && $model->save()) { |
|
| 128 | return $this->redirect(Url::previous()); |
|
| 129 | } else { |
|
| 130 | return $this->render( |
|
| 131 | 'update', |
|
| 132 | [ |
|
| 133 | 'model' => $model, |
|
| 134 | ] |
|
| 135 | ); |
|
| 136 | } |
|
| 137 | } |
|
| 138 | ||
| 139 | /** |
|
| 140 | * Deletes an existing Html model. |
|
| 141 | * If deletion is successful, the browser will be redirected to the 'index' page. |
|
| 142 | * |
|
| 143 | * @param integer $id |
|
| 144 | * |
|
| 145 | * @return mixed |
|
| 146 | */ |
|
| 147 | public function actionDelete($id) |
|
| 148 | { |
|
| 149 | try { |
|
| 150 | $this->findModel($id)->delete(); |
|
| 151 | } catch (\Exception $e) { |
|
| 152 | $msg = (isset($e->errorInfo[2])) ? $e->errorInfo[2] : $e->getMessage(); |
|
| 153 | \Yii::$app->getSession()->addFlash('error', $msg); |
|
| 154 | return $this->redirect(Url::previous()); |
|
| 155 | } |
|
| 156 | ||
| 157 | // TODO: improve detection |
|
| 158 | $isPivot = strstr('$id', ','); |
|
| 159 | if ($isPivot == true) { |
|
| 160 | return $this->redirect(Url::previous()); |
|
| 161 | } elseif (isset(\Yii::$app->session['__crudReturnUrl']) && \Yii::$app->session['__crudReturnUrl'] != '/') { |
|
| 162 | Url::remember(null); |
|
| 163 | $url = \Yii::$app->session['__crudReturnUrl']; |
|
| 164 | \Yii::$app->session['__crudReturnUrl'] = null; |
|
| 165 | ||
| 166 | return $this->redirect($url); |
|
| 167 | } else { |
|
| 168 | return $this->redirect(['index']); |
|
| 169 | } |
|
| 170 | } |
|
| 171 | ||
| @@ 18-168 (lines=151) @@ | ||
| 15 | /** |
|
| 16 | * LessController implements the CRUD actions for Less model. |
|
| 17 | */ |
|
| 18 | class LessController extends Controller |
|
| 19 | { |
|
| 20 | /** |
|
| 21 | * @var boolean whether to enable CSRF validation for the actions in this controller. |
|
| 22 | * CSRF validation is enabled only when both this property and [[Request::enableCsrfValidation]] are true. |
|
| 23 | */ |
|
| 24 | public $enableCsrfValidation = false; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * @inheritdoc |
|
| 28 | */ |
|
| 29 | public function behaviors() |
|
| 30 | { |
|
| 31 | return [ |
|
| 32 | 'access' => [ |
|
| 33 | 'class' => AccessControl::className(), |
|
| 34 | 'rules' => [ |
|
| 35 | [ |
|
| 36 | 'allow' => true, |
|
| 37 | 'matchCallback' => function ($rule, $action) { |
|
| 38 | return \Yii::$app->user->can( |
|
| 39 | $this->module->id.'_'.$this->id.'_'.$action->id, |
|
| 40 | ['route' => true] |
|
| 41 | ); |
|
| 42 | }, |
|
| 43 | ] |
|
| 44 | ] |
|
| 45 | ] |
|
| 46 | ]; |
|
| 47 | } |
|
| 48 | ||
| 49 | /** |
|
| 50 | * Lists all Less models. |
|
| 51 | * @return mixed |
|
| 52 | */ |
|
| 53 | public function actionIndex() |
|
| 54 | { |
|
| 55 | $searchModel = new LessSearch; |
|
| 56 | $dataProvider = $searchModel->search($_GET); |
|
| 57 | ||
| 58 | Tabs::clearLocalStorage(); |
|
| 59 | ||
| 60 | Url::remember(); |
|
| 61 | \Yii::$app->session['__crudReturnUrl'] = null; |
|
| 62 | ||
| 63 | return $this->render( |
|
| 64 | 'index', |
|
| 65 | [ |
|
| 66 | 'dataProvider' => $dataProvider, |
|
| 67 | 'searchModel' => $searchModel, |
|
| 68 | ] |
|
| 69 | ); |
|
| 70 | } |
|
| 71 | ||
| 72 | /** |
|
| 73 | * Displays a single Less model. |
|
| 74 | * |
|
| 75 | * @param integer $id |
|
| 76 | * |
|
| 77 | * @return mixed |
|
| 78 | */ |
|
| 79 | public function actionView($id) |
|
| 80 | { |
|
| 81 | \Yii::$app->session['__crudReturnUrl'] = Url::previous(); |
|
| 82 | Url::remember(); |
|
| 83 | Tabs::rememberActiveState(); |
|
| 84 | ||
| 85 | return $this->render( |
|
| 86 | 'view', |
|
| 87 | [ |
|
| 88 | 'model' => $this->findModel($id), |
|
| 89 | ] |
|
| 90 | ); |
|
| 91 | } |
|
| 92 | ||
| 93 | /** |
|
| 94 | * Creates a new Less model. |
|
| 95 | * If creation is successful, the browser will be redirected to the 'view' page. |
|
| 96 | * @return mixed |
|
| 97 | */ |
|
| 98 | public function actionCreate() |
|
| 99 | { |
|
| 100 | $model = new Less; |
|
| 101 | ||
| 102 | try { |
|
| 103 | if ($model->load($_POST) && $model->save()) { |
|
| 104 | return $this->redirect(Url::previous()); |
|
| 105 | } elseif (!\Yii::$app->request->isPost) { |
|
| 106 | $model->load($_GET); |
|
| 107 | } |
|
| 108 | } catch (\Exception $e) { |
|
| 109 | $msg = (isset($e->errorInfo[2])) ? $e->errorInfo[2] : $e->getMessage(); |
|
| 110 | $model->addError('_exception', $msg); |
|
| 111 | } |
|
| 112 | return $this->render('create', ['model' => $model]); |
|
| 113 | } |
|
| 114 | ||
| 115 | /** |
|
| 116 | * Updates an existing Less model. |
|
| 117 | * If update is successful, the browser will be redirected to the 'view' page. |
|
| 118 | * |
|
| 119 | * @param integer $id |
|
| 120 | * |
|
| 121 | * @return mixed |
|
| 122 | */ |
|
| 123 | public function actionUpdate($id) |
|
| 124 | { |
|
| 125 | $model = $this->findModel($id); |
|
| 126 | ||
| 127 | if ($model->load($_POST) && $model->save()) { |
|
| 128 | return $this->redirect(Url::previous()); |
|
| 129 | } else { |
|
| 130 | return $this->render( |
|
| 131 | 'update', |
|
| 132 | [ |
|
| 133 | 'model' => $model, |
|
| 134 | ] |
|
| 135 | ); |
|
| 136 | } |
|
| 137 | } |
|
| 138 | ||
| 139 | /** |
|
| 140 | * Deletes an existing Less model. |
|
| 141 | * If deletion is successful, the browser will be redirected to the 'index' page. |
|
| 142 | * |
|
| 143 | * @param integer $id |
|
| 144 | * |
|
| 145 | * @return mixed |
|
| 146 | */ |
|
| 147 | public function actionDelete($id) |
|
| 148 | { |
|
| 149 | try { |
|
| 150 | $this->findModel($id)->delete(); |
|
| 151 | } catch (\Exception $e) { |
|
| 152 | $msg = (isset($e->errorInfo[2])) ? $e->errorInfo[2] : $e->getMessage(); |
|
| 153 | \Yii::$app->getSession()->addFlash('error', $msg); |
|
| 154 | return $this->redirect(Url::previous()); |
|
| 155 | } |
|
| 156 | ||
| 157 | // TODO: improve detection |
|
| 158 | $isPivot = strstr('$id', ','); |
|
| 159 | if ($isPivot == true) { |
|
| 160 | return $this->redirect(Url::previous()); |
|
| 161 | } elseif (isset(\Yii::$app->session['__crudReturnUrl']) && \Yii::$app->session['__crudReturnUrl'] != '/') { |
|
| 162 | Url::remember(null); |
|
| 163 | $url = \Yii::$app->session['__crudReturnUrl']; |
|
| 164 | \Yii::$app->session['__crudReturnUrl'] = null; |
|
| 165 | ||
| 166 | return $this->redirect($url); |
|
| 167 | } else { |
|
| 168 | return $this->redirect(['index']); |
|
| 169 | } |
|
| 170 | } |
|
| 171 | ||