use namespace

master
Jean-Christian Paul Denis 2023-03-21 22:13:14 +01:00
parent cdec9676bf
commit 31fdc55947
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
2 changed files with 107 additions and 54 deletions

78
src/Backend.php 100644
View File

@ -0,0 +1,78 @@
<?php
/**
* @brief postslistOptions, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\postslistOptions;
use ArrayObject;
use dcAuth;
use dcCore;
use dcNsProcess;
use dcPostsActions;
/**
* Improve admin class
*
* Add menu and dashboard icons, load Improve action modules.
*/
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->auth->check(
dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]),
dcCore::app()->blog->id
);
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehavior('adminPostsActions', function (dcPostsActions $pa) {
$pa->addAction(
[
__('Comments') => [
__('Mark as opened') => 'commentsopen',
__('Mark as closed') => 'commentsclose',
__('Delete all comments') => 'commentsdelete',
],
__('Trackbacks') => [
__('Mark as opened') => 'trackbacksopen',
__('Mark as closed') => 'trackbacksclose',
__('Delete all trackbacks') => 'trackbacksdelete',
],
],
function (dcPostsActions $pa, ArrayObject $post) {
$actions = [
'commentsopen',
'commentsclose',
'commentsdelete',
'trackbacksopen',
'trackbacksclose',
'trackbacksdelete',
];
if (in_array($pa->getAction(), $actions)) {
BackendBehaviors::{$pa->getAction()}($pa, $post);
}
}
);
});
return true;
}
}

View File

@ -10,65 +10,40 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
}
if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->id)) { namespace Dotclear\Plugin\postslistOptions;
return null;
}
dcCore::app()->addBehavior('adminPostsActions', function (dcPostsActions $pa) { use ArrayObject;
$pa->addAction( use dcBlog;
[ use dcCore;
__('Comments') => [ use dcPage;
__('Mark as opened') => 'commentsopen', use dcPostsActions;
__('Mark as closed') => 'commentsclose', use Exception;
__('Delete all comments') => 'commentsdelete', use form;
], use html;
__('Trackbacks') => [
__('Mark as opened') => 'trackbacksopen',
__('Mark as closed') => 'trackbacksclose',
__('Delete all trackbacks') => 'trackbacksdelete',
],
],
function (dcPostsActions $pa, ArrayObject $post) {
$actions = [
'commentsopen',
'commentsclose',
'commentsdelete',
'trackbacksopen',
'trackbacksclose',
'trackbacksdelete',
];
if (in_array($pa->getAction(), $actions)) {
behaviorsPostlistOptions::{$pa->getAction()}($pa, $post);
}
}
);
});
class behaviorsPostlistOptions class BackendBehaviors
{ {
public static function commentsOpen(dcPostsActions $pa, ArrayObject $post) public static function commentsOpen(dcPostsActions $pa, ArrayObject $post): void
{ {
foreach (self::getPostsIds($pa) as $post_id) { foreach (self::getPostsIds($pa) as $post_id) {
self::updPostOption($post_id, 'post_open_comment', 1); self::updPostOption($post_id, 'post_open_comment', 1);
} }
dcAdminNotices::addSuccessNotice(__('Comments successfully opened.')); dcPage::addSuccessNotice(__('Comments successfully opened.'));
$pa->redirect(true); $pa->redirect(true);
} }
public static function commentsClose(dcPostsActions $pa, ArrayObject $post) public static function commentsClose(dcPostsActions $pa, ArrayObject $post): void
{ {
foreach (self::getPostsIds($pa) as $post_id) { foreach (self::getPostsIds($pa) as $post_id) {
self::updPostOption($post_id, 'post_open_comment', 0); self::updPostOption($post_id, 'post_open_comment', 0);
} }
dcAdminNotices::addSuccessNotice(__('Comments successfully closed.')); dcPage::addSuccessNotice(__('Comments successfully closed.'));
$pa->redirect(true); $pa->redirect(true);
} }
public static function commentsDelete(dcPostsActions $pa, ArrayObject $post) public static function commentsDelete(dcPostsActions $pa, ArrayObject $post): void
{ {
$ids = self::getPostsIds($pa); $ids = self::getPostsIds($pa);
@ -99,30 +74,30 @@ class behaviorsPostlistOptions
self::delPostComments($post_id, false); self::delPostComments($post_id, false);
self::updPostOption($post_id, 'nb_comment', 0); self::updPostOption($post_id, 'nb_comment', 0);
} }
dcAdminNotices::addSuccessNotice(__('Comments successfully deleted.')); dcPage::addSuccessNotice(__('Comments successfully deleted.'));
$pa->redirect(true); $pa->redirect(true);
} }
} }
public static function trackbacksOpen(dcPostsActions $pa, ArrayObject $post) public static function trackbacksOpen(dcPostsActions $pa, ArrayObject $post): void
{ {
foreach (self::getPostsIds($pa) as $post_id) { foreach (self::getPostsIds($pa) as $post_id) {
self::updPostOption($post_id, 'post_open_tb', 1); self::updPostOption($post_id, 'post_open_tb', 1);
} }
dcAdminNotices::addSuccessNotice(__('Trackbacks successfully opened.')); dcPage::addSuccessNotice(__('Trackbacks successfully opened.'));
$pa->redirect(true); $pa->redirect(true);
} }
public static function trackbacksClose(dcPostsActions $pa, ArrayObject $post) public static function trackbacksClose(dcPostsActions $pa, ArrayObject $post): void
{ {
foreach (self::getPostsIds($pa) as $post_id) { foreach (self::getPostsIds($pa) as $post_id) {
self::updPostOption($post_id, 'post_open_tb', 0); self::updPostOption($post_id, 'post_open_tb', 0);
} }
dcAdminNotices::addSuccessNotice(__('Trackbacks successfully closed.')); dcPage::addSuccessNotice(__('Trackbacks successfully closed.'));
$pa->redirect(true); $pa->redirect(true);
} }
public static function trackbacksDelete(dcPostsActions $pa, ArrayObject $post) public static function trackbacksDelete(dcPostsActions $pa, ArrayObject $post): void
{ {
$ids = self::getPostsIds($pa); $ids = self::getPostsIds($pa);
@ -153,12 +128,12 @@ class behaviorsPostlistOptions
self::delPostComments($post_id, true); self::delPostComments($post_id, true);
self::updPostOption($post_id, 'nb_trackback', 0); self::updPostOption($post_id, 'nb_trackback', 0);
} }
dcAdminNotices::addSuccessNotice(__('Trackbacks successfully deleted.')); dcPage::addSuccessNotice(__('Trackbacks successfully deleted.'));
$pa->redirect(true); $pa->redirect(true);
} }
} }
private static function getPostsIds(dcPostsActions $pa) private static function getPostsIds(dcPostsActions $pa): array
{ {
$posts_ids = $pa->getIDs(); $posts_ids = $pa->getIDs();
if (empty($posts_ids)) { if (empty($posts_ids)) {
@ -168,13 +143,13 @@ class behaviorsPostlistOptions
return $posts_ids; return $posts_ids;
} }
private static function updPostOption($id, $option, $value) private static function updPostOption(int $id, string $option, int $value): void
{ {
$id = abs((int) $id); $id = abs((int) $id);
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcBlog::POST_TABLE_NAME); $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcBlog::POST_TABLE_NAME);
$cur->{$option} = $value; $cur->setField($option, $value);
$cur->post_upddt = date('Y-m-d H:i:s'); $cur->setField('post_upddt', date('Y-m-d H:i:s'));
$cur->update( $cur->update(
'WHERE post_id = ' . $id . ' ' . 'WHERE post_id = ' . $id . ' ' .
@ -183,7 +158,7 @@ class behaviorsPostlistOptions
dcCore::app()->blog->triggerBlog(); dcCore::app()->blog->triggerBlog();
} }
private static function delPostComments($id, $tb = false) private static function delPostComments(int $id, bool $tb = false): void
{ {
$params = [ $params = [
'no_content' => true, 'no_content' => true,
@ -194,7 +169,7 @@ class behaviorsPostlistOptions
while ($comments->fetch()) { while ($comments->fetch()) {
// slower but preserve behaviors // slower but preserve behaviors
dcCore::app()->blog->delComment($comments->__get('comment_id')); dcCore::app()->blog->delComment($comments->f('comment_id'));
} }
} }
} }