saba/_public.php

338 lines
9.5 KiB
PHP
Raw Normal View History

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')) {
return null;
2021-08-21 20:12:28 +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) {
return null;
2021-08-21 20:12:28 +00:00
}
# translation
2022-11-17 21:41:04 +00:00
l10n::set(__DIR__ . '/locales/' . dcCore::app()->lang . '/public');
# widget
2022-11-17 21:41:04 +00:00
require_once __DIR__ . '/_widgets.php';
# template path
2022-11-17 21:41:04 +00:00
dcCore::app()->tpl->setPath(
dcCore::app()->tpl->getPath(),
__DIR__ . '/default-templates/'
);
# behavior
2022-11-17 21:41:04 +00:00
dcCore::app()->addBehavior(
'templateCustomSortByAlias',
['pubSaba', 'templateCustomSortByAlias']
);
2022-11-17 21:41:04 +00:00
dcCore::app()->addBehavior(
'urlHandlerBeforeGetData',
['pubSaba', 'urlHandlerBeforeGetData']
);
2022-11-17 21:41:04 +00:00
dcCore::app()->addBehavior(
'coreBlogBeforeGetPosts',
['pubSaba', 'coreBlogBeforeGetPosts']
);
# url
2022-11-17 21:41:04 +00:00
if (dcCore::app()->blog->settings->saba->error) {
dcCore::app()->url->registerError(['urlSaba', 'error']);
}
2021-08-21 20:12:28 +00:00
class pubSaba
{
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',
];
}
public static function urlHandlerBeforeGetData($_ctx)
{
$options = tplSaba::getSabaDefaultPostsOptions();
if (!empty($_GET['q']) && 1 < strlen($_GET['q'])) {
# search string
$params = new ArrayObject(['search' => rawurldecode($_GET['q'])]);
2022-11-17 21:41:04 +00:00
$options = self::getPostsParams($params);
$options['q'] = rawurldecode($_GET['q']);
# 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);
}
# 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;
$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);
unset($params);
}
2022-11-17 21:41:04 +00:00
dcCore::app()->ctx->saba_options = $options;
}
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();
$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';
$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 ';
}
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';
}
}
# 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);
if (!$v) {
continue;
}
2022-11-17 21:41:04 +00:00
$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'])) {
2022-11-17 21:41:04 +00:00
$types = dcCore::app()->getPostTypes();
foreach ($get['q_type'] as $v) {
if (!$types[$v]) {
continue;
}
2022-11-17 21:41:04 +00:00
$options['q_type'][] = $v;
$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();
$options['q_age'] = $get['q_age'];
2021-09-02 20:53:22 +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) . "'";
$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';
}
$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]),
'post'
); //?! post_type
}
2022-11-17 21:41:04 +00:00
return $options;
}
# Ajouter la condition "ou" <20> la recherche
public static function coreBlogBeforeGetPosts($p)
{
if (empty($p['search'])) {
return;
}
self::getPostsParams($p);
$OR = [];
2022-11-17 21:41:04 +00:00
foreach (explode(',', $p['search']) as $sentence) {
$AND = [];
$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) . "%'";
}
if (!empty($AND)) {
2022-11-17 21:41:04 +00:00
$OR[] = ' (' . implode(' AND ', $AND) . ') ';
}
}
if (!empty($OR)) {
$p['search'] = '';
2022-11-17 21:41:04 +00:00
$p['sql'] .= 'AND (' . implode(' OR ', $OR) . ') ';
}
}
2021-08-21 20:12:28 +00:00
}
class urlSaba extends dcUrlHandlers
{
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);
$_SERVER['QUERY_STRING'] = '';
# Claim comes from 404
$GLOBALS['_from_error'] = true;
# Serve saba
self::serveDocument('saba_404_default.html');
return true;
}
}
2021-08-21 20:12:28 +00:00
}
class tplSaba
{
public static function getSabaDefaultPostsOptions()
{
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' => [],
];
}
public static function getSabaFormOptions()
{
return [
2022-11-17 21:41:04 +00:00
__('Selected entry') => 'selected',
__('With comments') => 'comment',
__('With trackbacks') => 'trackback',
];
}
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',
];
}
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',
__('From 6 to 12 month') => '15552000,31536000',
2022-11-17 21:41:04 +00:00
__('More than a year') => '31536000,0',
];
}
public static function getSabaFormTypes()
{
$know = [
2022-11-17 21:41:04 +00:00
'post' => __('Entry'),
'page' => __('Page'),
'pollsfactory' => __('Poll'),
2022-11-17 21:41:04 +00:00
'eventhandler' => __('Event'),
];
// todo: add behavior for unknow types
2022-11-17 21:41:04 +00:00
$rs = [];
$types = dcCore::app()->getPostTypes();
2022-11-17 21:41:04 +00:00
foreach ($types as $k => $v) {
if (!$v['public_url']) {
continue;
}
2022-11-17 21:41:04 +00:00
$rs[$know[$k] ?? __($k)] = $k;
}
return $rs;
}
2022-11-17 21:41:04 +00:00
}