PolicyController.php 7.4 KB

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