Your IP : 216.73.216.79


Current Path : /var/www/html/jppmintranetv2/backend/modules/orgchart/controllers/
Upload File :
Current File : /var/www/html/jppmintranetv2/backend/modules/orgchart/controllers/OrganizationChartController.php

<?php

namespace backend\modules\orgchart\controllers;

use Yii;
use backend\modules\orgchart\models\OrganizationChart;
use backend\modules\orgchart\models\OrganizationChartSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * OrganizationChartController implements the CRUD actions for OrganizationChart model.
 */
class OrganizationChartController extends Controller {

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['GET'],
                ],
            ],
        ];
    }

    /**
     * Lists all OrganizationChart models.
     * @return mixed
     */
    public function actionIndex($dept) {

        return $this->render('index', [
                    'dept' => $dept,
        ]);
    }

    public function actionDept() {

        return $this->render('dept');
    }

    /**
     * Displays a single OrganizationChart model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new OrganizationChart model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($dept, $parent_id = '') {
        $model = new OrganizationChart();
        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->dept = $dept;
        if (!empty($parent_id))
            $model->sorting = (OrganizationChart::find()->where(['=', 'parent_id', $parent_id])->andwhere(['=', 'dept', $dept])->count() + 1);
        else
            $model->sorting = (OrganizationChart::find()->where(['IS', 'parent_id', NULL])->andwhere(['=', 'dept', $dept])->count() + 1);
        $model->parent_id = $parent_id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'dept' => $dept]);
        }
        return $this->render('create', ['model' => $model, 'dept' => $dept,]);
    }

    /**
     * Updates an existing OrganizationChart model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
        $dept = $model->dept;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'dept' => $dept]);
        }

        return $this->render('update', ['model' => $model, 'dept' => $dept,]);
    }

    /**
     * Deletes an existing OrganizationChart model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id) {
        $model = $this->findModel($id);
        if (!empty($model)) {
            $dept = $model->dept;
            $model->delete();
            return $this->redirect(['index', 'dept' => $dept]);
        }
    }

    /**
     * Finds the OrganizationChart model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return OrganizationChart the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = OrganizationChart::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    public function actionIncreasesort($id, $parent_id = '') {

        if (($model = OrganizationChart::findOne($id)) !== null) {
            $new_sort = $model->sorting - 1;
            if ($new_sort < 0)
                $new_sort = 0;
            $model->sorting = $new_sort;
            $model->save(false);

            $query = OrganizationChart::find()->where(['>=', 'sorting', $new_sort])->andwhere(['!=', 'id', $id])->andwhere(['=', 'dept', $model->dept]);

            if (empty($parent_id))
                $query->andwhere(['IS', 'parent_id', null]);
            else
                $query->andwhere(['=', 'parent_id', $parent_id]);

            $query->orderby(['sorting' => SORT_ASC]);
            $modelReorder = $query->all();
            if (!empty($modelReorder)) {
                foreach ($modelReorder as $rowReorder) {
                    $new_sort++;
                    $rowReorder->sorting = $new_sort;
                    $rowReorder->save(false);
                }
            }

            return $this->redirect(['index', 'dept' => $model->dept]);
        }
    }

    public function actionDecreasesort($id, $parent_id = '') {

        if (($model = OrganizationChart::findOne($id)) !== null) {
            $new_sort = $model->sorting + 1;
            if ($new_sort < 0)
                $new_sort = 0;
            $model->sorting = $new_sort;
            $model->save(false);

            $query = OrganizationChart::find()->where(['<=', 'sorting', $new_sort])->andwhere(['!=', 'id', $id])->andwhere(['=', 'dept', $model->dept]);

            if (empty($parent_id))
                $query->andwhere(['IS', 'parent_id', null]);
            else
                $query->andwhere(['=', 'parent_id', $parent_id]);

            $query->orderby(['sorting' => SORT_DESC]);
            $modelReorder = $query->all();
            if (!empty($modelReorder)) {
                foreach ($modelReorder as $rowReorder) {
                    $new_sort--;
                    if ($new_sort < 0)
                        $new_sort = 0;
                    $rowReorder->sorting = $new_sort;
                    $rowReorder->save(false);
                }
            }

            return $this->redirect(['index', 'dept' => $model->dept]);
        }
    }

}