topWriter/src/Utils.php

184 lines
5.8 KiB
PHP
Raw Normal View History

2021-11-06 20:15:51 +00:00
<?php
/**
* @brief topWriter, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis, Pierre Van Glabeke
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-03-21 19:35:05 +00:00
declare(strict_types=1);
namespace Dotclear\Plugin\topWriter;
2021-11-06 20:15:51 +00:00
2023-03-21 19:35:05 +00:00
use dcAuth;
use dcBlog;
use dcCore;
use dcUtils;
use dt;
class Utils
2021-11-06 20:15:51 +00:00
{
2023-03-21 19:35:05 +00:00
public static function posts(string $period, int $limit, bool $sort_desc = true): array
2021-11-06 20:15:51 +00:00
{
$req = 'SELECT COUNT(*) AS count, U.user_id ' .
2023-03-21 19:35:05 +00:00
'FROM ' . dcCore::app()->prefix . dcBlog::POST_TABLE_NAME . ' P ' .
'INNER JOIN ' . dcCore::app()->prefix . dcAuth::USER_TABLE_NAME . ' U ON U.user_id = P.user_id ' .
2022-11-15 10:08:10 +00:00
"WHERE blog_id='" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
2021-11-06 20:15:51 +00:00
'AND post_status=1 AND user_status=1 ' .
self::period('post_dt', $period) .
'GROUP BY U.user_id ' .
'ORDER BY count ' . ($sort_desc ? 'DESC' : 'ASC') . ' , U.user_id ASC ' .
2022-11-15 10:08:10 +00:00
dcCore::app()->con->limit(abs((int) $limit));
2021-11-06 20:15:51 +00:00
2022-11-15 10:08:10 +00:00
$rs = dcCore::app()->con->select($req);
2021-11-06 20:15:51 +00:00
if ($rs->isEmpty()) {
2023-03-21 19:35:05 +00:00
return [];
2021-11-06 20:15:51 +00:00
}
$res = [];
$i = 0;
while ($rs->fetch()) {
2022-11-15 10:08:10 +00:00
$user = dcCore::app()->con->select(
2023-03-21 19:35:05 +00:00
'SELECT * FROM ' . dcCore::app()->prefix . dcAuth::USER_TABLE_NAME . " WHERE user_id='" . $rs->user_id . "' "
2021-11-06 20:15:51 +00:00
);
if ($user->isEmpty()) {
continue;
}
$author = dcUtils::getUserCN(
$user->user_id,
$user->user_name,
$user->user_firstname,
$user->user_displayname
);
if (empty($author)) {
continue;
}
$i++;
2023-03-21 19:35:05 +00:00
if (dcCore::app()->blog->settings->get('authormode')->get('authormode_active')) {
2021-11-06 20:15:51 +00:00
$res[$i]['author_link'] = '<a href="' .
2022-11-15 10:08:10 +00:00
dcCore::app()->blog->url . dcCore::app()->url->getBase('author') . '/' . $user->user_id . '" ' .
2021-11-06 20:15:51 +00:00
'title="' . __('Author posts') . '">' . $author . '</a>';
} elseif ($user->user_url) {
$res[$i]['author_link'] = '<a href="' . $user->user_url . '" title="' .
__('Author link') . '">' . $author . '</a>';
}
$res[$i]['author'] = $author;
if ($rs->count == 0) {
$res[$i]['count'] = __('no entries');
} else {
2023-03-21 19:35:05 +00:00
$res[$i]['count'] = sprintf(__('one entry', '%s entries', (int) $rs->count), $rs->count);
2021-11-06 20:15:51 +00:00
}
}
2023-03-21 19:35:05 +00:00
return $i ? $res : [];
2021-11-06 20:15:51 +00:00
}
2023-03-21 19:35:05 +00:00
public static function comments(string $period, int $limit, bool $sort_desc = true, bool $exclude = false): array
2021-11-06 20:15:51 +00:00
{
$req = 'SELECT COUNT(*) AS count, comment_email ' .
2023-03-21 19:35:05 +00:00
'FROM ' . dcCore::app()->prefix . dcBlog::POST_TABLE_NAME . ' P, ' . dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME . ' C ' .
2021-11-06 20:15:51 +00:00
'WHERE P.post_id=C.post_id ' .
2022-11-15 10:08:10 +00:00
"AND blog_id='" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
2021-11-06 20:15:51 +00:00
'AND post_status=1 AND comment_status=1 ' .
self::period('comment_dt', $period);
if ($exclude) {
$req .= 'AND comment_email NOT IN (' .
' SELECT U.user_email ' .
2023-03-21 19:35:05 +00:00
' FROM ' . dcCore::app()->prefix . dcAuth::USER_TABLE_NAME . ' U' .
' INNER JOIN ' . dcCore::app()->prefix . dcBlog::POST_TABLE_NAME . ' P ON P.user_id = U.user_id ' .
2022-11-15 10:08:10 +00:00
" WHERE blog_id='" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
2021-11-06 20:15:51 +00:00
' GROUP BY U.user_email) ';
}
$req .= 'GROUP BY comment_email ' .
'ORDER BY count ' . ($sort_desc ? 'DESC' : 'ASC') . ' ' .
2022-11-15 10:08:10 +00:00
dcCore::app()->con->limit(abs((int) $limit));
2021-11-06 20:15:51 +00:00
2022-11-15 10:08:10 +00:00
$rs = dcCore::app()->con->select($req);
2021-11-06 20:15:51 +00:00
if ($rs->isEmpty()) {
2023-03-21 19:35:05 +00:00
return [];
2021-11-06 20:15:51 +00:00
}
$res = [];
$i = 0;
while ($rs->fetch()) {
2022-11-15 10:08:10 +00:00
$user = dcCore::app()->con->select(
2023-03-21 19:35:05 +00:00
'SELECT * FROM ' . dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME . ' ' .
2021-11-06 20:15:51 +00:00
"WHERE comment_email='" . $rs->comment_email . "' " .
'ORDER BY comment_dt DESC'
);
if (!$user->comment_author) {
continue;
}
$i++;
if ($user->comment_site) {
$res[$i]['author_link'] = '<a href="' . $user->comment_site . '" title="' .
__('Author link') . '">' . $user->comment_author . '</a>';
}
$res[$i]['author'] = $user->comment_author;
if ($rs->count == 0) {
$res[$i]['count'] = __('no comments');
} else {
2023-03-21 19:35:05 +00:00
$res[$i]['count'] = sprintf(__('one comment', '%s comments', (int) $rs->count), $rs->count);
2021-11-06 20:15:51 +00:00
}
}
2023-03-21 19:35:05 +00:00
return $i ? $res : [];
2021-11-06 20:15:51 +00:00
}
private static function period(string $field, string $period): string
{
$pattern = '%Y-%m-%d %H:%M:%S';
$time = 0;
switch ($period) {
case 'day':
$time = 3600 * 24;
2022-11-15 10:08:10 +00:00
break;
2021-11-06 20:15:51 +00:00
case 'week':
$time = 3600 * 24 * 7;
2022-11-15 10:08:10 +00:00
break;
2021-11-06 20:15:51 +00:00
case 'month':
$time = 3600 * 24 * 30;
2022-11-15 10:08:10 +00:00
break;
2021-11-06 20:15:51 +00:00
case 'year':
$time = 3600 * 24 * 30 * 12;
2022-11-15 10:08:10 +00:00
break;
2021-11-06 20:15:51 +00:00
default:
return '';
}
return "AND $field > TIMESTAMP '" . dt::str($pattern, time() - $time) . "' ";
}
2023-03-21 19:35:05 +00:00
public static function periods(): array
2021-11-06 20:15:51 +00:00
{
return [
__('last day') => 'day',
__('last week') => 'week',
__('last month') => 'month',
__('last year') => 'year',
2022-11-15 10:08:10 +00:00
__('from begining') => '',
2021-11-06 20:15:51 +00:00
];
}
}