use namespace

This commit is contained in:
Jean-Christian Denis 2023-03-25 21:36:05 +01:00
parent 5fc965192b
commit 592ff7dbbc
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951
3 changed files with 227 additions and 171 deletions

View file

@ -10,8 +10,32 @@
* @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);
namespace Dotclear\Plugin\lastBlogUpdate;
use dcCore;
use dcNsProcess;
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN');
return static::$init;
}
require __DIR__ . '/_widgets.php';
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'initWidgets' => [Widgets::class, 'initWidgets'],
]);
return true;
}
}

View file

@ -10,90 +10,32 @@
* @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);
require __DIR__ . '/_widgets.php';
namespace Dotclear\Plugin\lastBlogUpdate;
function lastBlogUpdateWidgetPublic($w)
use dcCore;
use dcNsProcess;
class Frontend extends dcNsProcess
{
if ($w->offline) {
return null;
public static function init(): bool
{
static::$init = true;
return static::$init;
}
# Nothing to display
if (!$w->checkHomeOnly(dcCore::app()->url->type)
|| !$w->blog_show && !$w->post_show && !$w->comment_show && !$w->media_show
|| !$w->blog_text && !$w->post_text && !$w->comment_text && !$w->media_text) {
return null;
public static function process(): bool
{
if (!static::$init) {
return false;
}
$blog = $post = $comment = $media = $addons = '';
dcCore::app()->addBehaviors([
'initWidgets' => [Widgets::class, 'initWidgets'],
]);
# Blog
if ($w->blog_show && $w->blog_text) {
$title = $w->blog_title ? sprintf('<strong>%s</strong>', html::escapeHTML($w->blog_title)) : '';
$text = dt::str($w->blog_text, dcCore::app()->blog->upddt, dcCore::app()->blog->settings->system->blog_timezone);
$blog = sprintf('<li>%s%s</li>', $title, $text);
}
# Post
if ($w->post_show && $w->post_text) {
$rs = dcCore::app()->blog->getPosts(['limit' => 1, 'no_content' => true]);
if (!$rs->isEmpty()) {
$title = $w->post_title ? sprintf('<strong>%s</strong>', html::escapeHTML($w->post_title)) : '';
$text = dt::str($w->post_text, strtotime($rs->post_upddt), dcCore::app()->blog->settings->system->blog_timezone);
$link = $rs->getURL();
$over = $rs->post_title;
$post = sprintf('<li>%s<a href="%s" title="%s">%s</a></li>', $title, $link, $over, $text);
return true;
}
}
# Comment
if ($w->comment_show && $w->comment_text) {
$rs = dcCore::app()->blog->getComments(['limit' => 1, 'no_content' => true]);
if (!$rs->isEmpty()) {
$title = $w->comment_title ? sprintf('<strong>%s</strong>', html::escapeHTML($w->comment_title)) : '';
$text = dt::str($w->comment_text, strtotime($rs->comment_upddt), dcCore::app()->blog->settings->system->blog_timezone);
$link = dcCore::app()->blog->url . dcCore::app()->getPostPublicURL($rs->post_type, html::sanitizeURL($rs->post_url)) . '#c' . $rs->comment_id;
$over = $rs->post_title;
$comment = sprintf('<li>%s<a href="%s" title="%s">%s</a></li>', $title, $link, $over, $text);
}
}
# Media
if ($w->media_show && $w->media_text) {
$rs = dcCore::app()->con->select(
'SELECT media_upddt FROM ' . dcCore::app()->prefix . dcMedia::MEDIA_TABLE_NAME . ' ' .
"WHERE media_path='" . dcCore::app()->con->escape(dcCore::app()->blog->settings->system->public_path) . "' " .
'ORDER BY media_upddt DESC ' . dcCore::app()->con->limit(1)
);
if (!$rs->isEmpty()) {
$title = $w->media_title ? sprintf('<strong>%s</strong>', html::escapeHTML($w->media_title)) : '';
$text = dt::str($w->media_text, strtotime($rs->f('media_upddt')), dcCore::app()->blog->settings->system->blog_timezone);
$media = sprintf('<li>%s%s</li>', $title, $text);
}
}
# --BEHAVIOR-- lastBlogUpdateWidgetParse
$addons = dcCore::app()->callBehavior('lastBlogUpdateWidgetParse', $w);
# Nothing to display
if (!$blog && !$post && !$comment && !$media && !$addons) {
return null;
}
# Display
return $w->renderDiv(
$w->content_only,
'lastblogupdate ' . $w->class,
'',
($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') .
sprintf('<ul>%s</ul>', $blog . $post . $comment . $media . $addons)
);
}

View file

@ -10,19 +10,26 @@
* @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', 'lastBlogUpdateWidgetAdmin');
namespace Dotclear\Plugin\lastBlogUpdate;
function lastBlogUpdateWidgetAdmin($w)
use dcCore;
use dcMedia;
use Dotclear\Helper\Html\Html;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
use dt;
class Widgets
{
public static function initWidgets(WidgetsStack $w): void
{
$w
->create(
'lastblogupdate',
__('LastBlogUpdate: dates of lastest updates'),
'lastBlogUpdateWidgetPublic',
[self::class, 'parseWidget'],
null,
'Show the dates of last updates of your blog in a widget'
)
@ -109,3 +116,86 @@ function lastBlogUpdateWidgetAdmin($w)
->addClass()
->addOffline();
}
public static function parseWidget(WidgetsElement $w): string
{
if ($w->offline) {
return '';
}
# Nothing to display
if (!$w->checkHomeOnly(dcCore::app()->url->type)
|| !$w->blog_show && !$w->post_show && !$w->comment_show && !$w->media_show
|| !$w->blog_text && !$w->post_text && !$w->comment_text && !$w->media_text) {
return '';
}
$blog = $post = $comment = $media = $addons = '';
# Blog
if ($w->blog_show && $w->blog_text) {
$title = $w->blog_title ? sprintf('<strong>%s</strong>', Html::escapeHTML($w->blog_title)) : '';
$text = dt::str($w->blog_text, (int) dcCore::app()->blog->upddt, dcCore::app()->blog->settings->get('system')->get('blog_timezone'));
$blog = sprintf('<li>%s %s</li>', $title, $text);
}
# Post
if ($w->post_show && $w->post_text) {
$rs = dcCore::app()->blog->getPosts(['limit' => 1, 'no_content' => true]);
if (!$rs->isEmpty()) {
$title = $w->post_title ? sprintf('<strong>%s</strong>', Html::escapeHTML($w->post_title)) : '';
$text = dt::str($w->post_text, (int) strtotime($rs->f('post_upddt')), dcCore::app()->blog->settings->get('system')->get('blog_timezone'));
$link = $rs->getURL();
$over = $rs->f('post_title');
$post = sprintf('<li>%s <a href="%s" title="%s">%s</a></li>', $title, $link, $over, $text);
}
}
# Comment
if ($w->comment_show && $w->comment_text) {
$rs = dcCore::app()->blog->getComments(['limit' => 1, 'no_content' => true]);
if (!$rs->isEmpty()) {
$title = $w->comment_title ? sprintf('<strong>%s</strong>', Html::escapeHTML($w->comment_title)) : '';
$text = dt::str($w->comment_text, (int) strtotime($rs->f('comment_upddt')), dcCore::app()->blog->settings->get('system')->get('blog_timezone'));
$link = dcCore::app()->blog->url . dcCore::app()->getPostPublicURL($rs->f('post_type'), Html::sanitizeURL($rs->f('post_url'))) . '#c' . $rs->f('comment_id');
$over = $rs->f('post_title');
$comment = sprintf('<li>%s <a href="%s" title="%s">%s</a></li>', $title, $link, $over, $text);
}
}
# Media
if ($w->media_show && $w->media_text) {
$rs = dcCore::app()->con->select(
'SELECT media_upddt FROM ' . dcCore::app()->prefix . dcMedia::MEDIA_TABLE_NAME . ' ' .
"WHERE media_path='" . dcCore::app()->con->escapeStr(dcCore::app()->blog->settings->get('system')->get('public_path')) . "' " .
'ORDER BY media_upddt DESC ' . dcCore::app()->con->limit(1)
);
if (!$rs->isEmpty()) {
$title = $w->media_title ? sprintf('<strong>%s</strong>', Html::escapeHTML($w->media_title)) : '';
$text = dt::str($w->media_text, (int) strtotime($rs->f('media_upddt')), dcCore::app()->blog->settings->get('system')->get('blog_timezone'));
$media = sprintf('<li>%s %s</li>', $title, $text);
}
}
# --BEHAVIOR-- lastBlogUpdateWidgetParse
$addons = dcCore::app()->callBehavior('lastBlogUpdateWidgetParse', $w);
# Nothing to display
if (!$blog && !$post && !$comment && !$media && !$addons) {
return '';
}
# Display
return $w->renderDiv(
(bool) $w->content_only,
'lastblogupdate ' . $w->class,
'',
($w->title ? $w->renderTitle(Html::escapeHTML($w->title)) : '') .
sprintf('<ul>%s</ul>', $blog . $post . $comment . $media . $addons)
);
}
}