/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/CashBackPromotionsController.php (12051B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
*
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new UserPromotion();
$modelLang = new UserPromotionLang();
$modelUploadForm = new PromotionUploadForm();
$modelUser = new UserPromotionUser();
$modelBranch = new UserPromotionBranch();
$modelUploadForm->setScenario($modelUploadForm::SCENARIO_CREATE);
$appLanguages = Language::getAppLanguages();
return $this->render('create', [
'model' => $model,
'modelLang' => $modelLang,
'appLanguages' => $appLanguages,
'modelUploadForm' => $modelUploadForm,
'modelBranch' => $modelBranch,
'modelUser' => $modelUser,
]);
}
/**
* Save City
*
* @return Array
*
*/
public function actionSave($id = NULL)
{
$response = ['status' => STATUS_FAIL, 'msg' => '', 'error' => []];
$transaction = UserPromotion::getDb()->beginTransaction();
$model = new UserPromotion();
$modelLang = new UserPromotionLang();
$modelUploadForm = new PromotionUploadForm();
$modelBranch = new UserPromotionBranch();
$modelUser = new UserPromotionUser();
if ($id !== null) {
$model = $this->findModel($id);
$modelLang->user_promotion_id = $model->getPrimaryKey();
}
$appLanguages = Language::getAppLanguages();
$request = Yii::$app->getRequest()->post();
$model->load($request);
$modelLang->load($request);
$modelUser->load($request);
$modelBranch->load($request);
$modelLang->user_promotion_type = $model->user_promotion_type;
if ($modelLang->validate()) {
$model->payment_type = implode(',', $model->payment_type);
if (!$model->save()) {
$response['error'] = $this->prepareErrors($model);
goto skip;
}
$modelPromUserDel = [];
if($modelUser->user_id != null) {
foreach ($modelUser->user_id as $key => $value) {
$modelPromUser = UserPromotionUser::find()
->where([
'user_promotion_id' => $model->getPrimaryKey(),
'user_id' => $value
])->one();
if($modelPromUser == null) {
$modelPromUser = new UserPromotionUser();
}
$modelPromUser->user_id = $value;
$modelPromUser->user_promotion_id = $model->getPrimaryKey();
$modelPromUser->save(false);
$modelPromUserDel[] = $modelPromUser->getPrimaryKey();
}
}
UserPromotionUser::deleteAll(
[
'AND',
[
'not in', 'user_promotion_user_id', $modelPromUserDel
],
[
'user_promotion_id' => $model->getPrimaryKey()
]
]);
$modelPromBranchDel = [];
if($modelBranch->branch_id != null) {
foreach ($modelBranch->branch_id as $key => $value) {
$modelPromBranch = UserPromotionBranch::find()
->where([
'user_promotion_id' => $model->getPrimaryKey(),
'branch_id' => $value
])->one();
if($modelPromBranch == null) {
$modelPromBranch = new UserPromotionBranch();
}
//$modelPromBranch = new UserPromotionBranch();
$modelPromBranch->branch_id = $value;
$modelPromBranch->user_promotion_id = $model->getPrimaryKey();
$modelPromBranch->save(false);
$modelPromBranchDel[] = $modelPromBranch->getPrimaryKey();
}
}
UserPromotionBranch::deleteAll(
[
'AND',
[
'not in', 'user_promotion_branch_id', $modelPromBranchDel
],
[
'user_promotion_id' => $model->getPrimaryKey()
]
]);
$fileHelper = FileUploadHelper::getInstance()->convertFileArray();
foreach ($appLanguages as $langCode => $langName) {
$modelLang = UserPromotionLang::findOne([
'user_promotion_id' => $model->getPrimaryKey(),
'language_code' => $langCode
]);
if ($modelLang === null) {
$modelLang = new UserPromotionLang();
$modelLang->user_promotion_id = $model->getPrimaryKey();
}
$modelLang->loadTranslationAttribute($request, $langCode);
$modelLang->user_promotion_id = $model->getPrimaryKey();
$uploadedFile = $fileHelper->getFileInstance($modelUploadForm, 'promotion_image_path', $langCode);
if (!empty($uploadedFile)) {
$uploadHelper = UploadHelper::getInstance();
$uploadHelper->setPath($uploadHelper::PROMOTION);
$uploadHelper->setUploadedFile($uploadedFile);
$path = $uploadHelper->getPath(true);
$uploadedFile->saveAs($path);
$realpath = $uploadHelper->getRealPath($path);
$modelLang->user_promation_banner_path = '/'. $realpath ;
}
if (!$modelLang->save()) {
$response['error'] = $this->prepareErrors($modelLang);
goto skip;
} else {
$response['url'] = Url::to(['index']);
if ($id !== null) {
$adminUserData = Yii::$app->getUser()->getIdentity();
$adminUserName = $adminUserData->user_name;
ActivityLog::log($adminUserName.' '. 'Edited Promotion');
Com::successFlash(Yii::t('backend', "Updated Successfully"));
} else {
$adminUserData = Yii::$app->getUser()->getIdentity();
$adminUserName = $adminUserData->user_name;
ActivityLog::log($adminUserName.' '. 'created New Promotion');
Com::successFlash(Yii::t('backend', "Created Successfully"));
}
$response['status'] = STATUS_SUCCESS;
}
}
$transaction->commit();
} else {
$response['error'] = $this->prepareErrors($modelLang);
goto skip;
}
skip:
return $this->asJson($response);
}
/**
*
* @return string
*
* @Title("Update")
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelLang = UserPromotionLang::find()->where(['user_promotion_id' => $model->getPrimaryKey()])->one();
$modelUploadForm = new PromotionUploadForm();
$model->payment_type = explode(',', $model->payment_type);
$modelUser = new UserPromotionUser();
$modelUserData = UserPromotionUser::find()
->select('GROUP_CONCAT(user_id) AS user_id')
->where(['user_promotion_id' => $model->getPrimaryKey()])
->asArray()
->one();
$modelUser->user_id = explode(',', isset($modelUserData['user_id']) ? $modelUserData['user_id'] : '');
$modelBranch = new UserPromotionBranch();
$modelBranchData = UserPromotionBranch::find()
->select('GROUP_CONCAT(branch_id) AS branch_id')
->where(['user_promotion_id' => $model->getPrimaryKey()])
->asArray()
->one();
$modelBranch->branch_id = explode(',', isset($modelBranchData['branch_id']) ? $modelBranchData['branch_id'] : '');
UserPromotionLang::updateTranslationAttribute($modelLang, $model->getPrimaryKey());
$appLanguages = Language::getAppLanguages();
return $this->render('update', [
'model' => $model,
'modelLang' => $modelLang,
'appLanguages' => $appLanguages,
'modelUploadForm' => $modelUploadForm,
'modelBranch' => $modelBranch,
'modelUser' => $modelUser,
]);
}
/**
* Finds the Category model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return Category the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = UserPromotion::findOne(['user_promotion_key' => $id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
*
* @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->user_promotion_status = $model::DELETE;
$model->save(false);
Com::successFlash(Yii::t('backend', 'Deleted Successfully'));
return $this->redirect(['index']);
}
/**
*
* @return Json
* @throws \yii\base\InvalidParamException
*
*
* @Title("Change 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 Area';
goto skip;
}
$model->user_promotion_status = $post['status'];
$model->save(false);
$result['msg'] = Yii::t('backend', 'Deactivated successfully');
if ((int)$model->user_promotion_status === $model::ACTIVE) {
$result['msg'] = Yii::t('backend','Activated successfully');
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
}