/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/IngredientController.php (6954B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
*
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new Ingredient();
$modelLang = new IngredientLang();
$post = Yii::$app->getRequest()->post();
$appLanguages = Language::getAppLanguages();
$model->load($post);
$modelLang->vendor_id = $model->vendor_id;
if ($model->load($post) && $model->validate() && $modelLang->load($post) && $modelLang->validate()) {
$model->ingredient_type = $model::INGREDIENT_TYPE_MODIFIER ;
/**
* @author Gautam Elangovan
* The below code is written to differentiate the ingredient created by admin and vendor
*/
$model->user_type = $model::USER_TYPE_ADMIN;
if (AdminUser::isVendor()) {
$model->user_type = $model::USER_TYPE_VENDOR;
}
$model->save();
foreach($appLanguages as $langCode => $langName) {
$modelTranslation = new IngredientLang();
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->ingredient_id = $model->getPrimaryKey();
$modelTranslation->save();
}
Com::successFlash('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 = new IngredientLang();
$post = Yii::$app->getRequest()->post();
$appLanguages = Language::getAppLanguages();
IngredientLang::updateTranslationAttribute($modelLang, $model->getPrimaryKey());
$model->load($post);
$modelLang->vendor_id = $model->vendor_id;
if ( $model->load($post) && $model->validate() && $modelLang->load($post) && $modelLang->validate()) {
$model->ingredient_type = $model::INGREDIENT_TYPE_MODIFIER ;
/**
* @author Gautam Elangovan
* The below code is written to differentiate the ingredient created by admin and vendor
*/
$model->user_type = $model::USER_TYPE_ADMIN;
if (AdminUser::isVendor()) {
$model->user_type = $model::USER_TYPE_VENDOR;
}
$model->save();
foreach ($appLanguages as $langCode => $langName) {
$modelTranslation = IngredientLang::findOne([
'ingredient_id' => $model->getPrimaryKey(),
'language_code' => $langCode
]);
if ($modelTranslation === null) {
$modelTranslation = new IngredientLang();
$modelTranslation->ingredient_id = $model->getPrimaryKey();
}
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->save();
}
Com::successFlash('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 Ingredient');
return $this->redirect(['index']);
}
$model->ingredient_status = Ingredient::DELETE;
$model->save();
$adminUserData = Yii::$app->getUser()->getIdentity();
$adminUserName = $adminUserData->attributes['user_name'];
$branchName = IngredientLang::find()->where(['ingredient_id' => $model->ingredient_id,'language_code' => Yii::$app->language])->one();
ActivityLog::log($adminUserName.' '. 'deleted ingredient '.' '. $branchName->ingredient_name);
Com::successFlash('Deleted Successfully');
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return type
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = Ingredient::findOne(['ingredient_key' => $id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
*
* @return Json
* @throws \yii\base\InvalidParamException
*
* @Title("Ingredient 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 = Ingredient::findOne(['ingredient_key' => $post['key']]);
if ($model === null) {
$result['msg'] = 'Unable to find the Ingredient';
goto skip;
}
$model->ingredient_status = $post['status'];
$model->save(false);
$result['msg'] = 'Ingredient deactivated successfully';
if ((int)$model->ingredient_status === $model::ACTIVE) {
$result['msg'] = 'Ingredient activated successfully';
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
}