OSDN Git Service

Откорректированы сообщения и их перевод
[invent/invent.git] / controllers / StatusController.php
index 66fb62e..03cd197 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,12 +31,70 @@ class StatusController extends Controller
     }
 
     /**
-     * Lists all Status models.
+     * Добавление в случае необходимости состояния
+     * @param array $options
+     *        string 'name'  - Наименование состояния
+     * @return mixed
+     */
+    public function addIfNeed($options)
+    {
+        $result = [
+            'id' => FALSE,
+            'error' => Yii::t('status', 'Status: Key field "status" missing: ') . print_r($options, TRUE),
+        ];
+        if (is_array($options) && isset($options[ 'status' ]))
+        $model = Status::find()
+            ->where([ 'like', 'name', $options[ 'status' ]])
+            ->all();
+        if (count($model) > 0)
+        {
+            $result[ 'id' ] = $model[0]->id;
+            $result [ 'error' ] = '';
+        }
+        else
+        {
+            $model = new Status();
+            $model->name = $options[ 'status' ];
+            if ($model->validate() && $model->save())
+            {
+                $result[ 'id' ] = $model->id;
+                $result[ 'error' ] = '';
+            }
+            else
+            {
+                $result[ 'error' ] = Yii::t('status', 'Failed to add entry "{status}": ', $options) . print_r($model->errors, TRUE);
+            }
+        }
+        return $result;
+    }
+
+    /**
+     * Показ всех состояний предметов/оборудования.
      * @return mixed
      */
     public function actionIndex()
     {
+        if (! User::canPermission('createRecord'))
+        {
+            return $this->redirect(['site/index']);
+        }
         $searchModel = new StatusSearch();
+        if (isset(Yii::$app->request->queryParams['id'])) {
+            $id = Yii::$app->request->queryParams['id'];
+            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
+            //$dataProvider->query->select(Status::tableName() . '.id');
+            $pageSize = $dataProvider->pagination->pageSize;
+            $dataProvider->pagination = FALSE;
+            $rows = $dataProvider->getModels();
+            $page = 0;
+            foreach ($rows as $key => $val) {
+                if ($id == $val->id) {
+                    $page = ceil(($key + 1) / $pageSize);
+                    break;
+                }
+            }
+            return $this->redirect(['index', 'page' => $page]);
+        }
         $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
 
         return $this->render('index', [
@@ -45,29 +104,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 +143,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 +168,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']);