use namespace

This commit is contained in:
Jean-Christian Paul Denis 2023-03-21 22:41:18 +01:00
parent 322fe47d55
commit e96f5a1039
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
3 changed files with 118 additions and 68 deletions

View File

@ -10,8 +10,32 @@
* @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;
}
require __DIR__ . '/_widgets.php'; namespace Dotclear\Plugin\postInfoWidget;
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([
'initWidgets' => [Widgets::class, 'initWidgets'],
]);
return true;
}
}

View File

@ -10,8 +10,32 @@
* @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_RC_PATH')) { declare(strict_types=1);
return null;
}
require __DIR__ . '/_widgets.php'; namespace Dotclear\Plugin\postInfoWidget;
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,24 +10,27 @@
* @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_RC_PATH')) { declare(strict_types=1);
return null;
}
dcCore::app()->addBehavior( namespace Dotclear\Plugin\postInfoWidget;
'initWidgets',
['postInfoWidget', 'adminWidget']
);
class postInfoWidget use dcCore;
use dcRecord;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
use dt;
use html;
use l10n;
class Widgets
{ {
public static function adminWidget($w) public static function initWidgets(WidgetsStack $w): void
{ {
$w $w
->create( ->create(
'postinfowidget', 'postinfowidget',
__('PostInfoWidget: entry information list'), __('PostInfoWidget: entry information list'),
['postInfoWidget', 'publicWidget'], [self::class, 'publicWidget'],
null, null,
__('Show Entry informations on a widget') __('Show Entry informations on a widget')
) )
@ -174,64 +177,64 @@ class postInfoWidget
->addOffline(); ->addOffline();
} }
public static function publicWidget($w) public static function publicWidget(WidgetsElement $w): string
{ {
if ($w->offline) { if ($w->offline) {
return null; return '';
} }
if (dcCore::app()->url->type != 'post' if (dcCore::app()->url->type != 'post'
|| !dcCore::app()->ctx->posts->post_id) { || !dcCore::app()->ctx->posts->f('post_id')) {
return null; return '';
} }
$link = '<a href="%s">%s</a>'; $link = '<a href="%s">%s</a>';
$content = ''; $content = '';
if ($w->dt_str != '') { if ($w->dt_str != '') {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'date', 'date',
dt::str( dt::str(
$w->dt_str, $w->dt_str,
strtotime(dcCore::app()->ctx->posts->post_dt), (int) strtotime(dcCore::app()->ctx->posts->f('post_dt')),
dcCore::app()->blog->settings->system->blog_timezone dcCore::app()->blog->settings->get('system')->get('blog_timezone')
) )
); );
} }
if ($w->creadt_str != '') { if ($w->creadt_str != '') {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'create', 'create',
dt::str( dt::str(
$w->creadt_str, $w->creadt_str,
strtotime(dcCore::app()->ctx->posts->post_creadt), (int) strtotime(dcCore::app()->ctx->posts->post_creadt),
dcCore::app()->blog->settings->system->blog_timezone dcCore::app()->blog->settings->get('system')->get('blog_timezone')
) )
); );
} }
if ($w->upddt_str != '') { if ($w->upddt_str != '') {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'update', 'update',
dt::str( dt::str(
$w->upddt_str, $w->upddt_str,
strtotime(dcCore::app()->ctx->posts->post_upddt), (int) strtotime(dcCore::app()->ctx->posts->f('post_upddt')),
dcCore::app()->blog->settings->system->blog_timezone dcCore::app()->blog->settings->get('system')->get('blog_timezone')
) )
); );
} }
if ($w->lang_str != '') { if ($w->lang_str != '') {
$ln = l10n::getISOcodes(); $ln = l10n::getISOcodes();
$lang_code = dcCore::app()->ctx->posts->post_lang ? $lang_code = dcCore::app()->ctx->posts->f('post_lang') ?
dcCore::app()->ctx->posts->post_lang : dcCore::app()->ctx->posts->f('post_lang') :
dcCore::app()->blog->settings->system->lang; dcCore::app()->blog->settings->get('system')->get('lang');
$lang_name = $ln[$lang_code] ?? $lang_code; $lang_name = $ln[$lang_code] ?? $lang_code;
$lang_flag = file_exists( $lang_flag = file_exists(
dirname(__FILE__) . dirname(__DIR__) .
'/img/flags/' . '/img/flags/' .
$lang_code . '.png' $lang_code . '.png'
) ? ) ?
@ -240,7 +243,7 @@ class postInfoWidget
$lang_code . '.png" alt="' . $lang_name . '" />' : $lang_code . '.png" alt="' . $lang_name . '" />' :
''; '';
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'lang', 'lang',
str_replace( str_replace(
@ -252,7 +255,7 @@ class postInfoWidget
} }
if ($w->author_str != '') { if ($w->author_str != '') {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'author', 'author',
str_replace( str_replace(
@ -263,8 +266,8 @@ class postInfoWidget
); );
} }
if ($w->category_str != '' && dcCore::app()->ctx->posts->cat_id) { if ($w->category_str != '' && dcCore::app()->ctx->posts->f('cat_id')) {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'category', 'category',
str_replace( str_replace(
@ -272,7 +275,7 @@ class postInfoWidget
sprintf( sprintf(
$link, $link,
dcCore::app()->ctx->posts->getCategoryURL(), dcCore::app()->ctx->posts->getCategoryURL(),
html::escapeHTML(dcCore::app()->ctx->posts->cat_title) html::escapeHTML(dcCore::app()->ctx->posts->f('cat_title'))
), ),
html::escapeHTML($w->category_str) html::escapeHTML($w->category_str)
) )
@ -282,20 +285,20 @@ class postInfoWidget
if ($w->tag_str != '' && dcCore::app()->plugins->moduleExists('tags')) { if ($w->tag_str != '' && dcCore::app()->plugins->moduleExists('tags')) {
$meta = dcCore::app()->meta->getMetadata([ $meta = dcCore::app()->meta->getMetadata([
'meta_type' => 'tag', 'meta_type' => 'tag',
'post_id' => dcCore::app()->ctx->posts->post_id, 'post_id' => dcCore::app()->ctx->posts->f('post_id'),
]); ]);
$metas = []; $metas = [];
while ($meta->fetch()) { while ($meta->fetch()) {
$metas[$meta->meta_id] = sprintf( $metas[$meta->f('meta_id')] = sprintf(
$link, $link,
dcCore::app()->blog->url . dcCore::app()->blog->url .
dcCore::app()->url->getBase('tag') . '/' . dcCore::app()->url->getBase('tag') . '/' .
rawurlencode($meta->meta_id), rawurlencode($meta->f('meta_id')),
$meta->meta_id $meta->f('meta_id')
); );
} }
if (!empty($metas)) { if (!empty($metas)) {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'tag', 'tag',
str_replace( str_replace(
@ -336,7 +339,7 @@ class postInfoWidget
); );
} }
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'attachment', 'attachment',
str_replace( str_replace(
@ -348,7 +351,7 @@ class postInfoWidget
} }
if ($w->comment_str != '' && dcCore::app()->ctx->posts->commentsActive()) { if ($w->comment_str != '' && dcCore::app()->ctx->posts->commentsActive()) {
$nb = dcCore::app()->ctx->posts->nb_comment; $nb = (int) dcCore::app()->ctx->posts->f('nb_comment');
if ($nb == 0) { if ($nb == 0) {
$comment_numeric = 0; $comment_numeric = 0;
$comment_textual = __('no comment'); $comment_textual = __('no comment');
@ -376,7 +379,7 @@ class postInfoWidget
); );
} }
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'comment', 'comment',
str_replace( str_replace(
@ -388,7 +391,7 @@ class postInfoWidget
} }
if ($w->trackback_str != '' && dcCore::app()->ctx->posts->trackbacksActive()) { if ($w->trackback_str != '' && dcCore::app()->ctx->posts->trackbacksActive()) {
$nb = dcCore::app()->ctx->posts->nb_trackback; $nb = (int) dcCore::app()->ctx->posts->f('nb_trackback');
if ($nb == 0) { if ($nb == 0) {
$trackback_numeric = 0; $trackback_numeric = 0;
$trackback_textual = __('no trackback'); $trackback_textual = __('no trackback');
@ -416,7 +419,7 @@ class postInfoWidget
); );
} }
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'trackback', 'trackback',
str_replace( str_replace(
@ -428,7 +431,7 @@ class postInfoWidget
} }
if ($w->permalink_str) { if ($w->permalink_str) {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'permalink', 'permalink',
str_replace( str_replace(
@ -447,7 +450,7 @@ class postInfoWidget
} }
if ($w->feed && dcCore::app()->ctx->posts->commentsActive()) { if ($w->feed && dcCore::app()->ctx->posts->commentsActive()) {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'feed', 'feed',
sprintf( sprintf(
@ -455,15 +458,14 @@ class postInfoWidget
dcCore::app()->blog->url . dcCore::app()->blog->url .
dcCore::app()->url->getBase('feed') . dcCore::app()->url->getBase('feed') .
'/atom/comments/' . '/atom/comments/' .
dcCore::app()->ctx->posts->post_id, dcCore::app()->ctx->posts->f('post_id'),
__("This post's comments feed"), __("This post's comments feed")
html::escapeHTML($w->tag_str)
) )
); );
} }
if ($w->navprevpost) { if ($w->navprevpost) {
$npp = postInfoWidget::nav( $npp = self::nav(
dcCore::app()->ctx->posts, dcCore::app()->ctx->posts,
-1, -1,
false, false,
@ -471,7 +473,7 @@ class postInfoWidget
$w->navprevpost $w->navprevpost
); );
if ($npp) { if ($npp) {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'previous', 'previous',
$npp $npp
@ -479,7 +481,7 @@ class postInfoWidget
} }
} }
if ($w->navnextpost) { if ($w->navnextpost) {
$nnp = postInfoWidget::nav( $nnp = self::nav(
dcCore::app()->ctx->posts, dcCore::app()->ctx->posts,
1, 1,
false, false,
@ -487,7 +489,7 @@ class postInfoWidget
$w->navnextpost $w->navnextpost
); );
if ($nnp) { if ($nnp) {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'next', 'next',
$nnp $nnp
@ -496,7 +498,7 @@ class postInfoWidget
} }
if ($w->navprevcat) { if ($w->navprevcat) {
$npc = postInfoWidget::nav( $npc = self::nav(
dcCore::app()->ctx->posts, dcCore::app()->ctx->posts,
-1, -1,
true, true,
@ -504,7 +506,7 @@ class postInfoWidget
$w->navprevcat $w->navprevcat
); );
if ($npc) { if ($npc) {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'previous', 'previous',
$npc $npc
@ -513,7 +515,7 @@ class postInfoWidget
} }
if ($w->navnextcat) { if ($w->navnextcat) {
$nnc = postInfoWidget::nav( $nnc = self::nav(
dcCore::app()->ctx->posts, dcCore::app()->ctx->posts,
1, 1,
true, true,
@ -521,7 +523,7 @@ class postInfoWidget
$w->navnextcat $w->navnextcat
); );
if ($nnc) { if ($nnc) {
$content .= postInfoWidget::li( $content .= self::li(
$w, $w,
'next', 'next',
$nnc $nnc
@ -533,7 +535,7 @@ class postInfoWidget
$content .= dcCore::app()->callBehavior('postInfoWidgetPublic', $w); $content .= dcCore::app()->callBehavior('postInfoWidgetPublic', $w);
if (empty($content)) { if (empty($content)) {
return null; return '';
} }
/* /*
$rmv = ''; $rmv = '';
@ -562,7 +564,7 @@ class postInfoWidget
} }
//*/ //*/
return $w->renderDiv( return $w->renderDiv(
$w->content_only, (bool) $w->content_only,
'postinfowidget ' . $w->class, 'postinfowidget ' . $w->class,
'', '',
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') .
@ -570,7 +572,7 @@ class postInfoWidget
); );
} }
public static function li($w, $i, $c) public static function li(WidgetsElement $w, string $i, string $c): string
{ {
$s = ' style="padding-left:%spx;background: transparent url(\'' . $s = ' style="padding-left:%spx;background: transparent url(\'' .
dcCore::app()->blog->getQmarkURL() . dcCore::app()->blog->getQmarkURL() .
@ -587,7 +589,7 @@ class postInfoWidget
return sprintf($l, $i, $s, $c); return sprintf($l, $i, $s, $c);
} }
public static function nav($p, $d, $r, $t, $c) public static function nav(dcRecord $p, int $d, bool $r, string $t, string $c): string
{ {
$rs = dcCore::app()->blog->getNextPost($p, $d, $r); $rs = dcCore::app()->blog->getNextPost($p, $d, $r);
if (is_null($rs)) { if (is_null($rs)) {
@ -595,7 +597,7 @@ class postInfoWidget
} }
$l = '<a href="%s" title="%s">%s</a>'; $l = '<a href="%s" title="%s">%s</a>';
$u = $rs->getURL(); $u = $rs->getURL();
$e = html::escapeHTML($rs->post_title); $e = html::escapeHTML($rs->f('post_title'));
return str_replace( return str_replace(
['%T', '%F'], ['%T', '%F'],