/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/CuisineController.php (8207B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
*
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new Cuisine();
$modelLang = new CuisineLang();
$modelUploadForm = new CuisineUploadForm();
$modelUploadForm->setScenario($modelUploadForm::SCENARIO_CREATE);
$post = Yii::$app->getRequest()->post();
$appLanguages = Language::getAppLanguages();
if ($model->load($post) && $modelLang->load($post) && $model->validate() && $modelLang->validate()) {
$model->save();
$fileHelper = FileUploadHelper::getInstance()->convertFileArray();
foreach($appLanguages as $langCode => $langName) {
$modelTranslation = new CuisineLang();
$uploadedFile = $fileHelper->getFileInstance($modelUploadForm, 'cuisine_image_path', $langCode);
if (!empty($uploadedFile)) {
$uploadHelper = UploadHelper::getInstance();
$uploadHelper->setPath($uploadHelper::CUISINE);
$uploadHelper->setUploadedFile($uploadedFile);
$path = $uploadHelper->getPath(true);
$uploadedFile->saveAs($path);
$realpath = $uploadHelper->getRealPath($path);
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->cuisine_id = $model->getPrimaryKey();
$modelTranslation->cuisine_image_path = '/'. $realpath ;
$modelTranslation->save();
} else {
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->cuisine_id = $model->getPrimaryKey();
$modelTranslation->save();
}
}
Com::successFlash('Created Successfully');
return $this->redirect(['index']);
}
return $this->render('create', [
'model' => $model,
'modelLang' => $modelLang,
'modelUploadForm' => $modelUploadForm,
'appLanguages' => $appLanguages
]);
}
/**
*
* @param type $id
* @return string
*
* @Title("Update")
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelLang = new CuisineLang();
$modelUploadForm = new CuisineUploadForm();
$post = Yii::$app->getRequest()->post();
$appLanguages = Language::getAppLanguages();
CuisineLang::updateTranslationAttribute($modelLang, $model->getPrimaryKey());
if ($model->load($post) && $model->validate() && $modelLang->load($post) && $modelLang->validate()) {
$model->save();
$fileHelper = FileUploadHelper::getInstance()->convertFileArray();
foreach ($appLanguages as $langCode => $langName) {
$modelTranslation = CuisineLang::findOne([
'cuisine_id' => $model->getPrimaryKey(),
'language_code' => $langCode
]);
if ($modelTranslation === null) {
$modelTranslation = new CuisineLang();
$modelTranslation->cuisine_id = $model->getPrimaryKey();
}
$uploadedFile = $fileHelper->getFileInstance($modelUploadForm, 'cuisine_image_path', $langCode);
if (!empty($uploadedFile)) {
$uploadHelper = UploadHelper::getInstance();
$uploadHelper->setPath($uploadHelper::CUISINE);
$uploadHelper->setUploadedFile($uploadedFile);
$path = $uploadHelper->getPath(true);
$uploadedFile->saveAs($path);
$realpath = $uploadHelper->getRealPath($path);
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->cuisine_id = $model->getPrimaryKey();
$modelTranslation->cuisine_image_path = '/'. $realpath ;
$modelTranslation->save();
} else {
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->cuisine_id = $model->getPrimaryKey();
$modelTranslation->save();
}
}
Com::successFlash('Updated Successfully');
return $this->redirect(['index']);
}
return $this->render('update', [
'model' => $model,
'modelLang' => $modelLang,
'modelUploadForm' => $modelUploadForm,
'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 Cuisine');
return $this->redirect(['index']);
}
$model->cuisine_status = Cuisine::DELETE;
$model->save();
$adminUserData = Yii::$app->getUser()->getIdentity();
$adminUserName = $adminUserData->attributes['user_name'];
$branchName = CuisineLang::find()->where(['cuisine_id' => $model->cuisine_id,'language_code' => Yii::$app->language])->one();
ActivityLog::log($adminUserName.' '. 'deleted cuisine '.' '. $branchName->cuisine_name);
Com::successFlash('Deleted Successfully');
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return type
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = Cuisine::findOne(['cuisine_key' => $id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
*
* @return Json
* @throws \yii\base\InvalidParamException
*
* @Title("Cuisine 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 = Cuisine::findOne(['cuisine_key' => $post['key']]);
if ($model === null) {
$result['msg'] = 'Unable to find the Cuisine';
goto skip;
}
$model->cuisine_status = $post['status'];
$model->save(false);
$result['msg'] = 'Cuisine deactivated successfully';
if ((int)$model->cuisine_status === $model::ACTIVE) {
$result['msg'] = 'Cuisine activated successfully';
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
}