| Current Path : /var/www/html/brspace/backend/controllers/frontend/ |
| Current File : /var/www/html/brspace/backend/controllers/frontend/FrontendSiteController.php |
<?php
namespace backend\controllers\frontend;
use Yii;
use backend\models\frontend\FrontendSite;
use backend\models\frontendpage\FrontendPage;
use backend\models\frontend\FrontendSiteSearch;
use backend\models\frontendcontentassign\FrontendContentAssign;
use backend\modules\mimin\components\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\components\AccessRule;
use yii\helpers\Url;
/**
* FrontendSiteController implements the CRUD actions for FrontendSite model.
*/
class FrontendSiteController extends Controller {
/**
*
* @inheritdoc
*/
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => [
'POST'
]
]
]
];
}
public function createUrl() {
}
/**
* Lists all FrontendSite models.
*
* @return mixed
*/
public function actionIndex() {
$searchModel = new FrontendSiteSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]);
}
/**
* Displays a single FrontendSite model.
*
* @param integer $id
* @return mixed
*/
public function actionView($id) {
return $this->render('view', [
'model' => $this->findModel($id)
]);
}
/**
* Creates a new FrontendSite model.
* If creation is successful, the browser will be redirected to the 'view' page.
*
* @return mixed
*/
public function actionCreate() {
$model = new FrontendSite();
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']) . "';
});");
}
return $this->render('create', ['model' => $model]);
}
/**
* Updates an existing FrontendSite 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']) . "';
});");
}
return $this->render('update', ['model' => $model]);
}
/**
* Deletes an existing FrontendSite model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*
* @param integer $id
* @return mixed
*/
public function actionDelete() {
$id = $_POST['id'];
$status = 'failed';
if ($this->findModel($id)->delete())
$status = 'success';
return $status;
}
/**
* Finds the FrontendSite model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
* @param integer $id
* @return FrontendSite the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = FrontendSite::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionSetDefaultSite() {
$siteid = Yii::$app->request->get('site_id');
/* reseting the default value */
$siteDefault = FrontendSite::findOne([
'site_default' => 1
]);
if (!empty($siteDefault)) {
$siteDefault->site_default = 0;
$siteDefault->update();
return $this->actionIndex();
}
/* setting the default value */
$siteSetDefault = FrontendSite::find()->where('site_id = :siteid', [
':siteid' => $siteid
])->one();
$siteSetDefault->site_default = 1;
$siteSetDefault->update();
return $this->actionIndex();
}
/**
* Finds the FrontendSite model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
* @param integer
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDuplicate($id) {
//get site
$sites = FrontendSite::findOne($id);
if(empty($sites)) {
throw new \Exception("Site id don't exist");
}
//copy pages to site
yii::$app->db->transaction(function($db) use ($sites, $id) {
//create new site
$site = new FrontendSite();
$site->site_name = $sites->site_name . '_' . time();
$site->site_default = 0;
$site->site_theme = $sites->site_theme;
$site->slug_enable = $sites->slug_enable;
$site->site_slug = $sites->site_slug . '_' . time();
if(!$site->save()) {
throw new \Exception('Cannot duplicate site' . json_encode($site->errors));
}
//get all pages
$pages = FrontendPage::find()->where(['site_id' => $id])->all();
foreach($pages as $page) {
// $page->page_id
$newpage = new FrontendPage();
$newpage->site_id = $site->site_id;
$newpage->page_name = $page->page_name;
$newpage->page_setup = $page->page_setup;
// if(!$newpage->save()) {
// throw new \Exception("Cannot duplicate page, Error:" . json_encode($newpage->errors));
// }
$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(['index']);
}
}