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)
43 'error' => Yii::t('types', 'Types: key field "type" missing: ') . print_r($options, TRUE),
45 if (is_array($options) && isset($options[ 'type' ]))
47 $model = Types::find()
48 ->where([ 'like', 'name', $options[ 'type' ] ])
50 if (count($model) > 0)
52 $result['id'] = $model[0]->id;
53 $result['error'] = '';
58 $model->name = $options[ 'type' ];
59 if ($model->validate() && $model->save())
61 $result['id'] = $model->id;
62 $result['error'] = '';
66 $result['error'] = Yii::t('types', 'Failed to add entry "{type}": ', $options) . print_r($model->errors['name'], TRUE);
74 * Список всех типов предметов/оборудования.
77 public function actionIndex()
79 if (! User::canPermission('createRecord'))
81 return $this->redirect(['site/index']);
83 $searchModel = new TypesSearch();
84 if (isset(Yii::$app->request->queryParams['id'])) {
85 $id = Yii::$app->request->queryParams['id'];
86 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
87 //$dataProvider->query->select(Types::tableName() . '.id');
88 $pageSize = $dataProvider->pagination->pageSize;
89 $dataProvider->pagination = FALSE;
90 $rows = $dataProvider->getModels();
92 foreach ($rows as $key => $val) {
93 if ($id == $val->id) {
94 $page = ceil(($key + 1) / $pageSize);
98 return $this->redirect(['index', 'page' => $page]);
100 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
102 return $this->render('index', [
103 'searchModel' => $searchModel,
104 'dataProvider' => $dataProvider,
109 * Показ одного типа предмета/оборудования (не используется).
112 * @throws NotFoundHttpException если отсутствует тип предмета/оборудования
114 public function actionView($id)
116 if (! User::canPermission('updateRecord'))
118 return $this->redirect(['index']);
120 return $this->render('view', [
121 'model' => $this->findModel($id),
126 * Создание нового типа предмета/оборудования.
127 * При успешном создании, происходит переход к списку всех типов предметов/оборудования.
130 public function actionCreate()
132 if (! User::canPermission('createRecord'))
134 return $this->redirect(['site/index']);
136 $model = new Types();
138 if ($model->load(Yii::$app->request->post()) && $model->save())
140 return $this->redirect([ 'index', 'id' => $model->id ]);
143 return $this->render('create', [
149 * Изменение существующего типа предмета/оборудования.
150 * При успешном изменении, происходит переход к списку всех типов предметов/оборудования.
153 * @throws NotFoundHttpException если отсутствует тип предмета/оборудования
155 public function actionUpdate($id)
157 if (! User::canPermission('updateRecord'))
159 return $this->redirect(['site/index']);
161 $model = $this->findModel($id);
163 if ($model->load(Yii::$app->request->post()) && $model->save())
165 return $this->redirect([ 'index', 'id' => $model->id ]);
168 return $this->render('update', [
174 * Удаление существующего типа предмета/оборудования.
175 * При успешном удалении, происходит переход к списку всех типов предметов/оборудования.
178 * @throws NotFoundHttpException если отсутствует тип предмета/оборудования
180 public function actionDelete($id)
182 if (! User::canPermission('updateRecord'))
184 return $this->redirect(['site/index']);
186 $this->findModel($id)->delete();
188 return $this->redirect([ 'index' ]);
192 * Finds the Types model based on its primary key value.
193 * If the model is not found, a 404 HTTP exception will be thrown.
195 * @return Types the loaded model
196 * @throws NotFoundHttpException if the model cannot be found
198 protected function findModel($id)
200 if (($model = Types::findOne($id)) !== null)
205 throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));