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 "model", "type" missing: ') . print_r($options, TRUE),
40 // Если указан инвентарный номер
41 if (is_array($options) && isset($options[ 'model' ]))
43 $model = Models::find()
44 ->where([ 'name' => $options[ 'model' ]])->all(); // Ищем наименование модели предмета/обрудования
46 if (count($model) > 0) // Записи найдены, выводим первую совпавшую
48 $result[ 'id' ] = $model[ 0 ]->id;
49 $result[ 'error' ] = '';
53 $type = TypesController::addIfNeed($options); // Найдём или добавим тип
54 // Если тип не добавили
55 if($type[ 'id' ] === FALSE)
57 $result[ 'error' ] = '<br />' . $type[ 'error' ];
58 //$type[ 'id' ] = NULL; // сделаем его пустым
62 // Создаём новую запись модели предмета/оборудования
63 $model = new Models();
64 $model->name = $options[ 'model' ]; // Сетевое имя
65 $model->type_id = isset($type[ 'id' ]) ? $type[ 'id' ] : NULL; // Идентификатор типа
66 $model->product = isset($options[ 'product' ]) ? $options[ 'product' ] : NULL; // Код оборудования
67 $model->modelnumber = isset($options[ 'modelnum' ]) ? $options[ 'modelnum' ] : NULL; // Номер модели
69 if ($model->validate() && $model->save())
71 $result[ 'id' ] = $model->id; // Возвращаем идентификатор записанного оборудования
72 $result[ 'error' ] = '';
76 $result[ 'error' ] .= Yii::t('models', 'Models: Failed to add entry: ') . print_r($model->errors, TRUE);
86 * Lists all Models models.
89 public function actionIndex()
91 if (! User::canPermission('createRecord') )
93 return $this->redirect(['site/index']);
95 $searchModel = new ModelsSearch();
96 if (isset(Yii::$app->request->queryParams['id']))
98 $id = Yii::$app->request->queryParams['id'];
99 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
100 $pageSize = $dataProvider->pagination->pageSize;
101 $dataProvider->pagination = FALSE;
102 $rows = $dataProvider->getModels();
104 foreach ($rows as $key => $val)
108 $page = ceil(($key + 1) / $pageSize);
112 return $this->redirect(['index', 'page' => $page]);
114 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
116 return $this->render('index', [
117 'searchModel' => $searchModel,
118 'dataProvider' => $dataProvider,
123 * Displays a single Models model.
126 * @throws NotFoundHttpException if the model cannot be found
128 public function actionView($id)
130 if (! User::canPermission('updateRecord') )
132 return $this->redirect(['index']);
134 return $this->render('view', [
135 'model' => $this->findModel($id),
140 * Creates a new Models model.
141 * If creation is successful, the browser will be redirected to the 'view' page.
144 public function actionCreate()
146 if (! User::canPermission('createRecord') )
148 return $this->redirect(['site/index']);
150 $model = new Models();
152 if ($model->load(Yii::$app->request->post()) && $model->save()) {
153 return $this->redirect(['index', 'id' => $model->id]);
156 return $this->render('create', [
162 * Updates an existing Models model.
163 * If update is successful, the browser will be redirected to the 'view' page.
166 * @throws NotFoundHttpException if the model cannot be found
168 public function actionUpdate($id)
170 if (! User::canPermission('updateRecord') )
172 return $this->redirect(['index']);
174 $model = $this->findModel($id);
176 if ($model->load(Yii::$app->request->post()) && $model->save()) {
177 return $this->redirect(['index', 'id' => $model->id]);
180 return $this->render('update', [
186 * Deletes an existing Models model.
187 * If deletion is successful, the browser will be redirected to the 'index' page.
190 * @throws NotFoundHttpException if the model cannot be found
192 public function actionDelete($id)
194 if (! User::canPermission('updateRecord') )
196 return $this->redirect(['index']);
198 $this->findModel($id)->delete();
200 return $this->redirect(['index']);
204 * Finds the Models model based on its primary key value.
205 * If the model is not found, a 404 HTTP exception will be thrown.
207 * @return Models the loaded model
208 * @throws NotFoundHttpException if the model cannot be found
210 protected function findModel($id)
212 if (($model = Models::findOne($id)) !== null) {
216 throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));