jiangzixin 1 год назад
Родитель
Сommit
8e50b776a7

+ 77 - 41
server/backend/controllers/PolicyController.php

@@ -2,11 +2,13 @@
 
 namespace backend\controllers;
 
+use common\components\Controller;
 use common\models\Policy;
 use Yii;
+use yii\base\Exception;
 use yii\data\ActiveDataProvider;
 use yii\filters\VerbFilter;
-use yii\web\Controller;
+use yii\helpers\Url;
 use yii\web\NotFoundHttpException;
 use common\modules\config\models\Config;
 
@@ -33,6 +35,7 @@ class PolicyController extends Controller
      */
     public function actionIndex()
     {
+        Url::remember();
         $dataProvider = new ActiveDataProvider([
             'query' => Policy::find(),
         ]);
@@ -71,24 +74,42 @@ class PolicyController extends Controller
 		$configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy'])->all();
 		
 		if (Yii::$app->request->isPost) {
-			$data = Yii::$app->request->post();
-		
-			$arr = [];
-			foreach ($data as $key=>$value) {
-				if($key == '_csrfBackend' || $key == 'Policy' || $key == 'cengci'){
-					continue;
-				}
-//				if($value) $arr[] = $key.'_' . $value;
-                $arr[$key] = $value;
-			}
-
-//			$data['Policy']['filter'] = join(',',$arr);
-            $data['Policy']['filter'] = json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
-			$data['Policy']['cengci'] = join(',',$data['cengci']);
-
-			$model->load($data);
-			$model->save();
-			return $this->redirect(['index']);
+            $transaction = Yii::$app->db->beginTransaction();
+            try {
+                $data = Yii::$app->request->post();
+
+                $arr = [];
+                foreach ($data as $key=>$value) {
+                    if($key == '_csrfBackend' || $key == 'Policy' || $key == 'cengci'){
+                        continue;
+                    }
+                    $arr[$key] = $value;
+                }
+
+                $data['Policy']['filter'] = json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
+                if (empty($data['cengci'])) {
+                    throw new Exception('层次为空');
+                }
+                if (empty($data['Policy']['type'])) {
+                    throw new Exception('类型为空');
+                }
+
+                $data['Policy']['cengci'] = join(',',$data['cengci']);
+                $data['Policy']['type'] = join(',',$data['Policy']['type']);
+                $model->load($data);
+                $model->save();
+
+                if($model->hasErrors()) {
+                    throw new Exception(current($model->getErrors())[0]);
+                }
+
+                $transaction->commit();
+                Yii::$app->session->setFlash('success', '操作成功');
+            } catch (\Exception $e) {
+                $transaction->rollBack();
+                Yii::$app->session->setFlash('error', $e->getMessage());
+            }
+            return $this->goBack();
 		}
     
 		return $this->render('create', [
@@ -111,28 +132,43 @@ class PolicyController extends Controller
         $model = $this->findModel($id);
         
 		$configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy'])->all();
-		
 		if (Yii::$app->request->isPost) {
-			$data = Yii::$app->request->post();
-
-			$arr = [];
-			foreach ($data as $key=>$value) {
-				if($key == '_csrfBackend' || $key == 'Policy' || $key == 'cengci'){
-					continue;
-				}
-				
-//				if($value) $arr[] = $key.'_' . $value;
-                $arr[$key] = $value;
-			}
-		
-//			$data['Policy']['filter'] = join(',',$arr);
-            $data['Policy']['filter'] = json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
-			$data['Policy']['cengci'] = join(',',$data['cengci']);
-//			$data['Policy']['type'] = join(',',$data['type']);
-			$model->load($data);
-            $model->type = implode(',', $model->type);
-			$model->save();
-			return $this->redirect(['index']);
+            $transaction = Yii::$app->db->beginTransaction();
+            try {
+                $data = Yii::$app->request->post();
+
+                $arr = [];
+                foreach ($data as $key=>$value) {
+                    if($key == '_csrfBackend' || $key == 'Policy' || $key == 'cengci'){
+                        continue;
+                    }
+                    $arr[$key] = $value;
+                }
+
+                $data['Policy']['filter'] = json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
+                if (empty($data['cengci'])) {
+                    throw new Exception('层次为空');
+                }
+                if (empty($data['Policy']['type'])) {
+                    throw new Exception('类型为空');
+                }
+
+                $data['Policy']['cengci'] = join(',',$data['cengci']);
+                $data['Policy']['type'] = join(',',$data['Policy']['type']);
+                $model->load($data);
+                $model->save();
+
+                if($model->hasErrors()) {
+                    throw new Exception(current($model->getErrors())[0]);
+                }
+
+                $transaction->commit();
+                Yii::$app->session->setFlash('success', '操作成功');
+            } catch (\Exception $e) {
+                $transaction->rollBack();
+                Yii::$app->session->setFlash('error', $e->getMessage());
+            }
+            return $this->goBack();
 		}
 		
 //		$filterArr = [];
@@ -184,7 +220,7 @@ class PolicyController extends Controller
      *
      * @throws NotFoundHttpException if the model cannot be found
      */
-    protected function findModel($id)
+    public function findModel($id)
     {
         if (($model = Policy::findOne($id)) !== null) {
             return $model;

+ 4 - 3
server/backend/views/policy/_form.php

@@ -1,17 +1,18 @@
 <?php
 
 use yii\helpers\Html;
-use yii\widgets\ActiveForm;
+use backend\widgets\ActiveForm;
 
 /* @var $this yii\web\View */
 /* @var $model common\models\Policy */
-/* @var $form yii\widgets\ActiveForm */
+/* @var $form backend\widgets\ActiveForm */
 /* @var $configModels [] */
 ?>
 
 <div class="box box-success">
     <div class="box-body">
-        <?php $form = ActiveForm::begin(['enableClientValidation'=>false]); ?>
+        <?php $form = ActiveForm::begin([
+                'enableClientValidation'=>false]); ?>
 
         <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
         <?= $form->field($model, 'author')->textInput(['maxlength' => true]) ?>

+ 1 - 1
server/common/models/Policy.php

@@ -48,7 +48,7 @@ class Policy extends \yii\db\ActiveRecord
     public function rules()
     {
         return [
-            [['content', 'title', 'summary', 'type'], 'required'],
+            [['content', 'title', 'summary', 'type','cengci'], 'required'],
             [['content'], 'string'],
 			[['filter','cengci'], 'string'],
             ['markdown', 'default', 'value' => $this->getIsMarkdown()],