2021-08-23 23:55:52 +00:00
|
|
|
<?php
|
2023-10-11 19:02:43 +00:00
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Dotclear\Plugin\enhancePostContent;
|
2021-08-23 23:55:52 +00:00
|
|
|
|
2023-04-09 21:52:07 +00:00
|
|
|
use ArrayObject;
|
2023-10-07 22:35:35 +00:00
|
|
|
use Dotclear\App;
|
2023-04-09 08:17:36 +00:00
|
|
|
use Dotclear\Helper\Html\Html;
|
|
|
|
use Dotclear\Plugin\widgets\WidgetsStack;
|
|
|
|
use Dotclear\Plugin\widgets\WidgetsElement;
|
2021-08-23 23:55:52 +00:00
|
|
|
|
|
|
|
/**
|
2023-10-11 19:02:43 +00:00
|
|
|
* @brief enhancePostContent widgets class.
|
|
|
|
* @ingroup enhancePostContent
|
|
|
|
*
|
|
|
|
* @author Jean-Christian Denis
|
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
2021-08-23 23:55:52 +00:00
|
|
|
*/
|
2023-04-09 08:17:36 +00:00
|
|
|
class Widgets
|
2021-08-23 23:55:52 +00:00
|
|
|
{
|
2021-08-24 20:05:23 +00:00
|
|
|
/**
|
|
|
|
* Admin part for widget that show extracted content
|
2021-11-01 09:33:43 +00:00
|
|
|
*
|
2023-04-09 21:52:07 +00:00
|
|
|
* @param WidgetsStack $w WidgetsStack instance
|
2021-08-24 20:05:23 +00:00
|
|
|
*/
|
2023-04-09 08:17:36 +00:00
|
|
|
public static function initWidgets(WidgetsStack $w): void
|
2021-08-24 20:05:23 +00:00
|
|
|
{
|
|
|
|
$w->create(
|
|
|
|
'epclist',
|
2023-04-09 08:17:36 +00:00
|
|
|
My::name(),
|
|
|
|
[self::class, 'parseWidget'],
|
2021-08-24 20:05:23 +00:00
|
|
|
null,
|
|
|
|
__('List filtered contents.')
|
|
|
|
);
|
|
|
|
# Title
|
2023-04-23 09:30:44 +00:00
|
|
|
$w->__get('epclist')->addTitle(__('In this article'));
|
2021-08-24 20:05:23 +00:00
|
|
|
# Text
|
2023-04-23 09:30:44 +00:00
|
|
|
$w->__get('epclist')->setting(
|
2021-08-24 20:05:23 +00:00
|
|
|
'text',
|
|
|
|
__('Description:'),
|
|
|
|
'',
|
|
|
|
'text'
|
|
|
|
);
|
|
|
|
# Type
|
2023-04-23 09:30:44 +00:00
|
|
|
$w->__get('epclist')->setting(
|
2021-08-24 20:05:23 +00:00
|
|
|
'type',
|
|
|
|
__('Type:'),
|
|
|
|
'Definition',
|
|
|
|
'combo',
|
2023-04-21 13:46:40 +00:00
|
|
|
Epc::getFilters()->nid(true)
|
2021-08-24 20:05:23 +00:00
|
|
|
);
|
|
|
|
# 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)),
|
2021-08-24 20:05:23 +00:00
|
|
|
1,
|
|
|
|
'check'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
# Show count
|
2023-04-23 09:30:44 +00:00
|
|
|
$w->__get('epclist')->setting(
|
2021-08-24 20:05:23 +00:00
|
|
|
'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();
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public part for widget that show extracted content
|
2021-11-01 09:33:43 +00:00
|
|
|
*
|
2023-04-09 21:52:07 +00:00
|
|
|
* @param WidgetsElement $w WidgetsElement instance
|
2021-08-24 20:05:23 +00:00
|
|
|
*/
|
2023-04-09 08:17:36 +00:00
|
|
|
public static function parseWidget(WidgetsElement $w): string
|
2021-08-24 20:05:23 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2021-08-24 20:05:23 +00:00
|
|
|
# Page
|
2023-07-30 09:56:53 +00:00
|
|
|
if (!My::settings()->get('active')
|
2023-10-07 22:35:35 +00:00
|
|
|
|| !in_array(App::frontend()->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 '';
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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'])) {
|
2023-04-09 21:52:07 +00:00
|
|
|
$content .= call_user_func(
|
2023-04-21 22:21:30 +00:00
|
|
|
$info['cb'],
|
2023-04-09 21:52:07 +00:00
|
|
|
$w
|
2021-08-24 20:05:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($content)) {
|
2023-04-09 08:17:36 +00:00
|
|
|
return '';
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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'));
|
2021-08-24 20:05:23 +00:00
|
|
|
|
2023-04-21 13:46:40 +00:00
|
|
|
if (!is_null($filter)) {
|
|
|
|
$filter->widgetList($content, $w, $list);
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 21:52:07 +00:00
|
|
|
if (!count($list)) {
|
2023-04-09 08:17:36 +00:00
|
|
|
return '';
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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) {
|
2021-08-24 20:05:23 +00:00
|
|
|
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'] . ')' : '') .
|
2021-08-24 20:05:23 +00:00
|
|
|
'</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-08-24 20:05:23 +00:00
|
|
|
}
|
2021-11-01 09:33:43 +00:00
|
|
|
}
|