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.

201 lines
6.3 KiB

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