SurveyNewController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. case 'special':
  82. $typeDesc = '专项政策';
  83. break;
  84. }
  85. $configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => $type])->all();
  86. $cengciModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy', 'name' => 'cengci'])->all();
  87. if (Yii::$app->request->isPost) {
  88. $data = Yii::$app->request->post();
  89. $arr = [];
  90. foreach ($data as $key=>$value) {
  91. if($key == '_csrfBackend' || $key == 'SurveyNew'){
  92. continue;
  93. }
  94. $arr[$key] = $value;
  95. }
  96. $model->load($data);
  97. $model->save();
  98. return $this->redirect(['index']);
  99. }
  100. return $this->render('create', [
  101. 'model' => $model,
  102. 'type'=>$typeDesc,
  103. 'configModels' => $configModels,
  104. 'cengciModels' => $cengciModels,
  105. ]);
  106. }
  107. /**
  108. * Updates an existing SurveyNew model.
  109. * If update is successful, the browser will be redirected to the 'view' SurveyNew.
  110. *
  111. * @param int $id
  112. *
  113. * @return mixed
  114. */
  115. public function actionUpdate($id)
  116. {
  117. $model = $this->findModel($id);
  118. $typeDesc = '';
  119. switch ($model['type']) {
  120. case 'industry':
  121. $typeDesc = '现代产业';
  122. break;
  123. case 'education':
  124. $typeDesc = '高等教育';
  125. break;
  126. case 'medical':
  127. $typeDesc = '医疗卫生';
  128. break;
  129. case 'circuit':
  130. $typeDesc = '集成电路';
  131. break;
  132. case 'special':
  133. $typeDesc = '专项政策';
  134. break;
  135. }
  136. $configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => $model['type']])->all();
  137. if (Yii::$app->request->isPost) {
  138. $data = Yii::$app->request->post();
  139. $arr = [];
  140. foreach ($data as $key=>$value) {
  141. if($key == '_csrfBackend' || $key == 'SurveyNew'){
  142. continue;
  143. }
  144. $arr[$key] = $value;
  145. }
  146. $model->load($data);
  147. $model->save();
  148. return $this->redirect(['index']);
  149. }
  150. return $this->render('update', [
  151. 'model' => $model,
  152. 'type'=>$typeDesc,
  153. 'configModels' => $configModels,
  154. ]);
  155. }
  156. /**
  157. * Deletes an existing SurveyNew model.
  158. * If deletion is successful, the browser will be redirected to the 'index' SurveyNew.
  159. *
  160. * @param int $id
  161. *
  162. * @return mixed
  163. */
  164. public function actionDelete($id)
  165. {
  166. $this->findModel($id)->delete();
  167. return $this->redirect(['index']);
  168. }
  169. /**
  170. * Finds the SurveyNew model based on its primary key value.
  171. * If the model is not found, a 404 HTTP exception will be thrown.
  172. *
  173. * @param int $id
  174. *
  175. * @return SurveyNew the loaded model
  176. *
  177. * @throws NotFoundHttpException if the model cannot be found
  178. */
  179. protected function findModel($id)
  180. {
  181. if (($model = SurveyNew::findOne($id)) !== null) {
  182. return $model;
  183. } else {
  184. throw new NotFoundHttpException('The requested page does not exist.');
  185. }
  186. }
  187. }