SurveyNewController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace backend\controllers;
  3. use common\models\SurveyNew;
  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. * SurveyController implements the CRUD actions for SurveyNew model.
  12. */
  13. class SurveyNewController 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 SurveyNew models.
  28. *
  29. * @return mixed
  30. */
  31. public function actionIndex()
  32. {
  33. $dataProvider = new ActiveDataProvider([
  34. 'query' => SurveyNew::find(),
  35. ]);
  36. return $this->render('index', [
  37. 'dataProvider' => $dataProvider,
  38. ]);
  39. }
  40. /**
  41. * Displays a single SurveyNew 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 SurveyNew model.
  55. * If creation is successful, the browser will be redirected to the 'view' SurveyNew.
  56. *
  57. * @return mixed
  58. */
  59. public function actionCreate()
  60. {
  61. $model = new SurveyNew();
  62. $editor = request('editor') ? : config('page_editor_type');
  63. $model->markdown = $editor == 'markdown' ? 1 : 0;
  64. $type = Yii::$app->request->get('type','industry');
  65. $type = empty($type) ? 'industry' : $type;
  66. $model->type = $type;
  67. $typeDesc = '';
  68. switch ($type) {
  69. case 'industry':
  70. $typeDesc = '现代产业';
  71. break;
  72. case 'education':
  73. $typeDesc = '高等教育';
  74. break;
  75. case 'medical':
  76. $typeDesc = '医疗卫生';
  77. break;
  78. case 'circuit':
  79. $typeDesc = '集成电路';
  80. break;
  81. }
  82. $configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => $type])->all();
  83. $cengciModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy', 'name' => 'cengci'])->all();
  84. if (Yii::$app->request->isPost) {
  85. $data = Yii::$app->request->post();
  86. $arr = [];
  87. foreach ($data as $key=>$value) {
  88. if($key == '_csrfBackend' || $key == 'SurveyNew'){
  89. continue;
  90. }
  91. $arr[$key] = $value;
  92. }
  93. $model->load($data);
  94. $model->save();
  95. return $this->redirect(['index']);
  96. }
  97. return $this->render('create', [
  98. 'model' => $model,
  99. 'type'=>$typeDesc,
  100. 'configModels' => $configModels,
  101. 'cengciModels' => $cengciModels,
  102. ]);
  103. }
  104. /**
  105. * Updates an existing SurveyNew model.
  106. * If update is successful, the browser will be redirected to the 'view' SurveyNew.
  107. *
  108. * @param int $id
  109. *
  110. * @return mixed
  111. */
  112. public function actionUpdate($id)
  113. {
  114. $model = $this->findModel($id);
  115. $typeDesc = '';
  116. switch ($model['type']) {
  117. case 'industry':
  118. $typeDesc = '现代产业';
  119. break;
  120. case 'education':
  121. $typeDesc = '高等教育';
  122. break;
  123. case 'medical':
  124. $typeDesc = '医疗卫生';
  125. break;
  126. case 'circuit':
  127. $typeDesc = '集成电路';
  128. break;
  129. }
  130. $configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => $model['type']])->all();
  131. if (Yii::$app->request->isPost) {
  132. $data = Yii::$app->request->post();
  133. $arr = [];
  134. foreach ($data as $key=>$value) {
  135. if($key == '_csrfBackend' || $key == 'SurveyNew'){
  136. continue;
  137. }
  138. $arr[$key] = $value;
  139. }
  140. $model->load($data);
  141. $model->save();
  142. return $this->redirect(['index']);
  143. }
  144. return $this->render('update', [
  145. 'model' => $model,
  146. 'type'=>$typeDesc,
  147. 'configModels' => $configModels,
  148. ]);
  149. }
  150. /**
  151. * Deletes an existing SurveyNew model.
  152. * If deletion is successful, the browser will be redirected to the 'index' SurveyNew.
  153. *
  154. * @param int $id
  155. *
  156. * @return mixed
  157. */
  158. public function actionDelete($id)
  159. {
  160. $this->findModel($id)->delete();
  161. return $this->redirect(['index']);
  162. }
  163. /**
  164. * Finds the SurveyNew model based on its primary key value.
  165. * If the model is not found, a 404 HTTP exception will be thrown.
  166. *
  167. * @param int $id
  168. *
  169. * @return SurveyNew the loaded model
  170. *
  171. * @throws NotFoundHttpException if the model cannot be found
  172. */
  173. protected function findModel($id)
  174. {
  175. if (($model = SurveyNew::findOne($id)) !== null) {
  176. return $model;
  177. } else {
  178. throw new NotFoundHttpException('The requested page does not exist.');
  179. }
  180. }
  181. }