diff --git a/_prepend.php b/_prepend.php deleted file mode 100644 index 920c4e2..0000000 --- a/_prepend.php +++ /dev/null @@ -1,17 +0,0 @@ -autoload(['topWriter' => __DIR__ . '/inc/class.topwriter.php']); diff --git a/_public.php b/_public.php deleted file mode 100644 index 8ff9c3d..0000000 --- a/_public.php +++ /dev/null @@ -1,17 +0,0 @@ -addBehaviors([ + 'adminDashboardItemsV2' => [BackendBehaviors::class, 'adminDashboardItemsV2'], + 'adminDashboardOptionsFormV2' => [BackendBehaviors::class, 'adminDashboardOptionsFormV2'], + 'adminAfterDashboardOptionsUpdate' => [BackendBehaviors::class, 'adminAfterDashboardOptionsUpdate'], + 'initWidgets' => [Widgets::class, 'initWidgets'], + ]); + + return true; + } +} diff --git a/src/BackendBehaviors.php b/src/BackendBehaviors.php index e3e7d96..ca57b18 100644 --- a/src/BackendBehaviors.php +++ b/src/BackendBehaviors.php @@ -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('
  • %s %s (%s)
  • ', $k, $line['author'], $line['count']); + } - $li = []; - foreach ($lines as $k => $line) { - $li[] = sprintf('
  • %s %s (%s)
  • ', $k, $line['author'], $line['count']); + # Display + $__dashboard_items[0][] = '
    ' . + '

    ' . html::escapeHTML(__('Top writer: entries')) . '

    ' . + '' . + '
    '; } - - # Display - $__dashboard_items[0][] = '
    ' . - '

    ' . html::escapeHTML(__('Top writer: entries')) . '

    ' . - '' . - '
    '; } # 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('
  • %s %s (%s)
  • ', $k, $line['author'], $line['count']); + } - $li = []; - foreach ($lines as $k => $line) { - $li[] = sprintf('
  • %s %s (%s)
  • ', $k, $line['author'], $line['count']); + # Display + $__dashboard_items[0][] = '
    ' . + '

    ' . html::escapeHTML(__('Top writer: comments')) . '

    ' . + '' . + '
    '; } - - # Display - $__dashboard_items[0][] = '
    ' . - '

    ' . html::escapeHTML(__('Top writer: comments')) . '

    ' . - '' . - '
    '; } } - 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') . '

    ' . '

    ' . - form::combo('topWriterPostsPeriod', topWriter::periods(), $pref['topWriterPostsPeriod']) . '

    ' . + form::combo('topWriterPostsPeriod', Utils::periods(), $pref['topWriterPostsPeriod']) . '

    ' . '

    ' . form::number('topWriterPostsLimit', ['min' => 1, 'max' => 20, 'default' => $pref['topWriterPostsLimit']]) . '

    ' . '' . @@ -102,86 +87,86 @@ class topWriterAdmin form::checkbox('topWriterCommentsItems', 1, $pref['topWriterCommentsItems']) . ' ' . __('Show') . '

    ' . '

    ' . - form::combo('topWriterCommentsPeriod', topWriter::periods(), $pref['topWriterCommentsPeriod']) . '

    ' . + form::combo('topWriterCommentsPeriod', Utils::periods(), $pref['topWriterCommentsPeriod']) . '

    ' . '

    ' . form::number('topWriterCommentsLimit', ['min' => 1, 'max' => 20, 'default' => $pref['topWriterCommentsLimit']]) . '

    ' . ''; } - 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, ]; } } diff --git a/src/Frontend.php b/src/Frontend.php new file mode 100644 index 0000000..6e10052 --- /dev/null +++ b/src/Frontend.php @@ -0,0 +1,41 @@ +addBehaviors([ + 'initWidgets' => [Widgets::class, 'initWidgets'], + ]); + + return true; + } +} diff --git a/src/Utils.php b/src/Utils.php index f21e80b..402ad07 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -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'] = '' . $author . ''; @@ -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', diff --git a/src/Widgets.php b/src/Widgets.php index 5da16c0..4fd4d3c 100644 --- a/src/Widgets.php +++ b/src/Widgets.php @@ -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) {