/app/modules/gii/generators/crud/default/controller.php

https://github.com/xiongchuan86/openadm-yii2 · PHP · 205 lines · 138 code · 22 blank · 45 comment · 18 complexity · 988a0f282747ac1c487224dd094538a5 MD5 · raw file

  1. <?php
  2. /**
  3. * This is the template for generating a CRUD controller class file.
  4. */
  5. use yii\db\ActiveRecordInterface;
  6. use yii\helpers\StringHelper;
  7. /* @var $this yii\web\View */
  8. /* @var $generator yii\gii\generators\crud\Generator */
  9. $controllerClass = StringHelper::basename($generator->controllerClass);
  10. $modelClass = StringHelper::basename($generator->modelClass);
  11. $searchModelClass = StringHelper::basename($generator->searchModelClass);
  12. if ($modelClass === $searchModelClass) {
  13. $searchModelAlias = $searchModelClass . 'Search';
  14. }
  15. /* @var $class ActiveRecordInterface */
  16. $class = $generator->modelClass;
  17. $pks = $class::primaryKey();
  18. $urlParams = $generator->generateUrlParams();
  19. $actionParams = $generator->generateActionParams();
  20. $actionParamComments = $generator->generateActionParamComments();
  21. echo "<?php\n";
  22. ?>
  23. namespace <?= StringHelper::dirname(ltrim($generator->controllerClass, '\\')) ?>;
  24. use Yii;
  25. use <?= ltrim($generator->modelClass, '\\') ?>;
  26. <?php if (!empty($generator->searchModelClass)): ?>
  27. use <?= ltrim($generator->searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : "") ?>;
  28. <?php else: ?>
  29. use yii\data\ActiveDataProvider;
  30. <?php endif; ?>
  31. use <?= ltrim($generator->baseControllerClass, '\\') ?>;
  32. use yii\web\NotFoundHttpException;
  33. use yii\filters\VerbFilter;
  34. /**
  35. * <?= $controllerClass ?> implements the CRUD actions for <?= $modelClass ?> model.
  36. */
  37. class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->baseControllerClass) . "\n" ?>
  38. {
  39. /**
  40. * @inheritdoc
  41. */
  42. public function behaviors()
  43. {
  44. return [
  45. 'verbs' => [
  46. 'class' => VerbFilter::className(),
  47. 'actions' => [
  48. 'delete' => ['POST'],
  49. ],
  50. ],
  51. ];
  52. }
  53. /**
  54. * Lists all <?= $modelClass ?> models.
  55. * @return mixed
  56. */
  57. public function actionIndex()
  58. {
  59. <?php if (!empty($generator->searchModelClass)): ?>
  60. $searchModel = new <?= isset($searchModelAlias) ? $searchModelAlias : $searchModelClass ?>();
  61. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  62. return $this->render('index', [
  63. 'searchModel' => $searchModel,
  64. 'dataProvider' => $dataProvider,
  65. ]);
  66. <?php else: ?>
  67. $dataProvider = new ActiveDataProvider([
  68. 'query' => <?= $modelClass ?>::find(),
  69. ]);
  70. return $this->render('index', [
  71. 'dataProvider' => $dataProvider,
  72. ]);
  73. <?php endif; ?>
  74. }
  75. /**
  76. * Displays a single <?= $modelClass ?> model.
  77. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  78. * @return mixed
  79. */
  80. public function actionView(<?= $actionParams ?>)
  81. {
  82. return $this->render('view', [
  83. 'model' => $this->findModel(<?= $actionParams ?>),
  84. ]);
  85. }
  86. /**
  87. * Creates a new <?= $modelClass ?> model.
  88. * If creation is successful, the browser will be redirected to the 'view' page.
  89. * @return mixed
  90. */
  91. public function actionCreate()
  92. {
  93. $model = new <?= $modelClass ?>();
  94. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  95. return $this->redirect(['view', <?= $urlParams ?>]);
  96. } else {
  97. $errors = $model->getErrors();
  98. if(!empty($errors)){
  99. foreach ($errors as $field=>$val){
  100. Yii::$app->session->setFlash('warning',"Field:{$field},{$val[0]}");
  101. }
  102. }
  103. return $this->render('create', [
  104. 'model' => $model,
  105. ]);
  106. }
  107. }
  108. /**
  109. * Updates an existing <?= $modelClass ?> model.
  110. * If update is successful, the browser will be redirected to the 'view' page.
  111. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  112. * @return mixed
  113. */
  114. public function actionUpdate(<?= $actionParams ?>)
  115. {
  116. $model = $this->findModel(<?= $actionParams ?>);
  117. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  118. return $this->redirect(['view', <?= $urlParams ?>]);
  119. } else {
  120. return $this->render('update', [
  121. 'model' => $model,
  122. ]);
  123. }
  124. }
  125. /**
  126. * Deletes an existing <?= $modelClass ?> model.
  127. * If deletion is successful, the browser will be redirected to the 'index' page.
  128. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  129. * @return mixed
  130. */
  131. public function actionDelete(<?= $actionParams ?>)
  132. {
  133. $this->findModel(<?= $actionParams ?>)->delete();
  134. return $this->redirect(['index']);
  135. }
  136. /**
  137. * Finds the <?= $modelClass ?> model based on its primary key value.
  138. * If the model is not found, a 404 HTTP exception will be thrown.
  139. * <?= implode("\n * ", $actionParamComments) . "\n" ?>
  140. * @return <?= $modelClass ?> the loaded model
  141. * @throws NotFoundHttpException if the model cannot be found
  142. */
  143. protected function findModel(<?= $actionParams ?>)
  144. {
  145. <?php
  146. if (count($pks) === 1) {
  147. $condition = '$id';
  148. } else {
  149. $condition = [];
  150. foreach ($pks as $pk) {
  151. $condition[] = "'$pk' => \$$pk";
  152. }
  153. $condition = '[' . implode(', ', $condition) . ']';
  154. }
  155. ?>
  156. if (($model = <?= $modelClass ?>::findOne(<?= $condition ?>)) !== null) {
  157. return $model;
  158. } else {
  159. throw new NotFoundHttpException('The requested page does not exist.');
  160. }
  161. }
  162. public function actionDeletes()
  163. {
  164. $result= ['code'=>200];
  165. $data = [];
  166. $post = Yii::$app->request->post();
  167. if($post && isset($post['ids']) && is_array($post['ids'])){
  168. foreach ($post['ids'] as $id){
  169. $model = $this->findModel($id);
  170. $model->delete();
  171. }
  172. $result['data'] = $data;
  173. $result['msg'] = '删除完成!';
  174. }else{
  175. $result=['code'=>0,'msg'=>'请选择要删除的数据!'];
  176. }
  177. if (Yii::$app->request->isAjax) {
  178. Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  179. return $result;
  180. }
  181. return $this->redirect(['index']);
  182. }
  183. }