OSDN Git Service

Откорректированы сообщения и их перевод
[invent/invent.git] / controllers / TypesController.php
index 1e8cf31..c5e7307 100644 (file)
@@ -8,6 +8,7 @@ use app\models\TypesSearch;
 use yii\web\Controller;
 use yii\web\NotFoundHttpException;
 use yii\filters\VerbFilter;
+use app\models\User;
 
 /**
  * TypesController implements the CRUD actions for Types model.
@@ -21,53 +22,122 @@ class TypesController extends Controller
     {
         return [
             'verbs' => [
-                'class' => VerbFilter::className(),
+                'class'   => VerbFilter::className(),
                 'actions' => [
-                    'delete' => ['POST'],
+                    'delete' => [ 'POST' ],
                 ],
             ],
         ];
     }
 
     /**
-     * Lists all Types models.
+     * Добавление типа, если необходимо
+     * @param array $options
+     *        string 'name' - наименование
+     * @return integer|boolean - Идентификатор типа или FALSE в случае неудачи
+     */
+     public function addIfNeed($options)
+     {
+        $result = [
+            'id' => FALSE,
+            'error' => Yii::t('types', 'Types: key field "type" missing: ') . print_r($options, TRUE),
+        ];
+        if (is_array($options) && isset($options[ 'type' ]))
+        {
+            $model = Types::find()
+                ->where([ 'like', 'name', $options[ 'type' ] ])
+                ->all();
+            if (count($model) > 0)
+            {
+                $result['id'] = $model[0]->id;
+                $result['error'] = '';
+            }
+            else
+            {
+                $model = new Types();
+                $model->name = $options[ 'type' ];
+                if ($model->validate() && $model->save())
+                {
+                    $result['id'] = $model->id;
+                    $result['error'] = '';
+                }
+                else
+                {
+                    $result['error'] = Yii::t('types', 'Failed to add entry "{type}": ', $options) . print_r($model->errors['name'], TRUE);
+                }
+            }
+        }
+        return $result;
+     }
+
+    /**
+     * Список всех типов предметов/оборудования.
      * @return mixed
      */
     public function actionIndex()
     {
-        $searchModel = new TypesSearch();
+        if (! User::canPermission('createRecord'))
+        {
+            return $this->redirect(['site/index']);
+        }
+        $searchModel  = new TypesSearch();
+        if (isset(Yii::$app->request->queryParams['id'])) {
+            $id = Yii::$app->request->queryParams['id'];
+            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
+            //$dataProvider->query->select(Types::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', [
-            'searchModel' => $searchModel,
+            'searchModel'  => $searchModel,
             'dataProvider' => $dataProvider,
         ]);
     }
 
     /**
-     * Displays a single Types 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 Types 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 Types();
 
-        if ($model->load(Yii::$app->request->post()) && $model->save()) {
-            return $this->redirect(['index', 'id' => $model->id]);
+        if ($model->load(Yii::$app->request->post()) && $model->save())
+        {
+            return $this->redirect([ 'index', 'id' => $model->id ]);
         }
 
         return $this->render('create', [
@@ -76,18 +146,23 @@ class TypesController extends Controller
     }
 
     /**
-     * Updates an existing Types 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(['site/index']);
+        }
         $model = $this->findModel($id);
 
-        if ($model->load(Yii::$app->request->post()) && $model->save()) {
-            return $this->redirect(['index', 'id' => $model->id]);
+        if ($model->load(Yii::$app->request->post()) && $model->save())
+        {
+            return $this->redirect([ 'index', 'id' => $model->id ]);
         }
 
         return $this->render('update', [
@@ -96,17 +171,21 @@ class TypesController extends Controller
     }
 
     /**
-     * Deletes an existing Types 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(['site/index']);
+        }
         $this->findModel($id)->delete();
 
-        return $this->redirect(['index']);
+        return $this->redirect([ 'index' ]);
     }
 
     /**
@@ -118,7 +197,8 @@ class TypesController extends Controller
      */
     protected function findModel($id)
     {
-        if (($model = Types::findOne($id)) !== null) {
+        if (($model = Types::findOne($id)) !== null)
+        {
             return $model;
         }