PolicyController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. return $this->redirect(['index']);
  112. }
  113. $filterArr = [];
  114. $filter = explode(',',$model->filter);
  115. if(!empty($filter)){
  116. foreach ($filter as $key=>$value) {
  117. $arr = explode('_',$value);
  118. $filterArr[$arr[0]] = $arr[1];
  119. }
  120. }
  121. $model->cengci = explode(',',$model->cengci);
  122. $model->filter = $filterArr;
  123. return $this->render('update', [
  124. 'model' => $model,
  125. 'configModels' => $configModels,
  126. ]);
  127. }
  128. /**
  129. * Deletes an existing Survey model.
  130. * If deletion is successful, the browser will be redirected to the 'index' Survey.
  131. *
  132. * @param int $id
  133. *
  134. * @return mixed
  135. */
  136. public function actionDelete($id)
  137. {
  138. $this->findModel($id)->delete();
  139. return $this->redirect(['index']);
  140. }
  141. /**
  142. * Finds the Survey model based on its primary key value.
  143. * If the model is not found, a 404 HTTP exception will be thrown.
  144. *
  145. * @param int $id
  146. *
  147. * @return Survey the loaded model
  148. *
  149. * @throws NotFoundHttpException if the model cannot be found
  150. */
  151. protected function findModel($id)
  152. {
  153. if (($model = Policy::findOne($id)) !== null) {
  154. return $model;
  155. } else {
  156. throw new NotFoundHttpException('The requested page does not exist.');
  157. }
  158. }
  159. }