PolicyController.php 6.9 KB

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