PolicyController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. }
  74. $data['Policy']['filter'] = join(',',$arr);
  75. $data['Policy']['cengci'] = join(',',$data['cengci']);
  76. $model->load($data);
  77. $model->save();
  78. return $this->redirect(['index']);
  79. }
  80. return $this->render('create', [
  81. 'model' => $model,
  82. 'configModels' => $configModels,
  83. ]);
  84. }
  85. /**
  86. * Updates an existing Survey model.
  87. * If update is successful, the browser will be redirected to the 'view' Survey.
  88. *
  89. * @param int $id
  90. *
  91. * @return mixed
  92. */
  93. public function actionUpdate($id)
  94. {
  95. $model = $this->findModel($id);
  96. $configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy'])->all();
  97. if (Yii::$app->request->isPost) {
  98. $data = Yii::$app->request->post();
  99. $arr = [];
  100. foreach ($data as $key=>$value) {
  101. if($key == '_csrfBackend' || $key == 'Policy' || $key == 'cengci'){
  102. continue;
  103. }
  104. if($value) $arr[] = $key.'_' . $value;
  105. }
  106. $data['Policy']['filter'] = join(',',$arr);
  107. $data['Policy']['cengci'] = join(',',$data['cengci']);
  108. //
  109. $model->load($data);
  110. $model->save();
  111. // var_dump($model->cengci);die();
  112. return $this->redirect(['index']);
  113. }
  114. $filterArr = [];
  115. $filter = explode(',',$model->filter);
  116. if(!empty($filter)){
  117. foreach ($filter as $key=>$value) {
  118. $arr = explode('_',$value);
  119. $filterArr[$arr[0]] = $arr[1];
  120. }
  121. }
  122. $model->cengci = explode(',',$model->cengci);
  123. $model->filter = $filterArr;
  124. return $this->render('update', [
  125. 'model' => $model,
  126. 'configModels' => $configModels,
  127. ]);
  128. }
  129. /**
  130. * Deletes an existing Survey model.
  131. * If deletion is successful, the browser will be redirected to the 'index' Survey.
  132. *
  133. * @param int $id
  134. *
  135. * @return mixed
  136. */
  137. public function actionDelete($id)
  138. {
  139. $this->findModel($id)->delete();
  140. return $this->redirect(['index']);
  141. }
  142. /**
  143. * Finds the Survey model based on its primary key value.
  144. * If the model is not found, a 404 HTTP exception will be thrown.
  145. *
  146. * @param int $id
  147. *
  148. * @return Survey the loaded model
  149. *
  150. * @throws NotFoundHttpException if the model cannot be found
  151. */
  152. protected function findModel($id)
  153. {
  154. if (($model = Policy::findOne($id)) !== null) {
  155. return $model;
  156. } else {
  157. throw new NotFoundHttpException('The requested page does not exist.');
  158. }
  159. }
  160. }