3 namespace app\controllers;
7 use app\models\ModelsSearch;
10 use yii\web\Controller;
11 use yii\web\NotFoundHttpException;
12 use yii\filters\VerbFilter;
15 * ModelsController implements the CRUD actions for Models model.
17 class ModelsController extends Controller
22 public function behaviors()
26 'class' => VerbFilter::className(),
34 public function addIfNeed($options)
38 'error' => Yii::t('models', 'Models: Key field missing "model"') . print_r($options, TRUE),
40 // Если указан инвентарный номер
41 if (is_array($options) && isset($options[ 'model' ]))
43 $model = Models::find()
44 ->where([ 'model' => $options[ 'model' ]])->all(); // Ищем наименование модели предмета/обрудования
46 if (count($model) > 0) // Записи найдены, выводим первую совпавшую
48 $result[ 'id' ] = $model[ 0 ]->id;
49 $result[ 'error' ] = '';
53 // Внесённой модели оборудования не найдено. Добавим новую запись
54 // Если указан тип предмета/оборудования
55 if (isset($options[ 'type' ]))
57 $type = TypesController::addIfNeed($options[ 'type' ]); // Найдём или добавим тип
58 // Если тип не добавили
59 if($type[ 'id' ] === FALSE)
61 $result[ 'error' ] = $type[ 'error' ] . '<br />';
62 $type[ 'id' ] = NULL; // сделаем его пустым
65 // Создаём новую запись модели предмета/оборудования
66 $model = new Models();
67 $model->name = $options[ 'model' ]; // Сетевое имя
68 $model->type_id = isset($type[ 'id' ]) ? $type[ 'id' ] : NULL; // Идентификатор типа
69 $model->product = isset($options[ 'product' ]) ? $options[ 'product' ] : NULL; // Код оборудования
70 $model->modelnumber = isset($options[ 'modelnum' ]) ? $options[ 'modelnum' ] : NULL; // Номер модели
72 if ($model->validate() && $model->save())
74 $result[ 'id' ] = $model->id; // Возвращаем идентификатор записанного оборудования
75 $result[ 'error' ] = '';
79 $result[ 'error' ] .= Yii::t('models', 'Models: Failed to add entry :') . print_r($model->errors(), TRUE);
88 * Lists all Models models.
91 public function actionIndex()
93 if (! User::canPermission('createRecord') )
95 return $this->redirect(['site/index']);
97 $searchModel = new ModelsSearch();
98 if (isset(Yii::$app->request->queryParams['id']))
100 $id = Yii::$app->request->queryParams['id'];
101 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
102 $pageSize = $dataProvider->pagination->pageSize;
103 $dataProvider->pagination = FALSE;
104 $rows = $dataProvider->getModels();
106 foreach ($rows as $key => $val)
110 $page = ceil(($key + 1) / $pageSize);
114 return $this->redirect(['index', 'page' => $page]);
116 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
118 return $this->render('index', [
119 'searchModel' => $searchModel,
120 'dataProvider' => $dataProvider,
125 * Displays a single Models model.
128 * @throws NotFoundHttpException if the model cannot be found
130 public function actionView($id)
132 if (! User::canPermission('updateRecord') )
134 return $this->redirect(['index']);
136 return $this->render('view', [
137 'model' => $this->findModel($id),
142 * Creates a new Models model.
143 * If creation is successful, the browser will be redirected to the 'view' page.
146 public function actionCreate()
148 if (! User::canPermission('createRecord') )
150 return $this->redirect(['site/index']);
152 $model = new Models();
154 if ($model->load(Yii::$app->request->post()) && $model->save()) {
155 return $this->redirect(['index', 'id' => $model->id]);
158 return $this->render('create', [
164 * Updates an existing Models model.
165 * If update is successful, the browser will be redirected to the 'view' page.
168 * @throws NotFoundHttpException if the model cannot be found
170 public function actionUpdate($id)
172 if (! User::canPermission('updateRecord') )
174 return $this->redirect(['index']);
176 $model = $this->findModel($id);
178 if ($model->load(Yii::$app->request->post()) && $model->save()) {
179 return $this->redirect(['index', 'id' => $model->id]);
182 return $this->render('update', [
188 * Deletes an existing Models model.
189 * If deletion is successful, the browser will be redirected to the 'index' page.
192 * @throws NotFoundHttpException if the model cannot be found
194 public function actionDelete($id)
196 if (! User::canPermission('updateRecord') )
198 return $this->redirect(['index']);
200 $this->findModel($id)->delete();
202 return $this->redirect(['index']);
206 * Finds the Models model based on its primary key value.
207 * If the model is not found, a 404 HTTP exception will be thrown.
209 * @return Models the loaded model
210 * @throws NotFoundHttpException if the model cannot be found
212 protected function findModel($id)
214 if (($model = Models::findOne($id)) !== null) {
218 throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));