enhancePostContent/src/Widgets.php

152 lines
3.9 KiB
PHP
Raw Normal View History

2021-08-23 23:55:52 +00:00
<?php
2021-09-02 18:35:25 +00:00
/**
* @brief enhancePostContent, a plugin for Dotclear 2
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @author Jean-Christian Denis and Contributors
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-04-09 08:17:36 +00:00
declare(strict_types=1);
namespace Dotclear\Plugin\enhancePostContent;
2021-08-23 23:55:52 +00:00
use ArrayObject;
2023-04-09 08:17:36 +00:00
use dcCore;
use Dotclear\Helper\Html\Html;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
2021-08-23 23:55:52 +00:00
/**
* @ingroup DC_PLUGIN_ENHANCEPOSTCONTENT
* @brief Filter posts content - widgets methods.
* @since 2.6
*/
2023-04-09 08:17:36 +00:00
class Widgets
2021-08-23 23:55:52 +00:00
{
/**
* Admin part for widget that show extracted content
2021-11-01 09:33:43 +00:00
*
* @param WidgetsStack $w WidgetsStack instance
*/
2023-04-09 08:17:36 +00:00
public static function initWidgets(WidgetsStack $w): void
{
$w->create(
'epclist',
2023-04-09 08:17:36 +00:00
My::name(),
[self::class, 'parseWidget'],
null,
__('List filtered contents.')
);
# Title
2023-04-23 09:30:44 +00:00
$w->__get('epclist')->addTitle(__('In this article'));
# Text
2023-04-23 09:30:44 +00:00
$w->__get('epclist')->setting(
'text',
__('Description:'),
'',
'text'
);
# Type
2023-04-23 09:30:44 +00:00
$w->__get('epclist')->setting(
'type',
__('Type:'),
'Definition',
'combo',
2023-04-21 13:46:40 +00:00
Epc::getFilters()->nid(true)
);
# Content
2023-04-21 22:21:30 +00:00
foreach (Epc::widgetAllowedTemplateValue() as $name => $info) {
2023-04-23 09:30:44 +00:00
$w->__get('epclist')->setting(
2023-04-21 22:21:30 +00:00
'content' . $info['id'],
sprintf(__('Enable filter on %s'), __($name)),
1,
'check'
);
}
# Show count
2023-04-23 09:30:44 +00:00
$w->__get('epclist')->setting(
'show_total',
__('Show the number of appearance'),
1,
'check'
);
2021-10-31 22:22:34 +00:00
# widget options
2023-04-23 09:30:44 +00:00
$w->__get('epclist')
2021-10-31 22:22:34 +00:00
->addContentOnly()
->addClass()
->addOffline();
}
/**
* Public part for widget that show extracted content
2021-11-01 09:33:43 +00:00
*
* @param WidgetsElement $w WidgetsElement instance
*/
2023-04-09 08:17:36 +00:00
public static function parseWidget(WidgetsElement $w): string
{
2023-04-23 09:30:44 +00:00
if ($w->__get('offline')) {
2023-04-09 08:17:36 +00:00
return '';
2021-10-31 22:22:34 +00:00
}
# Page
2023-07-30 09:56:53 +00:00
if (!My::settings()->get('active')
2023-04-21 00:10:54 +00:00
|| !in_array(dcCore::app()->ctx?->__get('current_tpl'), ['post.html', 'page.html'])
2021-10-31 22:22:34 +00:00
) {
2023-04-09 08:17:36 +00:00
return '';
}
# Content
$content = '';
2023-04-21 22:21:30 +00:00
foreach (Epc::widgetAllowedTemplateValue() as $info) {
$ns = 'content' . $info['id'];
2023-04-23 09:30:44 +00:00
if ($w->__get($ns) && is_callable($info['cb'])) {
$content .= call_user_func(
2023-04-21 22:21:30 +00:00
$info['cb'],
$w
);
}
}
if (empty($content)) {
2023-04-09 08:17:36 +00:00
return '';
}
# Filter
2023-04-21 13:46:40 +00:00
$list = new ArrayObject();
2023-04-23 09:30:44 +00:00
$filter = Epc::getFilters()->get($w->__get('type'));
2023-04-21 13:46:40 +00:00
if (!is_null($filter)) {
$filter->widgetList($content, $w, $list);
}
if (!count($list)) {
2023-04-09 08:17:36 +00:00
return '';
}
# Parse result
$res = '';
2021-11-01 09:33:43 +00:00
foreach ($list as $line) {
2023-04-21 22:21:30 +00:00
if ((int) $line['total'] == 0) {
continue;
}
2023-04-21 22:21:30 +00:00
$res .= '<li>' . $line['replacement'] .
2023-04-23 09:30:44 +00:00
($w->__get('show_total') ? ' (' . $line['total'] . ')' : '') .
'</li>';
}
2023-04-09 08:17:36 +00:00
return empty($res) ? '' : $w->renderDiv(
2023-04-23 09:30:44 +00:00
(bool) $w->__get('content_only'),
$w->__get('class'),
'id="epc_' . $w->__get('type') . '"',
($w->__get('title') ? $w->renderTitle(Html::escapeHTML($w->__get('title'))) : '') .
($w->__get('text') ? '<p>' . Html::escapeHTML($w->__get('text')) . '</p>' : '') .
2021-08-24 21:17:43 +00:00
'<ul>' . $res . '</ul>'
);
}
2021-11-01 09:33:43 +00:00
}