kokjan/frontend/controllers/SiteController.php

617 lines
19 KiB
PHP
Raw Normal View History

2026-02-25 06:59:34 +00:00
<?php
namespace frontend\controllers;
use common\models\CmsEgp;
use common\models\CmsPage;
use common\models\CmsPost;
use frontend\models\ResendVerificationEmailForm;
use frontend\models\SearchForm;
use frontend\models\VerifyEmailForm;
use Yii;
use yii\base\InvalidArgumentException;
use yii\data\ActiveDataProvider;
use yii\helpers\Url;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\ContactForm;
use yii\web\Response;
use yii2fullcalendar\models\Event;
/**
* Site controller
*/
class SiteController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup'],
'rules' => [
[
'actions' => ['signup'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
if ($action->id == 'error')
$this->layout = 'blank';
return true;
} else {
return false;
}
}
2026-02-25 06:59:34 +00:00
/**
* {@inheritdoc}
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/*public function actionGreeting()
{
$this->layout = 'blank';
$response = $this->getGreeting();
//var_dump($response->id);
//die();
if (is_object($response) && isset($response->img)) {
$session = Yii::$app->session;
if ((!isset($session['greeting']['img']) || !isset($session['greeting']['background'])
|| !isset($session['greeting']['description'])
|| !isset($session['greeting']['name'])
|| !isset($session['greeting']['color'])
|| !isset($session['greeting']['font_link'])
|| !isset($session['greeting']['font_css'])
|| !isset($session['greeting']['description2'])
|| !isset($session['greeting']['btn_status1'])
)
|| $session['greeting']['img'] != $response->img
|| $session['greeting']['background'] != $response->background
|| $session['greeting']['description'] != $response->description
|| $session['greeting']['name'] != $response->name
|| $session['greeting']['color'] != $response->color
|| $session['greeting']['font_link'] != $response->font_link
|| $session['greeting']['font_css'] != $response->font_css
|| $session['greeting']['description2'] != $response->description2
|| $session['greeting']['btn_status1'] != $response->btn_status1
) {
$session['greeting'] = [
'img' => $response->img,
'background' => $response->background,
'name' => $response->name,
'description' => $response->description,
'color' => $response->color,
'font_link' => $response->font_link,
'font_css' => $response->font_css,
'description2' => $response->description2,
'btn_status1' => $response->btn_status1,
];
}
return $this->render('greeting');
}
return $this->redirect(Yii::$app->homeUrl);
}
public function getGreeting()
{
$url = 'https://eofficeth.com/frontend/web/index.php?r=greeting/index';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return json_decode($response);
}*/
/**
* Displays homepage.
*
* @return mixed
*/
public function actionIndex()
{
//$response = $this->getGreeting();
//var_dump($response->id);
//die();
/* if (is_object($response) && isset($response->img)) {
$session = Yii::$app->session;
if (!isset($session['greeting'])) {
$session['greeting'] = [
'img' => $response->img,
'background' => $response->background,
'name' => $response->name,
'description' => $response->description,
'color' => $response->color,
'font_link' => $response->font_link,
'font_css' => $response->font_css,
'description2' => $response->description2,
'btn_status1' => $response->btn_status1,
];
return $this->redirect(['greeting']);
}
}*/
$model = new SearchForm();
if ($model->load(Yii::$app->request->get())) {
}
$dataProviderNews = new ActiveDataProvider([
'query' => CmsPost::find()->where(['cms_category_id' => 1, 'status' => 1])->limit(6),
'sort' => [
'defaultOrder' => [
'publish_at' => SORT_DESC
]
],
/*
'pagination' => [
'pageSize' => 6,
'pageParam' => 'news'
]*/
'pagination' => false,
]);
$dataProviderActivity = new ActiveDataProvider([
'query' => CmsPost::find()->where(['cms_category_id' => 2, 'status' => 1])->limit(6),
'sort' => [
'defaultOrder' => [
'publish_at' => SORT_DESC
]
],
/*
'pagination' => [
'pageSize' => 6,
'pageParam' => 'activity'
]*/
'pagination' => false,
]);
$dataProviderProcurement = new ActiveDataProvider([
'query' => CmsPost::find()->where(['in', 'cms_category_id', [3, 4, 5]])->andWhere(['status' => 1])->limit(50),
'sort' => [
'defaultOrder' => [
'publish_at' => SORT_DESC
]
],
/*
'pagination' => [
'pageSize' => 50,
'pageParam' => 'procurement'
]*/
'pagination' => false,
]);
$dataProviderJob = new ActiveDataProvider([
'query' => CmsPost::find()->where(['cms_category_id' => 6, 'status' => 1])->limit(50),
'sort' => [
'defaultOrder' => [
'publish_at' => SORT_DESC
]
],
/*
'pagination' => [
'pageSize' => 50,
'pageParam' => 'jobs'
]*/
'pagination' => false,
]);
$dataProviderPlace = null;
2026-02-25 06:59:34 +00:00
$dataProviderProduct = null;
2026-02-25 06:59:34 +00:00
///dla
/*
$dataProviderDla = false;
$context = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$url = 'http://www.dla.go.th/servlet/RssServlet';
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => 'UTF-8'
]);
$data = curl_exec($curl);
curl_close($curl);
if ($data) {
$xml = @simplexml_load_string($data);
//echo '<pre />';
//print_r($xml);
//die();
$dla = json_encode($xml, JSON_UNESCAPED_UNICODE);
$dla = json_decode($dla, true);
//var_dump($dla['DOCUMENT']);
//die();
$dataProviderDla = new ArrayDataProvider([
'allModels' => $dla['DOCUMENT'],
'sort' => [
'attributes' => [
'DOCUMENT_TOPIC' => [
'asc' => ['DOCUMENT_TOPIC' => SORT_ASC, 'DOCUMENT_TOPIC' => SORT_ASC],
'desc' => ['DOCUMENT_TOPIC' => SORT_DESC, 'DOCUMENT_TOPIC' => SORT_DESC],
'default' => SORT_ASC,
],
'ORG' => [
'asc' => ['ORG' => SORT_ASC, 'ORG' => SORT_ASC],
'desc' => ['ORG' => SORT_DESC, 'ORG' => SORT_DESC],
'default' => SORT_ASC,
],
'DOCUMENT_NO' => [
'asc' => ['DOCUMENT_NO' => SORT_ASC, 'DOCUMENT_NO' => SORT_ASC],
'desc' => ['DOCUMENT_NO' => SORT_DESC, 'DOCUMENT_NO' => SORT_DESC],
'default' => SORT_ASC,
],
'DOCUMENT_DATE' => [
'asc' => ['DOCUMENT_DATE' => SORT_ASC, 'DOCUMENT_DATE' => SORT_ASC],
'desc' => ['DOCUMENT_DATE' => SORT_DESC, 'DOCUMENT_DATE' => SORT_DESC],
'default' => SORT_ASC,
],
'RECORD_DATE' => [
'asc' => ['RECORD_DATE' => SORT_ASC, 'RECORD_DATE' => SORT_ASC],
'desc' => ['RECORD_DATE' => SORT_DESC, 'RECORD_DATE' => SORT_DESC],
'default' => SORT_DESC,
],
],
'defaultOrder' => [
'RECORD_DATE' => SORT_DESC
]
],
'pagination' => [
'pageSize' => 5,
'pageParam' => 'dla'
]
]);
}//xml*/
return $this->render('index', [
'model' => $model,
'dataProviderNews' => $dataProviderNews,
'dataProviderActivity' => $dataProviderActivity,
'dataProviderProcurement' => $dataProviderProcurement,
'dataProviderJob' => $dataProviderJob,
'dataProviderPlace' => $dataProviderPlace,
'dataProviderProduct' => $dataProviderProduct,
]);
}
public function actionSearch()
{
$search = new SearchForm();
$pageDataProvider = false;
$postDataProvider = false;
if ($search->load(Yii::$app->request->get())) {
$pageDataProvider = new ActiveDataProvider([
'query' => CmsPage::find()->filterWhere(['like', 'name', $search->q])->orFilterWhere(['like', 'description', $search->q]),
'sort' => [
'defaultOrder' => [
'id' => SORT_DESC
]
]
]);
$postDataProvider = new ActiveDataProvider([
'query' => CmsPost::find()->filterWhere(['like', 'name', $search->q])->orFilterWhere(['like', 'description', $search->q]),
'sort' => [
'defaultOrder' => [
'publish_at' => SORT_DESC
]
]
]);
}
return $this->render('search', [
'search' => $search,
'pageDataProvider' => $pageDataProvider,
'postDataProvider' => $postDataProvider,
]);
}
/**
* Logs in a user.
*
* @return mixed
*/
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
} else {
$model->password = '';
return $this->render('login', [
'model' => $model,
]);
}
}
/**
* Logs out the current user.
*
* @return mixed
*/
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
/**
* Displays contact page.
*
* @return mixed
*/
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
} else {
Yii::$app->session->setFlash('error', 'There was an error sending your message.');
}
return $this->refresh();
} else {
return $this->render('contact', [
'model' => $model,
]);
}
}
/**
* Displays about page.
*
* @return mixed
*/
public function actionAbout()
{
return $this->render('about');
}
/**
* Signs user up.
*
* @return mixed
*/
/*
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post()) && $model->signup()) {
Yii::$app->session->setFlash('success', 'Thank you for registration. Please check your inbox for verification email.');
return $this->goHome();
}
return $this->render('signup', [
'model' => $model,
]);
}
*/
/**
* Requests password reset.
*
* @return mixed
*/
public function actionRequestPasswordReset()
{
$model = new PasswordResetRequestForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->sendEmail()) {
Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
return $this->goHome();
} else {
Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
}
}
return $this->render('requestPasswordResetToken', [
'model' => $model,
]);
}
/**
* Resets password.
*
* @param string $token
* @return mixed
* @throws BadRequestHttpException
*/
public function actionResetPassword($token)
{
try {
$model = new ResetPasswordForm($token);
} catch (InvalidArgumentException $e) {
throw new BadRequestHttpException($e->getMessage());
}
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
Yii::$app->session->setFlash('success', 'New password saved.');
return $this->goHome();
}
return $this->render('resetPassword', [
'model' => $model,
]);
}
/**
* Verify email address
*
* @param string $token
* @return Response
* @throws BadRequestHttpException
*/
public function actionVerifyEmail($token)
{
try {
$model = new VerifyEmailForm($token);
} catch (InvalidArgumentException $e) {
throw new BadRequestHttpException($e->getMessage());
}
if ($user = $model->verifyEmail()) {
if (Yii::$app->user->login($user)) {
Yii::$app->session->setFlash('success', 'Your email has been confirmed!');
return $this->goHome();
}
}
Yii::$app->session->setFlash('error', 'Sorry, we are unable to verify your account with provided token.');
return $this->goHome();
}
/**
* Resend verification email
*
* @return mixed
*/
public function actionResendVerificationEmail()
{
$model = new ResendVerificationEmailForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->sendEmail()) {
Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
return $this->goHome();
}
Yii::$app->session->setFlash('error', 'Sorry, we are unable to resend verification email for the provided email address.');
}
return $this->render('resendVerificationEmail', [
'model' => $model
]);
}
/**
* @param null $start
* @param null $end
* @param null $_
* @return array
*/
public function actionJsoncalendar($start = NULL, $end = NULL, $_ = NULL)
{
Yii::$app->response->format = Response::FORMAT_JSON;
$times = CmsPost::find()->all();
$events = [];
foreach ($times as $time) {
//Testing
$Event = new Event();
$Event->id = $time->id;
$Event->title = $time->name;
$Event->start = date('Y-m-d\TH:i:s\Z', strtotime($time->publish_at));
2026-02-25 06:59:34 +00:00
//$Event->end = date('Y-m-d\TH:i:s\Z', strtotime($time->date_end . ' ' . $time->time_end));
$Event->allDay = true;
$Event->url = Url::to(['post/view', 'id' => $time->id, true]);
$events[] = $Event;
}
foreach (CmsEgp::find()->orderBy(['id' => SORT_DESC])->all() as $egp) {
$Event = new Event();
$Event->id = $egp->id;
$Event->title = $egp->name;
$Event->start = date('Y-m-d\TH:i:s\Z', strtotime($egp->pub_date));
//$Event->end = date('Y-m-d\TH:i:s\Z', strtotime($time->date_end . ' ' . $time->time_end));
$Event->allDay = true;
$Event->url = $egp->link;
$events[] = $Event;
}
return $events;
}
public function actionEgp()
{
return $this->render('egp');
}
/**
* @return string
*/
public function actionFormService()
{
return $this->render('form-service');
}
public function actionFormWebsite()
{
return $this->render('form-website');
}
public function actionDesign()
{
return $this->render('design');
}
}