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