You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.1 KiB

  1. <?php
  2. namespace backend\controllers;
  3. use common\models\ars\City;
  4. use common\models\ars\Province;
  5. use Yii;
  6. use common\models\ars\ExpressTemplate;
  7. use common\models\searchs\ExpressTemplateSearch;
  8. use yii\caching\Cache;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. use yii\filters\VerbFilter;
  12. use yii\web\Response;
  13. use yii\widgets\ActiveForm;
  14. /**
  15. * ExpressTemplateController implements the CRUD actions for ExpressTemplate model.
  16. */
  17. class ExpressTemplateController extends Controller
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function behaviors()
  23. {
  24. return [
  25. 'verbs' => [
  26. 'class' => VerbFilter::className(),
  27. 'actions' => [
  28. 'delete' => ['POST'],
  29. ],
  30. ],
  31. ];
  32. }
  33. /**
  34. * Lists all ExpressTemplate models.
  35. * @return mixed
  36. */
  37. public function actionIndex()
  38. {
  39. $searchModel = new ExpressTemplateSearch();
  40. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  41. return $this->render('index', [
  42. 'searchModel' => $searchModel,
  43. 'dataProvider' => $dataProvider,
  44. 'columns' => $searchModel->columns()
  45. ]);
  46. }
  47. /**
  48. * Displays a single ExpressTemplate model.
  49. * @param integer $id
  50. * @return mixed
  51. * @throws NotFoundHttpException if the model cannot be found
  52. */
  53. public function actionView($id)
  54. {
  55. return $this->render('view', [
  56. 'model' => $this->findModel($id),
  57. ]);
  58. }
  59. /**
  60. * Creates a new ExpressTemplate model.
  61. * If creation is successful, the browser will be redirected to the 'view' page.
  62. * @return mixed
  63. */
  64. public function actionCreate()
  65. {
  66. $model = new ExpressTemplate();
  67. if (Yii::$app->request->isPost) {
  68. $data = Yii::$app->request->post('ExpressTemplate');
  69. if (Yii::$app->request->isAjax) {
  70. $model->load($data, '');
  71. Yii::$app->response->format = Response::FORMAT_JSON;
  72. $data = ActiveForm::validate($model);
  73. $data['status'] = 2;
  74. return $data;
  75. }
  76. if (Yii::$app->request->post('area') == null) {
  77. return $this->redirect(Yii::$app->request->referrer . '?status=1');
  78. }
  79. $cityIds = array_keys(Yii::$app->request->post('area'));
  80. $data['city'] = implode(',', $cityIds);
  81. $model->load($data, '');
  82. $model->save();
  83. return $this->redirect('index');
  84. }
  85. $data = [];
  86. $provinces = Province::find()->cache(0)->all();
  87. foreach ($provinces as $k => $v) {
  88. $data[$k]['province'] = $v->name;
  89. $cities = City::find()->cache(0)
  90. ->where(['province_id' => $v->province_id])
  91. ->all();
  92. foreach ($cities as $city) {
  93. $data[$k]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  94. }
  95. }
  96. return $this->render('create', [
  97. 'model' => $model,
  98. 'data' => $data
  99. ]);
  100. }
  101. /**
  102. * Updates an existing ExpressTemplate model.
  103. * If update is successful, the browser will be redirected to the 'view' page.
  104. * @param integer $id
  105. * @return mixed
  106. * @throws NotFoundHttpException if the model cannot be found
  107. */
  108. public function actionUpdate($id)
  109. {
  110. $model = $this->findModel($id);
  111. $data = Yii::$app->request->post('ExpressTemplate');
  112. if ($data) {
  113. if (Yii::$app->request->post('area') == null) {
  114. return $this->redirect(Yii::$app->request->referrer . '&status=1');
  115. }
  116. $cityIds = array_keys(Yii::$app->request->post('area'));
  117. $data['city'] = implode(',', $cityIds);
  118. $model->load($data, '');
  119. $model->save();
  120. return $this->render('view', ['model' => ExpressTemplate::findOne($model->id)]);
  121. }
  122. $data = [];
  123. $provinces = Province::find()->cache(0)->all();
  124. foreach ($provinces as $k => $v) {
  125. $data[$k]['province'] = $v->name;
  126. $cities = City::find()->cache(0)
  127. ->where(['province_id' => $v->province_id])
  128. ->all();
  129. foreach ($cities as $city) {
  130. $data[$k]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  131. }
  132. }
  133. return $this->render('update', [
  134. 'model' => $model, 'data' => $data, 'cities' => explode(',', $model->city)
  135. ]);
  136. }
  137. /**
  138. * Deletes an existing ExpressTemplate model.
  139. * If deletion is successful, the browser will be redirected to the 'index' page.
  140. * @param integer $id
  141. * @return mixed
  142. * @throws NotFoundHttpException if the model cannot be found
  143. */
  144. public function actionDelete($id)
  145. {
  146. $this->findModel($id)->delete();
  147. return $this->redirect(['index']);
  148. }
  149. /**
  150. * Finds the ExpressTemplate model based on its primary key value.
  151. * If the model is not found, a 404 HTTP exception will be thrown.
  152. * @param integer $id
  153. * @return ExpressTemplate the loaded model
  154. * @throws NotFoundHttpException if the model cannot be found
  155. */
  156. protected function findModel($id)
  157. {
  158. if (($model = ExpressTemplate::findOne($id)) !== null) {
  159. return $model;
  160. }
  161. throw new NotFoundHttpException('The requested page does not exist.');
  162. }
  163. /**
  164. * @author iron
  165. * 文件导出
  166. */
  167. public function actionExport()
  168. {
  169. $searchModel = new ExpressTemplateSearch();
  170. $params = Yii::$app->request->queryParams;
  171. if ($params['page-type'] == 'all') {
  172. $dataProvider = $searchModel->allData($params);
  173. } else {
  174. $dataProvider = $searchModel->search($params);
  175. }
  176. \iron\widget\Excel::export([
  177. 'models' => $dataProvider->getModels(),
  178. 'format' => 'Xlsx',
  179. 'asAttachment' => true,
  180. 'fileName' =>'Express Templates'. "-" .date('Y-m-d H/i/s', time()),
  181. 'columns' => $searchModel->columns()
  182. ]);
  183. }
  184. }