OSDN Git Service

Добавлен контроль доступа по ролям (RBAC). Пользователи хранятся в базе данных. Созда...
[invent/invent.git] / controllers / StatusController.php
1 <?php
2
3 namespace app\controllers;
4
5 use Yii;
6 use app\models\Status;
7 use app\models\StatusSearch;
8 use yii\web\Controller;
9 use yii\web\NotFoundHttpException;
10 use yii\filters\VerbFilter;
11 use app\models\User;
12
13 /**
14  * StatusController implements the CRUD actions for Status model.
15  */
16 class StatusController extends Controller
17 {
18     /**
19      * {@inheritdoc}
20      */
21     public function behaviors()
22     {
23         return [
24             'verbs' => [
25                 'class' => VerbFilter::className(),
26                 'actions' => [
27                     'delete' => ['POST'],
28                 ],
29             ],
30         ];
31     }
32
33     /**
34      * Добавление в случае необходимости состояния
35      * @param array $options
36      *        string 'name'  - Наименование состояния
37      * @return mixed
38      */
39     public function addIfNeed($options)
40     {
41         if (is_array($options) && isset($options[ 'name' ]))
42         $status = Status::find()
43             ->where([ 'like', 'name', $options[ 'name' ]])
44             ->all();
45         if (count($status) > 0)
46         {
47             return $status[0]->id;
48         }
49         $status = new Status();
50         $status->name = $options[ 'name' ];
51         if ($status->validate() && $status->save())
52         {
53             return $status->id;
54         }
55         return FALSE;
56     }
57
58     /**
59      * Показ всех состояний предметов/оборудования.
60      * @return mixed
61      */
62     public function actionIndex()
63     {
64         if (! User::canPermission('createRecord'))
65         {
66             return $this->redirect(['site/index']);
67         }
68         $searchModel = new StatusSearch();
69         $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
70
71         return $this->render('index', [
72             'searchModel' => $searchModel,
73             'dataProvider' => $dataProvider,
74         ]);
75     }
76
77     /**
78      * Показ одного состояния предмета/оборудования (не используется).
79      * @param integer $id
80      * @return mixed
81      * @throws NotFoundHttpException если отсутствует состояние
82      */
83     public function actionView($id)
84     {
85         if (! User::canPermission('updateRecord'))
86         {
87             return $this->redirect(['index']);
88         }
89         return $this->render('view', [
90             'model' => $this->findModel($id),
91         ]);
92     }
93
94     /**
95      * Создание новго состояния предмета/оборудования.
96      * В случае успешного создания, переход осуществляется к списку всех состояний предметов/оборудования.
97      * @return mixed
98      */
99     public function actionCreate()
100     {
101         if (! User::canPermission('createRecord'))
102         {
103             return $this->redirect(['site/index']);
104         }
105         $model = new Status();
106
107         if ($model->load(Yii::$app->request->post()) && $model->save()) {
108             return $this->redirect(['index', 'id' => $model->id]);
109         }
110
111         return $this->render('create', [
112             'model' => $model,
113         ]);
114     }
115
116     /**
117      * Изменение состояния предмета/оборудования.
118      * В случае успешного изменения, переход осуществляется к списку всех состояний предметов/оборудования.
119      * @param integer $id
120      * @return mixed
121      * @throws NotFoundHttpException если отсутствует состояние предмета/оборудования
122      */
123     public function actionUpdate($id)
124     {
125         if (! User::canPermission('updateRecord'))
126         {
127             return $this->redirect(['index']);
128         }
129         $model = $this->findModel($id);
130
131         if ($model->load(Yii::$app->request->post()) && $model->save()) {
132             return $this->redirect(['index', 'id' => $model->id]);
133         }
134
135         return $this->render('update', [
136             'model' => $model,
137         ]);
138     }
139
140     /**
141      * Deletes an existing Status model.
142      * В случае успешного удаления, переход осуществляется к списку всех состояний предметов/оборудования.
143      * @param integer $id
144      * @return mixed
145      * @throws NotFoundHttpException если отсутствует состояние предмета/оборудования
146      */
147     public function actionDelete($id)
148     {
149         if (! User::canPermission('updateRecord'))
150         {
151             return $this->redirect(['index']);
152         }
153         $this->findModel($id)->delete();
154
155         return $this->redirect(['index']);
156     }
157
158     /**
159      * Finds the Status model based on its primary key value.
160      * If the model is not found, a 404 HTTP exception will be thrown.
161      * @param integer $id
162      * @return Status the loaded model
163      * @throws NotFoundHttpException if the model cannot be found
164      */
165     protected function findModel($id)
166     {
167         if (($model = Status::findOne($id)) !== null) {
168             return $model;
169         }
170
171         throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
172     }
173 }