/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/LoyalityPointController.php (6655B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
*
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new LoyalityPoint();
$modelLang = new LoyalityPointReference();
return $this->render('create', [
'model' => $model,
'modelLang' => $modelLang,
]);
}
/**
* Save City
*
* @return Array
*
*/
public function actionSave($id = NULL)
{
$response = ['status' => STATUS_FAIL, 'msg' => '', 'error' => []];
$transaction = LoyalityPoint::getDb()->beginTransaction();
$model = new LoyalityPoint();
$modelLang = new LoyalityPointReference();
if ($id !== null) {
$model = $this->findModel($id);
}
$request = Yii::$app->getRequest()->post();
$model->load($request);
$modelLang->load($request);
if ($model->validate()) {
if (!$model->save()) {
$response['error'] = $this->prepareErrors($model);
goto skip;
}
$LoyalityPoints = [];
foreach ($modelLang->start_qty as $key => $locationCity) {
$modelLocation = null;
if (isset($modelLang->loyality_point_reference_id[$key])) {
$modelLocation = LoyalityPointReference::find()
->where([
'loyality_point_reference_id' => $modelLang->loyality_point_reference_id[$key],
'loyality_point_id' => $model->getPrimaryKey(),
])->one();
}
if($modelLocation == null) {
$modelLocation = new LoyalityPointReference();
$modelLocation->loyality_point_id = $model->getPrimaryKey();
}
$modelLocation->start_qty = $modelLang->start_qty[$key];
$modelLocation->end_qty = $modelLang->end_qty[$key];
$modelLocation->loyality_point = $modelLang->loyality_point[$key];
if (!$modelLocation->save(false)) {
$response['error'] = $this->prepareErrors($modelProduct);
goto skip;
}
$LoyalityPoints[] = $modelLocation->getPrimaryKey();
}
LoyalityPointReference::DeleteAll(['AND',
[
'loyality_point_id' => $model->getPrimaryKey(),
],
[
'NOT IN',
'loyality_point_reference_id',
$LoyalityPoints
]
]);
$transaction->commit();
$response['status'] = STATUS_SUCCESS;
$response['url'] = Url::to(['index']);
} else {
$response['error'] = $this->prepareErrors($model);
goto skip;
}
skip:
return $this->asJson($response);
}
/**
*
* @return string
*
* @Title("Update")
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelLang = new LoyalityPointReference;
$modelLangData = LoyalityPointReference::find()
->where(['loyality_point_id' => $model->getPrimaryKey()])
->all();
return $this->render('update', [
'model' => $model,
'modelLang' => $modelLang,
'modelLangData' => $modelLangData
]);
}
/**
* 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 = LoyalityPoint::findOne(['loyality_point_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->loyality_point_status = $model::DELETE;
$model->save();
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->loyality_point_status = $post['status'];
$model->save(false);
$result['msg'] = Yii::t('backend','deactivated successfully');
if ((int)$model->loyality_point_status === $model::ACTIVE) {
$result['msg'] = Yii::t('backend','activated successfully');
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
}