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.

199 lines
6.2 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. $model->calculation = ExpressTemplate::CALCULATION_NUMBER;
  68. if (Yii::$app->request->isPost) {
  69. $data = Yii::$app->request->post('ExpressTemplate');
  70. if (Yii::$app->request->isAjax) {
  71. $model->load($data, '');
  72. Yii::$app->response->format = Response::FORMAT_JSON;
  73. $data = ActiveForm::validate($model);
  74. $data['status'] = 2;
  75. return $data;
  76. }
  77. if (Yii::$app->request->post('area') == null) {
  78. return $this->redirect(Yii::$app->request->referrer . '?status=1');
  79. }
  80. $cityIds = array_keys(Yii::$app->request->post('area'));
  81. $data['city'] = implode(',', $cityIds);
  82. $model->load($data, '');
  83. $model->save();
  84. return $this->redirect('index');
  85. }
  86. $data = [];
  87. $provinces = Province::find()->cache(0)->all();
  88. foreach ($provinces as $k => $v) {
  89. $data[$k]['province'] = $v->name;
  90. $cities = City::find()->cache(0)
  91. ->where(['province_id' => $v->province_id])
  92. ->all();
  93. foreach ($cities as $city) {
  94. $data[$k]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  95. }
  96. }
  97. return $this->render('create', [
  98. 'model' => $model,
  99. 'data' => $data
  100. ]);
  101. }
  102. /**
  103. * Updates an existing ExpressTemplate model.
  104. * If update is successful, the browser will be redirected to the 'view' page.
  105. * @param integer $id
  106. * @return mixed
  107. * @throws NotFoundHttpException if the model cannot be found
  108. */
  109. public function actionUpdate($id)
  110. {
  111. $model = $this->findModel($id);
  112. $data = Yii::$app->request->post('ExpressTemplate');
  113. if ($data) {
  114. if (Yii::$app->request->post('area') == null) {
  115. return $this->redirect(Yii::$app->request->referrer . '&status=1');
  116. }
  117. $cityIds = array_keys(Yii::$app->request->post('area'));
  118. $data['city'] = implode(',', $cityIds);
  119. $model->load($data, '');
  120. $model->save();
  121. return $this->render('view', ['model' => ExpressTemplate::findOne($model->id)]);
  122. }
  123. $data = [];
  124. $provinces = Province::find()->cache(0)->all();
  125. foreach ($provinces as $k => $v) {
  126. $data[$k]['province'] = $v->name;
  127. $cities = City::find()->cache(0)
  128. ->where(['province_id' => $v->province_id])
  129. ->all();
  130. foreach ($cities as $city) {
  131. $data[$k]['city'][] = ['id' => $city->city_id, 'name' => $city->name];
  132. }
  133. }
  134. return $this->render('update', [
  135. 'model' => $model, 'data' => $data, 'cities' => explode(',', $model->city)
  136. ]);
  137. }
  138. /**
  139. * Deletes an existing ExpressTemplate model.
  140. * If deletion is successful, the browser will be redirected to the 'index' page.
  141. * @param integer $id
  142. * @return mixed
  143. * @throws NotFoundHttpException if the model cannot be found
  144. */
  145. public function actionDelete($id)
  146. {
  147. $this->findModel($id)->delete();
  148. return $this->redirect(['index']);
  149. }
  150. /**
  151. * Finds the ExpressTemplate model based on its primary key value.
  152. * If the model is not found, a 404 HTTP exception will be thrown.
  153. * @param integer $id
  154. * @return ExpressTemplate the loaded model
  155. * @throws NotFoundHttpException if the model cannot be found
  156. */
  157. protected function findModel($id)
  158. {
  159. if (($model = ExpressTemplate::findOne($id)) !== null) {
  160. return $model;
  161. }
  162. throw new NotFoundHttpException('The requested page does not exist.');
  163. }
  164. /**
  165. * @author iron
  166. * 文件导出
  167. */
  168. public function actionExport()
  169. {
  170. $searchModel = new ExpressTemplateSearch();
  171. $params = Yii::$app->request->queryParams;
  172. if ($params['page-type'] == 'all') {
  173. $dataProvider = $searchModel->allData($params);
  174. } else {
  175. $dataProvider = $searchModel->search($params);
  176. }
  177. \iron\widget\Excel::export([
  178. 'models' => $dataProvider->getModels(),
  179. 'format' => 'Xlsx',
  180. 'asAttachment' => true,
  181. 'fileName' =>'Express Templates'. "-" .date('Y-m-d H/i/s', time()),
  182. 'columns' => $searchModel->columns()
  183. ]);
  184. }
  185. }