use namespace

This commit is contained in:
Jean-Christian Denis 2023-04-25 18:36:48 +02:00
parent ddb5967403
commit 326e1a72cf
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951
11 changed files with 620 additions and 414 deletions

View file

@ -10,34 +10,67 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return;
declare(strict_types=1);
namespace Dotclear\Plugin\saba;
use dcCore;
use dcNsProcess;
use dcSettings;
use Dotclear\Helper\Html\Form\{
Checkbox,
Label,
Para
};
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN')
&& My::phpCompliant();
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
// add blog preferences form
'adminBlogPreferencesFormV2' => function (dcSettings $blog_settings): void {
echo
'<div class="fieldset">' .
'<h4 id="saba_params">' . __('Search Across Blog Archive') . '</h4>' .
// saba_active
(new Para())->items([
(new Checkbox('saba_active', (bool) $blog_settings->get(My::id())->get('active')))->value(1),
(new Label(__('Enable advanced search on this blog'), Label::OUTSIDE_LABEL_AFTER))->for('saba_active')->class('classic'),
])->render() .
// saba_error
(new Para())->items([
(new Checkbox('saba_error', (bool) $blog_settings->get(My::id())->get('error')))->value(1),
(new Label(__('Enable suggestion for page 404'), Label::OUTSIDE_LABEL_AFTER))->for('saba_error')->class('classic'),
])->render() .
'<p class="form-note">' .
__('This suggests visitors some posts on page 404.') .
'</p>' .
'</div>';
},
// save blog preference form
'adminBeforeBlogSettingsUpdate' => function (dcSettings $blog_settings): void {
$blog_settings->get(My::id())->put('active', !empty($_POST['saba_active']));
$blog_settings->get(My::id())->put('error', !empty($_POST['saba_error']));
},
// init widget
'initWidgets' => [Widgets::class, 'initWidgets'],
]);
return true;
}
}
# settings namespace
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
# widget
require __DIR__ . '/_widgets.php';
# behaviors
dcCore::app()->addBehavior('adminBlogPreferencesFormV2', function ($blog_settings) {
echo
'<div class="fieldset">' .
'<h4 id="saba_params">' . __('Search Across Blog Archive') . '</h4>' .
'<p><label class="classic">' .
form::checkbox('saba_active', '1', (bool) $blog_settings->get(basename(__DIR__))->active) .
__('Enable advanced search on this blog') . '</label></p>' .
'<p><label class="classic">' .
form::checkbox('saba_error', '1', (bool) $blog_settings->get(basename(__DIR__))->error) .
__('Enable suggestion for page 404') . '</label></p>' .
'<p class="form-note">' .
__('This suggests visitors some posts on page 404.') .
'</p>' .
'</div>';
});
dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', function ($blog_settings) {
$blog_settings->get(basename(__DIR__))->put('active', !empty($_POST['saba_active']));
$blog_settings->get(basename(__DIR__))->put('error', !empty($_POST['saba_error']));
});

View file

@ -10,342 +10,45 @@
* @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);
# setting
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
namespace Dotclear\Plugin\saba;
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('active')) {
return null;
}
use dcCore;
use dcNsProcess;
# translation
l10n::set(__DIR__ . '/locales/' . dcCore::app()->lang . '/public');
# widget
require __DIR__ . '/_widgets.php';
# template path
dcCore::app()->tpl->setPath(
dcCore::app()->tpl->getPath(),
__DIR__ . '/default-templates/'
);
# behavior
dcCore::app()->addBehavior(
'templateCustomSortByAlias',
['pubSaba', 'templateCustomSortByAlias']
);
dcCore::app()->addBehavior(
'urlHandlerBeforeGetData',
['pubSaba', 'urlHandlerBeforeGetData']
);
dcCore::app()->addBehavior(
'coreBlogBeforeGetPosts',
['pubSaba', 'coreBlogBeforeGetPosts']
);
# url
if (dcCore::app()->blog->settings->get(basename(__DIR__))->get('error')) {
dcCore::app()->url->registerError(['urlSaba', 'error']);
}
class pubSaba
class Frontend extends dcNsProcess
{
public static function templateCustomSortByAlias($alias)
public static function init(): bool
{
$alias['post'] = [
'title' => 'post_title',
'selected' => 'post_selected',
'author' => 'user_id',
'date' => 'post_dt',
'update' => 'post_upddt',
'id' => 'post_id',
'comment' => 'nb_comment',
'trackback' => 'nb_trackback',
];
static::$init = My::phpCompliant();
return static::$init;
}
public static function urlHandlerBeforeGetData($_)
public static function process(): bool
{
$options = tplSaba::getSabaDefaultPostsOptions();
if (!empty($_GET['q']) && 1 < strlen($_GET['q'])) {
# search string
$params = new ArrayObject(['search' => rawurldecode($_GET['q'])]);
$options = self::getPostsParams($params);
$options['q'] = rawurldecode($_GET['q']);
# count
dcCore::app()->public->search = rawurldecode($_GET['q']);
if (dcCore::app()->public->search) {
dcCore::app()->public->search_count = dcCore::app()->blog->getPosts($params, true)->f(0);
}
# pagintaion
$_page_number = dcCore::app()->public->getPageNumber();
if ($_page_number < 1) {
$_page_number = 1;
}
$params['limit'] = dcCore::app()->ctx->__get('nb_entry_per_page');
$params['limit'] = [(($_page_number - 1) * $params['limit']), $params['limit']];
# get posts
$posts = dcCore::app()->blog->getPosts($params);
if ($posts->isEmpty()) { // hack: don't breack context
$params = ['limit' => $params['limit']];
$posts = dcCore::app()->blog->getPosts($params);
}
dcCore::app()->ctx->__set('post_params', $params);
dcCore::app()->ctx->__set('posts', $posts);
unset($params);
}
dcCore::app()->ctx->__set('saba_options', $options);
}
public static function getPostsParams(&$params)
{
if (!isset($params['sql'])) {
$params['sql'] = '';
if (!static::$init) {
return false;
}
$params['post_type'] = [];
# retreive _GET
$qs = $_SERVER['QUERY_STRING'];
$qs = preg_replace('#(^|/)page/([0-9]+)#', '', $qs);
parse_str($qs, $get);
# search string
$options = tplSaba::getSabaDefaultPostsOptions();
$options['q'] = $params['search'];
# options
if (!empty($get['q_opt'])) {
if (in_array('selected', $get['q_opt'])) {
$options['q_opt'][] = 'selected';
$params['post_selected'] = 1;
}
if (in_array('comment', $get['q_opt'])) {
$options['q_opt'][] = 'comment';
$params['sql'] = 'AND nb_comment > 0 ';
}
if (in_array('trackback', $get['q_opt'])) {
$options['q_opt'][] = 'trackback';
$params['sql'] = 'AND nb_trackback > 0';
}
if (is_null(dcCore::app()->blog) || !dcCore::app()->blog->settings->get(My::id())->get('active')) {
return false;
}
# categories
if (!empty($get['q_cat'])) {
$cats = [];
foreach ($get['q_cat'] as $v) {
$v = abs((int) $v);
if (!$v) {
continue;
}
$cats[] = "C.cat_id = '" . $v . "'";
$options['q_cat'][] = $v;
}
if (!empty($cats)) {
$params['sql'] .= 'AND (' . implode(' OR ', $cats) . ') ';
}
if (dcCore::app()->blog->settings->get(My::id())->get('error')) {
dcCore::app()->url->registerError([UrlHandler::class, 'error']);
}
# post types
if (!empty($get['q_type'])) {
$types = dcCore::app()->getPostTypes();
foreach ($get['q_type'] as $v) {
if (!$types[$v]) {
continue;
}
$options['q_type'][] = $v;
$params['post_type'][] = $v;
}
} else {
$params['post_type'][] = 'post';
}
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), My::path() . DIRECTORY_SEPARATOR . 'default-templates');
# age
$ages = tplSaba::getSabaFormAges();
if (!empty($get['q_age']) && in_array($get['q_age'], $ages)) {
$age = explode(',', $get['q_age']);
$ts = time();
$options['q_age'] = $get['q_age'];
dcCore::app()->addBehaviors([
'templateCustomSortByAlias' => [FrontendBehaviors::class, 'templateCustomSortByAlias'],
'urlHandlerBeforeGetData' => [FrontendBehaviors::class, 'urlHandlerBeforeGetData'],
'coreBlogBeforeGetPosts' => [FrontendBehaviors::class, 'coreBlogBeforeGetPosts'],
'initWidgets' => [Widgets::class, 'initWidgets'],
]);
if ($age[0]) {
$params['sql'] .= "AND P.post_dt < '" .
dt::str('%Y-%m-%d %H:%m:%S', $ts - (int) $age[0]) . "' ";
}
if ($age[1]) {
$params['sql'] .= "AND P.post_dt > '" .
dt::str('%Y-%m-%d %H:%m:%S', $ts - (int) $age[1]) . "' ";
}
}
# user
if (!empty($get['q_user'])) {
$users = [];
foreach ($get['q_user'] as $v) {
$users[] = "U.user_id = '" . dcCore::app()->con->escape($v) . "'";
$options['q_user'][] = $v;
}
if (!empty($users)) {
$params['sql'] .= 'AND (' . implode(' OR ', $users) . ') ';
}
}
#order
$sort = 'desc';
if (!empty($get['q_rev'])) {
$options['q_rev'] = '1';
$sort = 'asc';
}
$orders = tplSaba::getSabaFormOrders();
if (!empty($get['q_order']) && in_array($get['q_order'], $orders)) {
$options['q_order'] = $get['q_order'];
$params['order'] = dcCore::app()->tpl->getSortByStr(
new ArrayObject(['sortby' => $get['q_order'], 'order' => $sort]),
'post'
); //?! post_type
}
return $options;
}
# Ajouter la condition "ou" à la recherche
public static function coreBlogBeforeGetPosts($p)
{
if (empty($p['search'])) {
return;
}
self::getPostsParams($p);
$OR = [];
# decoupe un peu plus la recherche
$splits = preg_split("#[\s//,-_]+#", $p['search']);
if (!$splits) {
$splits = explode(',', $p['search']);
}
foreach ($splits as $sentence) {
$AND = [];
$words = text::splitWords($sentence);
foreach ($words as $word) {
$AND[] = "post_words LIKE '%" . dcCore::app()->con->escape($word) . "%'";
}
if (!empty($AND)) {
$OR[] = ' (' . implode(' AND ', $AND) . ') ';
}
}
if (!empty($OR)) {
$p['search'] = '';
$p['sql'] .= 'AND (' . implode(' OR ', $OR) . ') ';
}
}
}
class urlSaba extends dcUrlHandlers
{
public static function error($args, $type, $e)
{
if ($e->getCode() == 404) {
$q = explode('/', $args);
if (empty($q)) {
return false;
}
dcCore::app()->callBehavior('sabaBeforeErrorDocument');
# Clean URI
$_GET['q'] = implode('%20', $q);
$_SERVER['QUERY_STRING'] = '';
# Claim comes from 404
$GLOBALS['_from_error'] = true;
# Serve saba
$tplset = dcCore::app()->themes->moduleInfo(dcCore::app()->blog->settings->get('system')->get('theme'), 'tplset');
self::serveDocument('saba_404_' . (!empty($tplset) && in_array($tplset, ['dotty', 'mustek']) ? $tplset : 'default') . '.html');
return true;
}
}
}
class tplSaba
{
public static function getSabaDefaultPostsOptions()
{
return [
'q' => '',
'q_opt' => [],
'q_cat' => [],
'q_age' => '0,0',
'q_user' => [],
'q_order' => 'date',
'q_rev' => '0',
'q_type' => [],
];
}
public static function getSabaFormOptions()
{
return [
__('Selected entry') => 'selected',
__('With comments') => 'comment',
__('With trackbacks') => 'trackback',
];
}
public static function getSabaFormOrders()
{
return [
__('Title') => 'title',
__('Selected entry') => 'selected',
__('Author') => 'author',
__('Date') => 'date',
__('Update') => 'update',
__('Comments count') => 'comment',
__('Trackbacks count') => 'trackback',
];
}
public static function getSabaFormAges()
{
return [
__('All') => '0,0',
__('Less than a month') => '0,2592000',
__('From 1 to 6 month') => '2592000,15552000',
__('From 6 to 12 month') => '15552000,31536000',
__('More than a year') => '31536000,0',
];
}
public static function getSabaFormTypes()
{
$know = [
'post' => __('Entry'),
'page' => __('Page'),
'pollsfactory' => __('Poll'),
'eventhandler' => __('Event'),
];
// todo: add behavior for unknow types
$rs = [];
$types = dcCore::app()->getPostTypes();
foreach ($types as $k => $v) {
if (!$v['public_url']) {
continue;
}
$rs[$know[$k] ?? __($k)] = $k;
}
return $rs;
return true;
}
}

225
src/FrontendBehaviors.php Normal file
View file

@ -0,0 +1,225 @@
<?php
/**
* @brief saba, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\saba;
use ArrayObject;
use dcCore;
use Dotclear\Helper\Date;
use Dotclear\Helper\Text;
use context;
class FrontendBehaviors
{
public static function templateCustomSortByAlias(ArrayObject $alias): void
{
$alias['post'] = [
'title' => 'post_title',
'selected' => 'post_selected',
'author' => 'user_id',
'date' => 'post_dt',
'update' => 'post_upddt',
'id' => 'post_id',
'comment' => 'nb_comment',
'trackback' => 'nb_trackback',
];
}
public static function urlHandlerBeforeGetData(context $_): void
{
if (is_null(dcCore::app()->blog) || is_null(dcCore::app()->ctx)) {
return;
}
$options = Utils::getSabaDefaultPostsOptions();
if (!empty($_GET['q']) && 1 < strlen($_GET['q'])) {
# pagintaion
$_page_number = dcCore::app()->public->getPageNumber();
if ($_page_number < 1) {
$_page_number = 1;
}
$limit = (int) dcCore::app()->ctx->__get('nb_entry_per_page');
$params = [
'limit' => [(($_page_number - 1) * $limit), $limit],
'search' => rawurldecode($_GET['q']),
];
# search string
$params = new ArrayObject($params);
$options = self::getPostsParams($params);
$options['q'] = rawurldecode($_GET['q']);
# count
dcCore::app()->public->search = rawurldecode($_GET['q']);
if (dcCore::app()->public->search) {
dcCore::app()->public->search_count = dcCore::app()->blog->getPosts($params, true)->f(0);
}
# get posts
$posts = dcCore::app()->blog->getPosts($params);
if ($posts->isEmpty()) { // hack: don't breack context
$params = ['limit' => $params['limit']];
$posts = dcCore::app()->blog->getPosts($params);
}
dcCore::app()->ctx->__set('post_params', $params);
dcCore::app()->ctx->__set('posts', $posts);
unset($params);
}
dcCore::app()->ctx->__set('saba_options', $options);
}
public static function getPostsParams(ArrayObject $params): array
{
if (!isset($params['sql'])) {
$params['sql'] = '';
}
$params['post_type'] = [];
# retreive _GET
$qs = $_SERVER['QUERY_STRING'];
$qs = preg_replace('#(^|/)page/([0-9]+)#', '', $qs);
parse_str($qs, $get);
# search string
$options = Utils::getSabaDefaultPostsOptions();
$options['q'] = $params['search'];
# options
if (!empty($get['q_opt']) && is_array($get['q_opt'])) {
if (in_array('selected', $get['q_opt'])) {
$options['q_opt'][] = 'selected';
$params['post_selected'] = 1;
}
if (in_array('comment', $get['q_opt'])) {
$options['q_opt'][] = 'comment';
$params['sql'] = 'AND nb_comment > 0 ';
}
if (in_array('trackback', $get['q_opt'])) {
$options['q_opt'][] = 'trackback';
$params['sql'] = 'AND nb_trackback > 0';
}
}
# categories
if (!empty($get['q_cat']) && is_array($get['q_cat'])) {
$cats = [];
foreach ($get['q_cat'] as $v) {
$v = abs((int) $v);
if (!$v) {
continue;
}
$cats[] = "C.cat_id = '" . $v . "'";
$options['q_cat'][] = $v;
}
if (!empty($cats)) {
$params['sql'] .= 'AND (' . implode(' OR ', $cats) . ') ';
}
}
# post types
if (!empty($get['q_type']) && is_array($get['q_type'])) {
$types = dcCore::app()->getPostTypes();
foreach ($get['q_type'] as $v) {
if (!$types[$v]) {
continue;
}
$options['q_type'][] = $v;
$params['post_type'][] = $v;
}
} else {
$params['post_type'][] = 'post';
}
# age
$ages = Utils::getSabaFormAges();
if (!empty($get['q_age']) && is_string($get['q_age']) && in_array($get['q_age'], $ages)) {
$age = explode(',', $get['q_age']);
$ts = time();
$options['q_age'] = $get['q_age'];
if ($age[0]) {
$params['sql'] .= "AND P.post_dt < '" .
Date::str('%Y-%m-%d %H:%m:%S', $ts - (int) $age[0]) . "' ";
}
if ($age[1]) {
$params['sql'] .= "AND P.post_dt > '" .
Date::str('%Y-%m-%d %H:%m:%S', $ts - (int) $age[1]) . "' ";
}
}
# user
if (!empty($get['q_user']) && is_array($get['q_user'])) {
$users = [];
foreach ($get['q_user'] as $v) {
$users[] = "U.user_id = '" . dcCore::app()->con->escapeStr((string) $v) . "'";
$options['q_user'][] = $v;
}
$params['sql'] .= 'AND (' . implode(' OR ', $users) . ') ';
}
#order
$sort = 'desc';
if (!empty($get['q_rev'])) {
$options['q_rev'] = '1';
$sort = 'asc';
}
$orders = Utils::getSabaFormOrders();
if (!empty($get['q_order']) && in_array($get['q_order'], $orders)) {
$options['q_order'] = $get['q_order'];
$params['order'] = dcCore::app()->tpl->getSortByStr(
new ArrayObject(['sortby' => $get['q_order'], 'order' => $sort]),
'post'
); //?! post_type
}
return $options;
}
# Ajouter la condition "ou" à la recherche
public static function coreBlogBeforeGetPosts(ArrayObject $p): void
{
if (empty($p['search'])) {
return;
}
self::getPostsParams($p);
$OR = [];
# decoupe un peu plus la recherche
$splits = preg_split("#[\s//,-_]+#", $p['search']);
if (!$splits) {
$splits = explode(',', $p['search']);
}
foreach ($splits as $sentence) {
$AND = [];
$words = Text::splitWords($sentence);
foreach ($words as $word) {
$AND[] = "post_words LIKE '%" . dcCore::app()->con->escapeStr((string) $word) . "%'";
}
if (!empty($AND)) {
$OR[] = ' (' . implode(' AND ', $AND) . ') ';
}
}
if (!empty($OR)) {
$p['search'] = '';
$p['sql'] .= 'AND (' . implode(' OR ', $OR) . ') ';
}
}
}

58
src/My.php Normal file
View file

@ -0,0 +1,58 @@
<?php
/**
* @brief saba, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\saba;
use dcCore;
/**
* Plugin definitions
*/
class My
{
/** @var string Required php version */
public const PHP_MIN = '7.4';
/**
* This module id
*/
public static function id(): string
{
return basename(dirname(__DIR__));
}
/**
* This module name
*/
public static function name(): string
{
return __((string) dcCore::app()->plugins->moduleInfo(self::id(), 'name'));
}
/**
* This module root path
*/
public static function path(): string
{
return dirname(__DIR__);
}
/**
* Check php version
*/
public static function phpCompliant(): bool
{
return version_compare(phpversion(), self::PHP_MIN, '>=');
}
}

View file

@ -10,20 +10,51 @@
* @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);
if (defined('ACTIVITY_REPORT_V2')) {
dcCore::app()->__get('activityReport')->addAction(
'blog',
'saba404',
__('404 error (saba)'),
__('New 404 error page at "%s"'),
'sabaBeforeErrorDocument',
function () {
$logs = [dcCore::app()->blog->url . urldecode($_SERVER['QUERY_STRING'])];
dcCore::app()->__get('activityReport')->addLog('blog', 'saba404', $logs);
namespace Dotclear\Plugin\saba;
use dcCore;
use dcNsProcess;
use Dotclear\Plugin\activityReport\{
Action,
ActivityReport,
Group
};
class Prepend extends dcNsProcess
{
public static function init(): bool
{
static::$init = My::phpCompliant();
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
);
// log frontend page 404 intercepted by saba
if (defined('ACTIVITY_REPORT') && ACTIVITY_REPORT == 3) {
$group = new Group(My::id(), My::name());
$group->add(new Action(
'saba404',
__('404 error (saba)'),
__('New 404 error page at "%s"'),
'sabaBeforeErrorDocument',
function () {
$url = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->url;
$logs = [$url . urldecode($_SERVER['QUERY_STRING'])];
ActivityReport::instance()->addLog(My::id(), 'saba404', $logs);
}
));
ActivityReport::instance()->groups->add($group);
}
return true;
}
}

47
src/UrlHandler.php Normal file
View file

@ -0,0 +1,47 @@
<?php
/**
* @brief saba, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\saba;
use dcCore;
use dcUrlHandlers;
use Exception;
class UrlHandler extends dcUrlHandlers
{
public static function error(?string $args, string $type, Exception $e): void
{
if ($e->getCode() == 404) {
$q = explode('/', (string) $args);
if (count($q) < 2) {
return;
}
dcCore::app()->callBehavior('sabaBeforeErrorDocument');
# Clean URI
$_GET['q'] = implode('%20', $q);
$_SERVER['QUERY_STRING'] = '';
# Claim comes from 404
$GLOBALS['_from_error'] = true;
# Serve saba
self::serveDocument('saba_404.html');
# stop here
exit(1);
}
}
}

90
src/Utils.php Normal file
View file

@ -0,0 +1,90 @@
<?php
/**
* @brief saba, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\saba;
use dcCore;
class Utils
{
public static function getSabaDefaultPostsOptions(): array
{
return [
'q' => '',
'q_opt' => [],
'q_cat' => [],
'q_age' => '0,0',
'q_user' => [],
'q_order' => 'date',
'q_rev' => '0',
'q_type' => [],
];
}
public static function getSabaFormOptions(): array
{
return [
__('Selected entry') => 'selected',
__('With comments') => 'comment',
__('With trackbacks') => 'trackback',
];
}
public static function getSabaFormOrders(): array
{
return [
__('Title') => 'title',
__('Selected entry') => 'selected',
__('Author') => 'author',
__('Date') => 'date',
__('Update') => 'update',
__('Comments count') => 'comment',
__('Trackbacks count') => 'trackback',
];
}
public static function getSabaFormAges(): array
{
return [
__('All') => '0,0',
__('Less than a month') => '0,2592000',
__('From 1 to 6 month') => '2592000,15552000',
__('From 6 to 12 month') => '15552000,31536000',
__('More than a year') => '31536000,0',
];
}
public static function getSabaFormTypes(): array
{
$know = [
'post' => __('Entry'),
'page' => __('Page'),
'pollsfactory' => __('Poll'),
'eventhandler' => __('Event'),
];
// todo: add behavior for unknow types
$rs = [];
$types = dcCore::app()->getPostTypes();
foreach ($types as $k => $v) {
if (!$v['public_url']) {
continue;
}
$rs[$know[$k] ?? __($k)] = $k;
}
return $rs;
}
}

View file

@ -10,21 +10,30 @@
* @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', ['sabaWidget', 'setWidget']);
namespace Dotclear\Plugin\saba;
class sabaWidget
use dcCore;
use dcUtils;
use Dotclear\Helper\Html\Html;
use Dotclear\Plugin\widgets\WidgetsStack;
use Dotclear\Plugin\widgets\WidgetsElement;
class Widgets
{
public static function setWidget($w)
/**
* Widget initialisation.
*
* @param WidgetsStack $w WidgetsStack instance
*/
public static function initWidgets(WidgetsStack $w): void
{
$w
->create(
'saba',
__('Advanced search'),
['sabaWidget', 'getWidget'],
[self::class, 'parseWidget'],
null,
__('Add more search options on public side')
)
@ -74,31 +83,39 @@ class sabaWidget
->addOffline();
}
public static function getWidget($w)
/**
* Public part for widget
*
* @param WidgetsElement $w WidgetsElement instance
*/
public static function parseWidget(WidgetsElement $w): string
{
$s = dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
if (!$s->get('active')
|| !$s->get('error') && dcCore::app()->url->type == '404'
|| $w->offline
if (is_null(dcCore::app()->blog)
|| is_null(dcCore::app()->ctx)
|| !dcCore::app()->blog->settings->get(My::id())->get('active')
|| !dcCore::app()->blog->settings->get(My::id())->get('error') && dcCore::app()->url->type == '404'
|| $w->__get('offline')
) {
return;
return '';
}
$saba_options = dcCore::app()->ctx->saba_options ?? tplSaba::getSabaDefaultPostsOptions();
$res = '';
$saba_options = dcCore::app()->ctx->__get('saba_options') ?? [];
if (!is_array($saba_options) || empty($saba_options)) {
$saba_options = Utils::getSabaDefaultPostsOptions();
}
$res = '';
# advenced search only on search page
# advanced search only on search page
if (dcCore::app()->url->type == 'search') {
# order
if (!$w->saba_filter_orders) {
if (!$w->__get('saba_filter_orders')) {
$ct = '';
foreach (tplSaba::getSabaFormOrders() as $k => $v) {
foreach (Utils::getSabaFormOrders() as $k => $v) {
$ct .= '<li><label><input name="q_order" type="radio" value="' .
$v . '" ' .
($v == $saba_options['q_order'] ? 'checked="checked" ' : '') .
'/> ' . html::escapeHTML($k) . '</label></li>';
'/> ' . Html::escapeHTML($k) . '</label></li>';
}
if (!empty($ct)) {
$ct .= '<li><label><input name="q_rev" type="checkbox" value="1" ' .
@ -109,18 +126,18 @@ class sabaWidget
}
# options
if (!$w->saba_filter_options) {
if (!$w->__get('saba_filter_options')) {
$ct = '';
$rm = explode(',', $w->saba_remove_options);
$rm = explode(',', (string) $w->__get('saba_remove_options'));
foreach (tplSaba::getSabaFormOptions() as $k => $v) {
foreach (Utils::getSabaFormOptions() as $k => $v) {
if (in_array($v, $rm)) {
continue;
}
$ct .= '<li><label><input name="q_opt[]" type="checkbox" value="' .
$v . '" ' .
(in_array($v, $saba_options['q_opt']) ? 'checked="checked" ' : '') .
'/> ' . html::escapeHTML($k) . '</label></li>';
'/> ' . Html::escapeHTML($k) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter options')) . sprintf('<ul>%s</ul>', $ct);
@ -128,14 +145,14 @@ class sabaWidget
}
# ages
if (!$w->saba_filter_ages) {
if (!$w->__get('saba_filter_ages')) {
$ct = '';
foreach (tplSaba::getSabaFormAges() as $k => $v) {
foreach (Utils::getSabaFormAges() as $k => $v) {
$ct .= '<li><label><input name="q_age" type="radio" value="' .
$v . '" ' .
($v == $saba_options['q_age'] ? 'checked="checked" ' : '') .
'/> ' . html::escapeHTML($k) . '</label></li>';
'/> ' . Html::escapeHTML($k) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter by age')) . sprintf('<ul>%s</ul>', $ct);
@ -143,18 +160,18 @@ class sabaWidget
}
# types
if (!$w->saba_filter_types) {
if (!$w->__get('saba_filter_types')) {
$ct = '';
$rm = explode(',', $w->saba_remove_types);
$rm = explode(',', $w->__get('saba_remove_types'));
foreach (tplSaba::getSabaFormTypes() as $k => $v) {
foreach (Utils::getSabaFormTypes() as $k => $v) {
if (in_array($v, $rm)) {
continue;
}
$ct .= '<li><label><input name="q_type[]" type="checkbox" value="' .
$v . '" ' .
(in_array($v, $saba_options['q_type']) ? 'checked="checked" ' : '') .
'/> ' . html::escapeHTML($k) . '</label></li>';
'/> ' . Html::escapeHTML($k) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter by type')) . sprintf('<ul>%s</ul>', $ct);
@ -162,19 +179,19 @@ class sabaWidget
}
# categories
if (!$w->saba_filter_categories) {
if (!$w->__get('saba_filter_categories')) {
$ct = '';
$rm = explode(',', $w->saba_remove_categories);
$rm = explode(',', $w->__get('saba_remove_categories'));
$rs = dcCore::app()->blog->getCategories();
while ($rs->fetch()) {
if (in_array($rs->cat_id, $rm) || in_array($rs->cat_url, $rm)) {
if (in_array($rs->f('cat_id'), $rm) || in_array($rs->f('cat_url'), $rm)) {
continue;
}
$ct .= '<li><label><input name="q_cat[]" type="checkbox" value="' .
$rs->cat_id . '" ' .
(in_array($rs->cat_id, $saba_options['q_cat']) ? 'checked="checked" ' : '') .
'/> ' . html::escapeHTML($rs->cat_title) . '</label></li>';
$rs->f('cat_id') . '" ' .
(in_array($rs->f('cat_id'), $saba_options['q_cat']) ? 'checked="checked" ' : '') .
'/> ' . Html::escapeHTML($rs->f('cat_title')) . '</label></li>';
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter by category')) . sprintf('<ul>%s</ul>', $ct);
@ -182,19 +199,21 @@ class sabaWidget
}
# authors
if (!$w->saba_filter_authors) {
if (!$w->__get('saba_filter_authors')) {
$ct = '';
$rm = explode(',', $w->saba_remove_authors);
$rm = explode(',', $w->__get('saba_remove_authors'));
$rs = dcCore::app()->blog->getPostsUsers();
while ($rs->fetch()) {
if (in_array($rs->user_id, $rm)) {
if (in_array($rs->f('user_id'), $rm)) {
continue;
}
$ct .= '<li><label><input name="q_user[]" type="checkbox" value="' .
$rs->user_id . '" ' .
(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>';
$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" ' : '',
Html::escapeHTML(dcUtils::getUserCN($rs->f('user_id'), $rs->f('user_name'), $rs->f('user_firstname'), $rs->f('user_displayname')))
);
}
if (!empty($ct)) {
$res .= $w->renderTitle(__('Filter by author')) . sprintf('<ul>%s</ul>', $ct);
@ -203,14 +222,14 @@ class sabaWidget
}
return $w->renderDiv(
$w->content_only,
$w->class,
(bool) $w->__get('content_only'),
$w->__get('class'),
'id="search"',
($w->title ? $w->renderTitle('<label for="q">' . html::escapeHTML($w->title) . '</label>') : '') .
($w->__get('title') ? $w->renderTitle('<label for="q">' . Html::escapeHTML($w->__get('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) . '"' : '') .
Html::escapeHTML($saba_options['q']) . '" ' .
($w->__get('placeholder') ? 'placeholder="' . Html::escapeHTML($w->__get('placeholder')) . '"' : '') .
' aria-label="' . __('Search') . '"/> ' .
'<input type="submit" class="submit" value="ok" title="' . __('Search') . '" /></p>' .
$res .