OSDN Git Service

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