3 namespace app\controllers;
7 use app\models\StatusSearch;
8 use yii\web\Controller;
9 use yii\web\NotFoundHttpException;
10 use yii\filters\VerbFilter;
14 * StatusController implements the CRUD actions for Status model.
16 class StatusController extends Controller
21 public function behaviors()
25 'class' => VerbFilter::className(),
34 * Добавление в случае необходимости состояния
35 * @param array $options
36 * string 'name' - Наименование состояния
39 public function addIfNeed($options)
43 'error' => Yii::t('status', 'Status: Key field "status" missing: ') . print_r($options, TRUE),
45 if (is_array($options) && isset($options[ 'status' ]))
46 $model = Status::find()
47 ->where([ 'like', 'name', $options[ 'status' ]])
49 if (count($model) > 0)
51 $result[ 'id' ] = $model[0]->id;
52 $result [ 'error' ] = '';
56 $model = new Status();
57 $model->name = $options[ 'status' ];
58 if ($model->validate() && $model->save())
60 $result[ 'id' ] = $model->id;
61 $result[ 'error' ] = '';
65 $result[ 'error' ] = Yii::t('status', 'Failed to add entry "{status}": ', $options) . print_r($model->errors, TRUE);
72 * Показ всех состояний предметов/оборудования.
75 public function actionIndex()
77 if (! User::canPermission('createRecord'))
79 return $this->redirect(['site/index']);
81 $searchModel = new StatusSearch();
82 if (isset(Yii::$app->request->queryParams['id'])) {
83 $id = Yii::$app->request->queryParams['id'];
84 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
85 //$dataProvider->query->select(Status::tableName() . '.id');
86 $pageSize = $dataProvider->pagination->pageSize;
87 $dataProvider->pagination = FALSE;
88 $rows = $dataProvider->getModels();
90 foreach ($rows as $key => $val) {
91 if ($id == $val->id) {
92 $page = ceil(($key + 1) / $pageSize);
96 return $this->redirect(['index', 'page' => $page]);
98 $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
100 return $this->render('index', [
101 'searchModel' => $searchModel,
102 'dataProvider' => $dataProvider,
107 * Показ одного состояния предмета/оборудования (не используется).
110 * @throws NotFoundHttpException если отсутствует состояние
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 * Создание новго состояния предмета/оборудования.
125 * В случае успешного создания, переход осуществляется к списку всех состояний предметов/оборудования.
128 public function actionCreate()
130 if (! User::canPermission('createRecord'))
132 return $this->redirect(['site/index']);
134 $model = new Status();
136 if ($model->load(Yii::$app->request->post()) && $model->save()) {
137 return $this->redirect(['index', 'id' => $model->id]);
140 return $this->render('create', [
146 * Изменение состояния предмета/оборудования.
147 * В случае успешного изменения, переход осуществляется к списку всех состояний предметов/оборудования.
150 * @throws NotFoundHttpException если отсутствует состояние предмета/оборудования
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 Status model.
171 * В случае успешного удаления, переход осуществляется к списку всех состояний предметов/оборудования.
174 * @throws NotFoundHttpException если отсутствует состояние предмета/оборудования
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 Status model based on its primary key value.
189 * If the model is not found, a 404 HTTP exception will be thrown.
191 * @return Status the loaded model
192 * @throws NotFoundHttpException if the model cannot be found
194 protected function findModel($id)
196 if (($model = Status::findOne($id)) !== null) {
200 throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));