OSDN Git Service

Исправлен список всех перемещений. Добавлен тест проверки возможности открытия этого...
[invent/invent.git] / controllers / TypesController.php
index e215743..c5e7307 100644 (file)
@@ -38,23 +38,36 @@ class TypesController extends Controller
      */
      public function addIfNeed($options)
      {
-        if (is_array($options) && isset($options[ 'name' ]))
+        $result = [
+            'id' => FALSE,
+            'error' => Yii::t('types', 'Types: key field "type" missing: ') . print_r($options, TRUE),
+        ];
+        if (is_array($options) && isset($options[ 'type' ]))
         {
-            $type = Types::find()
-                ->where([ 'like', 'name', $options[ 'name' ] ])
+            $model = Types::find()
+                ->where([ 'like', 'name', $options[ 'type' ] ])
                 ->all();
-            if (count($type) > 0)
+            if (count($model) > 0)
             {
-                return $type[0]->id;
+                $result['id'] = $model[0]->id;
+                $result['error'] = '';
             }
-            $type = new Types();
-            $type->name = $options[ 'name' ];
-            if ($type->validate() && $type->save())
+            else
             {
-                return $type->id;
+                $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 FALSE;
+        return $result;
      }
 
     /**
@@ -68,6 +81,22 @@ class TypesController extends Controller
             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', [