saba/src/Widgets.php

235 lines
8.8 KiB
PHP
Raw Permalink Normal View History

<?php
2023-10-20 19:11:30 +00:00
2023-04-25 16:36:48 +00:00
declare(strict_types=1);
namespace Dotclear\Plugin\saba;
2023-10-20 19:11:30 +00:00
use Dotclear\App;
2023-04-25 16:36:48 +00:00
use Dotclear\Helper\Html\Html;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
2023-10-20 19:11:30 +00:00
/**
* @brief saba widgets class.
* @ingroup saba
*
* @author Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-04-25 16:36:48 +00:00
class Widgets
{
2023-04-25 16:36:48 +00:00
/**
* Widget initialisation.
*
* @param WidgetsStack $w WidgetsStack instance
*/
public static function initWidgets(WidgetsStack $w): void
{
$w
->create(
'saba',
__('Advanced search'),
2023-10-20 19:11:30 +00:00
self::parseWidget(...),
null,
__('Add more search options on public side')
)
->addTitle(__('Search'))
->setting(
'saba_filter_types',
__('Disable filter on post types'),
0,
'check'
)
->setting('saba_remove_types', __('Hidden types:'), '')
->setting(
'saba_filter_options',
__('Disable filter on post options'),
0,
'check'
)
->setting('saba_remove_options', __('Hidden options:'), '')
->setting(
'saba_filter_categories',
__('Disable filter on categories'),
0,
'check'
)
->setting('saba_remove_categories', __('Hidden categories:'), '')
->setting(
'saba_filter_authors',
__('Disable filter on authors'),
0,
'check'
)
->setting('saba_remove_authors', __('Hidden authors:'), '')
->setting(
'saba_filter_orders',
__('Disable filter on order'),
0,
'check'
)
->setting(
'saba_filter_ages',
__('Disable filter on age'),
0,
'check'
)
->addContentOnly()
->addClass()
->addOffline();
}
2023-04-25 16:36:48 +00:00
/**
* Public part for widget
*
* @param WidgetsElement $w WidgetsElement instance
*/
public static function parseWidget(WidgetsElement $w): string
{
2023-10-20 19:11:30 +00:00
if (!App::blog()->isDefined()
|| !App::blog()->settings()->get(My::id())->get('active')
|| !App::blog()->settings()->get(My::id())->get('error') && App::url()->type == '404'
2023-04-25 16:36:48 +00:00
|| $w->__get('offline')
2022-12-17 15:52:15 +00:00
) {
2023-04-25 16:36:48 +00:00
return '';
}
2023-10-20 19:11:30 +00:00
$saba_options = App::frontend()->context()->__get('saba_options') ?? [];
2023-04-25 16:36:48 +00:00
if (!is_array($saba_options) || empty($saba_options)) {
$saba_options = Utils::getSabaDefaultPostsOptions();
}
$res = '';
2023-04-25 16:36:48 +00:00
# advanced search only on search page
2023-10-20 19:11:30 +00:00
if (App::url()->type == 'search') {
# order
2023-04-25 16:36:48 +00:00
if (!$w->__get('saba_filter_orders')) {
$ct = '';
2023-04-25 16:36:48 +00:00
foreach (Utils::getSabaFormOrders() as $k => $v) {
2022-11-17 21:41:04 +00:00
$ct .= '<li><label><input name="q_order" type="radio" value="' .
$v . '" ' .
2022-12-17 15:52:15 +00:00
($v == $saba_options['q_order'] ? 'checked="checked" ' : '') .
2023-11-04 20:34:46 +00:00
'/> ' . Html::escapeHTML((string) $k) . '</label></li>';
}
if (!empty($ct)) {
2022-11-17 21:41:04 +00:00
$ct .= '<li><label><input name="q_rev" type="checkbox" value="1" ' .
2022-12-17 15:52:15 +00:00
(!empty($saba_options['q_rev']) ? 'checked="checked" ' : '') .
'/> ' . __('Reverse order') . '</label></li>';
$res .= $w->renderTitle(__('Filter order')) . sprintf('<ul>%s</ul>', $ct);
}
}
# options
2023-04-25 16:36:48 +00:00
if (!$w->__get('saba_filter_options')) {
$ct = '';
2023-04-25 16:36:48 +00:00
$rm = explode(',', (string) $w->__get('saba_remove_options'));
2023-04-25 16:36:48 +00:00
foreach (Utils::getSabaFormOptions() as $k => $v) {
if (in_array($v, $rm)) {
continue;
}
2022-11-17 21:41:04 +00:00
$ct .= '<li><label><input name="q_opt[]" type="checkbox" value="' .
$v . '" ' .
2022-12-17 15:52:15 +00:00
(in_array($v, $saba_options['q_opt']) ? 'checked="checked" ' : '') .
2023-11-04 20:34:46 +00:00
'/> ' . Html::escapeHTML((string) $k) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter options')) . sprintf('<ul>%s</ul>', $ct);
}
}
# ages
2023-04-25 16:36:48 +00:00
if (!$w->__get('saba_filter_ages')) {
$ct = '';
2023-04-25 16:36:48 +00:00
foreach (Utils::getSabaFormAges() as $k => $v) {
2022-11-17 21:41:04 +00:00
$ct .= '<li><label><input name="q_age" type="radio" value="' .
$v . '" ' .
2022-12-17 15:52:15 +00:00
($v == $saba_options['q_age'] ? 'checked="checked" ' : '') .
2023-11-04 20:34:46 +00:00
'/> ' . Html::escapeHTML((string) $k) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter by age')) . sprintf('<ul>%s</ul>', $ct);
}
}
# types
2023-04-25 16:36:48 +00:00
if (!$w->__get('saba_filter_types')) {
$ct = '';
2023-04-25 16:36:48 +00:00
$rm = explode(',', $w->__get('saba_remove_types'));
2023-04-25 16:36:48 +00:00
foreach (Utils::getSabaFormTypes() as $k => $v) {
if (in_array($v, $rm)) {
continue;
}
2022-11-17 21:41:04 +00:00
$ct .= '<li><label><input name="q_type[]" type="checkbox" value="' .
$v . '" ' .
2022-12-17 15:52:15 +00:00
(in_array($v, $saba_options['q_type']) ? 'checked="checked" ' : '') .
2023-11-04 20:34:46 +00:00
'/> ' . Html::escapeHTML((string) $k) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter by type')) . sprintf('<ul>%s</ul>', $ct);
}
}
# categories
2023-04-25 16:36:48 +00:00
if (!$w->__get('saba_filter_categories')) {
$ct = '';
2023-04-25 16:36:48 +00:00
$rm = explode(',', $w->__get('saba_remove_categories'));
2023-10-20 19:11:30 +00:00
$rs = App::blog()->getCategories();
while ($rs->fetch()) {
2023-04-25 16:36:48 +00:00
if (in_array($rs->f('cat_id'), $rm) || in_array($rs->f('cat_url'), $rm)) {
2022-11-17 21:41:04 +00:00
continue;
}
2022-11-17 21:41:04 +00:00
$ct .= '<li><label><input name="q_cat[]" type="checkbox" value="' .
2023-04-25 16:36:48 +00:00
$rs->f('cat_id') . '" ' .
(in_array($rs->f('cat_id'), $saba_options['q_cat']) ? 'checked="checked" ' : '') .
2023-11-04 20:34:46 +00:00
'/> ' . Html::escapeHTML((string) $rs->f('cat_title')) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter by category')) . sprintf('<ul>%s</ul>', $ct);
}
}
# authors
2023-04-25 16:36:48 +00:00
if (!$w->__get('saba_filter_authors')) {
$ct = '';
2023-04-25 16:36:48 +00:00
$rm = explode(',', $w->__get('saba_remove_authors'));
2023-10-20 19:11:30 +00:00
$rs = App::blog()->getPostsUsers();
while ($rs->fetch()) {
2023-04-25 16:36:48 +00:00
if (in_array($rs->f('user_id'), $rm)) {
2022-11-17 21:41:04 +00:00
continue;
}
2023-04-25 16:36:48 +00:00
$ct .= sprintf(
'<li><label><input name="q_user[]" type="checkbox" value="%s" %s/> %s</label></li>',
$rs->f('user_id'),
in_array($rs->f('user_id'), $saba_options['q_user']) ? 'checked="checked" ' : '',
2023-10-20 19:11:30 +00:00
Html::escapeHTML(App::users()->getUserCN($rs->f('user_id'), $rs->f('user_name'), $rs->f('user_firstname'), $rs->f('user_displayname')))
2023-04-25 16:36:48 +00:00
);
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter by author')) . sprintf('<ul>%s</ul>', $ct);
}
}
}
2022-12-17 15:52:15 +00:00
return $w->renderDiv(
2023-04-25 16:36:48 +00:00
(bool) $w->__get('content_only'),
$w->__get('class'),
2022-12-17 15:52:15 +00:00
'id="search"',
2023-04-25 16:36:48 +00:00
($w->__get('title') ? $w->renderTitle('<label for="q">' . Html::escapeHTML($w->__get('title')) . '</label>') : '') .
2023-10-20 19:11:30 +00:00
'<form action="' . App::blog()->url() . '" method="get" role="search">' .
2022-12-17 15:52:15 +00:00
'<p><input type="text" size="10" maxlength="255" id="q" name="q" value="' .
2023-04-25 16:36:48 +00:00
Html::escapeHTML($saba_options['q']) . '" ' .
($w->__get('placeholder') ? 'placeholder="' . Html::escapeHTML($w->__get('placeholder')) . '"' : '') .
2022-12-17 15:52:15 +00:00
' aria-label="' . __('Search') . '"/> ' .
'<input type="submit" class="submit" value="ok" title="' . __('Search') . '" /></p>' .
$res .
'</form>'
);
}
2022-11-17 21:41:04 +00:00
}