/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/CountryController.php (5234B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new Country();
$modelLang = new CountryLang();
$post = Yii::$app->getRequest()->post();
$appLanguages = Language::getAppLanguages();
if ($model->load($post) && $model->validate()) {
$model->save();
foreach($appLanguages as $langCode => $langName) {
$modelTranslation = new CountryLang();
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->country_id = $model->getPrimaryKey();
$modelTranslation->save();
}
Com::successFlash('Created Successfully');
return $this->redirect(['index']);
}
return $this->render('create', [
'model' => $model,
'modelLang' => $modelLang,
'appLanguages' => $appLanguages
]);
}
/**
*
* @param type $id
* @return string
*
* @Title("Update")
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelLang = new CountryLang();
$post = Yii::$app->getRequest()->post();
CountryLang::updateTranslationAttribute($modelLang, $model->getPrimaryKey());
$appLanguages = Language::getAppLanguages();
if ($model->load($post) && $model->validate()) {
$model->save();
foreach ($appLanguages as $langCode => $langName) {
$modelTranslation = CountryLang::findOne([
'country_id' => $model->getPrimaryKey(),
'language_code' => $langCode
]);
if ($modelTranslation === null) {
$modelTranslation = new CountryLang();
$modelTranslation->country_id = $model->getPrimaryKey();
}
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->save();
}
Com::successFlash('Updated Successfully');
return $this->redirect(['index']);
}
return $this->render('update', [
'model' => $model,
'modelLang' => $modelLang,
'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 Country');
return $this->redirect(['index']);
}
$model->country_status = Country::DELETE;
$model->save();
Com::successFlash('Deleted Successfully');
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return type
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = Country::findOne(['country_key' => $id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
* @return Json
* @throws \yii\base\InvalidParamException
*
* @Title("Country 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 = Country::findOne(['country_key' => $post['key']]);
if ($model === null) {
$result['msg'] = 'Unable to find the Country';
goto skip;
}
$model->country_status = $post['status'];
$model->save(false);
$result['msg'] = 'Country deactivated successfully';
if ((int)$model->country_status === $model::ACTIVE) {
$result['msg'] = 'Country activated successfully';
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
}