Your IP : 216.73.216.79


Current Path : /var/www/html/portalv3/backend/controllers/frontend/
Upload File :
Current File : /var/www/html/portalv3/backend/controllers/frontend/FrontendPageController.php

<?php

namespace backend\controllers\frontend;

use Yii;
use backend\models\frontendpage\FrontendPage;
use backend\models\frontendpage\FrontendPageSearch;
use backend\models\frontendcontentcategory\FrontendContentCategory;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\frontend\FrontendSite;
use yii\db\QueryBuilder;
use yii\filters\AccessControl;
use common\components\AccessRule;
use backend\models\frontendcontentassign\FrontendContentAssign;
use yii\helpers\Url;

/**
 * FrontendPageController implements the CRUD actions for FrontendPage model.
 */
class FrontendPageController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'ruleConfig' => [
                    'class' => AccessRule::className(),
                ],
                'rules' => [
                    [
                        'actions' => ['index', 'create', 'update', 'view', 'delete', 'set-default-page', 'duplicate'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                // 'setDefaultPage' => ['POST'],
                ],
            ],
        ];
    }

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

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

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

    /**
     * Creates a new FrontendPage model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new FrontendPage();
        $siteid = filter_var($_GET['FrontendPageSearch']['site_id'], FILTER_SANITIZE_NUMBER_INT);
        $model->site_id = $siteid;
        $findDefault = FrontendPage::find()->where(['site_id' => $siteid, 'page_default' => '1'])->one();

        if ($model->load(Yii::$app->request->post())) {
            $model->site_id = $siteid;

            if (empty($findDefault)) {
                $model->page_default = 1;
            }

            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index', 'FrontendPageSearch' => ['site_id' => $model->site_id]]) . "';
                });");
            }
        }
        return $this->render('create', [
                    'model' => $model,
        ]);
    }

    /**
     * Updates an existing FrontendPage 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()) {
            $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index', 'FrontendPageSearch' => ['site_id' => $model->site_id]]) . "';
                });");
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing FrontendPage model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    /*public function actionDelete($id) {
        $assign = FrontendContentAssign::find()->where(['page_id' => $id])->all();
        $siteid = filter_var($_GET['FrontendPageSearch']['site_id'], FILTER_SANITIZE_NUMBER_INT);

        \Yii::$app->db->transaction(function($db) use ($id, $assign) {
            $this->findModel($id)->delete();

            foreach ($assign as $a) {
                $a->delete();
            }
        });

        return $this->redirect(['index', 'FrontendPageSearch' => ['site_id' => $siteid]]);
    }*/
    
    public function actionDelete() {
        $id = $_POST['id'];
        $status = 'failed';
        if ($this->findModel($id)->delete())
            $status = 'success';
        return $status;
    }

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

    public function actionSetDefaultPage() {
        $siteid = Yii::$app->request->get('siteid');
        $pageid = Yii::$app->request->get('pageid');

        /* reseting the default page value */
        Yii::$app->db->createCommand()->update(
                'frontend_page', //table
                ['page_default' => 0], //column
                'site_id = :siteid', //condition
                [':siteid' => $siteid] //bind
        )->execute();

        /* set the default page value */
        $frontendPage = FrontendPage::findOne($pageid);
        $frontendPage->page_default = 1;
        $frontendPage->update(false);

        if ($frontendPage) {
            return $this->redirect(['/frontend/frontend-page/index', 'FrontendPageSearch' => ['site_id' => $siteid]]);
        }
    }

    public function actionDuplicate($id, $name) {
        $page = FrontendPage::findOne($id);
        $newPage = new FrontendPage();

        $newPage->page_name = $name;
        $newPage->site_id = $page->site_id;
        $newPage->page_setup = $page->page_setup;

        $assigns = FrontendContentAssign::find()->where(['page_id' => $page->page_id])->all();

        \Yii::$app->db->transaction(function($db) use ($newPage, $assigns) {

            if ($newPage->save()) {
                foreach ($assigns as $assign) {
                    $newContent = new FrontendContentAssign();
                    $newContent->page_id = $newPage->page_id;
                    $newContent->content_id = $assign->content_id;
                    $newContent->content_order = $assign->content_order;
                    $newContent->category_id = $assign->category_id;
                    $newContent->status = $assign->status;
                    $newContent->save();
                }
            }
        });

        return $this->redirect(['/frontend/frontend-page/index', 'FrontendPageSearch' => ['site_id' => $page->site_id]]);
    }

}