OSDN Git Service

Откорректированы сообщения и их перевод
[invent/invent.git] / controllers / LocationsController.php
index d094b5b..cd66800 100644 (file)
@@ -8,6 +8,7 @@ use app\models\LocationsSearch;
 use yii\web\Controller;
 use yii\web\NotFoundHttpException;
 use yii\filters\VerbFilter;
+use app\models\User;
 
 /**
  * LocationsController implements the CRUD actions for Locations model.
@@ -21,53 +22,146 @@ class LocationsController extends Controller
     {
         return [
             'verbs' => [
-                'class' => VerbFilter::className(),
+                'class'   => VerbFilter::className(),
                 'actions' => [
-                    'delete' => ['POST'],
+                    'delete' => [ 'POST' ],
                 ],
             ],
         ];
     }
 
     /**
-     * Lists all Locations models.
+     * Добавление места разположения
+     *
+     * @param array $options
+     *        string 'name'    - наименование места расположения
+     *        string|NULL  'region'    - наименование региона/подразделения
+     *        integer|NULL 'region_id' - идентификатор региона/подразделения
+     * @return integer|boolean - идентификатор записи места расположения или FALSE
+     */
+    public static function addIfNeed($options)
+    {
+        $result = [
+            'id'    => FALSE,
+            'error' => Yii::t('locations', 'Locations: Key field missing "location" and "region": ') . print_r($options, TRUE),
+        ];
+        if (is_array($options) && isset($options[ 'location' ]) && (isset($options[ 'region' ]) || isset($options[ 'region_id' ])))
+        {
+            if (isset($options[ 'region' ]))
+            {
+                $region = RegionsController::addIfNeed($options);
+            }
+            else
+            {
+                $region[ 'id' ] = $options[ 'region_id' ];
+            }
+            if ($region[ 'id' ] !== FALSE) {
+                // Ищем расположение, совпадающее по наименованию и региону/подразделению
+                $model = Locations::find()
+                    ->where([ 'like', 'name', $options[ 'location' ]])
+                    ->andWhere([ 'region_id' => $region[ 'id' ]])
+                    ->all();
+                if (count($model) > 0)
+                {
+                    // Если нашли, возвращаем идентификатор записи
+                    $result[ 'id' ] = $model[0]->id;
+                    $result[ 'error' ] = '';
+                }
+                else
+                {
+                    // Не нашли, пробуем добавить место расположения
+                    $model = new Locations();
+                    $model->name = $options[ 'location' ];
+                    $model->region_id = $region[ 'id' ];
+                    if($model->validate() && $model->save())
+                    {
+                        // Если удалось сохранить, вернём идентификатор места расположения
+                        $result[ 'id' ] = $model->id;
+                        $result[ 'error' ] = '';
+                    }
+                    else
+                    {
+                        $result[ 'error' ] = Yii::t('locations', 'Error to create location "{location}": ', $options) . print_r($model->errors, TRUE);
+                    }
+                }
+            }
+            else
+            {
+                $result[ 'error' ] .= '<br />' . $region[ 'error' ];
+            }
+        }
+        return $result; // Записать не удалось, вернём FALSE
+    }
+
+    /**
+     * Список всех мест/размещений.
      * @return mixed
      */
     public function actionIndex()
     {
+        if (! User::canPermission('createRecord'))
+        {
+            return $this->redirect(['site/index']);
+        }
+
         $searchModel = new LocationsSearch();
+        if (isset(Yii::$app->request->queryParams['id'])) {
+            $id = Yii::$app->request->queryParams['id'];
+            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
+            //$dataProvider->query->select([Locations::tableName() . '.id', Locations::tableName() . '.name']);
+            $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 Locations 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 Locations 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 Locations();
 
-        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 +170,23 @@ class LocationsController extends Controller
     }
 
     /**
-     * Updates an existing Locations 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 +195,21 @@ class LocationsController extends Controller
     }
 
     /**
-     * Deletes an existing Locations 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']);
+        return $this->redirect([ 'index' ]);
     }
 
     /**
@@ -118,7 +221,8 @@ class LocationsController extends Controller
      */
     protected function findModel($id)
     {
-        if (($model = Locations::findOne($id)) !== null) {
+        if (($model = Locations::findOne($id)) !== null)
+        {
             return $model;
         }