OSDN Git Service

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