OSDN Git Service

Модификация импорта
[invent/invent.git] / controllers / StatusController.php
index 66fb62e..f4c5840 100644 (file)
@@ -8,6 +8,7 @@ use app\models\StatusSearch;
 use yii\web\Controller;
 use yii\web\NotFoundHttpException;
 use yii\filters\VerbFilter;
+use app\models\User;
 
 /**
  * StatusController implements the CRUD actions for Status model.
@@ -30,11 +31,40 @@ class StatusController extends Controller
     }
 
     /**
-     * Lists all Status models.
+     * Добавление в случае необходимости состояния
+     * @param array $options
+     *        string 'name'  - Наименование состояния
+     * @return mixed
+     */
+    public function addIfNeed($options)
+    {
+        if (is_array($options) && isset($options[ 'name' ]))
+        $status = Status::find()
+            ->where([ 'like', 'name', $options[ 'name' ]])
+            ->all();
+        if (count($status) > 0)
+        {
+            return $status[0]->id;
+        }
+        $status = new Status();
+        $status->name = $options[ 'name' ];
+        if ($status->validate() && $status->save())
+        {
+            return $status->id;
+        }
+        return FALSE;
+    }
+
+    /**
+     * Показ всех состояний предметов/оборудования.
      * @return mixed
      */
     public function actionIndex()
     {
+        if (! User::canPermission('createRecord'))
+        {
+            return $this->redirect(['site/index']);
+        }
         $searchModel = new StatusSearch();
         $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
 
@@ -45,29 +75,37 @@ class StatusController extends Controller
     }
 
     /**
-     * Displays a single Status model.
+     * Показ одного состояния предмета/оборудования (не используется).
      * @param integer $id
      * @return mixed
-     * @throws NotFoundHttpException if the model cannot be found
+     * @throws NotFoundHttpException если отсутствует состояние
      */
     public function actionView($id)
     {
+        if (! User::canPermission('updateRecord'))
+        {
+            return $this->redirect(['index']);
+        }
         return $this->render('view', [
             'model' => $this->findModel($id),
         ]);
     }
 
     /**
-     * Creates a new Status model.
-     * If creation is successful, the browser will be redirected to the 'view' page.
+     * Создание новго состояния предмета/оборудования.
+     * В случае успешного создания, переход осуществляется к списку всех состояний предметов/оборудования.
      * @return mixed
      */
     public function actionCreate()
     {
+        if (! User::canPermission('createRecord'))
+        {
+            return $this->redirect(['site/index']);
+        }
         $model = new Status();
 
         if ($model->load(Yii::$app->request->post()) && $model->save()) {
-            return $this->redirect(['view', 'id' => $model->id]);
+            return $this->redirect(['index', 'id' => $model->id]);
         }
 
         return $this->render('create', [
@@ -76,18 +114,22 @@ class StatusController extends Controller
     }
 
     /**
-     * Updates an existing Status model.
-     * If update is successful, the browser will be redirected to the 'view' page.
+     * Изменение состояния предмета/оборудования.
+     * В случае успешного изменения, переход осуществляется к списку всех состояний предметов/оборудования.
      * @param integer $id
      * @return mixed
-     * @throws NotFoundHttpException if the model cannot be found
+     * @throws NotFoundHttpException если отсутствует состояние предмета/оборудования
      */
     public function actionUpdate($id)
     {
+        if (! User::canPermission('updateRecord'))
+        {
+            return $this->redirect(['index']);
+        }
         $model = $this->findModel($id);
 
         if ($model->load(Yii::$app->request->post()) && $model->save()) {
-            return $this->redirect(['view', 'id' => $model->id]);
+            return $this->redirect(['index', 'id' => $model->id]);
         }
 
         return $this->render('update', [
@@ -97,13 +139,17 @@ class StatusController extends Controller
 
     /**
      * Deletes an existing Status model.
-     * If deletion is successful, the browser will be redirected to the 'index' page.
+     * В случае успешного удаления, переход осуществляется к списку всех состояний предметов/оборудования.
      * @param integer $id
      * @return mixed
-     * @throws NotFoundHttpException if the model cannot be found
+     * @throws NotFoundHttpException если отсутствует состояние предмета/оборудования
      */
     public function actionDelete($id)
     {
+        if (! User::canPermission('updateRecord'))
+        {
+            return $this->redirect(['index']);
+        }
         $this->findModel($id)->delete();
 
         return $this->redirect(['index']);