|
@@ -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;
|