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