158 lines
4.5 KiB
PHP
158 lines
4.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: Peak
|
|
* Date: 14/1/2560
|
|
* Time: 18:00
|
|
*/
|
|
|
|
namespace common\components;
|
|
|
|
use Yii;
|
|
use DateTime;
|
|
use yii\base\Component;
|
|
use yii\helpers\Html;
|
|
|
|
class Hanuman extends Component
|
|
{
|
|
|
|
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'].'/images/social.jpg';
|
|
} else {
|
|
$first_img = $matches[1][0];
|
|
}
|
|
return $first_img;
|
|
}
|
|
|
|
public function getPostStatus($val)
|
|
{
|
|
return $val == 0 ? '<span class="badge badge-danger">ไม่เผยแพร่</span>' : '<span class="badge badge-success">เผยแพร่</span>';
|
|
}
|
|
|
|
public function getYesNo($val)
|
|
{
|
|
return $val == 0 ? '<span class="badge badge-danger">ไม่</span>' : '<span class="badge badge-success">ใช่</span>';
|
|
}
|
|
|
|
|
|
public function sendNotify($message)
|
|
{
|
|
$line_api = 'https://notify-api.line.me/api/notify';
|
|
|
|
$line_token = 'uXJFyJ0A0AGJGvfCt3rDdk9TndbnHGwvnjYJiTGimZA'; //ABT
|
|
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL,"https://notify-api.line.me/api/notify");
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, 'message='.$message);
|
|
// follow redirects
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Content-type: application/x-www-form-urlencoded',
|
|
'Authorization: Bearer '.$line_token,
|
|
]);
|
|
// receive server response ...
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$server_output = curl_exec ($ch);
|
|
|
|
curl_close ($ch);
|
|
}
|
|
|
|
public function calculateTime($times) {
|
|
$i = 0;
|
|
foreach ($times as $time) {
|
|
sscanf($time, '%d:%d', $min, $sec);
|
|
$i += $min * 60 + $sec;
|
|
}
|
|
|
|
if($m = floor($i / 60)) {
|
|
$i %= 60;
|
|
}
|
|
if($h = floor($m / 60)) {
|
|
$m %= 60;
|
|
}
|
|
|
|
return sprintf('%2d ชั่วโมง %02d นาที', $h, $m);
|
|
}
|
|
|
|
public function simpleSlug($str)
|
|
{
|
|
$slug = preg_replace('@[\s!:;_\?=\\\+\*/%&#]+@', '-', $str);
|
|
$slug = mb_strtolower($slug, Yii::$app->charset);
|
|
$slug = trim($slug, '-');
|
|
|
|
return $slug;
|
|
}
|
|
|
|
/** ตัวเก็บสถิติ เหลือปีนี้กับเดือนนี้ ยัง 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;
|
|
}
|
|
|
|
}
|