2021-08-21 20:12:28 +00:00
|
|
|
|
<?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
|
|
|
|
|
*/
|
2021-08-21 20:12:28 +00:00
|
|
|
|
if (!defined('DC_RC_PATH')) {
|
2021-08-21 22:06:07 +00:00
|
|
|
|
return null;
|
2021-08-21 20:12:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 23:28:59 +00:00
|
|
|
|
# setting
|
2022-11-17 21:41:04 +00:00
|
|
|
|
dcCore::app()->blog->settings->addNamespace('saba');
|
2021-08-21 20:12:28 +00:00
|
|
|
|
|
2022-11-17 21:41:04 +00:00
|
|
|
|
if (!dcCore::app()->blog->settings->saba->active) {
|
2021-08-21 22:06:07 +00:00
|
|
|
|
return null;
|
2021-08-21 20:12:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 23:28:59 +00:00
|
|
|
|
# translation
|
2022-11-17 21:41:04 +00:00
|
|
|
|
l10n::set(__DIR__ . '/locales/' . dcCore::app()->lang . '/public');
|
2021-08-22 23:28:59 +00:00
|
|
|
|
|
|
|
|
|
# widget
|
2022-11-17 21:41:04 +00:00
|
|
|
|
require_once __DIR__ . '/_widgets.php';
|
2021-08-21 22:06:07 +00:00
|
|
|
|
|
2021-08-22 23:28:59 +00:00
|
|
|
|
# template path
|
2022-11-17 21:41:04 +00:00
|
|
|
|
dcCore::app()->tpl->setPath(
|
|
|
|
|
dcCore::app()->tpl->getPath(),
|
|
|
|
|
__DIR__ . '/default-templates/'
|
2021-08-21 22:06:07 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
# behavior
|
2022-11-17 21:41:04 +00:00
|
|
|
|
dcCore::app()->addBehavior(
|
2021-08-21 22:06:07 +00:00
|
|
|
|
'templateCustomSortByAlias',
|
|
|
|
|
['pubSaba', 'templateCustomSortByAlias']
|
|
|
|
|
);
|
2022-11-17 21:41:04 +00:00
|
|
|
|
dcCore::app()->addBehavior(
|
2021-08-21 22:06:07 +00:00
|
|
|
|
'urlHandlerBeforeGetData',
|
|
|
|
|
['pubSaba', 'urlHandlerBeforeGetData']
|
|
|
|
|
);
|
2022-11-17 21:41:04 +00:00
|
|
|
|
dcCore::app()->addBehavior(
|
2021-08-22 23:28:59 +00:00
|
|
|
|
'coreBlogBeforeGetPosts',
|
|
|
|
|
['pubSaba', 'coreBlogBeforeGetPosts']
|
2021-08-21 22:06:07 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
# url
|
2022-11-17 21:41:04 +00:00
|
|
|
|
if (dcCore::app()->blog->settings->saba->error) {
|
|
|
|
|
dcCore::app()->url->registerError(['urlSaba', 'error']);
|
2021-08-22 23:28:59 +00:00
|
|
|
|
}
|
2021-08-21 22:06:07 +00:00
|
|
|
|
|
2021-08-21 20:12:28 +00:00
|
|
|
|
class pubSaba
|
|
|
|
|
{
|
2021-08-21 22:06:07 +00:00
|
|
|
|
public static function templateCustomSortByAlias($alias)
|
|
|
|
|
{
|
|
|
|
|
$alias['post'] = [
|
2022-11-17 21:41:04 +00:00
|
|
|
|
'title' => 'post_title',
|
|
|
|
|
'selected' => 'post_selected',
|
|
|
|
|
'author' => 'user_id',
|
|
|
|
|
'date' => 'post_dt',
|
|
|
|
|
'update' => 'post_upddt',
|
|
|
|
|
'id' => 'post_id',
|
|
|
|
|
'comment' => 'nb_comment',
|
|
|
|
|
'trackback' => 'nb_trackback',
|
2021-08-21 22:06:07 +00:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function urlHandlerBeforeGetData($_ctx)
|
|
|
|
|
{
|
2021-08-22 23:28:59 +00:00
|
|
|
|
$options = tplSaba::getSabaDefaultPostsOptions();
|
2021-08-21 22:06:07 +00:00
|
|
|
|
|
|
|
|
|
if (!empty($_GET['q']) && 1 < strlen($_GET['q'])) {
|
|
|
|
|
# search string
|
2021-08-22 23:28:59 +00:00
|
|
|
|
$params = new ArrayObject(['search' => rawurldecode($_GET['q'])]);
|
2021-08-21 22:06:07 +00:00
|
|
|
|
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$options = self::getPostsParams($params);
|
2021-08-22 23:28:59 +00:00
|
|
|
|
$options['q'] = rawurldecode($_GET['q']);
|
2021-08-21 22:06:07 +00:00
|
|
|
|
|
|
|
|
|
# count
|
2022-11-17 21:41:04 +00:00
|
|
|
|
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);
|
2021-08-21 22:06:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# pagintaion
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$_page_number = dcCore::app()->public->getPageNumber();
|
|
|
|
|
if ($_page_number < 1) {
|
|
|
|
|
$_page_number = 1;
|
|
|
|
|
}
|
|
|
|
|
$params['limit'] = dcCore::app()->ctx->nb_entry_per_page;
|
2021-08-21 22:06:07 +00:00
|
|
|
|
$params['limit'] = [(($_page_number - 1) * $params['limit']), $params['limit']];
|
|
|
|
|
|
|
|
|
|
# get posts
|
2022-11-17 21:41:04 +00:00
|
|
|
|
dcCore::app()->ctx->post_params = $params;
|
|
|
|
|
dcCore::app()->ctx->posts = dcCore::app()->blog->getPosts($params);
|
2021-08-21 22:06:07 +00:00
|
|
|
|
unset($params);
|
|
|
|
|
}
|
2022-11-17 21:41:04 +00:00
|
|
|
|
dcCore::app()->ctx->saba_options = $options;
|
2021-08-21 22:06:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 23:28:59 +00:00
|
|
|
|
public static function getPostsParams($params)
|
|
|
|
|
{
|
|
|
|
|
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
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$options = tplSaba::getSabaDefaultPostsOptions();
|
2021-08-22 23:28:59 +00:00
|
|
|
|
$options['q'] = $params['search'];
|
|
|
|
|
|
|
|
|
|
# options
|
|
|
|
|
if (!empty($get['q_opt'])) {
|
|
|
|
|
if (in_array('selected', $get['q_opt'])) {
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$options['q_opt'][] = 'selected';
|
2021-08-22 23:28:59 +00:00
|
|
|
|
$params['post_selected'] = 1;
|
|
|
|
|
}
|
|
|
|
|
if (in_array('comment', $get['q_opt'])) {
|
|
|
|
|
$options['q_opt'][] = 'comment';
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$params['sql'] = 'AND nb_comment > 0 ';
|
2021-08-22 23:28:59 +00:00
|
|
|
|
}
|
|
|
|
|
if (in_array('trackback', $get['q_opt'])) {
|
|
|
|
|
$options['q_opt'][] = 'trackback';
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$params['sql'] = 'AND nb_trackback > 0';
|
2021-08-22 23:28:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# categories
|
|
|
|
|
if (!empty($get['q_cat'])) {
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$cats = [];
|
|
|
|
|
foreach ($get['q_cat'] as $v) {
|
|
|
|
|
$v = abs((int) $v);
|
2021-08-22 23:28:59 +00:00
|
|
|
|
if (!$v) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$cats[] = "C.cat_id = '" . $v . "'";
|
2021-08-22 23:28:59 +00:00
|
|
|
|
$options['q_cat'][] = $v;
|
|
|
|
|
}
|
|
|
|
|
if (!empty($cats)) {
|
|
|
|
|
$params['sql'] .= 'AND (' . implode(' OR ', $cats) . ') ';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# post types
|
|
|
|
|
if (!empty($get['q_type'])) {
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$types = dcCore::app()->getPostTypes();
|
|
|
|
|
foreach ($get['q_type'] as $v) {
|
2021-08-22 23:28:59 +00:00
|
|
|
|
if (!$types[$v]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$options['q_type'][] = $v;
|
2021-08-22 23:28:59 +00:00
|
|
|
|
$params['post_type'][] = $v;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$params['post_type'][] = 'post';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# age
|
|
|
|
|
$ages = tplSaba::getSabaFormAges();
|
|
|
|
|
if (!empty($get['q_age']) && in_array($get['q_age'], $ages)) {
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$age = explode(',', $get['q_age']);
|
|
|
|
|
$ts = time();
|
2021-08-22 23:28:59 +00:00
|
|
|
|
$options['q_age'] = $get['q_age'];
|
2021-09-02 20:53:22 +00:00
|
|
|
|
|
2021-08-22 23:28:59 +00:00
|
|
|
|
if ($age[0]) {
|
|
|
|
|
$params['sql'] .= "AND P.post_dt < '" .
|
|
|
|
|
dt::str('%Y-%m-%d %H:%m:%S', $ts - $age[0]) . "' ";
|
|
|
|
|
}
|
|
|
|
|
if ($age[1]) {
|
|
|
|
|
$params['sql'] .= "AND P.post_dt > '" .
|
|
|
|
|
dt::str('%Y-%m-%d %H:%m:%S', $ts - $age[1]) . "' ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# user
|
|
|
|
|
if (!empty($get['q_user'])) {
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$users = [];
|
|
|
|
|
foreach ($get['q_user'] as $v) {
|
|
|
|
|
$users[] = "U.user_id = '" . dcCore::app()->con->escape($v) . "'";
|
2021-08-22 23:28:59 +00:00
|
|
|
|
$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';
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$sort = 'asc';
|
2021-08-22 23:28:59 +00:00
|
|
|
|
}
|
|
|
|
|
$orders = tplSaba::getSabaFormOrders();
|
|
|
|
|
if (!empty($get['q_order']) && in_array($get['q_order'], $orders)) {
|
|
|
|
|
$options['q_order'] = $get['q_order'];
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$params['order'] = dcCore::app()->tpl->getSortByStr(
|
|
|
|
|
new ArrayObject(['sortby' => $get['q_order'], 'order' => $sort]),
|
2021-08-22 23:28:59 +00:00
|
|
|
|
'post'
|
|
|
|
|
); //?! post_type
|
|
|
|
|
}
|
2022-11-17 21:41:04 +00:00
|
|
|
|
|
2021-08-22 23:28:59 +00:00
|
|
|
|
return $options;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-21 22:06:07 +00:00
|
|
|
|
# Ajouter la condition "ou" <20> la recherche
|
2021-08-22 23:28:59 +00:00
|
|
|
|
public static function coreBlogBeforeGetPosts($p)
|
2021-08-21 22:06:07 +00:00
|
|
|
|
{
|
2021-08-22 23:28:59 +00:00
|
|
|
|
if (empty($p['search'])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self::getPostsParams($p);
|
2021-08-21 22:06:07 +00:00
|
|
|
|
|
|
|
|
|
$OR = [];
|
2022-11-17 21:41:04 +00:00
|
|
|
|
foreach (explode(',', $p['search']) as $sentence) {
|
|
|
|
|
$AND = [];
|
2021-08-21 22:06:07 +00:00
|
|
|
|
$words = text::splitWords($sentence);
|
2022-11-17 21:41:04 +00:00
|
|
|
|
foreach ($words as $word) {
|
|
|
|
|
$AND[] = "post_words LIKE '%" . dcCore::app()->con->escape($word) . "%'";
|
2021-08-21 22:06:07 +00:00
|
|
|
|
}
|
|
|
|
|
if (!empty($AND)) {
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$OR[] = ' (' . implode(' AND ', $AND) . ') ';
|
2021-08-21 22:06:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!empty($OR)) {
|
2021-08-22 23:28:59 +00:00
|
|
|
|
$p['search'] = '';
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$p['sql'] .= 'AND (' . implode(' OR ', $OR) . ') ';
|
2021-08-21 22:06:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-21 20:12:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class urlSaba extends dcUrlHandlers
|
|
|
|
|
{
|
2021-08-21 22:06:07 +00:00
|
|
|
|
public static function error($args, $type, $e)
|
|
|
|
|
{
|
|
|
|
|
if ($e->getCode() == 404) {
|
|
|
|
|
$q = explode('/', $args);
|
|
|
|
|
if (empty($q)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Clean URI
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$_GET['q'] = implode('%20', $q);
|
2021-08-21 22:06:07 +00:00
|
|
|
|
$_SERVER['QUERY_STRING'] = '';
|
|
|
|
|
|
|
|
|
|
# Claim comes from 404
|
|
|
|
|
$GLOBALS['_from_error'] = true;
|
|
|
|
|
|
|
|
|
|
# Serve saba
|
2021-08-22 23:28:59 +00:00
|
|
|
|
self::serveDocument('saba_404_default.html');
|
2021-08-21 22:06:07 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-21 20:12:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class tplSaba
|
|
|
|
|
{
|
2021-08-22 23:28:59 +00:00
|
|
|
|
public static function getSabaDefaultPostsOptions()
|
2021-08-21 22:06:07 +00:00
|
|
|
|
{
|
|
|
|
|
return [
|
2022-11-17 21:41:04 +00:00
|
|
|
|
'q' => '',
|
|
|
|
|
'q_opt' => [],
|
|
|
|
|
'q_cat' => [],
|
|
|
|
|
'q_age' => '0,0',
|
|
|
|
|
'q_user' => [],
|
|
|
|
|
'q_order' => 'date',
|
|
|
|
|
'q_rev' => '0',
|
|
|
|
|
'q_type' => [],
|
2021-08-21 22:06:07 +00:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getSabaFormOptions()
|
|
|
|
|
{
|
|
|
|
|
return [
|
2022-11-17 21:41:04 +00:00
|
|
|
|
__('Selected entry') => 'selected',
|
|
|
|
|
__('With comments') => 'comment',
|
|
|
|
|
__('With trackbacks') => 'trackback',
|
2021-08-21 22:06:07 +00:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getSabaFormOrders()
|
|
|
|
|
{
|
|
|
|
|
return [
|
2022-11-17 21:41:04 +00:00
|
|
|
|
__('Title') => 'title',
|
|
|
|
|
__('Selected entry') => 'selected',
|
|
|
|
|
__('Author') => 'author',
|
|
|
|
|
__('Date') => 'date',
|
|
|
|
|
__('Update') => 'update',
|
|
|
|
|
__('Comments count') => 'comment',
|
|
|
|
|
__('Trackbacks count') => 'trackback',
|
2021-08-21 22:06:07 +00:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getSabaFormAges()
|
|
|
|
|
{
|
|
|
|
|
return [
|
2022-11-17 21:41:04 +00:00
|
|
|
|
__('All') => '0,0',
|
|
|
|
|
__('Less than a month') => '0,2592000',
|
|
|
|
|
__('From 1 to 6 month') => '2592000,15552000',
|
2021-08-21 22:06:07 +00:00
|
|
|
|
__('From 6 to 12 month') => '15552000,31536000',
|
2022-11-17 21:41:04 +00:00
|
|
|
|
__('More than a year') => '31536000,0',
|
2021-08-21 22:06:07 +00:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getSabaFormTypes()
|
|
|
|
|
{
|
|
|
|
|
$know = [
|
2022-11-17 21:41:04 +00:00
|
|
|
|
'post' => __('Entry'),
|
|
|
|
|
'page' => __('Page'),
|
2021-08-21 22:06:07 +00:00
|
|
|
|
'pollsfactory' => __('Poll'),
|
2022-11-17 21:41:04 +00:00
|
|
|
|
'eventhandler' => __('Event'),
|
2021-08-21 22:06:07 +00:00
|
|
|
|
];
|
|
|
|
|
// todo: add behavior for unknow types
|
|
|
|
|
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$rs = [];
|
|
|
|
|
$types = dcCore::app()->getPostTypes();
|
2021-08-21 22:06:07 +00:00
|
|
|
|
|
2022-11-17 21:41:04 +00:00
|
|
|
|
foreach ($types as $k => $v) {
|
2021-08-21 22:06:07 +00:00
|
|
|
|
if (!$v['public_url']) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-11-17 21:41:04 +00:00
|
|
|
|
$rs[$know[$k] ?? __($k)] = $k;
|
2021-08-21 22:06:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $rs;
|
|
|
|
|
}
|
2022-11-17 21:41:04 +00:00
|
|
|
|
}
|