/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/CmsController.php (6026B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
*
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new Cms();
$modelLang = new CmsLang();
$appLanguages = Language::getAppLanguages();
if ( Yii::$app->request->post() ) {
$post = Yii::$app->getRequest()->post();
if ($model->load($post) && $model->validate() && $modelLang->load($post)) {
$model->save();
foreach($appLanguages as $langCode => $langName) {
$modelLang = new CmsLang();
$modelLang->loadTranslationAttribute($post, $langCode);
$modelLang->cms_id = $model->getPrimaryKey();
$modelLang->save();
}
Com::successFlash(Yii::t('backend','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 = CmsLang::find()->where(['cms_id' => $model->cms_id])->one();
CmsLang::updateTranslationAttribute($modelLang, $model->getPrimaryKey());
$post = Yii::$app->getRequest()->post();
$cms_slug = $model->slug;
$appLanguages = Language::getAppLanguages();
if ($model->load($post) && $model->validate() && $modelLang->load($post)) {
$model->slug = $cms_slug;
$model->save();
foreach ($appLanguages as $langCode => $langName) {
$modelTranslation = CmsLang::findOne([
'cms_id' => $model->getPrimaryKey(),
'language_code' => $langCode
]);
if ($modelTranslation === null) {
$modelTranslation = new CmsLang();
$modelTranslation->cms_id = $model->getPrimaryKey();
}
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->save();
$modelLang->save();
}
Com::successFlash(Yii::t('backend','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 Cms');
return $this->redirect(['index']);
}
$model->cms_status = Cms::DELETE;
$model->save(false);
Com::successFlash(Yii::t('backend','Deleted Successfully'));
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return type
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = Cms::findOne(['cms_key' => $id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
*
* @return Json
* @throws \yii\base\InvalidParamException
*
* @Title("Cms 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 = Cms::findOne(['cms_key' => $post['key']]);
if ($model === null) {
$result['msg'] = 'Unable to find the Cms';
goto skip;
}
if($model->is_default == Cms::DEFAULT_PAGE) {
$result['msg'] = 'Not allowed';
goto skip;
}
$model->cms_status = $post['status'];
$model->save(false);
$result['msg'] = Yii::t('backend','Cms deactivated successfully');
if ((int)$model->cms_status === $model::ACTIVE) {
$result['msg'] = Yii::t('backend','Cms activated successfully');
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
public function actionView($id)
{
$model = Cms::find()
->alias('C')
->select(['C.*', 'CL.*' ])
->leftJoin(['CL' => CmsLang::tableName()], 'CL.cms_id = C.cms_id')
->where(['C.cms_key' => $id])
->one();
return $this->render('view', [
'model' => $model,
]);
}
}