OSDN Git Service

Откорректированы сообщения и их перевод
[invent/invent.git] / controllers / StatusController.php
index f4c5840..03cd197 100644 (file)
@@ -38,21 +38,34 @@ class StatusController extends Controller
      */
     public function addIfNeed($options)
     {
-        if (is_array($options) && isset($options[ 'name' ]))
-        $status = Status::find()
-            ->where([ 'like', 'name', $options[ 'name' ]])
+        $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($status) > 0)
+        if (count($model) > 0)
         {
-            return $status[0]->id;
+            $result[ 'id' ] = $model[0]->id;
+            $result [ 'error' ] = '';
         }
-        $status = new Status();
-        $status->name = $options[ 'name' ];
-        if ($status->validate() && $status->save())
+        else
         {
-            return $status->id;
+            $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 FALSE;
+        return $result;
     }
 
     /**
@@ -66,6 +79,22 @@ class StatusController extends Controller
             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', [