/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/CityController.php (5518B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
*
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new City();
$modelLang = new CityLang();
$country = Country::getcountry();
$post = Yii::$app->getRequest()->post();
$appLanguages = Language::getAppLanguages();
if ($model->load($post) && $model->validate()) {
$model->save();
foreach($appLanguages as $langCode => $langName) {
$modelTranslation = new CityLang();
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->city_id = $model->getPrimaryKey();
$modelTranslation->save();
}
Com::successFlash(Yii::t('backend','Created Successfully'));
return $this->redirect(['index']);
}
return $this->render('create', [
'model' => $model,
'modelLang' => $modelLang,
'country' => $country,
'appLanguages' => $appLanguages
]);
}
/**
*
* @param type $id
* @return string
*
* @Title("Update")
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelLang = new CityLang();
$country = Country::getcountry();
$post = Yii::$app->getRequest()->post();
CityLang::updateTranslationAttribute($modelLang, $model->getPrimaryKey());
$appLanguages = Language::getAppLanguages();
if ($model->load($post) && $model->validate()) {
$model->save();
foreach ($appLanguages as $langCode => $langName) {
$modelTranslation = CityLang::findOne([
'city_id' => $model->getPrimaryKey(),
'language_code' => $langCode
]);
if ($modelTranslation === null) {
$modelTranslation = new CityLang();
$modelTranslation->city_id = $model->getPrimaryKey();
}
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->save();
}
Com::successFlash(Yii::t('backend','Updated Successfully'));
return $this->redirect(['index']);
}
return $this->render('update', [
'model' => $model,
'modelLang' => $modelLang,
'country' => $country,
'appLanguages' => $appLanguages
]);
}
/**
*
* @param type $id
* @return string
*
* @Title("Delete")
*/
public function actionDelete($id)
{
$model = $this->findModel($id);
if ($model === null) {
Com::failureFlash('Unable to find the Ingredient');
return $this->redirect(['index']);
}
$model->city_status = City::DELETE;
$model->save();
Com::successFlash(Yii::t('backend','Deleted Successfully'));
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return type
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = City::findOne(['city_key' => $id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
*
* @return Json
* @throws \yii\base\InvalidParamException
*
*
* @Title("City Status")
*/
public function actionChangeStatus()
{
$params = ['key', 'status'];
$post = Yii::$app->getRequest()->post();
$result = ['status' => STATUS_FAIL, 'msg' => ''];
foreach ($params as $param) {
if (!array_key_exists($param, $post)) {
$result['msg'] = 'Required param missing';
goto skip;
}
}
$model = City::findOne(['city_key' => $post['key']]);
if ($model === null) {
$result['msg'] = 'Unable to find the City';
goto skip;
}
$model->city_status = $post['status'];
$model->save(false);
$result['msg'] = Yii::t('backend', 'City deactivated successfully');
if ((int)$model->city_status === $model::ACTIVE) {
$result['msg'] = Yii::t('backend', 'City activated successfully');
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
}