/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/BannerController.php (7100B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
*
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new Banner();
$modelLang = new BannerLang();
$modelCity = new City();
$modelUploadForm = new BannerUploadForm();
$modelVendor = new Vendor();
$post = Yii::$app->getRequest()->post();
if ($model->load($post) && $modelLang->load($post) && $model->validate()) {
$uploadedFile = UploadedFile::getInstance($modelUploadForm, 'banner_image_path');
$model->banner_status = Banner::ACTIVE;
if (!$uploadedFile == null) {
$uploadHelper = UploadHelper::getInstance();
$uploadHelper->setPath($uploadHelper::BANNER);
$uploadHelper->setUploadedFile($uploadedFile);
$path = $uploadHelper->getPath(true);
$uploadedFile->saveAs($path);
$model->save();
$realpath = $uploadHelper->getRealPath($path);
$modelLang->banner_id = $model->getPrimaryKey();
$modelLang->language_code = Yii::$app->language;
$modelLang->banner_image_path = '/'. $realpath ;
$modelLang->save();
Com::successFlash('Created Successfully');
return $this->redirect(['index']);
} else {
$model->save();
$modelLang->banner_id = $model->getPrimaryKey();
$modelLang->language_code = Yii::$app->language;
$modelLang->save();
Com::successFlash('Created Successfully');
return $this->redirect(['index']);
}
}
return $this->render('create', [
'model' => $model,
'modelLang' => $modelLang,
'modelUploadForm' => $modelUploadForm,
'modelVendor' => $modelVendor,
'modelCity' => $modelCity
]);
}
/**
*
* @param type $id
* @return string
*
*
* @Title("Update")
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelLang = BannerLang::find()
->select([
'banner_lang_id',
'banner_name',
'banner_image_path'
])
->where(['banner_id' => $model->getPrimaryKey()])
->one();
$modelCity = new City();
$banner_image = $modelLang->banner_image_path;
$modelVendor = new Vendor();
$modelUploadForm = new BannerUploadForm();
$post = Yii::$app->getRequest()->post();
if ($model->load($post) && $model->validate()) {
$modelLang->load($post);
$uploadedFile = UploadedFile::getInstance($modelUploadForm, 'banner_image_path');
if (!empty($uploadedFile)) {
$uploadHelper = UploadHelper::getInstance();
$uploadHelper->setPath($uploadHelper::BANNER);
$uploadHelper->setUploadedFile($uploadedFile);
$path = $uploadHelper->getPath(true);
$model->save();
$uploadedFile->saveAs($path);
$realPath = $uploadHelper->getRealPath($path);
$modelLang->banner_image_path = '/'. $realPath ;
} else {
$modelLang->banner_image_path = $banner_image;
}
if(!$model->save()) {
return $model;
}
$modelLang->banner_id = $model->getPrimaryKey();
$modelLang->language_code = Yii::$app->language;
$modelLang->save();
Com::successFlash('Updated Successfully');
return $this->redirect(['index']);
}
return $this->render('update', [
'model' => $model,
'modelLang' => $modelLang,
'modelUploadForm' => $modelUploadForm,
'modelVendor' => $modelVendor,
'modelCity' => $modelCity
]);
}
/**
*
* @param type $id
* @return type
*
*
* @Title("Delete")
*/
public function actionDelete($id)
{
$model = $this->findModel($id);
if ($model === null) {
Com::failureFlash('Unable to find the Banner');
return $this->redirect(['index']);
}
BannerLang::deleteAll(['banner_id' => $model->banner_id]);
Banner::deleteAll(['banner_key' => $id]);
$model->save();
Com::successFlash('Deleted Successfully');
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return model
* @throws NotFoundHttpException
*
*/
protected function findModel($id)
{
if (($model = Banner::findOne(['banner_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 = $this->findModel($post['key']);
if ($model === null) {
$result['msg'] = 'Unable to find the City';
goto skip;
}
$model->banner_status = $post['status'];
$model->save(false);
$result['msg'] = Yii::t('backend', 'Deactivated successfully');
if ((int)$model->banner_status === $model::ACTIVE) {
$result['msg'] = Yii::t('backend', 'Activated successfully');
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
}