2026-02-25 06:59:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace common\components;
|
|
|
|
|
|
2026-02-27 02:56:17 +00:00
|
|
|
use common\models\CmsMenuFrontend;
|
2026-02-25 06:59:34 +00:00
|
|
|
use common\models\Counter;
|
|
|
|
|
use SimpleXMLElement;
|
|
|
|
|
use Yii;
|
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
use yii\helpers\Html;
|
|
|
|
|
|
|
|
|
|
class AbtComponent
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @param $content
|
|
|
|
|
* @return false|int|string
|
|
|
|
|
*/
|
|
|
|
|
/*public function getFirstImage($content)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//$first_img = '';
|
|
|
|
|
$first_img = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (empty($first_img) || !isset($matches[1][0])) {
|
|
|
|
|
$first_img = Yii::$app->params['frontendUrl'] . '/img/nopic.png';
|
|
|
|
|
} else {
|
|
|
|
|
$first_img = $matches[1][0];
|
|
|
|
|
}
|
|
|
|
|
return $first_img;
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ดึงรูปภาพแรกจาก HTML content
|
|
|
|
|
* @param string $content
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getFirstImage($content)
|
|
|
|
|
{
|
|
|
|
|
$defaultImage = Yii::$app->params['frontendUrl'] . '/img/nopic.png';
|
|
|
|
|
|
|
|
|
|
if (empty($content)) {
|
|
|
|
|
return $defaultImage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
preg_match('/<img[^>]+src=["\']([^"\']+)["\']/i', $content, $matches);
|
|
|
|
|
|
|
|
|
|
if (!empty($matches[1])) {
|
|
|
|
|
return $matches[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $defaultImage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $channel
|
|
|
|
|
* @param string $indexName
|
|
|
|
|
* @return bool|string
|
|
|
|
|
*/
|
|
|
|
|
public function getRss($channel)
|
|
|
|
|
{
|
|
|
|
|
$returnString = '';
|
|
|
|
|
try {
|
|
|
|
|
$xml = simplexml_load_file($channel);
|
|
|
|
|
|
|
|
|
|
if ($xml) {
|
|
|
|
|
$returnString .= '<ul>';
|
|
|
|
|
foreach ($xml->channel->item as $item) {
|
|
|
|
|
$returnString .= '<li><a href="' . $item->link . '" target="_blank">' . $item->title . '</a> <small>เมื่อ ' . Yii::$app->formatter->asDate(strtotime(substr($item->pubDate, 0, 16))) . '</small></li>';
|
|
|
|
|
}
|
|
|
|
|
$returnString .= '</ul>';
|
|
|
|
|
return $returnString;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return false; //$e->getMessage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $str
|
|
|
|
|
* @return bool|false|string|string[]|null
|
|
|
|
|
*/
|
|
|
|
|
public function simpleSlug($str)
|
|
|
|
|
{
|
|
|
|
|
$slug = preg_replace('@[\s!:;_\?=\\\+\*/%&#]+@', '-', $str);
|
|
|
|
|
$slug = mb_strtolower($slug, Yii::$app->charset);
|
|
|
|
|
$slug = trim($slug, '-');
|
|
|
|
|
|
|
|
|
|
return $slug;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getNew($publish_at)
|
|
|
|
|
{
|
|
|
|
|
$date1 = date_create(date('Y-m-d'));
|
|
|
|
|
$date2 = date_create($publish_at);
|
|
|
|
|
$diff = date_diff($date1, $date2);
|
|
|
|
|
|
|
|
|
|
//return $diff->format('%a');
|
|
|
|
|
if ($diff->format('%a') <= 7) {
|
|
|
|
|
return Html::img(Yii::getAlias('@web') . '/img/new.gif', ['width' => 30]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** ตัวเก็บสถิติ เหลือปีนี้กับเดือนนี้ ยัง error */
|
|
|
|
|
|
2026-02-27 05:17:24 +00:00
|
|
|
/**
|
|
|
|
|
* ตรวจสอบว่าเป็น Bot หรือ Search Engine หรือไม่
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isBot()
|
|
|
|
|
{
|
|
|
|
|
$userAgent = Yii::$app->request->userAgent;
|
|
|
|
|
if (empty($userAgent)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bots = [
|
|
|
|
|
'googlebot', 'bingbot', 'slurp', 'duckduckbot', 'baiduspider', 'yandexbot', 'sogou', 'exabot', 'facebot', 'ia_archiver',
|
|
|
|
|
'facebookexternalhit', 'twitterbot', 'rogerbot', 'linkedinbot', 'embedly', 'quora link preview', 'showyoubot', 'outbrain',
|
|
|
|
|
'pinterest/0.', 'slackbot', 'vkShare', 'W3C_Validator', 'redditbot', 'Applebot', 'WhatsApp', 'flipboard', 'tumblr',
|
|
|
|
|
'bitlybot', 'SkypeShell', 'msnbot', 'crawler', 'spider', 'robot', 'bot'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach ($bots as $bot) {
|
|
|
|
|
if (stripos($userAgent, $bot) !== false) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 06:59:34 +00:00
|
|
|
public function setCounter()
|
|
|
|
|
{
|
2026-02-27 05:17:24 +00:00
|
|
|
// ไม่นับถ้าเป็น Bot
|
|
|
|
|
if ($this->isBot()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 06:59:34 +00:00
|
|
|
$session = Yii::$app->session;
|
|
|
|
|
if (!$session['visitor'] && $session['visitor'] != $_SERVER['REMOTE_ADDR']) {
|
|
|
|
|
$session['visitor'] = $_SERVER['REMOTE_ADDR'];
|
|
|
|
|
|
|
|
|
|
$model = Counter::findOne(['date_visit' => date('Y-m-d')]);
|
|
|
|
|
if (!$model) {
|
|
|
|
|
$model = new Counter();
|
|
|
|
|
$model->date_visit = date('Y-m-d');
|
|
|
|
|
$model->visit = 1;
|
|
|
|
|
} else {
|
|
|
|
|
$model->visit = $model->visit + 1;
|
|
|
|
|
}
|
|
|
|
|
$model->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCounterToday()
|
|
|
|
|
{
|
2026-02-27 00:05:47 +00:00
|
|
|
$counter = Counter::find()->where(['date_visit' => date('Y-m-d')])->one();
|
|
|
|
|
return isset($counter) ? (int)$counter->visit : 0;
|
2026-02-25 06:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCounterYesterday()
|
|
|
|
|
{
|
2026-02-27 00:05:47 +00:00
|
|
|
$counter = Counter::find()->where(['date_visit' => date('Y-m-d', strtotime('Yesterday'))])->one();
|
|
|
|
|
return isset($counter) ? (int)$counter->visit : 0;
|
2026-02-25 06:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCounterThisMonth()
|
|
|
|
|
{
|
2026-02-27 00:05:47 +00:00
|
|
|
$firstDay = date('Y-m-01');
|
|
|
|
|
$lastDay = date('Y-m-t');
|
|
|
|
|
return (int)Counter::find()
|
|
|
|
|
->where(['between', 'date_visit', $firstDay, $lastDay])
|
|
|
|
|
->sum('visit');
|
2026-02-25 06:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCounterLastMonth()
|
|
|
|
|
{
|
2026-02-27 00:05:47 +00:00
|
|
|
$firstDay = date('Y-m-01', strtotime('-1 month'));
|
|
|
|
|
$lastDay = date('Y-m-t', strtotime('-1 month'));
|
|
|
|
|
return (int)Counter::find()
|
|
|
|
|
->where(['between', 'date_visit', $firstDay, $lastDay])
|
|
|
|
|
->sum('visit');
|
2026-02-25 06:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCounterThisYear()
|
|
|
|
|
{
|
2026-02-27 00:05:47 +00:00
|
|
|
$firstDay = date('Y-01-01');
|
|
|
|
|
$lastDay = date('Y-12-31');
|
|
|
|
|
return (int)Counter::find()
|
|
|
|
|
->where(['between', 'date_visit', $firstDay, $lastDay])
|
|
|
|
|
->sum('visit');
|
2026-02-25 06:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCounterLastYear()
|
|
|
|
|
{
|
2026-02-27 00:05:47 +00:00
|
|
|
$firstDay = date('Y-01-01', strtotime('-1 year'));
|
|
|
|
|
$lastDay = date('Y-12-31', strtotime('-1 year'));
|
|
|
|
|
return (int)Counter::find()
|
|
|
|
|
->where(['between', 'date_visit', $firstDay, $lastDay])
|
|
|
|
|
->sum('visit');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCounterTotal()
|
|
|
|
|
{
|
|
|
|
|
return (int)Counter::find()->sum('visit');
|
2026-02-25 06:59:34 +00:00
|
|
|
}
|
2026-02-27 02:56:17 +00:00
|
|
|
|
|
|
|
|
/**
|
2026-02-27 05:17:24 +00:00
|
|
|
* ดึงเมนูหน้าบ้านตามตำแหน่ง (รองรับหลายชั้น)
|
2026-02-27 02:56:17 +00:00
|
|
|
* @param string $position 'navbar' หรือ 'left_menu'
|
2026-02-27 05:17:24 +00:00
|
|
|
* @param int|null $parentId
|
2026-02-27 02:56:17 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2026-02-27 05:17:24 +00:00
|
|
|
public function getFrontendMenu($position, $parentId = null)
|
2026-02-27 02:56:17 +00:00
|
|
|
{
|
|
|
|
|
$models = CmsMenuFrontend::find()
|
2026-02-27 05:17:24 +00:00
|
|
|
->where(['position' => $position, 'status' => 1, 'parent_id' => $parentId])
|
2026-02-27 02:56:17 +00:00
|
|
|
->orderBy(['sort_order' => SORT_ASC])
|
|
|
|
|
->all();
|
|
|
|
|
|
|
|
|
|
$menu = [];
|
|
|
|
|
foreach ($models as $model) {
|
|
|
|
|
$menu[] = [
|
|
|
|
|
'name' => $model->name,
|
|
|
|
|
'link' => $model->link,
|
|
|
|
|
'icon' => $model->icon,
|
2026-02-27 05:17:24 +00:00
|
|
|
'items' => $this->getFrontendMenu($position, $model->id) // Recursive call
|
2026-02-27 02:56:17 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
return $menu;
|
|
|
|
|
}
|
2026-02-25 06:59:34 +00:00
|
|
|
}
|