topWriter/src/BackendBehaviors.php

186 lines
7.3 KiB
PHP
Raw Normal View History

2015-04-25 18:37:25 +00:00
<?php
2021-09-02 21:22:11 +00:00
/**
* @brief topWriter, a plugin for Dotclear 2
2021-11-06 20:15:51 +00:00
*
2021-09-02 21:22:11 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-06 20:15:51 +00:00
*
2021-09-02 21:22:11 +00:00
* @author Jean-Christian Denis, Pierre Van Glabeke
2021-11-06 20:15:51 +00:00
*
2021-09-02 21:22:11 +00:00
* @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);
2015-04-25 18:37:25 +00:00
2023-03-21 19:35:05 +00:00
namespace Dotclear\Plugin\topWriter;
2021-11-06 20:15:51 +00:00
2023-03-21 19:35:05 +00:00
use ArrayObject;
use dcCore;
use form;
use html;
2021-11-06 20:15:51 +00:00
/**
* @ingroup DC_PLUGIN_TOPWRITER
* @brief Display most active users - admin methods.
* @since 2.6
*/
2023-03-21 19:35:05 +00:00
class BackendBehaviors
2021-11-06 20:15:51 +00:00
{
2023-03-21 19:35:05 +00:00
public static function adminDashboardItemsV2(ArrayObject $__dashboard_items): void
2021-11-06 20:15:51 +00:00
{
2022-11-15 10:08:10 +00:00
$pref = self::setDefaultPref();
2021-11-06 20:15:51 +00:00
# top posts
if ($pref['topWriterPostsItems']) {
2023-03-21 19:35:05 +00:00
$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']);
}
# Display
$__dashboard_items[0][] = '<div class="box small" id="topWriterPostsItems">' .
'<h3>' . html::escapeHTML(__('Top writer: entries')) . '</h3>' .
'<ul>' . implode('', $li) . '</ul>' .
'</div>';
2021-11-06 20:15:51 +00:00
}
}
# top comments
if ($pref['topWriterCommentsItems']) {
2023-03-21 19:35:05 +00:00
$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']);
}
# Display
$__dashboard_items[0][] = '<div class="box small" id="topWriterCommentsItems">' .
'<h3>' . html::escapeHTML(__('Top writer: comments')) . '</h3>' .
'<ul>' . implode('', $li) . '</ul>' .
'</div>';
2021-11-06 20:15:51 +00:00
}
}
}
2023-03-21 19:35:05 +00:00
public static function adminDashboardOptionsFormV2(): void
2021-11-06 20:15:51 +00:00
{
2022-11-15 10:08:10 +00:00
$pref = self::setDefaultPref();
2021-11-06 20:15:51 +00:00
echo
'<div class="fieldset">' .
'<h4>' . __('Top writer: entries') . '</h4>' .
'<p><label class="classic" for="topWriterPostsItems">' .
form::checkbox('topWriterPostsItems', 1, $pref['topWriterPostsItems']) . ' ' .
__('Show') . '</label></p>' .
'<p><label class="classic" for="topWriterPostsPeriod">' . __('Period:') . ' </label>' .
2023-03-21 19:35:05 +00:00
form::combo('topWriterPostsPeriod', Utils::periods(), $pref['topWriterPostsPeriod']) . '</p>' .
2021-11-06 20:15:51 +00:00
'<p><label class="classic" for="topWriterPostsLimit">' . __('Limit:') . ' </label>' .
form::number('topWriterPostsLimit', ['min' => 1, 'max' => 20, 'default' => $pref['topWriterPostsLimit']]) . '</p>' .
'</div>' .
'<div class="fieldset">' .
'<h4>' . __('Top writer: comments') . '</h4>' .
'<p><label class="classic" for="topWriterCommentsItems">' .
form::checkbox('topWriterCommentsItems', 1, $pref['topWriterCommentsItems']) . ' ' .
__('Show') . '</label></p>' .
'<p><label class="classic" for="topWriterCommentsPeriod">' . __('Period:') . ' </label>' .
2023-03-21 19:35:05 +00:00
form::combo('topWriterCommentsPeriod', Utils::periods(), $pref['topWriterCommentsPeriod']) . '</p>' .
2021-11-06 20:15:51 +00:00
'<p><label class="classic" for="topWriterCommentsLimit">' . __('Limit:') . ' </label>' .
form::number('topWriterCommentsLimit', ['min' => 1, 'max' => 20, 'default' => $pref['topWriterCommentsLimit']]) . '</p>' .
'</div>';
}
2023-03-21 19:35:05 +00:00
public static function adminAfterDashboardOptionsUpdate(?string $user_id): void
2021-11-06 20:15:51 +00:00
{
2023-03-21 19:35:05 +00:00
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsItems',
!empty($_POST['topWriterPostsItems']),
'boolean'
);
2023-03-21 19:35:05 +00:00
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsPeriod',
(string) $_POST['topWriterPostsPeriod'],
'string'
);
2023-03-21 19:35:05 +00:00
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsLimit',
(int) $_POST['topWriterPostsLimit'],
'integer'
);
2023-03-21 19:35:05 +00:00
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsItems',
!empty($_POST['topWriterCommentsItems']),
'boolean'
);
2023-03-21 19:35:05 +00:00
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsPeriod',
(string) $_POST['topWriterCommentsPeriod'],
'string'
);
2023-03-21 19:35:05 +00:00
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsLimit',
(int) $_POST['topWriterCommentsLimit'],
'integer'
);
}
2023-03-21 19:35:05 +00:00
private static function setDefaultPref(): array
2021-11-06 20:15:51 +00:00
{
2023-03-21 19:35:05 +00:00
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterPostsItems')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsItems',
false,
'boolean'
);
}
2023-03-21 19:35:05 +00:00
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterPostsPeriod')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsPeriod',
'month',
'string'
);
}
2023-03-21 19:35:05 +00:00
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterPostsLimit')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsLimit',
10,
'integer'
);
}
2023-03-21 19:35:05 +00:00
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterCommentsItems')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsItems',
false,
'boolean'
);
}
2023-03-21 19:35:05 +00:00
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterCommentsPeriod')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsPeriod',
'month',
'string'
);
}
2023-03-21 19:35:05 +00:00
if (!dcCore::app()->auth->user_prefs->get('dashboard')->prefExists('topWriterCommentsLimit')) {
dcCore::app()->auth->user_prefs->get('dashboard')->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsLimit',
10,
'integer'
);
}
return [
2023-03-21 19:35:05 +00:00
'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,
2021-11-06 20:15:51 +00:00
];
}
}