diff --git a/controllers/CityController.php b/controllers/CityController.php new file mode 100644 index 0000000..fff2e2a --- /dev/null +++ b/controllers/CityController.php @@ -0,0 +1,164 @@ + [ + 'class' => AccessControl::className(), + 'rules' => [ + [ + 'actions' => [ + 'login', + 'error', + ], + 'allow' => true, + ], + [ + 'allow' => true, + 'roles' => [ '@' ], + ], + ], + ], + 'verbs' => [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all City models. + * @return mixed + */ + public function actionIndex() + { + $dataProvider = new ActiveDataProvider([ + 'query' => City::find()->with(['lang']), + ]); + + return $this->render('index', [ + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single City model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new City model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new City(); + $model->generateLangs(); + if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) { + return $this->redirect( + [ + 'view', + 'id' => $model->id, + ] + ); + } + return $this->render( + 'create', + [ + 'model' => $model, + 'modelLangs' => $model->modelLangs, + ] + ); + } + + /** + * Updates an existing City model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + $model->generateLangs(); + + if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) { + return $this->redirect( + [ + 'view', + 'id' => $model->id, + ] + ); + } + return $this->render( + 'update', + [ + 'model' => $model, + 'modelLangs' => $model->modelLangs, + ] + ); + } + + /** + * Deletes an existing City model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the City model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return City the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = City::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/controllers/ShopController.php b/controllers/ShopController.php new file mode 100644 index 0000000..f037afe --- /dev/null +++ b/controllers/ShopController.php @@ -0,0 +1,166 @@ + [ + 'class' => AccessControl::className(), + 'rules' => [ + [ + 'actions' => [ + 'login', + 'error', + ], + 'allow' => true, + ], + [ + 'allow' => true, + 'roles' => [ '@' ], + ], + ], + ], + 'verbs' => [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all Shop models. + * @return mixed + */ + public function actionIndex() + { + $dataProvider = new ActiveDataProvider([ + 'query' => Shop::find(), + ]); + + return $this->render('index', [ + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Shop model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Shop model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Shop(); + $model->generateLangs(); + $model->getCities(); + if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) { + return $this->redirect( + [ + 'view', + 'id' => $model->id, + ] + ); + } + return $this->render( + 'create', + [ + 'model' => $model, + 'modelLangs' => $model->modelLangs, + ] + ); + + } + + /** + * Updates an existing Shop model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + $model->generateLangs(); + $model->getCities(); + $model->loadWithLangs(\Yii::$app->request); + //print_r(\Yii::$app->request); + if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) { + return $this->redirect( + [ + 'view', + 'id' => $model->id, + ] + ); + } + return $this->render( + 'update', + [ + 'model' => $model, + 'modelLangs' => $model->modelLangs, + ] + ); + } + + /** + * Deletes an existing Shop model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Shop model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Shop the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Shop::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/models/City.php b/models/City.php new file mode 100644 index 0000000..200d2d8 --- /dev/null +++ b/models/City.php @@ -0,0 +1,89 @@ + [ + 'class' => LanguageBehavior::className(), + ], + ]; + } + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [['sort'], 'integer'], + [['status'], 'boolean'], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'sort' => Yii::t('stock', 'Sort'), + 'status' => Yii::t('stock', 'Status'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getCityLangs() + { + return $this->hasMany(CityLang::className(), ['city_id' => 'id']) + ->inverseOf('city'); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguages() + { + return $this->hasMany(Language::className(), ['id' => 'language_id'])->viaTable('city_lang', ['city_id' => 'id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getShops() + { + return $this->hasMany(Shop::className(), ['city_id' => 'id']); + } +} diff --git a/models/CityLang.php b/models/CityLang.php new file mode 100644 index 0000000..20b63cc --- /dev/null +++ b/models/CityLang.php @@ -0,0 +1,113 @@ + [ + 'class' => SlugBehavior::className(), + 'action' => 'city/view', + 'params' => [ + 'id' => 'city_id', + ], + 'fields' => [ + 'title' => \Yii::t('stock', 'Title'), + 'description' => \Yii::t('stock', 'Description'), + ], + ], + ]; + } + public function rules() + { + return [ + [ + [ + 'title', + ], + 'required', + ], + [ + [ 'description' ], + 'string', + ], + [ + [ + 'title', + 'aliasValue', + ], + 'string', + 'max' => 255, + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'city_id' => 'City ID', + 'language_id' => 'Language ID', + 'title' => \Yii::t('stock', 'Title'), + 'alias_id' => 'Alias ID', + 'description' => \Yii::t('stock', 'Description'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getAlias() + { + return $this->hasOne(Alias::className(), ['id' => 'alias_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getCity() + { + return $this->hasOne(City::className(), ['id' => 'city_id'])->inverseOf('cityLangs'); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguage() + { + return $this->hasOne(Language::className(), ['id' => 'language_id']); + } +} diff --git a/models/Shop.php b/models/Shop.php new file mode 100644 index 0000000..4206a16 --- /dev/null +++ b/models/Shop.php @@ -0,0 +1,118 @@ + [ + 'class' => LanguageBehavior::className(), + ], + ]; + } + /** + * @inheritdoc + */ + public function rules() + { + return [ + [['city_id', 'sort'], 'integer'], + [['status'], 'boolean'], + [['mode', 'modeStr'], 'safe'], + [['coords', 'coordsArr'], 'safe'], + [['city_id'], 'exist', 'skipOnError' => true, 'targetClass' => City::className(), 'targetAttribute' => ['city_id' => 'id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'mode' => \Yii::t('stock', 'Mode'), + 'city_id' => 'City ID', + 'sort' => \Yii::t('stock', 'Sort'), + 'status' => \Yii::t('stock', 'Status'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getCity() + { + return $this->hasOne(City::className(), ['id' => 'city_id']); + } + + public function getCities(){ + $cities = City::find()->with(['lang'])->all(); + foreach($cities as $city){ + $this->cities[$city->id] = $city->lang->title; + } + + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getShopLangs() + { + return $this->hasMany(ShopLang::className(), ['shop_id' => 'id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguages() + { + return $this->hasMany(Language::className(), ['id' => 'language_id'])->viaTable('shop_lang', ['shop_id' => 'id']); + } + + public function setModeStr($value){ + $this->mode = Json::encode($value); + } + + public function getModeStr(){ + return Json::decode($this->mode); + } + + public function setCoordsArr($value){ + $this->coords = Json::encode($value); + } + + public function getCoordsArr(){ + return Json::decode($this->coords); + } + + +} diff --git a/models/ShopLang.php b/models/ShopLang.php new file mode 100644 index 0000000..d038aff --- /dev/null +++ b/models/ShopLang.php @@ -0,0 +1,105 @@ + [ + 'class' => SlugBehavior::className(), + 'action' => 'shop/view', + 'params' => [ + 'id' => 'shop_id', + ], + 'fields' => [ + 'title' => \Yii::t('stock', 'Title'), + 'description' => \Yii::t('stock', 'Description'), + 'address' => \Yii::t('stock', 'Address'), + ], + ], + ]; + } + public function rules() + { + return [ + [['shop_id', 'language_id', 'title'], 'required'], + [['shop_id', 'language_id', 'alias_id'], 'integer'], + [['description'], 'string'], + [['title'], 'string', 'max' => 255], + [['address'], 'string', 'max' => 255], + [['alias_id'], 'unique'], + [['alias_id'], 'exist', 'skipOnError' => true, 'targetClass' => Alias::className(), 'targetAttribute' => ['alias_id' => 'id']], + [['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => Language::className(), 'targetAttribute' => ['language_id' => 'id']], + [['shop_id'], 'exist', 'skipOnError' => true, 'targetClass' => Shop::className(), 'targetAttribute' => ['shop_id' => 'id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'shop_id' => 'Shop ID', + 'language_id' => 'Language ID', + 'title' => \Yii::t('stock', 'Title'), + 'alias_id' => 'Alias ID', + 'description' => \Yii::t('stock', 'Description'), + 'address' => \Yii::t('stock', 'Address'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getAlias() + { + return $this->hasOne(Alias::className(), ['id' => 'alias_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguage() + { + return $this->hasOne(Language::className(), ['id' => 'language_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getShop() + { + return $this->hasOne(Shop::className(), ['id' => 'shop_id']); + } +} diff --git a/views/city/_form.php b/views/city/_form.php new file mode 100644 index 0000000..5221fb1 --- /dev/null +++ b/views/city/_form.php @@ -0,0 +1,33 @@ + + +
+ = Html::a(\Yii::t('stock', 'Create City'), ['create'], ['class' => 'btn btn-success']) ?> +
+ = GridView::widget([ + 'dataProvider' => $dataProvider, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'id', + [ + 'attribute' => \Yii::t('stock', 'Title'), + 'value' => 'lang.title', + ], + 'sort', + 'status:boolean', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> + ++ = Html::a(\Yii::t('stock', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> + = Html::a(\Yii::t('stock', 'Delete'), ['delete', 'id' => $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +
+ + = DetailView::widget([ + 'model' => $model, + 'attributes' => [ + 'id', + 'lang.title', + [ + 'attribute' => 'lang.alias.value', + 'label' => \Yii::t('catalog', 'Alias'), + ], + 'lang.description:html', + 'sort', + 'status:boolean', + ], + ]) ?> + ++ = Html::a(\Yii::t('stock', 'Create Shop'), ['create'], ['class' => 'btn btn-success']) ?> +
+ = GridView::widget([ + 'dataProvider' => $dataProvider, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'id', + [ + 'attribute' => \Yii::t('stock', 'Address'), + 'value' => 'lang.address', + ], + 'sort', + 'status:boolean', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> + ++ = Html::a(\Yii::t('stock', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> + = Html::a(\Yii::t('stock', 'Delete'), ['delete', 'id' => $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +
+ + = DetailView::widget([ + 'model' => $model, + 'attributes' => [ + 'id', + 'lang.title', + [ + 'attribute' => 'lang.alias.value', + 'label' => \Yii::t('catalog', 'Alias'), + ], + 'lang.description:html', + 'lang.address', + 'sort', + 'status:boolean', + ], + ]) ?> + +