OSDN Git Service

Откорректированы сообщения и их перевод
[invent/invent.git] / controllers / LocationsController.php
index 8c423d1..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.
@@ -40,37 +41,56 @@ class LocationsController extends Controller
      */
     public static function addIfNeed($options)
     {
-        if (is_array($options) && isset($options[ 'name' ]) && (isset($options[ 'region' ]) || isset($options[ 'region_id' ])))
+        $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_id = RegionsController::addIfNeed([ 'name' => $options[ 'region' ]]);
+                $region = RegionsController::addIfNeed($options);
             }
             else
             {
-                $region_id = $options[ 'region_id' ];
+                $region[ 'id' ] = $options[ 'region_id' ];
             }
-            if ($region_id !== FALSE) {
+            if ($region[ 'id' ] !== FALSE) {
                 // Ищем расположение, совпадающее по наименованию и региону/подразделению
-                $location = Locations::find()
-                    ->where([ 'like', 'name', $options[ 'name' ]])
-                    ->andWhere([ 'region_id' => $region_id ])
+                $model = Locations::find()
+                    ->where([ 'like', 'name', $options[ 'location' ]])
+                    ->andWhere([ 'region_id' => $region[ 'id' ]])
                     ->all();
-                if (count($location) > 0)
+                if (count($model) > 0)
                 {
-                    return $location[0]->id; // Если нашли, возвращаем идентификатор записи
+                    // Если нашли, возвращаем идентификатор записи
+                    $result[ 'id' ] = $model[0]->id;
+                    $result[ 'error' ] = '';
                 }
-                // Не нашли, пробуем добавить место расположения
-                $location = new Locations();
-                $location->name = $options[ 'name' ];
-                $location->region_id = $region_id;
-                if($location->validate() && $location->save())
+                else
                 {
-                    return $location->id; // Если удалось сохранить, вернём идентификатор места расположения
+                    // Не нашли, пробуем добавить место расположения
+                    $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 FALSE; // Записать не удалось, вернём FALSE
+        return $result; // Записать не удалось, вернём FALSE
     }
 
     /**
@@ -79,7 +99,28 @@ class LocationsController extends Controller
      */
     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', [
@@ -96,6 +137,10 @@ class LocationsController extends Controller
      */
     public function actionView($id)
     {
+        if (! User::canPermission('updateRecord'))
+        {
+            return $this->redirect(['index']);
+        }
         return $this->render('view', [
             'model' => $this->findModel($id),
         ]);
@@ -108,6 +153,10 @@ class LocationsController extends Controller
      */
     public function actionCreate()
     {
+        if (! User::canPermission('createRecord'))
+        {
+            return $this->redirect(['site/index']);
+        }
         $model = new Locations();
 
         if ($model->load(Yii::$app->request->post()) && $model->save())
@@ -129,6 +178,10 @@ class LocationsController extends Controller
      */
     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())
@@ -150,6 +203,10 @@ class LocationsController extends Controller
      */
     public function actionDelete($id)
     {
+        if (! User::canPermission('updateRecord'))
+        {
+            return $this->redirect(['index']);
+        }
         $this->findModel($id)->delete();
 
         return $this->redirect([ 'index' ]);