| Current Path : /var/www/html/swcorpdataraya/backend/controllers/frontend/ |
| Current File : /var/www/html/swcorpdataraya/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 backend\models\i18n\DmgI18nMessage;
use backend\models\i18n\DmgI18nSourceMessage;
use backend\models\frontendcontentassign\FrontendContentAssign;
use backend\modules\mimin\components\AccessControl;
use backend\models\frontendcontenthistory\FrontendContentHistory;
use yii\web\NotFoundHttpException;
use common\components\PortalDBContent;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\db\Query;
use yii\helpers\Url;
/**
* 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'] : '';
/* update last update */
//$page = FrontendPage::findOne($pageid);
//$page->last_update = PortalDBContent::formatMicrotime();
//$page->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->redirect([
'index',
'id' => $model->content_id,
'FrontendContentSearch[page_id]' => $pageid,
'page_name' => $pagename
]); */
}
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()) {
// from frontend content assign
$pageid = filter_var($page_id, FILTER_SANITIZE_NUMBER_INT);
if (!empty($page_id) && !empty($page_name) && !empty($site_id)) {
/* update last update */
$page = FrontendPage::findOne($pageid);
$page->last_update = PortalDBContent::formatMicrotime();
if (!$page->save()) {
throw new \Exception(json_encode($page->errors));
}
}
$this->getView()->registerJs(
<<<JS
swal.fire({
title:'Saved',
icon: 'success',
text: 'Data has been saved successfully',
}).then(function(result){
window.location = '" . Url::toRoute(['index']) . "';
});
JS
);
}
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'
]);
} */
/*public function actionDelete() {
$id = $_POST['id'];
$status = 'failed';
$page = FrontendContentAssign::find()->where(['content_id' => $id])->one();
Yii::$app->db->transaction(function() use ($page, $id) {
$this->findModel($id)->delete();
$page->delete();
});
//if ($this->findModel($id)->delete())
$status = 'success';
return $status;
}*/
public function actionDelete()
{
$id = $_POST['id'];
$status = 'failed';
FrontendContentAssign::deleteAll(['content_id' => $id]);
if ($this->findModel($id)->delete())
$status = 'success';
return $status;
}
/**
* 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);
$pageid = filter_var($_GET['page_id'], FILTER_SANITIZE_NUMBER_INT);
$page_name = filter_var(Yii::$app->request->get('page_name'), FILTER_SANITIZE_SPECIAL_CHARS);
$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())) {
Yii::$app->db->transaction(function ($db) use ($model, $category, &$pageid, &$id) {
/* update last update */
$page = FrontendPage::findOne($pageid);
$page->last_update = PortalDBContent::formatMicrotime();
$page->save();
/** update all pages base on this content */
$frontend_content_assign = FrontendContentAssign::find()
->where(['content_id' => $id])
->all();
if(!empty($frontend_content_assign)) {
/** loop trough all content page_id */
foreach($frontend_content_assign as $fcs) {
$page = FrontendPage::findOne($fcs->page_id);
$page->last_update = PortalDBContent::formatMicrotime();
$page->save();
}
}
/* 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]' => $pageid,
'page_name' => $page_name
]);
}
}
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'];
$model->theme_id = $post['theme_id'] ?? 0;
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 {
/* update last update */
$page = FrontendPage::findOne($pageid);
$page->last_update = PortalDBContent::formatMicrotime();
$page->save();
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, &$pageid) {
/** update other page when one of their component is updated **/
$content_assign = FrontendContentAssign::find()
->where(['content_id' => $category->content_id])
->all();
foreach ($content_assign as $content) {
/* update last update */
$page = FrontendPage::findOne($content->page_id);
if (!empty($page)) {
$page->last_update = PortalDBContent::formatMicrotime();
$page->save();
}
}
/* 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 actionAssets()
{
return $this->render('assets');
}
}