| Current Path : /var/www/html/dyna-crm/controllers/ |
| Current File : /var/www/html/dyna-crm/controllers/ParamlistController.php |
<?php
namespace app\controllers;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Yii;
use app\models\ParamList;
use app\models\ParamListSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\ParamCat;
use yii\helpers\Url;
use app\models\Log;
use app\models\LogSearch;
/**
* ParamlistController implements the CRUD actions for ParamList model.
*/
class ParamlistController extends Controller {
public $menu_id = 3;
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['index', 'create', 'update', 'view'],
'rules' => [
// allow authenticated users
[
'allow' => true,
'roles' => ['@'],
],
// everything else is denied
],
],
];
}
/**
* Lists all ParamList models.
* @return mixed
*/
public function actionIndex() {
$ParamCat = new ParamCat();
$cat_uuid = Yii::$app->getRequest()->getQueryParam('cat_id');
$cat_id = $ParamCat->findID($cat_uuid);
$searchModel = new ParamListSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $cat_id);
return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
}
public function actionLog($uuid) {
$searchModel = new LogSearch();
$searchModel->menu_id = $this->menu_id;
$searchModel->data_uuid = $uuid;
$dataProvider = $searchModel->log(Yii::$app->request->queryParams);
return $this->render('log', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
}
/**
* Displays a single ParamList model.
* @param integer $id
* @param string $uuid
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id, $uuid) {
return $this->render('view', ['model' => $this->findModel($id, $uuid),]);
}
/**
* Creates a new ParamList model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new ParamList();
$cat_uuid = Yii::$app->getRequest()->getQueryParam('cat_id');
$ParamCat = new ParamCat();
$model->cat_id = $ParamCat->findID($cat_uuid);
$model->created_dt = date("Y-m-d H:i:s");
$model->created_by = Yii::$app->user->identity->id;
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$model->uuid = Uuid::uuid4();
$model->status = 1;
if ($model->load(Yii::$app->request->post())) {
if (empty($model->sort))
$model->sort = 1;
else
$model->sort = $model->sort + 1;
if ($model->save()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'create', $model->uuid);
$this->updatesorting($model->cat_id, $model->sort, $model->id);
$this->getView()->registerJs("swal.fire({
position: 'center-center',type: 'success',title: 'Data has been saved',showConfirmButton: false,timer: 1500,
}).then(function(result){window.location = '" . Url::to(['paramlist/index', 'cat_id' => $cat_uuid]) . "';});");
}
}
return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
}
/**
* Updates an existing ParamList model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @param string $uuid
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($uuid) {
$model = $this->findModel($uuid);
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$ori_sort = $model->sort;
$change_sort = 0;
if ($model->load(Yii::$app->request->post())) {
if ($model->sort != $ori_sort) {
$change_sort = 1;
}
if ($change_sort == 1) {
if (empty($model->sort))
$model->sort = 1;
else
$model->sort = $model->sort + 1;
}
if ($model->save()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'update', $model->uuid);
if ($change_sort == 1) {
$this->updatesorting($model->cat_id, $model->sort, $model->id);
}
$this->updatesorting2($model->cat_id);
$this->getView()->registerJs("swal.fire({
position: 'center-center',type: 'success',title: 'Data has been saved',showConfirmButton: false,timer: 1500,
}).then(function(result){window.location = '" . Url::to(['paramlist/index', 'cat_id' => Yii::$app->getRequest()->getQueryParam('cat_id')]) . "';});");
}
}
return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
}
public function updatesorting($cat_id, $sort, $current_id) {
$count = $sort;
$model = Paramlist::find()->where(['and', "cat_id=$cat_id", "sort>=$sort", "id!=$current_id"])->orderBy(['sort' => SORT_ASC])->all();
if (!empty($model)) {
foreach ($model as $row) {
$count++;
Yii::$app->db->createCommand("UPDATE param_list SET sort='$count' WHERE id=" . $row->id)->execute();
}
}
}
public function updatesorting2($cat_id) {
$count = 1;
$model = Paramlist::find()->where("cat_id=$cat_id")->orderBy(['sort' => SORT_ASC])->all();
if (!empty($model)) {
foreach ($model as $row) {
Yii::$app->db->createCommand("UPDATE param_list SET sort='$count' WHERE id=" . $row->id)->execute();
$count++;
}
}
}
/**
* Deletes an existing ParamList model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @param string $uuid
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id, $uuid) {
$this->findModel($id, $uuid)->delete();
return $this->redirect(['index']);
}
/**
* Finds the ParamList model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @param string $uuid
* @return ParamList the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($uuid) {
if (($model = ParamList::findOne(['uuid' => $uuid])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}