topWriter/_admin.php

201 lines
7.4 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
*/
2015-04-25 18:37:25 +00:00
if (!defined('DC_CONTEXT_ADMIN')) {
2021-08-27 20:09:31 +00:00
return null;
2015-04-25 18:37:25 +00:00
}
2022-11-15 10:08:10 +00:00
require __DIR__ . '/_widgets.php';
2021-11-06 20:15:51 +00:00
# Dashboard item and user preference
2022-11-15 10:08:10 +00:00
dcCore::app()->addBehavior(
'adminDashboardItemsV2',
2021-11-06 20:15:51 +00:00
['topWriterAdmin', 'adminDashboardItems']
);
2022-11-15 10:08:10 +00:00
dcCore::app()->addBehavior(
'adminDashboardOptionsFormV2',
2021-11-06 20:15:51 +00:00
['topWriterAdmin', 'adminDashboardOptionsForm']
);
2022-11-15 10:08:10 +00:00
dcCore::app()->addBehavior(
2021-11-06 20:15:51 +00:00
'adminAfterDashboardOptionsUpdate',
['topWriterAdmin', 'adminAfterDashboardOptionsUpdate']
);
/**
* @ingroup DC_PLUGIN_TOPWRITER
* @brief Display most active users - admin methods.
* @since 2.6
*/
class topWriterAdmin
{
2022-11-15 10:08:10 +00:00
public static function adminDashboardItems($__dashboard_items)
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']) {
2022-11-15 10:08:10 +00:00
$lines = topWriter::posts($pref['topWriterPostsPeriod'], $pref['topWriterPostsLimit']);
2021-11-06 20:15:51 +00:00
if (empty($lines)) {
return null;
}
$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>';
}
# top comments
if ($pref['topWriterCommentsItems']) {
2022-11-15 10:08:10 +00:00
$lines = topWriter::comments($pref['topWriterCommentsPeriod'], $pref['topWriterCommentsLimit']);
2021-11-06 20:15:51 +00:00
if (empty($lines)) {
return null;
}
$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>';
}
}
2022-11-15 10:08:10 +00:00
public static function adminDashboardOptionsForm()
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>' .
form::combo('topWriterPostsPeriod', topWriter::periods(), $pref['topWriterPostsPeriod']) . '</p>' .
'<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>' .
form::combo('topWriterCommentsPeriod', topWriter::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)
{
2022-11-15 10:08:10 +00:00
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsItems',
!empty($_POST['topWriterPostsItems']),
'boolean'
);
2022-11-15 10:08:10 +00:00
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsPeriod',
(string) $_POST['topWriterPostsPeriod'],
'string'
);
2022-11-15 10:08:10 +00:00
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsLimit',
(int) $_POST['topWriterPostsLimit'],
'integer'
);
2022-11-15 10:08:10 +00:00
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsItems',
!empty($_POST['topWriterCommentsItems']),
'boolean'
);
2022-11-15 10:08:10 +00:00
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsPeriod',
(string) $_POST['topWriterCommentsPeriod'],
'string'
);
2022-11-15 10:08:10 +00:00
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsLimit',
(int) $_POST['topWriterCommentsLimit'],
'integer'
);
}
2022-11-15 10:08:10 +00:00
private static function setDefaultPref()
2021-11-06 20:15:51 +00:00
{
2022-11-15 10:08:10 +00:00
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterPostsItems')) {
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsItems',
false,
'boolean'
);
}
2022-11-15 10:08:10 +00:00
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterPostsPeriod')) {
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsPeriod',
'month',
'string'
);
}
2022-11-15 10:08:10 +00:00
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterPostsLimit')) {
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterPostsLimit',
10,
'integer'
);
}
2022-11-15 10:08:10 +00:00
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterCommentsItems')) {
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsItems',
false,
'boolean'
);
}
2022-11-15 10:08:10 +00:00
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterCommentsPeriod')) {
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsPeriod',
'month',
'string'
);
}
2022-11-15 10:08:10 +00:00
if (!dcCore::app()->auth->user_prefs->dashboard->prefExists('topWriterCommentsLimit')) {
dcCore::app()->auth->user_prefs->dashboard->put(
2021-11-06 20:15:51 +00:00
'topWriterCommentsLimit',
10,
'integer'
);
}
return [
2022-11-15 10:08:10 +00:00
'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,
2021-11-06 20:15:51 +00:00
];
}
}