/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/UnitController.php (7115B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
*
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new Unit();
$model->unit_status = Unit::ACTIVE;
$modelLang = new UnitLang();
$post = Yii::$app->getRequest()->post();
$appLanguages = Language::getAppLanguages();
if($model->load($post)) {
if ($modelLang->load($post) && $model->validate() && $modelLang->validate()) {
if(!$model->save()){
goto skip;
}
foreach ($appLanguages as $langCode => $langName) {
$modelTranslation = new UnitLang();
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->unit_id = $model->getPrimaryKey();
$modelTranslation->save();
}
$adminUserData = Yii::$app->getUser()->getIdentity();
$adminUserName = $adminUserData->attributes['user_name'];
$brandName = $post['UnitLang']['unit_name']['en'];
ActivityLog::log($adminUserName.' '. 'created a New Unit Named '.' '. $brandName);
Com::successFlash(Yii::t('backend','Created Successfully'));
return $this->redirect(['index']);
}
}
skip:
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 UnitLang();
$post = Yii::$app->getRequest()->post();
$appLanguages = Language::getAppLanguages();
UnitLang::updateTranslationAttribute($modelLang, $model->getPrimaryKey());
$modelLang->load($post);
if ($model->load($post) && $model->validate() && $modelLang->validate()) {
if(!$model->save()){
goto skip;
}
foreach ($appLanguages as $langCode => $langName) {
$modelTranslation = UnitLang::findOne([
'unit_id' => $model->getPrimaryKey(),
'language_code' => $langCode
]);
if ($modelTranslation === null) {
$modelTranslation = new UnitLang();
$modelTranslation->unit_id = $model->getPrimaryKey();
}
$modelTranslation->loadTranslationAttribute($post, $langCode);
$modelTranslation->unit_id = $model->getPrimaryKey();
$modelTranslation->save();
}
$adminUserData = Yii::$app->getUser()->getIdentity();
$adminUserName = $adminUserData->attributes['user_name'];
$brandName = $post['UnitLang']['unit_name']['en'];
ActivityLog::log($adminUserName.' '. 'updated New Unit Named '.' '. $brandName);
Com::successFlash(Yii::t('backend','Updated Successfully'));
return $this->redirect(['index']);
}
skip:
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 Unit');
return $this->redirect(['index']);
}
$model->unit_status = Unit::DELETE;
$query = Unit::find()
->alias('U')
->select([
'U.*',
'UL.unit_name' ])
->where(['unit_key'=>$id])
->leftJoin(['UL' => UnitLang::tableName()], 'UL.unit_id = U.unit_id')->one();
if($model->save()) {
$adminUserData = Yii::$app->getUser()->getIdentity();
$adminUserName = $adminUserData->attributes['user_name'];
$brandName = $query->unit_name;
ActivityLog::log($adminUserName.' '. 'deleted new Unit Named '.' '. $brandName);
}
Com::successFlash(Yii::t('backend','Deleted Successfully'));
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return type
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = Unit::findOne(['unit_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 = Unit::findOne(['unit_key' => $post['key']]);
if ($model === null) {
$result['msg'] = 'Unable to find the Unit';
goto skip;
}
$model->unit_status = $post['status'];
$model->save(false);
$result['msg'] = Yii::t('backend','Unit deactivated successfully');
if ((int)$model->unit_status === $model::ACTIVE) {
$result['msg'] = Yii::t('backend','Unit activated successfully');
}
$result['status'] = STATUS_SUCCESS;
skip:
return $this->asJson($result);
}
}