PolicyController.php 6.7 KB

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