PolicyController.php 7.6 KB

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