/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/CouponController.php (15245B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams, Coupon::ACTIVE)
]
);
}
/**
*
* @return string
*
* @Title("Inactive List")
*/
public function actionCouponInactive()
{
$searchModel = new CouponSearch();
return $this->render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams, Coupon::INACTIVE)
]
);
}
/**
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new Coupon();
//$modelLang = new CouponLang();
//$modelUploadForm = new CouponUploadForm();
$modelCouponVendor = new CouponVendor();
$modelCouponUser = new CouponUser();
$modelVendor = new Vendor();
$modelUser = new User();
$post = Yii::$app->getRequest()->post();
//$appLanguages = Language::getAppLanguages();
if ($model->load($post) && $model->validate()) {
$modelCouponVendor->load($post);
$modelCouponUser->load($post);
//$modelLang->load($post);
if (AdminUser::isVendor()) {
$model->is_all_vendor = $model::IS_ALL_VENDOR_INACTIVE;
$post['CouponVendor']['vendor_id'][] = Yii::$app->getUser()->getIdentity()->vendor_id;
}
// $model->coupon_status = Coupon::ACTIVE;
if(AdminUser::isVendor()){
$model->coupon_status = Coupon::INACTIVE;
}
$model->save();
// $fileHelper = FileUploadHelper::getInstance()->convertFileArray();
//
// foreach($appLanguages as $langCode => $langName) {
// $modelTranslation = new CouponLang();
// $uploadedFile = $fileHelper->getFileInstance($modelUploadForm, 'coupon_image', $langCode);
// if (!empty($uploadedFile)) {
// $uploadHelper = UploadHelper::getInstance();
// $uploadHelper->setPath($uploadHelper::COUPON);
// $uploadHelper->setUploadedFile($uploadedFile);
// $path = $uploadHelper->getPath(true);
// $uploadedFile->saveAs($path);
// $realpath = $uploadHelper->getRealPath($path);
// $modelTranslation->loadTranslationAttribute($post, $langCode);
// $modelTranslation->coupon_id = $model->getPrimaryKey();
// $modelTranslation->coupon_image = '/'. $realpath ;
// $modelTranslation->save();
// } else {
// $modelTranslation->loadTranslationAttribute($post, $langCode);
// $modelTranslation->coupon_id = $model->getPrimaryKey();
// $modelTranslation->save();
// }
// }
if($post['CouponVendor']['vendor_id'] != '') {
foreach($post['CouponVendor']['vendor_id'] as $vendorValue) {
$modelCouponVendor = new CouponVendor();
$modelCouponVendor->vendor_id = $vendorValue;
$modelCouponVendor->coupon_id = $model->getPrimaryKey();
$modelCouponVendor->save();
}
}
if($post['CouponUser']['user_id'] != '') {
foreach($post['CouponUser']['user_id'] as $userValue) {
$modelCouponUser = new CouponUser();
$modelCouponUser->user_id = $userValue;
$modelCouponUser->coupon_id = $model->getPrimaryKey();
$modelCouponUser->save();
$modelUser = User::find()
->select([
'email',
'first_name',
'last_name'
])
->where(['user_id' => $userValue])
->one();
$mailerQueueHelper = MailerQueueHelper::getInstance()
->setTo($modelUser->email)
->setSubject('New Coupon offer')
->setView(
'UserCouponDetails',
[
'username' => $modelUser->first_name.' '.$modelUser->last_name,
'couponCode' => $model->coupon_code,
'email' => $modelUser->email
])
->push();
}
}
Com::successFlash('Created Successfully');
return $this->redirect(['index']);
}
return $this->render('create', [
'model' => $model,
'modelCouponVendor' => $modelCouponVendor,
'modelCouponUser' => $modelCouponUser,
'modelVendor' => $modelVendor,
//'modelLang' => $modelLang,
//'modelUploadForm' => $modelUploadForm,
'modelUser' => $modelUser,
//'appLanguages' => $appLanguages
]);
}
/**
*
* @param type $id
* @return string
*
* @Title("Update")
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
// $modelLang = CouponLang::find()
// ->where(['coupon_id' => $model->getPrimaryKey()])
// ->one();
//
// $modelUploadForm = new CouponUploadForm();
// $modelCouponImage = $modelLang->coupon_image;
$modelCouponVendorData = CouponVendor::find()
->select('GROUP_CONCAT(vendor_id) AS vendor_id')
->where(['coupon_id' => $model->getPrimaryKey()])
->asArray()
->one();
$modelCouponVendor = new CouponVendor();
$modelVendor = new Vendor();
$modelCouponUserData = CouponUser::find()
->select('GROUP_CONCAT(user_id) AS user_id')
->where(['coupon_id' => $model->getPrimaryKey()])
->asArray()
->one();
$modelCouponUser = new CouponUser();
$modelUser = new User();
$post = Yii::$app->getRequest()->post();
// $appLanguages = Language::getAppLanguages();
// CouponLang::updateTranslationAttribute($modelLang, $model->getPrimaryKey());
if ($model->load($post) && $model->validate()) {
$modelCouponVendor->load($post);
$modelCouponUser->load($post);
//$modelLang->load($post);
$model->save();
$fileHelper = FileUploadHelper::getInstance()->convertFileArray();
// foreach ($appLanguages as $langCode => $langName) {
// $modelTranslation = CouponLang::findOne([
// 'coupon_id' => $model->getPrimaryKey(),
// 'language_code' => $langCode
// ]);
//
// if ($modelTranslation === null) {
// $modelTranslation = new CouponLang();
// $modelTranslation->coupon_id = $model->getPrimaryKey();
// }
//
// $uploadedFile = $fileHelper->getFileInstance($modelUploadForm, 'coupon_image', $langCode);
//
// if (!empty($uploadedFile)) {
// $uploadHelper = UploadHelper::getInstance();
// $uploadHelper->setPath($uploadHelper::COUPON);
// $uploadHelper->setUploadedFile($uploadedFile);
// $path = $uploadHelper->getPath(true);
// $uploadedFile->saveAs($path);
// $realpath = $uploadHelper->getRealPath($path);
// $modelTranslation->loadTranslationAttribute($post, $langCode);
// $modelTranslation->coupon_id = $model->getPrimaryKey();
// $modelTranslation->coupon_image = '/'. $realpath ;
// $modelTranslation->save();
// } else {
// $modelTranslation->loadTranslationAttribute($post, $langCode);
// $modelTranslation->coupon_id = $model->getPrimaryKey();
// $modelTranslation->save();
// }
// }
if ( (int)$model->is_all_vendor === Coupon::IS_ALL_VENDOR_INACTIVE ) {
$modelCouponVendor->load($post);
$availableVendorId = [];
if ($post['CouponVendor']['vendor_id'] != '' ) {
foreach($post['CouponVendor']['vendor_id'] as $vendorValue) {
$modelCouponVendor = CouponVendor::find()->where([
'coupon_id' => $model->getPrimaryKey(),
'vendor_id' => $vendorValue,
])->one();
if ($modelCouponVendor === null) {
$modelCouponVendor = new CouponVendor();
$modelCouponVendor->vendor_id = $vendorValue;
}
$modelCouponVendor->coupon_id = $model->getPrimaryKey();
$modelCouponVendor->save();
$availableVendorId[] = $vendorValue;
}
CouponVendor::deleteAll(['AND', ['not in', 'vendor_id', $availableVendorId], ['coupon_id' => $model->getPrimaryKey()]]);
}
} else {
CouponVendor::deleteAll(['coupon_id' => $model->getPrimaryKey()]);
}
if ((int)$model->coupon_scope === Coupon::COUPON_SCOPE_USER ) {
$modelCouponUser->load($post);
$availableUserId = [];
if ($post['CouponUser']['user_id'] != '' ) {
foreach($post['CouponUser']['user_id'] as $userValue) {
$modelCouponUser = CouponUser::find()->where([
'coupon_id' => $model->getPrimaryKey(),
'user_id' => $userValue,
])->one();
if ($modelCouponUser === null) {
$modelCouponUser = new CouponUser();
$modelCouponUser->user_id = $userValue;
}
$modelCouponUser->coupon_id = $model->getPrimaryKey();
$modelCouponUser->save();
$availableUserId[] = $userValue;
}
CouponUser::deleteAll(['AND', ['not in', 'user_id', $availableUserId], ['coupon_id' => $model->getPrimaryKey()]]);
}
} else {
CouponUser::deleteAll(['coupon_id' => $model->getPrimaryKey()]);
}
Com::successFlash('Updated Successfully');
return $this->redirect(['index']);
}
return $this->render('update', [
'model' => $model,
'modelCouponVendor' => $modelCouponVendor,
'modelCouponVendorData' => $modelCouponVendorData,
'modelVendor' => $modelVendor,
//'modelLang' => $modelLang,
'modelCouponUser' => $modelCouponUser,
'modelCouponUserData' => $modelCouponUserData,
//'modelUploadForm' => $modelUploadForm,
'modelUser' => $modelUser,
//'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 Coupon');
return $this->redirect(['index']);
}
$adminUserData = Yii::$app->getUser()->getIdentity();
$adminUserName = $adminUserData->attributes['user_name'];
// $coupon = CouponLang::find()->where(['coupon_id' => $model->coupon_id,'language_code' => Yii::$app->language])->one();
// print_r($coupon); die;
ActivityLog::log($adminUserName.' '. 'deleted coupon '.' '. $model->coupon_code);
$model->coupon_status = Coupon::DELETE;
$model->save();
Com::successFlash('Deleted Successfully');
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return type
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = Coupon::findOne(['coupon_key' => $id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
*
* @return Json
* @throws \yii\base\InvalidParamException
*
* @Title("Coupon 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 = Coupon::findOne(['coupon_key' => $post['key']]);
if ($model === null) {
$result['msg'] = 'Unable to find the Coupon';
goto skip;
}
$model->coupon_status = $post['status'];
$model->save(false);
$result['msg'] = Yii::t('backend', 'Coupon deactivated successfully');
if ((int)$model->coupon_status === $model::ACTIVE) {
$result['msg'] = Yii::t('backend', 'Coupon activated successfully');
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
}