/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/ItemIngredientGroupController.php (11105B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
*
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new ItemIngredientGroup();
$model->setScenario($model::SCENARIO_CREATE);
$modelLang = new ItemIngredientGroupLang();
$modelMap = new ItemIngredientMapping();
$post = Yii::$app->getRequest()->post();
$appLanguages = Language::getAppLanguages();
$model->load($post);
$modelLang->vendor_id = $model->vendor_id;
if ($model->load($post) && $modelLang->load($post) && $modelLang->vendor_id = $model->vendor_id && $modelLang->validate()) {
if ($model->save()) {
ItemIngredientMapping::deleteAll(['item_ingredient_group_id' => $model->getPrimaryKey()]);
foreach($appLanguages as $langCode => $langName) {
$modelTranslation = new ItemIngredientGroupLang();
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->item_ingredient_group_id = $model->getPrimaryKey();
$modelTranslation->save();
}
foreach (explode(',', $post['ItemIngredientGroup']['mapping']) as $mappingKey => $mappingValue) {
$modelMapmodel = new ItemIngredientMapping();
$modelMapmodel->item_ingredient_group_id = $model->getPrimaryKey();
$modelMapmodel->ingredient_id = $mappingValue;
$modelMapmodel->default_status = (isset($post['ingredient_default'][$mappingValue])) ? 2 : 0;
$modelMapmodel->price = (isset($post['price_default'][$mappingValue])) ? $post['price_default'][$mappingValue] : 0;
$modelMapmodel->load($post);
$modelMapmodel->save();
}
Com::successFlash('Created Successfully');
return $this->redirect(['index']);
}
}
return $this->render('create', [
'model' => $model,
'modelLang' => $modelLang,
'mappedIds' => '',
'appLanguages' => $appLanguages
]);
}
/**
*
* @param type $id
* @return string
*
* @Title("Update")
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$model->setScenario($model::SCENARIO_CREATE);
$modelLang = new ItemIngredientGroupLang();
$modelMap = ItemIngredientMapping::find()->where(['item_ingredient_group_id' => $model->getPrimaryKey()])->all();
$mappedIds = '';
foreach ($modelMap as $modelMapKey => $modelMapValue) {
$mappedIds .= $modelMapValue['ingredient_id'].",";
}
$mappedIds = rtrim($mappedIds, ',');
$post = Yii::$app->getRequest()->post();
$appLanguages = Language::getAppLanguages();
ItemIngredientGroupLang::updateTranslationAttribute($modelLang, $model->getPrimaryKey());
$model->load($post);
//sprint_r($model); die;
$modelLang->vendor_id = $model->vendor_id;
if ($model->load($post) && $model->validate() && $modelLang->load($post) && $modelLang->validate()) {
//;
//print_r($modelLang->validate()); die;
// if( $modelLang->load($post))
if ($model->save()) {
foreach ($appLanguages as $langCode => $langName) {
$modelTranslation = ItemIngredientGroupLang::findOne([
'item_ingredient_group_id' => $model->getPrimaryKey(),
'language_code' => $langCode
]);
if ($modelTranslation === null) {
$modelTranslation = new ItemIngredientGroupLang();
$modelTranslation->item_ingredient_group_id = $model->getPrimaryKey();
}
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->save();
}
ItemIngredientMapping::deleteAll(['item_ingredient_group_id' => $model->getPrimaryKey()]);
$mappingArray = explode(',', $post['ItemIngredientGroup']['mapping']);
foreach ($mappingArray as $mappingKey => $mappingValue) {
$modelMapping = ItemIngredientMapping::find()->where([
'item_ingredient_group_id' => $model->getPrimaryKey(),
'ingredient_id' => $mappingValue,
])->one();
if ($modelMapping === null) {
$modelMapping = new ItemIngredientMapping();
$modelMapping->item_ingredient_group_id = $model->getPrimaryKey();
$modelMapping->ingredient_id = $mappingValue;
}
$modelMapping->default_status = (isset($post['ingredient_default'][$mappingValue])) ? 2 : 1;
$modelMapping->price = (isset($post['price_default'][$mappingValue])) ? $post['price_default'][$mappingValue] : 0;
if (!$modelMapping->save()) {
echo"
";print_r($modelMapping->getErrors());
exit;
}
}
}
Com::successFlash('Updated Successfully');
return $this->redirect(['index']);
}
return $this->render('update', [
'model' => $model,
'modelLang' => $modelLang,
'mappedIds' => $mappedIds,
'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 Item Ingredient Group');
return $this->redirect(['index']);
}
$model->item_ingredient_status = ItemIngredientGroup::DELETE;
$model->save();
$adminUserData = Yii::$app->getUser()->getIdentity();
$adminUserName = $adminUserData->attributes['user_name'];
$branchName = ItemIngredientGroupLang::find()->where(['item_ingredient_group_id' => $model->item_ingredient_group_id,'language_code' => Yii::$app->language])->one();
ActivityLog::log($adminUserName.' '. 'deleted item ingredient group '.' '. $branchName->group_name);
Com::successFlash('Deleted Successfully');
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return $model
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = ItemIngredientGroup::findOne(['item_ingredient_group_key' => $id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
*
* @return Json
* @throws \yii\base\InvalidParamException
*
* @Title("Item Ingredient Group 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 = ItemIngredientGroup::findOne(['item_ingredient_group_key' => $post['key']]);
if ($model === null) {
$result['msg'] = 'Unable to find the Item';
goto skip;
}
$model->item_ingredient_status = $post['status'];
$model->save(false);
$result['msg'] = 'Ingredient group deactivated successfully';
if ((int)$model->item_ingredient_status === $model::ACTIVE) {
$result['msg'] = 'Ingredient group activated successfully';
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
/**
*
* @param type $id
* @return $model
* @throws NotFoundHttpException
*/
protected function findImageModel($id)
{
$model = ItemImage::find()
->alias('IM')
->select('IM.item_image_path')
->leftJoin(['I' => Item::tableName()], 'IM.item_id = I.item_id')
->where(['I.item_key' => $id])
->one();
return $model;
if ($model === null) {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
* @return string
*/
public function actionGetBranch()
{
$post = Yii::$app->request->post();
$vendorId = $post['id'];
$branches = Branch::find()
->alias('B')
->select('BL.branch_name')
->leftJoin(['BL' => BranchLang::tableName()], 'BL.branch_id = B.branch_id')
->where(['B.vendor_id' => $vendorId])
->all();
$option = Html::tag('option', 'Please Select', ['value' => '']);#'';
foreach ($branches as $branch) {
$option .= Html::tag('option', $branch->branch_name, ['value' => $branch->branch_id]);
}
echo $option;
}
public function actionVendorIng()
{
$post = Yii::$app->request->post();
$model = ItemIngredientGroup::getIngredients('','NOT IN','', $post['id']);
$html = $this->renderPartial('ingredient_group_ajax',['ingredientData' => $model]);
return $this->asJson($html);
}
}