diff --git a/backend/controllers/ExpressTemplateController.php b/backend/controllers/ExpressTemplateController.php index 602385f..a03621e 100644 --- a/backend/controllers/ExpressTemplateController.php +++ b/backend/controllers/ExpressTemplateController.php @@ -2,12 +2,17 @@ namespace backend\controllers; +use common\models\ars\City; +use common\models\ars\Province; use Yii; use common\models\ars\ExpressTemplate; use common\models\searchs\ExpressTemplateSearch; +use yii\caching\Cache; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; +use yii\web\Response; +use yii\widgets\ActiveForm; /** * ExpressTemplateController implements the CRUD actions for ExpressTemplate model. @@ -67,12 +72,39 @@ class ExpressTemplateController extends Controller { $model = new ExpressTemplate(); - if ($model->load(Yii::$app->request->post()) && $model->save()) { + if (Yii::$app->request->isPost) { + $data = Yii::$app->request->post('ExpressTemplate'); + if (Yii::$app->request->isAjax) { + $model->load($data, ''); + Yii::$app->response->format = Response::FORMAT_JSON; + $data = ActiveForm::validate($model); + $data['status'] = 2; + return $data; + } + if (Yii::$app->request->post('area') == null) { + return $this->redirect(Yii::$app->request->referrer . '?status=1'); + } + $cityIds = array_keys(Yii::$app->request->post('area')); + $data['city'] = implode(',', $cityIds); + $model->load($data, ''); + $model->save(); return $this->redirect('index'); } + $data = []; + $provinces = Province::find()->cache(0)->all(); + foreach ($provinces as $k => $v) { + $data[$k]['province'] = $v->name; + $cities = City::find()->cache(0) + ->where(['province_id' => $v->province_id]) + ->all(); + foreach ($cities as $city) { + $data[$k]['city'][] = ['id' => $city->city_id, 'name' => $city->name]; + } + } return $this->render('create', [ 'model' => $model, + 'data' => $data ]); } @@ -86,13 +118,30 @@ class ExpressTemplateController extends Controller public function actionUpdate($id) { $model = $this->findModel($id); - - if ($model->load(Yii::$app->request->post()) && $model->save()) { - return $this->redirect('index'); + $data = Yii::$app->request->post('ExpressTemplate'); + if ($data) { + if (Yii::$app->request->post('area') == null) { + return $this->redirect(Yii::$app->request->referrer . '&status=1'); + } + $cityIds = array_keys(Yii::$app->request->post('area')); + $data['city'] = implode(',', $cityIds); + $model->load($data, ''); + $model->save(); + return $this->render('view', ['model' => ExpressTemplate::findOne($model->id)]); + } + $data = []; + $provinces = Province::find()->cache(0)->all(); + foreach ($provinces as $k => $v) { + $data[$k]['province'] = $v->name; + $cities = City::find()->cache(0) + ->where(['province_id' => $v->province_id]) + ->all(); + foreach ($cities as $city) { + $data[$k]['city'][] = ['id' => $city->city_id, 'name' => $city->name]; + } } - return $this->render('update', [ - 'model' => $model, + 'model' => $model, 'data' => $data, 'cities' => explode(',', $model->city) ]); } diff --git a/backend/views/express-template/create.php b/backend/views/express-template/create.php index f939012..df1198e 100644 --- a/backend/views/express-template/create.php +++ b/backend/views/express-template/create.php @@ -7,7 +7,7 @@ use kartik\tabs\TabsX; /* @var $this yii\web\View */ /* @var $model common\models\ars\ExpressTemplate */ -$this->title = '创建 Express Template'; +$this->title = '创建运费模板'; $this->params['breadcrumbs'][] = ['label' => 'Express Templates', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; Yii::$app->params['bsVersion'] = '4.x'; @@ -28,6 +28,11 @@ Yii::$app->params['bsVersion'] = '4.x'; 'form' => $form, ]), ], + [ + 'label' => ' 选择配送区域', + 'content' => $this->render('area', ['data' => $data, 'form' => $form, 'cities' => [] + ]), + ], ], 'position' => TabsX::POS_ABOVE, 'encodeLabels' => false diff --git a/backend/views/express-template/update.php b/backend/views/express-template/update.php index 20c70fd..3e5d1c8 100644 --- a/backend/views/express-template/update.php +++ b/backend/views/express-template/update.php @@ -1,19 +1,51 @@ title = '编辑 Express Template: ' . $model->name; +$this->title = '编辑运费模板: ' . $model->name; $this->params['breadcrumbs'][] = ['label' => 'Express Templates', 'url' => ['index']]; $this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]]; $this->params['breadcrumbs'][] = 'Update '; +Yii::$app->params['bsVersion'] = '4.x'; ?>