166 lines
4.6 KiB
PHP
166 lines
4.6 KiB
PHP
<?php
|
|
|
|
|
|
namespace common\components;
|
|
|
|
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 */
|
|
|
|
public function setCounter()
|
|
{
|
|
$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()
|
|
{
|
|
$counter = Counter::find()->where(['date_visit' => date('Y-m-d')])->one();
|
|
return isset($counter) ? $counter->visit : 0;
|
|
}
|
|
|
|
public function getCounterYesterday()
|
|
{
|
|
$counter = Counter::find()->where(['date_visit' => date('Y-m-d', strtotime('Yesterday'))])->one();
|
|
return isset($counter) ? $counter->visit : 0;
|
|
}
|
|
|
|
public function getCounterThisMonth()
|
|
{
|
|
$counter = Counter::find()->where(['MONTH(date_visit)' => date('m')])->sum('visit');
|
|
return isset($counter) ? $counter : 0;
|
|
}
|
|
|
|
public function getCounterLastMonth()
|
|
{
|
|
$counter = Counter::find()->where(['MONTH(date_visit)' => date('m', strtotime('-1 month'))])->one();
|
|
return isset($counter) ? $counter->visit : 0;
|
|
}
|
|
|
|
public function getCounterThisYear()
|
|
{
|
|
$counter = Counter::find()->where(['YEAR(date_visit)' => date('Y')])->sum('visit');
|
|
return isset($counter) ? $counter : 0;
|
|
}
|
|
|
|
public function getCounterLastYear()
|
|
{
|
|
$counter = Counter::find()->where(['YEAR(date_visit)' => date('Y', strtotime('-1 year'))])->one();
|
|
return isset($counter) ? $counter->visit : 0;
|
|
}
|
|
}
|