| Current Path : /var/www/html/sprm/backend/controllers/ |
| Current File : /var/www/html/sprm/backend/controllers/CategoryController.php |
<?php
namespace backend\controllers;
use Yii;
use backend\models\Category;
use backend\models\CategorySearch;
use backend\models\CategoryLang;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* CategoryController implements the CRUD actions for Category model.
*/
class CategoryController extends Controller {
/**
* @inheritdoc
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Category models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new CategorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Category model.
* @param string $id
* @return mixed
*/
public function actionView($id) {
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Category model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new Category();
if ($model->load(Yii::$app->request->post())) {
$parent_id = $_POST['Category']['parent_id'];
if ($parent_id != "") { //checking if ada parent id
$model->attributes = $_POST['Category'];
$model->parent_id = $parent_id;
} else {
$model->parent_id = 0;
}
$test = $model->parent_id; //dapatkan value parent id yang dipilih
if ($test == 0) { //jika tiada parent id yang dipilih
$model->level = 1; //level =1
} elseif ($test != 0) {
$ck1Category = Category::find()->where(['parent_id' => $test])->one(); //checking parent id = value $test da ada dlm db
if (empty($ck1Category)) {
//1st time
$ck2Category = Category::findOne($test);
if (!empty($ck2Category)) {
$parentid_data = $ck2Category->parent_id;
$level = $ck2Category->level;
$result = $level + 1;
} else
$result = "";
} else {
//jika masuk lebih dari 1 untuk sama level
$ck3Category = Category::find()->where(['parent_id' => $parent_id])->one();
$result = $ck3Category->level;
}
$model->level = $result;
}
if ($model->save()) {
$modelCatLang = new CategoryLang();
$modelCatLang->title = $model->titlebm;
$modelCatLang->cat_id = $model->id;
$modelCatLang->cat_lang = '2';
if ($modelCatLang->save()) {
$log = new \backend\models\Log();
$log->module = Yii::$app->controller->id;
$log->details = Yii::$app->controller->action->id . ":" . $model->id;
$log->save();
Yii::$app->session->setFlash('success', Yii::t('app', 'The category has been successfully added.'));
return $this->redirect(['index', 'id' => $model->id]);
}
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Category model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param string $id
* @return mixed
*/
public function actionUpdate($id) {
$model = $this->findModel($id);
$modelCatLang = CategoryLang::find()->where('cat_id=' . $id)->one();
if ($model->load(Yii::$app->request->post())) {
$parent_id = $_POST['Category']['parent_id'];
if ($parent_id != "") {
$model->attributes = $_POST['Category'];
$model->parent_id = $parent_id;
} else {
$model->parent_id = 0;
}
$test = $model->parent_id;
if ($test == 0) {
$model->level = 1;
} elseif ($test != 0) {
$ck1Category = Category::find()->where(['parent_id' => $test])->one();
if (empty($ck1Category)) {
$ck2Category = Category::findOne($test);
if (!empty($ck2Category)) {
$parentid_data = $ck2Category->parent_id;
$level = $ck2Category->level;
$result = $level + 1;
} else
$result = "";
} else {
//jika masuk lebih dari 1 untuk sama level
$ck3Category = Category::find()->where(['parent_id' => $parent_id])->one();
$result = $ck3Category->level;
}
$model->level = $result;
}
if ($model->save()) {
$modelCatLang->title = $model->titlebm;
$modelCatLang->cat_id = $model->id;
$modelCatLang->cat_lang = '2';
if ($modelCatLang->save()) {
$log = new \backend\models\Log();
$log->module = Yii::$app->controller->id;
$log->details = Yii::$app->controller->action->id . ":" . $model->id;
$log->save();
Yii::$app->session->setFlash('success', Yii::t('app', 'The category has been successfully updated.'));
return $this->redirect(['index', 'id' => $model->id]);
}
}
} else {
return $this->render('update', [
'model' => $model,
'modelCatLang' => $modelCatLang,
]);
}
}
/**
* Deletes an existing Category model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param string $id
* @return mixed
*/
public function actionDelete($id) {
$this->findModel($id)->delete();
$log = new \backend\models\Log();
$log->module = Yii::$app->controller->id;
$log->details = Yii::$app->controller->action->id . ":" . $id;
$log->save();
return $this->redirect(['index']);
}
/**
* Finds the Category model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return Category the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = Category::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}