SurveyNewController.php 4.7 KB

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