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());
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 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
100 return $this->render('index', [
101 'searchModel' => $searchModel,
102 'dataProvider' => $dataProvider,
107 * Displays a single Models model.
110 * @throws NotFoundHttpException if the model cannot be found
112 public function actionView($id)
114 if (! User::canPermission('updateRecord') )
116 return $this->redirect(['index']);
118 return $this->render('view', [
119 'model' => $this->findModel($id),
124 * Creates a new Models model.
125 * If creation is successful, the browser will be redirected to the 'view' page.
128 public function actionCreate()
130 if (! User::canPermission('createRecord') )
132 return $this->redirect(['site/index']);
134 $model = new Models();
136 if ($model->load(Yii::$app->request->post()) && $model->save()) {
137 return $this->redirect(['index', 'id' => $model->id]);
140 return $this->render('create', [
146 * Updates an existing Models model.
147 * If update is successful, the browser will be redirected to the 'view' page.
150 * @throws NotFoundHttpException if the model cannot be found
152 public function actionUpdate($id)
154 if (! User::canPermission('updateRecord') )
156 return $this->redirect(['index']);
158 $model = $this->findModel($id);
160 if ($model->load(Yii::$app->request->post()) && $model->save()) {
161 return $this->redirect(['index', 'id' => $model->id]);
164 return $this->render('update', [
170 * Deletes an existing Models model.
171 * If deletion is successful, the browser will be redirected to the 'index' page.
174 * @throws NotFoundHttpException if the model cannot be found
176 public function actionDelete($id)
178 if (! User::canPermission('updateRecord') )
180 return $this->redirect(['index']);
182 $this->findModel($id)->delete();
184 return $this->redirect(['index']);
188 * Finds the Models model based on its primary key value.
189 * If the model is not found, a 404 HTTP exception will be thrown.
191 * @return Models the loaded model
192 * @throws NotFoundHttpException if the model cannot be found
194 protected function findModel($id)
196 if (($model = Models::findOne($id)) !== null) {
200 throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));