saba/src/Widgets.php

221 lines
8.1 KiB
PHP
Raw Normal View History

<?php
2021-09-02 20:53:22 +00:00
/**
* @brief saba, a plugin for Dotclear 2
2022-11-17 21:41:04 +00:00
*
2021-09-02 20:53:22 +00:00
* @package Dotclear
* @subpackage Plugin
2022-11-17 21:41:04 +00:00
*
2021-09-02 20:53:22 +00:00
* @author Jean-Christian Denis and Contributors
2022-11-17 21:41:04 +00:00
*
2021-09-02 20:53:22 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return null;
}
2022-11-17 21:41:04 +00:00
dcCore::app()->addBehavior('initWidgets', ['sabaWidget', 'setWidget']);
class sabaWidget
{
public static function setWidget($w)
{
$w
->create(
'saba',
__('Advanced search'),
2022-11-17 21:41:04 +00:00
['sabaWidget', 'getWidget'],
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();
}
public static function getWidget($w)
{
2022-12-22 22:27:12 +00:00
$s = dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
2021-09-02 20:53:22 +00:00
2022-12-22 22:27:12 +00:00
if (!$s->get('active')
|| !$s->get('error') && dcCore::app()->url->type == '404'
2022-12-17 15:52:15 +00:00
|| $w->offline
) {
return;
}
2022-12-17 15:52:15 +00:00
$saba_options = dcCore::app()->ctx->saba_options ?? tplSaba::getSabaDefaultPostsOptions();
2022-12-17 16:39:16 +00:00
$res = '';
# advenced search only on search page
2022-11-17 21:41:04 +00:00
if (dcCore::app()->url->type == 'search') {
# order
if (!$w->saba_filter_orders) {
$ct = '';
2022-11-17 21:41:04 +00:00
foreach (tplSaba::getSabaFormOrders() as $k => $v) {
$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" ' : '') .
'/> ' . html::escapeHTML($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
if (!$w->saba_filter_options) {
$ct = '';
$rm = explode(',', $w->saba_remove_options);
2022-11-17 21:41:04 +00:00
foreach (tplSaba::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" ' : '') .
'/> ' . html::escapeHTML($k) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter options')) . sprintf('<ul>%s</ul>', $ct);
}
}
# ages
if (!$w->saba_filter_ages) {
$ct = '';
2022-11-17 21:41:04 +00:00
foreach (tplSaba::getSabaFormAges() as $k => $v) {
$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" ' : '') .
'/> ' . html::escapeHTML($k) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter by age')) . sprintf('<ul>%s</ul>', $ct);
}
}
# types
if (!$w->saba_filter_types) {
$ct = '';
$rm = explode(',', $w->saba_remove_types);
2022-11-17 21:41:04 +00:00
foreach (tplSaba::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" ' : '') .
'/> ' . html::escapeHTML($k) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter by type')) . sprintf('<ul>%s</ul>', $ct);
}
}
# categories
if (!$w->saba_filter_categories) {
$ct = '';
$rm = explode(',', $w->saba_remove_categories);
2022-11-17 21:41:04 +00:00
$rs = dcCore::app()->blog->getCategories();
while ($rs->fetch()) {
if (in_array($rs->cat_id, $rm) || in_array($rs->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="' .
$rs->cat_id . '" ' .
2022-12-17 15:52:15 +00:00
(in_array($rs->cat_id, $saba_options['q_cat']) ? 'checked="checked" ' : '') .
'/> ' . html::escapeHTML($rs->cat_title) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter by category')) . sprintf('<ul>%s</ul>', $ct);
}
}
# authors
if (!$w->saba_filter_authors) {
$ct = '';
$rm = explode(',', $w->saba_remove_authors);
2022-11-17 21:41:04 +00:00
$rs = dcCore::app()->blog->getPostsUsers();
while ($rs->fetch()) {
if (in_array($rs->user_id, $rm)) {
2022-11-17 21:41:04 +00:00
continue;
}
2022-11-17 21:41:04 +00:00
$ct .= '<li><label><input name="q_user[]" type="checkbox" value="' .
$rs->user_id . '" ' .
2022-12-17 15:52:15 +00:00
(in_array($rs->user_id, $saba_options['q_user']) ? 'checked="checked" ' : '') .
'/> ' . html::escapeHTML(dcUtils::getUserCN($rs->user_id, $rs->user_name, $rs->user_firstname, $rs->user_displayname)) . '</label></li>';
}
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(
$w->content_only,
$w->class,
'id="search"',
($w->title ? $w->renderTitle('<label for="q">' . html::escapeHTML($w->title) . '</label>') : '') .
'<form action="' . dcCore::app()->blog->url . '" method="get" role="search">' .
'<p><input type="text" size="10" maxlength="255" id="q" name="q" value="' .
html::escapeHTML($saba_options['q']) . '" ' .
($w->placeholder ? 'placeholder="' . html::escapeHTML($w->placeholder) . '"' : '') .
' aria-label="' . __('Search') . '"/> ' .
'<input type="submit" class="submit" value="ok" title="' . __('Search') . '" /></p>' .
$res .
'</form>'
);
}
2022-11-17 21:41:04 +00:00
}