Your IP : 216.73.216.79


Current Path : /var/www/html/donate/backend/modules/menu/controllers/
Upload File :
Current File : /var/www/html/donate/backend/modules/menu/controllers/FrontendController.php

<?php

namespace backend\modules\menu\controllers;

use Yii;
use backend\modules\menu\models\frontend\Menu;
use backend\modules\menu\models\frontend\MenuSearch;
use backend\modules\menu\models\frontend\RoleMapping;
use backend\modules\mimin\components\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * MenuController implements the CRUD actions for Menu model.
 */
class FrontendController extends Controller {

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

    /**
     * Lists all FrontendMenu models.
     *
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new MenuSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $model = new Menu();

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

    /**
     * Displays a single FrontendMenu model.
     *
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id)
        ]);
    }

    /**
     * Creates a new FrontendMenu model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     *
     * @return mixed
     */
    public function actionCreate() {
        $model = new Menu();

        if ($model->load(Yii::$app->request->post()) && $model->save(false)) {

            return $this->redirect([
                        'index'
            ]);
        } else {
            return $this->render('create', [
                        'model' => $model
            ]);
        }
    }

    /**
     * Updates an existing Menu model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect([
                        'index'
            ]);
        } else {
            return $this->render('update', [
                        'model' => $model
            ]);
        }
    }

    /**
     * Deletes an existing Menu model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect([
                    'index'
        ]);
    }

    /**
     * Deletes an existing Menu model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param integer $id
     * @return mixed
     */
    public function actionRoledelete($id) {
        $role = RoleMapping::findOne($id);

        $siteid = $role->site_id;
        $pageid = $role->page_id;

        $role->delete();
        return $this->redirect([
                    'assign-menu',
                    'site_id' => $siteid,
                    'page_id' => $pageid,
                    'set' => $role::getParam()['set']
        ]);
    }

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

    public function actionLevel(int $id, string $role, string $pos, string $sort, string $set) {
        $model = RoleMapping::findOne($id);

        /* parent */
        $hsort = RoleMapping::find()->where([
                    $pos,
                    'sort',
                    $model->sort
                ])
                ->andWhere([
                    'parent_id' => $model->parent_id,
                    'role_code' => $role,
                    'menu_set' => $set
                ])
                ->orderby('sort ' . $sort)
                ->one();
        /* position */
        $pos = ($pos == '<') ? 'top' : 'bottom';

        /* swap varible */
        if (!empty($hsort) && !empty($model)) {
            list ($model->sort, $hsort->sort) = [
                $hsort->sort,
                $model->sort
            ];

            Yii::$app->db->transaction(function () use ($model, $hsort) {
                $model->save(false);
                $hsort->save(false);
            });

            Yii::$app->session->setFlash('info', Yii::t('app', 'Change position to the ' . $pos));
        } else {
            Yii::$app->session->setFlash('warning', 'Already at the ' . $pos . '!');
        }

        return $this->redirect([
                    'assign-menu',
                    'site_id' => $model->site_id,
                    'page_id' => $model->page_id,
                    'set' => $set
        ]);
    }

    public function actionAssignMenu() {
        $model = new RoleMapping();

        if ($model->load(Yii::$app->request->post())) {

            $pid = (int) $model->parent_id ?? 0;
            $set = $model::getParam()['set'];

            /* find highest sort */
            $hsort = RoleMapping::find()->where([
                        'role_code' => $model->role_code,
                        'menu_set' => $set,
                        'parent_id' => $pid
                    ])
                    ->orderBy('sort DESC')
                    ->asArray()
                    ->one();
            $model->sort = 1;
            if (!empty($hsort['sort']))
                $model->sort = ($hsort['sort'] + 1);
            $model->menu_set = $set;
            $model->save(false);

            return $this->redirect([
                        'assign-menu',
                        'site_id' => $model->site_id,
                        'page_id' => $model->page_id,
                        'set' => $set
            ]);
        }

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

    public function actionDisabled($id) {
        $model = RoleMapping::findOne($id);

        if (!$model) {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
        $model->status = $model->status ? 0 : 1;
        $model->save();

        return $this->redirect([
                    'assign-menu',
                    'site_id' => $model->site_id,
                    'page_id' => $model->page_id,
                    'set' => $model->menu_set
        ]);
    }

    /*     * *
     * ENDPOINT
     */

    public function actionArticleItem() {
        $model = Menu::getArticleList();
        $arr = [];
        array_walk($model, function ($v, $i) use (&$arr) {
            $arr[$i]['id'] = $v['content_article_id'];
            $arr[$i]['text'] = $v['content_article_title_my'];
        });
        $this->asJson($arr);
    }

    public function actionSliderItem() {
        $model = Menu::getImageSliderList();
        $arr = [];
        array_walk($model, function ($v, $i) use (&$arr) {
            $arr[$i]['id'] = $v['content_image_slider_id'];
            $arr[$i]['text'] = $v['content_image_slider_title'];
        });
        $this->asJson($arr);
    }

}