/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/MallProductController.php (6398B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
*
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new MallProduct();
$modelLang = new MallProductLang();
$modelUploadForm = new MallProductUploadForm();
$modelUploadForm->setScenario($modelUploadForm::SCENARIO_CREATE);
$post = Yii::$app->getRequest()->post();
if ($model->load($post) && $modelLang->load($post) && $model->validate()) {
$uploadedFile = UploadedFile::getInstance($modelUploadForm, 'product_image_path');
$uploadHelper = UploadHelper::getInstance();
$uploadHelper->setPath($uploadHelper::MALL_PRODUCTS);
$uploadHelper->setUploadedFile($uploadedFile);
$path = $uploadHelper->getPath(true);
$uploadedFile->saveAs($path);
$model->save();
$realpath = $uploadHelper->getRealPath($path);
$modelLang->mall_product_id = $model->getPrimaryKey();
$modelLang->language_code = Yii::$app->language;
$modelLang->product_image_path = '/'. $realpath ;
$modelLang->save();
Com::successFlash('Created Successfully');
return $this->redirect(['index']);
}
return $this->render('create', [
'model' => $model,
'modelLang' => $modelLang,
'modelUploadForm' => $modelUploadForm
]);
}
/**
*
* @param type $id
* @return string
*
* @Title("Update")
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelLang = MallProductLang::find()
->select([
'mall_product_lang_id',
'product_name',
'product_description',
'product_image_path'
])
->where(['mall_product_id' => $model->getPrimaryKey()])
->one();
$product_image = $modelLang->product_image_path;
$modelUploadForm = new MallProductUploadForm();
$post = Yii::$app->getRequest()->post();
if ($model->load($post) && $model->validate()) {
$modelLang->load($post);
$uploadedFile = UploadedFile::getInstance($modelUploadForm, 'product_image_path');
if (!empty($uploadedFile)) {
$uploadHelper = UploadHelper::getInstance();
$uploadHelper->setPath($uploadHelper::MALL_PRODUCTS);
$uploadHelper->setUploadedFile($uploadedFile);
$path = $uploadHelper->getPath(true);
$model->save();
$uploadedFile->saveAs($path);
$realPath = $uploadHelper->getRealPath($path);
$modelLang->product_image_path = '/'. $realPath ;
} else {
$modelLang->product_image_path = $product_image;
}
if(!$model->save()) {
return $model;
}
$modelLang->mall_product_id = $model->getPrimaryKey();
$modelLang->language_code = Yii::$app->language;
$modelLang->save();
Com::successFlash('Updated Successfully');
return $this->redirect(['index']);
}
return $this->render('update', [
'model' => $model,
'modelLang' => $modelLang,
'modelUploadForm' => $modelUploadForm,
]);
}
/**
*
* @param type $id
* @return string
*
* @Title("Delete")
*/
public function actionDelete($id)
{
$model = $this->findModel($id);
if ($model === null) {
Com::failureFlash('Unable to find the Mall Product');
return $this->redirect(['index']);
}
$model->product_status = MallProduct::DELETE;
$model->save();
Com::successFlash('Deleted Successfully');
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return $model
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = MallProduct::findOne(['mall_product_key' => $id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
* @return Json
* @throws \yii\base\InvalidParamException
*
* @Title("Mall Product 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 = MallProduct::findOne(['mall_product_key' => $post['key']]);
if ($model === null) {
$result['msg'] = 'Unable to find the Mall Product';
goto skip;
}
$model->product_status = $post['status'];
$model->save(false);
$result['msg'] = 'Mall Product deactivated successfully';
if ((int)$model->product_status === $model::ACTIVE) {
$result['msg'] = 'Mall Product activated successfully';
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
}