3 namespace app\controllers;
7 use app\models\TypesSearch;
8 use yii\web\Controller;
9 use yii\web\NotFoundHttpException;
10 use yii\filters\VerbFilter;
14 * TypesController implements the CRUD actions for Types model.
16 class TypesController extends Controller
21 public function behaviors()
25 'class' => VerbFilter::className(),
27 'delete' => [ 'POST' ],
34 * Добавление типа, если необходимо
35 * @param array $options
36 * string 'name' - наименование
37 * @return integer|boolean - Идентификатор типа или FALSE в случае неудачи
39 public function addIfNeed($options)
41 if (is_array($options) && isset($options[ 'name' ]))
44 ->where([ 'like', 'name', $options[ 'name' ] ])
51 $type->name = $options[ 'name' ];
52 if ($type->validate() && $type->save())
61 * Список всех типов предметов/оборудования.
64 public function actionIndex()
66 if (! User::canPermission('createRecord'))
68 return $this->redirect(['site/index']);
70 $searchModel = new TypesSearch();
71 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
73 return $this->render('index', [
74 'searchModel' => $searchModel,
75 'dataProvider' => $dataProvider,
80 * Показ одного типа предмета/оборудования (не используется).
83 * @throws NotFoundHttpException если отсутствует тип предмета/оборудования
85 public function actionView($id)
87 if (! User::canPermission('updateRecord'))
89 return $this->redirect(['index']);
91 return $this->render('view', [
92 'model' => $this->findModel($id),
97 * Создание нового типа предмета/оборудования.
98 * При успешном создании, происходит переход к списку всех типов предметов/оборудования.
101 public function actionCreate()
103 if (! User::canPermission('createRecord'))
105 return $this->redirect(['site/index']);
107 $model = new Types();
109 if ($model->load(Yii::$app->request->post()) && $model->save())
111 return $this->redirect([ 'index', 'id' => $model->id ]);
114 return $this->render('create', [
120 * Изменение существующего типа предмета/оборудования.
121 * При успешном изменении, происходит переход к списку всех типов предметов/оборудования.
124 * @throws NotFoundHttpException если отсутствует тип предмета/оборудования
126 public function actionUpdate($id)
128 if (! User::canPermission('updateRecord'))
130 return $this->redirect(['site/index']);
132 $model = $this->findModel($id);
134 if ($model->load(Yii::$app->request->post()) && $model->save())
136 return $this->redirect([ 'index', 'id' => $model->id ]);
139 return $this->render('update', [
145 * Удаление существующего типа предмета/оборудования.
146 * При успешном удалении, происходит переход к списку всех типов предметов/оборудования.
149 * @throws NotFoundHttpException если отсутствует тип предмета/оборудования
151 public function actionDelete($id)
153 if (! User::canPermission('updateRecord'))
155 return $this->redirect(['site/index']);
157 $this->findModel($id)->delete();
159 return $this->redirect([ 'index' ]);
163 * Finds the Types model based on its primary key value.
164 * If the model is not found, a 404 HTTP exception will be thrown.
166 * @return Types the loaded model
167 * @throws NotFoundHttpException if the model cannot be found
169 protected function findModel($id)
171 if (($model = Types::findOne($id)) !== null)
176 throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));