PolicyController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace backend\controllers;
  3. use common\models\Policy;
  4. use Yii;
  5. use yii\data\ActiveDataProvider;
  6. use yii\filters\VerbFilter;
  7. use yii\web\Controller;
  8. use yii\web\NotFoundHttpException;
  9. use common\modules\config\models\Config;
  10. /**
  11. * PolicyController implements the CRUD actions for Survey model.
  12. */
  13. class PolicyController extends Controller
  14. {
  15. public function behaviors()
  16. {
  17. return [
  18. 'verbs' => [
  19. 'class' => VerbFilter::className(),
  20. 'actions' => [
  21. 'delete' => ['post'],
  22. ],
  23. ],
  24. ];
  25. }
  26. /**
  27. * Lists all Policy models.
  28. *
  29. * @return mixed
  30. */
  31. public function actionIndex()
  32. {
  33. $dataProvider = new ActiveDataProvider([
  34. 'query' => Policy::find(),
  35. ]);
  36. return $this->render('index', [
  37. 'dataProvider' => $dataProvider,
  38. ]);
  39. }
  40. /**
  41. * Displays a single Policy model.
  42. *
  43. * @param int $id
  44. *
  45. * @return mixed
  46. */
  47. public function actionView($id)
  48. {
  49. return $this->render('view', [
  50. 'model' => $this->findModel($id),
  51. ]);
  52. }
  53. /**
  54. * Creates a new Survey model.
  55. * If creation is successful, the browser will be redirected to the 'view' Survey.
  56. *
  57. * @return mixed
  58. */
  59. public function actionCreate()
  60. {
  61. $model = new Policy();
  62. $editor = request('editor') ? : config('page_editor_type');
  63. $model->markdown = $editor == 'markdown' ? 1 : 0;
  64. $configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy'])->all();
  65. if (Yii::$app->request->isPost) {
  66. $data = Yii::$app->request->post();
  67. $arr = [];
  68. foreach ($data as $key=>$value) {
  69. if($key == '_csrfBackend' || $key == 'Policy' || $key == 'cengci'){
  70. continue;
  71. }
  72. // if($value) $arr[] = $key.'_' . $value;
  73. $arr[$key] = $value;
  74. }
  75. // $data['Policy']['filter'] = join(',',$arr);
  76. $data['Policy']['filter'] = json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  77. $data['Policy']['cengci'] = join(',',$data['cengci']);
  78. $model->load($data);
  79. $model->save();
  80. return $this->redirect(['index']);
  81. }
  82. return $this->render('create', [
  83. 'model' => $model,
  84. 'configModels' => $configModels,
  85. ]);
  86. }
  87. /**
  88. * Updates an existing Survey model.
  89. * If update is successful, the browser will be redirected to the 'view' Survey.
  90. *
  91. * @param int $id
  92. *
  93. * @return mixed
  94. */
  95. public function actionUpdate($id)
  96. {
  97. $model = $this->findModel($id);
  98. $configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy'])->all();
  99. if (Yii::$app->request->isPost) {
  100. $data = Yii::$app->request->post();
  101. $arr = [];
  102. foreach ($data as $key=>$value) {
  103. if($key == '_csrfBackend' || $key == 'Policy' || $key == 'cengci'){
  104. continue;
  105. }
  106. // if($value) $arr[] = $key.'_' . $value;
  107. $arr[$key] = $value;
  108. }
  109. // $data['Policy']['filter'] = join(',',$arr);
  110. $data['Policy']['filter'] = json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  111. $data['Policy']['cengci'] = join(',',$data['cengci']);
  112. // $data['Policy']['type'] = join(',',$data['type']);
  113. $model->load($data);
  114. $model->type = implode(',', $model->type);
  115. $model->save();
  116. return $this->redirect(['index']);
  117. }
  118. // $filterArr = [];
  119. // $filter = explode(',',$model->filter);
  120. // if(!empty($filter)){
  121. // foreach ($filter as $key=>$value) {
  122. // if (empty($value)) {
  123. // continue;
  124. // }
  125. // $arr = explode('_',$value);
  126. // if (!empty($arr) && isset($arr[0]) && isset($arr[1])) {
  127. // $filterArr[$arr[0]] = $arr[1];
  128. // }
  129. // }
  130. // }
  131. $model->cengci = explode(',',$model->cengci);
  132. $model->type = explode(',',$model->type);
  133. // $model->filter = $filterArr;
  134. $model->filter = json_decode($model->filter,true);
  135. return $this->render('update', [
  136. 'model' => $model,
  137. 'configModels' => $configModels,
  138. ]);
  139. }
  140. /**
  141. * Deletes an existing Survey model.
  142. * If deletion is successful, the browser will be redirected to the 'index' Survey.
  143. *
  144. * @param int $id
  145. *
  146. * @return mixed
  147. */
  148. public function actionDelete($id)
  149. {
  150. $this->findModel($id)->delete();
  151. return $this->redirect(['index']);
  152. }
  153. /**
  154. * Finds the Survey model based on its primary key value.
  155. * If the model is not found, a 404 HTTP exception will be thrown.
  156. *
  157. * @param int $id
  158. *
  159. * @return Survey the loaded model
  160. *
  161. * @throws NotFoundHttpException if the model cannot be found
  162. */
  163. protected function findModel($id)
  164. {
  165. if (($model = Policy::findOne($id)) !== null) {
  166. return $model;
  167. } else {
  168. throw new NotFoundHttpException('The requested page does not exist.');
  169. }
  170. }
  171. }