| Current Path : /var/www/html/learn/backend/controllers/frontend/ |
| Current File : /var/www/html/learn/backend/controllers/frontend/FrontendContentController.php |
<?php
namespace backend\controllers\frontend;
use Yii;
use backend\models\frontendcontent\FrontendContent;
use backend\models\frontendcontent\FrontendContentSearch;
use backend\models\frontendpage\FrontendPage;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\i18n\DmgI18nMessage;
use backend\models\i18n\DmgI18nSourceMessage;
use backend\models\frontendcontentassign\FrontendContentAssign;
use yii\db\Query;
use backend\modules\mimin\components\AccessControl;
use common\components\AccessRule;
use Wa72\HtmlPageDom\HtmlPage;
use backend\models\frontendcontenthistory\FrontendContentHistory;
/**
* FrontendContentController implements the CRUD actions for FrontendContent model.
*/
class FrontendContentController extends Controller
{
/* max record of history */
const MAX_HISTORY = 5;
/**
*
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => [
'POST'
]
]
]
];
}
/**
* Lists all FrontendContent models.
*
* @return mixed
*/
public function actionIndex()
{
$searchModel = new FrontendContentSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
]);
}
/**
* Displays a single FrontendContent model.
*
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id)
]);
}
/**
* Creates a new FrontendContent model.
* If creation is successful, the browser will be redirected to the 'view' page.
*
* @return mixed
*/
public function actionCreate()
{
$model = new FrontendContent();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$pageid = isset($_GET['FrontendContentSearch']['page_id']) ? $_GET['FrontendContentSearch']['page_id'] : '';
$pagename = isset($_GET['page_name']) ? $_GET['page_name'] : '';
return $this->redirect([
'index',
'id' => $model->content_id,
'FrontendContentSearch[page_id]' => $pageid,
'page_name' => $pagename
]);
} else {
return $this->render('create', [
'model' => $model
]);
}
}
/**
* Updates an existing FrontendContent model.
* If update is successful, the browser will be redirected to the 'view' page.
*
* @param integer $id
* @return mixed
*/
public function actionUpdate($id, $page_id=null, $page_name=null, $site_id=null)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
// return $this->redirect(['index', 'id' => $model->content_id, 'FrontendContentSearch[page_id]'=>$_GET['FrontendContentSearch']['page_id'],'page_name'=>$_GET['page_name']]);
// from frontend content assign
if(!empty($page_id) && !empty($page_name) && !empty($site_id)){
return $this->redirect([
'frontend/frontend-content-assign/index',
'FrontendContentAssignSearch[page_id]' => filter_var($page_id, FILTER_SANITIZE_NUMBER_INT),
'site_id'=> filter_var($site_id, FILTER_SANITIZE_NUMBER_INT),
'page_name'=> filter_var($page_name, FILTER_SANITIZE_STRING)
]);
}
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model
]);
}
}
/**
* Deletes an existing FrontendContent model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$page = FrontendContentAssign::find()->where(['content_id' => $id])->one();
Yii::$app->db->transaction( function() use ($page, $id) {
$this->findModel($id)->delete();
$page->delete();
});
return $this->redirect([
'index'
]);
}
/**
* Finds the FrontendContent model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
* @param integer $id
* @return FrontendContent the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = FrontendContent::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionReqContentEdit($id)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$fc = FrontendContent::findOne($id);
$fcArr['content_id'] = $fc->content_id;
$fcArr['theme_id'] = $fc->theme_id;
$fcArr['content_name'] = $fc->content_name;
$fcArr['content_details'] = $fc->content_details;
$fcArr['content_is_php'] = $fc->content_is_php;
$fcArr['category_id'] = $fc->category_id;
return $fcArr;
}
public function actionAssignSaveContent($id)
{
$model = $this->findModel($id);
$category = FrontendContentAssign::findOne([
'content_id' => $id,
'page_id' => $_GET['page_id']
]);
$category->category_id = Yii::$app->request->post('FrontendContent')['category_id'];
if ($model->load(Yii::$app->request->post())) {
Yii::$app->db->transaction(function ($db) use ($model, $category) {
/* save */
$category->save();
$model->save();
/* rotate record */
$cntf = FrontendContentHistory::find()->select('count(*) as cnt')
->where([
'history_content_id' => $model->content_id
])
->asArray()
->one();
if ($cntf['cnt'] > self::MAX_HISTORY) {
$max = FrontendContentHistory::find()->where([
'history_content_id' => $model->content_id
])
->orderBy('created_at ASC')
->one();
$max->delete();
}
$frontend = new FrontendContentHistory();
$frontend->history_content_id = $model->content_id;
$frontend->history_content_title = $model->content_name;
$frontend->history_content = $model->content_details;
$frontend->history_page_id = $model->page_id;
$frontend->history_category_id = $model->category_id;
$frontend->created_at = date('Y-m-d H:i:s');
$frontend->created_by = Yii::$app->user->identity->id;
$frontend->save();
});
return $this->redirect([
'frontend/frontend-content-assign/index',
'id' => $model->content_id,
'FrontendContentAssignSearch[page_id]' => $_GET['page_id'],
'page_name' => $_GET['page_name']
]);
// return $this->redirect(['index']);
}
}
public function actionAssignCopyContent()
{
$post = Yii::$app->request->post('FrontendContent');
$pageid = $post['pageid'];
$siteid = $post['siteid'];
$pagename = $post['pagename'];
$model = new FrontendContent();
$model->content_name = $post['content_name'];
$model->content_details = $post['content_details'];
$model->content_is_php = $post['content_is_php'] ?? 0;
$model->category_id = $post['category_id'];
if ($model->save() != FALSE) {
$query = new Query();
$query->select('max(content_order) as cOrder')
->from('frontend_content_assign')
->where([
'page_id' => $pageid
]);
$row = $query->one();
$modelAssign = new FrontendContentAssign();
$modelAssign->content_id = $model->content_id;
$modelAssign->page_id = $pageid;
$modelAssign->content_order = $row['cOrder'] + 1;
$modelAssign->category_id = $model->category_id;
if ($modelAssign->save() == FALSE) {
throw new \Exception('There\'s some error when processing the request');
} else {
return $this->redirect([
'frontend/frontend-content-assign/index',
'id' => $model->content_id,
'FrontendContentAssignSearch' => [
'page_id' => $pageid
],
'site_id' => $siteid,
'page_name' => $pagename
]);
}
}
}
public function actionReqContentTrans($id)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$fc = FrontendContent::findOne($id);
$cd = $fc->content_details;
preg_match_all("#\[(translate)]([^\]]*?|(?R))\[\/translate\]#", $cd, $match);
/* get all array of message */
$dsm = DmgI18nSourceMessage::find()->where([
'message' => $match[2],
'category' => 'portal'
])
->asArray()
->all();
$cArray = [];
$i = 0;
foreach ($dsm as $dvalue) {
$cArray[$i] = $dvalue['message'];
$i ++;
}
$diff = array_diff($match[2], $cArray);
if (count($diff) > 0) {
foreach ($diff as $diffvalue) {
$idsm = new DmgI18nSourceMessage();
$idsm->message = $diffvalue;
$idsm->category = "portal";
$idsm->location = "Module";
$idsm->position = "Module";
$idsm->save(false);
}
}
/* get all array of message */
$nDsm = DmgI18nSourceMessage::find()->where([
'message' => $match[2],
'category' => 'portal'
])
->asArray()
->all();
$tArray = [];
$i = 0;
foreach ($nDsm as $ndvalue) {
$tArray[$i]['tid'] = $ndvalue['id'];
$tArray[$i]['message'] = $ndvalue['message'];
$tArray[$i]['translation'] = DmgI18nMessage::translationByid($ndvalue['id']);
$i ++;
}
/* array Union */
// $merge = array_merge(
// $cArray,
// array_diff($match[2], $cArray) // 5 6
// );
return $tArray;
}
public function actionAssignSaveAjaxContent($id, $pageid)
{
$model = $this->findModel($id);
$category = FrontendContentAssign::findOne([
'content_id' => $id,
'page_id' => $pageid
]);
$category->category_id = Yii::$app->request->post('FrontendContent')['category_id'];
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->db->transaction(function ($db) use ($model, $category) {
/* save */
$category->save();
$model->save();
/* rotate record */
$cntf = FrontendContentHistory::find()->select('count(*) as cnt')
->where([
'history_content_id' => $model->content_id
])
->asArray()
->one();
if ($cntf['cnt'] > self::MAX_HISTORY) {
$max = FrontendContentHistory::find()->where([
'history_content_id' => $model->content_id
])
->orderBy('created_at ASC')
->one();
$max->delete();
}
$frontend = new FrontendContentHistory();
$frontend->history_content_id = $model->content_id;
$frontend->history_content_title = $model->content_name;
$frontend->history_content = $model->content_details;
$frontend->history_page_id = $model->page_id;
$frontend->history_category_id = $model->category_id;
$frontend->created_at = date('Y-m-d H:i:s');
$frontend->created_by = Yii::$app->user->identity->id;
$frontend->save();
});
}
}
public function actionSaveTemplate()
{
/* header */
header("X-XSS-Protection: 0");
$model = new FrontendContent();
$content = $_POST['FrontendContent']['content_details'];
$html = $this->HtmlStripper($content);
if ($model->load(Yii::$app->request->post())) {
if (! empty($html))
$model->content_details = $html;
$model->save();
Yii::$app->session->setFlash('success', 'Template Data Has Been Save');
return $this->redirect([
'design/design/design'
]);
}
}
public function actionPreview()
{}
public function actionAjaxStripper()
{
header('Content-Type: application/json');
$html = $_POST['FrontendContent']['content_details'];
$phtml = $this->HtmlStripper($html);
$data['html'] = $phtml;
$data['date'] = date('Y-m-d H:i:s');
die(json_encode($data));
}
/* general function */
public function HtmlStripper($content)
{
$c = new HtmlPage($content);
/* array of striptag */
$secstripid = [
'keditor-container',
'keditor-component-content'
];
$secstripclass = [
'keditor-ui'
];
$divstripclass = [
'ui-sortable',
'keditor-container-content',
'keditor-ui',
'btn-container-reposition'
];
$astripclass = [
'btn-container-reposition',
'keditor-ui'
];
$forbiddenclass = [
'keditor-toolbar'
];
$forbiddendata = [
'component-text'
];
/* strip each class */
foreach ($secstripid as $section) {
$c->filter('section[id*="' . $section . '"] > *')->unwrap();
}
foreach ($secstripclass as $sectionclass) {
$c->filter('section[class*="' . $sectionclass . '"] > *')->unwrap();
}
foreach ($divstripclass as $divclass) {
$c->filter('div')->removeClass($divclass);
}
foreach ($astripclass as $aclass) {
$c->filter('a')->removeClass($aclass);
}
foreach ($forbiddenclass as $forbid) {
$c->filter('div')
->filter('.' . $forbid)
->remove();
}
foreach ($forbiddendata as $data) {
$c->filter('[data-type="' . $data . '"] > *')->unwrap();
}
$c->filter('data-dynamic-href')->removeAttr('data-type');
$c->filter('[id*="keditor-container-content"]')->removeAttr('id');
$c->filter('[id*="keditor-dynamic-element"]')->remove();
$c->filter('[class*="keditor-content-area"] > *')->unwrap();
/* remove html */
$html = $c->indent()->save();
$html = strtr($html, [
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">' => '',
'<html>' => '',
'<head>' => '',
'</head>' => '',
'<body>' => '',
'</body>' => '',
'</html>' => ''
]);
return $html;
}
public function actionAssets()
{
return $this->render('assets');
}
}