kokjan/common/components/AbtComponent.php

183 lines
5.0 KiB
PHP
Executable File

<?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) ? (int)$counter->visit : 0;
}
public function getCounterYesterday()
{
$counter = Counter::find()->where(['date_visit' => date('Y-m-d', strtotime('Yesterday'))])->one();
return isset($counter) ? (int)$counter->visit : 0;
}
public function getCounterThisMonth()
{
$firstDay = date('Y-m-01');
$lastDay = date('Y-m-t');
return (int)Counter::find()
->where(['between', 'date_visit', $firstDay, $lastDay])
->sum('visit');
}
public function getCounterLastMonth()
{
$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');
}
public function getCounterThisYear()
{
$firstDay = date('Y-01-01');
$lastDay = date('Y-12-31');
return (int)Counter::find()
->where(['between', 'date_visit', $firstDay, $lastDay])
->sum('visit');
}
public function getCounterLastYear()
{
$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');
}
}