OSDN Git Service

Исправлен список всех перемещений. Добавлен тест проверки возможности открытия этого...
[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         $result = [
42             'id' => FALSE,
43             'error' => Yii::t('status', 'Status: Key field "status" missing: ') . print_r($options, TRUE),
44         ];
45         if (is_array($options) && isset($options[ 'status' ]))
46         {
47             $model = Status::find()
48                 ->where([ 'like', 'name', $options[ 'status' ]])
49                 ->all();
50             if (count($model) > 0)
51             {
52                 $result[ 'id' ] = $model[0]->id;
53                 $result[ 'error' ] = '';
54             }
55             else
56             {
57                 $model = new Status();
58                 $model->name = $options[ 'status' ];
59                 if ($model->validate() && $model->save())
60                 {
61                     $result[ 'id' ] = $model->id;
62                     $result[ 'error' ] = '';
63                 }
64                 else
65                 {
66                     $result[ 'error' ] = Yii::t('status', 'Failed to add entry "{status}": ', $options) . print_r($model->errors, TRUE);
67                 }
68             }
69         }
70         return $result;
71     }
72
73     /**
74      * Показ всех состояний предметов/оборудования.
75      * @return mixed
76      */
77     public function actionIndex()
78     {
79         if (! User::canPermission('createRecord'))
80         {
81             return $this->redirect(['site/index']);
82         }
83         $searchModel = new StatusSearch();
84         if (isset(Yii::$app->request->queryParams['id'])) {
85             $id = Yii::$app->request->queryParams['id'];
86             $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
87             //$dataProvider->query->select(Status::tableName() . '.id');
88             $pageSize = $dataProvider->pagination->pageSize;
89             $dataProvider->pagination = FALSE;
90             $rows = $dataProvider->getModels();
91             $page = 0;
92             foreach ($rows as $key => $val) {
93                 if ($id == $val->id) {
94                     $page = ceil(($key + 1) / $pageSize);
95                     break;
96                 }
97             }
98             return $this->redirect(['index', 'page' => $page]);
99         }
100         $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
101
102         return $this->render('index', [
103             'searchModel' => $searchModel,
104             'dataProvider' => $dataProvider,
105         ]);
106     }
107
108     /**
109      * Показ одного состояния предмета/оборудования (не используется).
110      * @param integer $id
111      * @return mixed
112      * @throws NotFoundHttpException если отсутствует состояние
113      */
114     public function actionView($id)
115     {
116         if (! User::canPermission('updateRecord'))
117         {
118             return $this->redirect(['index']);
119         }
120         return $this->render('view', [
121             'model' => $this->findModel($id),
122         ]);
123     }
124
125     /**
126      * Создание новго состояния предмета/оборудования.
127      * В случае успешного создания, переход осуществляется к списку всех состояний предметов/оборудования.
128      * @return mixed
129      */
130     public function actionCreate()
131     {
132         if (! User::canPermission('createRecord'))
133         {
134             return $this->redirect(['site/index']);
135         }
136         $model = new Status();
137
138         if ($model->load(Yii::$app->request->post()) && $model->save()) {
139             return $this->redirect(['index', 'id' => $model->id]);
140         }
141
142         return $this->render('create', [
143             'model' => $model,
144         ]);
145     }
146
147     /**
148      * Изменение состояния предмета/оборудования.
149      * В случае успешного изменения, переход осуществляется к списку всех состояний предметов/оборудования.
150      * @param integer $id
151      * @return mixed
152      * @throws NotFoundHttpException если отсутствует состояние предмета/оборудования
153      */
154     public function actionUpdate($id)
155     {
156         if (! User::canPermission('updateRecord'))
157         {
158             return $this->redirect(['index']);
159         }
160         $model = $this->findModel($id);
161
162         if ($model->load(Yii::$app->request->post()) && $model->save()) {
163             return $this->redirect(['index', 'id' => $model->id]);
164         }
165
166         return $this->render('update', [
167             'model' => $model,
168         ]);
169     }
170
171     /**
172      * Deletes an existing Status model.
173      * В случае успешного удаления, переход осуществляется к списку всех состояний предметов/оборудования.
174      * @param integer $id
175      * @return mixed
176      * @throws NotFoundHttpException если отсутствует состояние предмета/оборудования
177      */
178     public function actionDelete($id)
179     {
180         if (! User::canPermission('updateRecord'))
181         {
182             return $this->redirect(['index']);
183         }
184         $this->findModel($id)->delete();
185
186         return $this->redirect(['index']);
187     }
188
189     /**
190      * Finds the Status model based on its primary key value.
191      * If the model is not found, a 404 HTTP exception will be thrown.
192      * @param integer $id
193      * @return Status the loaded model
194      * @throws NotFoundHttpException if the model cannot be found
195      */
196     protected function findModel($id)
197     {
198         if (($model = Status::findOne($id)) !== null) {
199             return $model;
200         }
201
202         throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
203     }
204 }