use namespace

This commit is contained in:
Jean-Christian Paul Denis 2023-03-21 20:35:05 +01:00
parent bad619c16f
commit 4df184ca2f
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
7 changed files with 195 additions and 160 deletions

View File

@ -1,17 +0,0 @@
<?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
*/
if (!defined('DC_RC_PATH')) {
return;
}
Clearbricks::lib()->autoload(['topWriter' => __DIR__ . '/inc/class.topwriter.php']);

View File

@ -1,17 +0,0 @@
<?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
*/
if (!defined('DC_RC_PATH')) {
return null;
}
require __DIR__ . '/_widgets.php';

44
src/Backend.php Normal file
View File

@ -0,0 +1,44 @@
<?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
*/
declare(strict_types=1);
namespace Dotclear\Plugin\topWriter;
use dcCore;
use dcNsProcess;
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN');
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'adminDashboardItemsV2' => [BackendBehaviors::class, 'adminDashboardItemsV2'],
'adminDashboardOptionsFormV2' => [BackendBehaviors::class, 'adminDashboardOptionsFormV2'],
'adminAfterDashboardOptionsUpdate' => [BackendBehaviors::class, 'adminAfterDashboardOptionsUpdate'],
'initWidgets' => [Widgets::class, 'initWidgets'],
]);
return true;
}
}

View File

@ -10,77 +10,62 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
declare(strict_types=1);
require __DIR__ . '/_widgets.php';
namespace Dotclear\Plugin\topWriter;
# Dashboard item and user preference
dcCore::app()->addBehavior(
'adminDashboardItemsV2',
['topWriterAdmin', 'adminDashboardItemsV2']
);
dcCore::app()->addBehavior(
'adminDashboardOptionsFormV2',
['topWriterAdmin', 'adminDashboardOptionsFormV2']
);
dcCore::app()->addBehavior(
'adminAfterDashboardOptionsUpdate',
['topWriterAdmin', 'adminAfterDashboardOptionsUpdate']
);
use ArrayObject;
use dcCore;
use form;
use html;
/**
* @ingroup DC_PLUGIN_TOPWRITER
* @brief Display most active users - admin methods.
* @since 2.6
*/
class topWriterAdmin
class BackendBehaviors
{
public static function adminDashboardItemsV2($__dashboard_items)
public static function adminDashboardItemsV2(ArrayObject $__dashboard_items): void
{
$pref = self::setDefaultPref();
# top posts
if ($pref['topWriterPostsItems']) {
$lines = topWriter::posts($pref['topWriterPostsPeriod'], $pref['topWriterPostsLimit']);
if (empty($lines)) {
return null;
}
$lines = Utils::posts($pref['topWriterPostsPeriod'], $pref['topWriterPostsLimit']);
if (!empty($lines)) {
$li = [];
foreach ($lines as $k => $line) {
$li[] = sprintf('<li><strong>%s</strong> %s (%s)</li>', $k, $line['author'], $line['count']);
}
$li = [];
foreach ($lines as $k => $line) {
$li[] = sprintf('<li><strong>%s</strong> %s (%s)</li>', $k, $line['author'], $line['count']);
# Display
$__dashboard_items[0][] = '<div class="box small" id="topWriterPostsItems">' .
'<h3>' . html::escapeHTML(__('Top writer: entries')) . '</h3>' .
'<ul>' . implode('', $li) . '</ul>' .
'</div>';
}
# Display
$__dashboard_items[0][] = '<div class="box small" id="topWriterPostsItems">' .
'<h3>' . html::escapeHTML(__('Top writer: entries')) . '</h3>' .
'<ul>' . implode('', $li) . '</ul>' .
'</div>';
}
# top comments
if ($pref['topWriterCommentsItems']) {
$lines = topWriter::comments($pref['topWriterCommentsPeriod'], $pref['topWriterCommentsLimit']);
if (empty($lines)) {
return null;
}
$lines = Utils::comments($pref['topWriterCommentsPeriod'], $pref['topWriterCommentsLimit']);
if (!empty($lines)) {
$li = [];
foreach ($lines as $k => $line) {
$li[] = sprintf('<li><strong>%s</strong> %s (%s)</li>', $k, $line['author'], $line['count']);
}
$li = [];
foreach ($lines as $k => $line) {
$li[] = sprintf('<li><strong>%s</strong> %s (%s)</li>', $k, $line['author'], $line['count']);
# Display
$__dashboard_items[0][] = '<div class="box small" id="topWriterCommentsItems">' .
'<h3>' . html::escapeHTML(__('Top writer: comments')) . '</h3>' .
'<ul>' . implode('', $li) . '</ul>' .
'</div>';
}
# Display
$__dashboard_items[0][] = '<div class="box small" id="topWriterCommentsItems">' .
'<h3>' . html::escapeHTML(__('Top writer: comments')) . '</h3>' .
'<ul>' . implode('', $li) . '</ul>' .
'</div>';
}
}
public static function adminDashboardOptionsFormV2()
public static function adminDashboardOptionsFormV2(): void
{
$pref = self::setDefaultPref();
@ -91,7 +76,7 @@ class topWriterAdmin
form::checkbox('topWriterPostsItems', 1, $pref['topWriterPostsItems']) . ' ' .
__('Show') . '</label></p>' .
'<p><label class="classic" for="topWriterPostsPeriod">' . __('Period:') . ' </label>' .
form::combo('topWriterPostsPeriod', topWriter::periods(), $pref['topWriterPostsPeriod']) . '</p>' .
form::combo('topWriterPostsPeriod', Utils::periods(), $pref['topWriterPostsPeriod']) . '</p>' .
'<p><label class="classic" for="topWriterPostsLimit">' . __('Limit:') . ' </label>' .
form::number('topWriterPostsLimit', ['min' => 1, 'max' => 20, 'default' => $pref['topWriterPostsLimit']]) . '</p>' .
'</div>' .
@ -102,86 +87,86 @@ class topWriterAdmin
form::checkbox('topWriterCommentsItems', 1, $pref['topWriterCommentsItems']) . ' ' .
__('Show') . '</label></p>' .
'<p><label class="classic" for="topWriterCommentsPeriod">' . __('Period:') . ' </label>' .
form::combo('topWriterCommentsPeriod', topWriter::periods(), $pref['topWriterCommentsPeriod']) . '</p>' .
form::combo('topWriterCommentsPeriod', Utils::periods(), $pref['topWriterCommentsPeriod']) . '</p>' .
'<p><label class="classic" for="topWriterCommentsLimit">' . __('Limit:') . ' </label>' .
form::number('topWriterCommentsLimit', ['min' => 1, 'max' => 20, 'default' => $pref['topWriterCommentsLimit']]) . '</p>' .
'</div>';
}
public static function adminAfterDashboardOptionsUpdate($user_id)
public static function adminAfterDashboardOptionsUpdate(?string $user_id): void
{
dcCore::app()->auth->user_prefs->dashboard->put(
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterPostsItems',
!empty($_POST['topWriterPostsItems']),
'boolean'
);
dcCore::app()->auth->user_prefs->dashboard->put(
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterPostsPeriod',
(string) $_POST['topWriterPostsPeriod'],
'string'
);
dcCore::app()->auth->user_prefs->dashboard->put(
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterPostsLimit',
(int) $_POST['topWriterPostsLimit'],
'integer'
);
dcCore::app()->auth->user_prefs->dashboard->put(
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterCommentsItems',
!empty($_POST['topWriterCommentsItems']),
'boolean'
);
dcCore::app()->auth->user_prefs->dashboard->put(
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterCommentsPeriod',
(string) $_POST['topWriterCommentsPeriod'],
'string'
);
dcCore::app()->auth->user_prefs->dashboard->put(
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterCommentsLimit',
(int) $_POST['topWriterCommentsLimit'],
'integer'
);
}
private static function setDefaultPref()
private static function setDefaultPref(): array
{
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterPostsItems')) {
dcCore::app()->auth->user_prefs->dashboard->put(
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterPostsItems')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterPostsItems',
false,
'boolean'
);
}
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterPostsPeriod')) {
dcCore::app()->auth->user_prefs->dashboard->put(
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterPostsPeriod')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterPostsPeriod',
'month',
'string'
);
}
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterPostsLimit')) {
dcCore::app()->auth->user_prefs->dashboard->put(
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterPostsLimit')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterPostsLimit',
10,
'integer'
);
}
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterCommentsItems')) {
dcCore::app()->auth->user_prefs->dashboard->put(
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterCommentsItems')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterCommentsItems',
false,
'boolean'
);
}
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterCommentsPeriod')) {
dcCore::app()->auth->user_prefs->dashboard->put(
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterCommentsPeriod')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterCommentsPeriod',
'month',
'string'
);
}
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterCommentsLimit')) {
dcCore::app()->auth->user_prefs->dashboard->put(
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterCommentsLimit')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
'topWriterCommentsLimit',
10,
'integer'
@ -189,12 +174,12 @@ class topWriterAdmin
}
return [
'topWriterPostsItems' => dcCore::app()->auth->user_prefs->dashboard->get('topWriterPostsItems'),
'topWriterPostsPeriod' => dcCore::app()->auth->user_prefs->dashboard->get('topWriterPostsPeriod'),
'topWriterPostsLimit' => dcCore::app()->auth->user_prefs->dashboard->get('topWriterPostsLimit') ?? 10,
'topWriterCommentsItems' => dcCore::app()->auth->user_prefs->dashboard->get('topWriterCommentsItems'),
'topWriterCommentsPeriod' => dcCore::app()->auth->user_prefs->dashboard->get('topWriterCommentsPeriod'),
'topWriterCommentsLimit' => dcCore::app()->auth->user_prefs->dashboard->get('topWriterCommentsLimit') ?? 10,
'topWriterPostsItems' => dcCore::app()->auth->user_prefs->get('dashboard')->get('topWriterPostsItems'),
'topWriterPostsPeriod' => dcCore::app()->auth->user_prefs->get('dashboard')->get('topWriterPostsPeriod'),
'topWriterPostsLimit' => dcCore::app()->auth->user_prefs->get('dashboard')->get('topWriterPostsLimit') ?? 10,
'topWriterCommentsItems' => dcCore::app()->auth->user_prefs->get('dashboard')->get('topWriterCommentsItems'),
'topWriterCommentsPeriod' => dcCore::app()->auth->user_prefs->get('dashboard')->get('topWriterCommentsPeriod'),
'topWriterCommentsLimit' => dcCore::app()->auth->user_prefs->get('dashboard')->get('topWriterCommentsLimit') ?? 10,
];
}
}

41
src/Frontend.php Normal file
View File

@ -0,0 +1,41 @@
<?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
*/
declare(strict_types=1);
namespace Dotclear\Plugin\topWriter;
use dcCore;
use dcNsProcess;
class Frontend extends dcNsProcess
{
public static function init(): bool
{
static::$init = true;
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'initWidgets' => [Widgets::class, 'initWidgets'],
]);
return true;
}
}

View File

@ -10,17 +10,23 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return;
}
declare(strict_types=1);
class topWriter
namespace Dotclear\Plugin\topWriter;
use dcAuth;
use dcBlog;
use dcCore;
use dcUtils;
use dt;
class Utils
{
public static function posts(string $period, int $limit, bool $sort_desc = true)
public static function posts(string $period, int $limit, bool $sort_desc = true): array
{
$req = 'SELECT COUNT(*) AS count, U.user_id ' .
'FROM ' . dcCore::app()->prefix . 'post P ' .
'INNER JOIN ' . dcCore::app()->prefix . 'user U ON U.user_id = P.user_id ' .
'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 ' .
"WHERE blog_id='" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
'AND post_status=1 AND user_status=1 ' .
self::period('post_dt', $period) .
@ -30,16 +36,14 @@ class topWriter
$rs = dcCore::app()->con->select($req);
if ($rs->isEmpty()) {
return null;
return [];
}
dcCore::app()->blog->settings->addNamespace('authormode');
$res = [];
$i = 0;
while ($rs->fetch()) {
$user = dcCore::app()->con->select(
'SELECT * FROM ' . dcCore::app()->prefix . "user WHERE user_id='" . $rs->user_id . "' "
'SELECT * FROM ' . dcCore::app()->prefix . dcAuth::USER_TABLE_NAME . " WHERE user_id='" . $rs->user_id . "' "
);
if ($user->isEmpty()) {
continue;
@ -56,7 +60,7 @@ class topWriter
}
$i++;
if (dcCore::app()->blog->settings->authormode->authormode_active) {
if (dcCore::app()->blog->settings->get('authormode')->get('authormode_active')) {
$res[$i]['author_link'] = '<a href="' .
dcCore::app()->blog->url . dcCore::app()->url->getBase('author') . '/' . $user->user_id . '" ' .
'title="' . __('Author posts') . '">' . $author . '</a>';
@ -69,21 +73,17 @@ class topWriter
if ($rs->count == 0) {
$res[$i]['count'] = __('no entries');
} else {
$res[$i]['count'] = sprintf(__('one entry', '%s entries', $rs->count), $rs->count);
$res[$i]['count'] = sprintf(__('one entry', '%s entries', (int) $rs->count), $rs->count);
}
}
if (!$i) {
return null;
}
return $res;
return $i ? $res : [];
}
public static function comments(string $period, int $limit, bool $sort_desc = true, $exclude = false)
public static function comments(string $period, int $limit, bool $sort_desc = true, bool $exclude = false): array
{
$req = 'SELECT COUNT(*) AS count, comment_email ' .
'FROM ' . dcCore::app()->prefix . 'post P, ' . dcCore::app()->prefix . 'comment C ' .
'FROM ' . dcCore::app()->prefix . dcBlog::POST_TABLE_NAME . ' P, ' . dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME . ' C ' .
'WHERE P.post_id=C.post_id ' .
"AND blog_id='" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
'AND post_status=1 AND comment_status=1 ' .
@ -92,8 +92,8 @@ class topWriter
if ($exclude) {
$req .= 'AND comment_email NOT IN (' .
' SELECT U.user_email ' .
' FROM ' . dcCore::app()->prefix . 'user U' .
' INNER JOIN ' . dcCore::app()->prefix . 'post P ON P.user_id = U.user_id ' .
' 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 ' .
" WHERE blog_id='" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
' GROUP BY U.user_email) ';
}
@ -104,14 +104,14 @@ class topWriter
$rs = dcCore::app()->con->select($req);
if ($rs->isEmpty()) {
return null;
return [];
}
$res = [];
$i = 0;
while ($rs->fetch()) {
$user = dcCore::app()->con->select(
'SELECT * FROM ' . dcCore::app()->prefix . 'comment ' .
'SELECT * FROM ' . dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME . ' ' .
"WHERE comment_email='" . $rs->comment_email . "' " .
'ORDER BY comment_dt DESC'
);
@ -131,15 +131,11 @@ class topWriter
if ($rs->count == 0) {
$res[$i]['count'] = __('no comments');
} else {
$res[$i]['count'] = sprintf(__('one comment', '%s comments', $rs->count), $rs->count);
$res[$i]['count'] = sprintf(__('one comment', '%s comments', (int) $rs->count), $rs->count);
}
}
if (!$i) {
return null;
}
return $res;
return $i ? $res : [];
}
private static function period(string $field, string $period): string
@ -174,7 +170,7 @@ class topWriter
return "AND $field > TIMESTAMP '" . dt::str($pattern, time() - $time) . "' ";
}
public static function periods()
public static function periods(): array
{
return [
__('last day') => 'day',

View File

@ -10,22 +10,25 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return null;
}
declare(strict_types=1);
dcCore::app()->addBehavior('initWidgets', ['topWriterWidget', 'init']);
namespace Dotclear\Plugin\topWriter;
class topWriterWidget
use dcCore;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
use html;
class Widgets
{
public static function init($w)
public static function initWidgets(WidgetsStack $w): void
{
#Top comments widget
$w
->create(
'topcom',
__('Top writer: comments'),
['topWriterWidget', 'topCom'],
[self::class, 'topComWidget'],
null,
__('List users who write more comments')
)
@ -41,7 +44,7 @@ class topWriterWidget
__('Period:'),
'year',
'combo',
topWriter::periods()
Utils::periods()
)
->setting(
'sort',
@ -75,7 +78,7 @@ class topWriterWidget
->create(
'toppost',
__('Top writer: entries'),
['topWriterWidget', 'topPost'],
[self::class, 'topPostWidget'],
null,
__('List users who write more posts')
)
@ -91,7 +94,7 @@ class topWriterWidget
__('Period:'),
'year',
'combo',
topWriter::periods()
Utils::periods()
)
->setting(
'sort',
@ -115,19 +118,19 @@ class topWriterWidget
->addOffline();
}
public static function topCom($w)
public static function topComWidget(WidgetsElement $w): string
{
if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type)) {
return null;
return '';
}
$lines = topWriter::comments($w->period, $w->limit, $w->sort == 'desc', $w->exclude);
$lines = Utils::comments($w->period, (int) $w->limit, $w->sort == 'desc', (bool) $w->exclude);
if (empty($lines)) {
return null;
return '';
}
return $w->renderDiv(
$w->content_only,
(bool) $w->content_only,
'topcomments ' . $w->class,
'',
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') .
@ -135,19 +138,19 @@ class topWriterWidget
);
}
public static function topPost($w)
public static function topPostWidget(WidgetsElement $w): string
{
if ($w->offline || !$w->checkHomeOnly(dcCore::app()->url->type)) {
return null;
return '';
}
$lines = topWriter::posts($w->period, $w->limit, $w->sort == 'desc');
$lines = Utils::posts($w->period, (int) $w->limit, $w->sort == 'desc');
if (empty($lines)) {
return null;
return '';
}
return $w->renderDiv(
$w->content_only,
(bool) $w->content_only,
'topentries ' . $w->class,
'',
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') .
@ -155,7 +158,7 @@ class topWriterWidget
);
}
private static function lines($lines, $id, $text)
private static function lines(array $lines, string $id, string $text): array
{
$li = [];
foreach ($lines as $k => $line) {