move to PSR-2 coding style and short array

This commit is contained in:
Jean-Christian Paul Denis 2021-08-22 00:06:07 +02:00
parent ac274d3349
commit f64f1f6157
5 changed files with 543 additions and 543 deletions

View File

@ -2,6 +2,7 @@
* update license
* fix query
* change template titles to h3
* move to PSR-2 coding style and short array
2013.10.28
* Switch to Dotclear 2.6

View File

@ -13,7 +13,7 @@ show related entries to visitor.
saba requires:
* administrator permissions
* Dotclear 2.6
* Dotclear 2.18
* Custom templates if default theme is not used.
## USAGE
@ -27,3 +27,9 @@ go to ''configure plugin'', fill in form.
If you use a custom theme (not Dotclear's default theme)
you must create custom templates that match yours.
You can find exemple in saba/default-tempaltes.
## MORE
* License : GNU GPL v2
* Source & contribution : [GitHub Page](https://github.com/JcDenis/saba)
* Packages & details: [Dotaddict Page](https://plugins.dotaddict.org/dc2/details/saba)

View File

@ -12,25 +12,19 @@
# -- END LICENSE BLOCK ------------------------------------
if (!defined('DC_RC_PATH')) {
return null;
}
$this->registerModule(
/* Name */
"saba",
/* Description*/
"Search across blog's archive",
/* Author */
"Jean-Christian Denis",
/* Version */
'2021.08.20.3',
/* Properies */
array(
'saba',
'Search across blog archive',
'Jean-Christian Denis and Contributors',
'2021.08.20.5',
[
'permissions' => 'admin',
'type' => 'plugin',
'dc_min' => '2.6',
'support' => 'http://jcd.lv/q=saba',
'details' => 'http://plugins.dotaddict.org/dc2/details/saba'
)
'dc_min' => '2.18',
'support' => 'https://github.com/JcDenis/saba',
'details' => 'https://plugins.dotaddict.org/dc2/details/saba'
]
);

View File

@ -12,100 +12,102 @@
# -- END LICENSE BLOCK ------------------------------------
if (!defined('DC_RC_PATH')) {
return null;
}
# Admin behaviors
$core->blog->settings->addNamespace('saba');
if ($core->blog->settings->saba->active) {
if (!$core->blog->settings->saba->active) {
return null;
}
# Register saba handler
$core->url->register(
# Register saba handler
$core->url->register(
'search',
'search',
'^search(/.+)?$',
array('urlSaba', 'saba')
);
['urlSaba', 'saba']
);
# Add saba templates path
$core->tpl->setPath(
# Add saba templates path
$core->tpl->setPath(
$core->tpl->getPath(),
dirname(__FILE__).'/default-templates/'
);
dirname(__FILE__) . '/default-templates/'
);
# templates
$core->tpl->addBlock(
# templates
$core->tpl->addBlock(
'SabaIf',
array('tplSaba', 'SabaIf')
);
$core->tpl->addBlock(
['tplSaba', 'SabaIf']
);
$core->tpl->addBlock(
'SabaEntries',
array('tplSaba', 'SabaEntries')
);
$core->tpl->addBlock(
['tplSaba', 'SabaEntries']
);
$core->tpl->addBlock(
'SabaFormIf',
array('tplSaba', 'SabaFormIf')
);
$core->tpl->addValue(
['tplSaba', 'SabaFormIf']
);
$core->tpl->addValue(
'SabaFormSearch',
array('tplSaba', 'SabaFormSearch')
);
$core->tpl->addValue(
['tplSaba', 'SabaFormSearch']
);
$core->tpl->addValue(
'SabaFormOptions',
array('tplSaba', 'SabaFormOptions')
);
$core->tpl->addValue(
['tplSaba', 'SabaFormOptions']
);
$core->tpl->addValue(
'SabaFormCategories',
array('tplSaba', 'SabaFormCategories')
);
$core->tpl->addValue(
['tplSaba', 'SabaFormCategories']
);
$core->tpl->addValue(
'SabaFormTypes',
array('tplSaba', 'SabaFormTypes')
);
$core->tpl->addValue(
['tplSaba', 'SabaFormTypes']
);
$core->tpl->addValue(
'SabaFormAges',
array('tplSaba', 'SabaFormAges')
);
$core->tpl->addValue(
['tplSaba', 'SabaFormAges']
);
$core->tpl->addValue(
'SabaFormOrders',
array('tplSaba', 'SabaFormOrders')
);
$core->tpl->addValue(
['tplSaba', 'SabaFormOrders']
);
$core->tpl->addValue(
'SabaFormAuthors',
array('tplSaba', 'SabaFormAuthors')
);
$core->tpl->addValue(
['tplSaba', 'SabaFormAuthors']
);
$core->tpl->addValue(
'SabaPaginationURL',
array('tplSaba', 'SabaPaginationURL')
);
$core->tpl->addValue(
['tplSaba', 'SabaPaginationURL']
);
$core->tpl->addValue(
'SabaURL',
array('tplSaba','SabaURL')
);
['tplSaba', 'SabaURL']
);
# behavior
$core->addBehavior(
# behavior
$core->addBehavior(
'templateCustomSortByAlias',
array('pubSaba', 'templateCustomSortByAlias')
);
$core->addBehavior(
['pubSaba', 'templateCustomSortByAlias']
);
$core->addBehavior(
'urlHandlerBeforeGetData',
array('pubSaba', 'urlHandlerBeforeGetData')
);
$core->addBehavior(
['pubSaba', 'urlHandlerBeforeGetData']
);
$core->addBehavior(
'corePostSearch',
array('pubSaba', 'corePostSearch')
);
$core->url->registerError(array('urlSaba','error'));
}
['pubSaba', 'corePostSearch']
);
# url
$core->url->registerError(['urlSaba', 'error']);
class pubSaba
{
public static function templateCustomSortByAlias($alias)
{
$alias['post'] = array(
$alias['post'] = [
'title' => 'post_title',
'selected' => 'post_selected',
'author' => 'user_id',
@ -114,23 +116,23 @@ class pubSaba
'id' => 'post_id',
'comment' => 'nb_comment',
'trackback' => 'nb_trackback'
);
];
}
public static function urlHandlerBeforeGetData($_ctx)
{
global $core;
$options = array(
'q' => '',
'q_opt' => array(),
'q_cat' => array(),
$options = [
'q'=> '',
'q_opt' => [],
'q_cat' => [],
'q_age' => '0,0',
'q_user' => array(),
'q_order' => 'date',
'q_user'=> [],
'q_order'=> 'date',
'q_rev' => '0',
'q_type' => array()
);
'q_type'=> []
];
if (!empty($_GET['q']) && 1 < strlen($_GET['q'])) {
# move to saba
@ -142,10 +144,10 @@ class pubSaba
$qs = preg_replace('#(^|/)page/([0-9]+)#', '', $qs);
parse_str($qs, $get);
$params = array(
$params = [
'sql'=>'',
'post_type' => array()
);
'post_type' => []
];
# search string
$params['search'] = rawurldecode($_GET['q']);
@ -176,11 +178,11 @@ class pubSaba
if (!$v) {
continue;
}
$cats[] = "C.cat_id = '".$v."'";
$cats[] = "C.cat_id = '" . $v . "'";
$options['q_cat'][] = $v;
}
if (!empty($cats)) {
$params['sql'] .= 'AND ('.implode(' OR ', $cats).') ';
$params['sql'] .= 'AND (' . implode(' OR ', $cats) . ') ';
}
}
@ -206,10 +208,12 @@ class pubSaba
$options['q_age'] = $get['q_age'];
if ($age[0]) {
$params['sql'] .= "AND P.post_dt < '".dt::str('%Y-%m-%d %H:%m:%S', $ts-$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])."' ";
$params['sql'] .= "AND P.post_dt > '" .
dt::str('%Y-%m-%d %H:%m:%S', $ts - $age[1]) . "' ";
}
}
@ -217,11 +221,11 @@ class pubSaba
if (!empty($get['q_user'])) {
$users = array();
foreach($get['q_user'] as $v) {
$users[] = "U.user_id = '".$core->con->escape($v)."'";
$users[] = "U.user_id = '" . $core->con->escape($v) . "'";
$options['q_user'][] = $v;
}
if (!empty($users)) {
$params['sql'] .= 'AND ('.implode(' OR ', $users).') ';
$params['sql'] .= 'AND (' . implode(' OR ', $users) . ') ';
}
}
@ -236,7 +240,9 @@ class pubSaba
$options['q_order'] = $get['q_order'];
$params['order'] = $core->tpl->getSortByStr(
array('sortby'=>$get['q_order'], 'order'=>$sort),'post'); //?! post_type
['sortby' => $get['q_order'], 'order' => $sort],
'post'
); //?! post_type
}
# count
@ -248,7 +254,7 @@ class pubSaba
# pagintaion
$_page_number = !isset($GLOBALS['_page_number']) ? 1 : $GLOBALS['_page_number'];
$params['limit'] = $_ctx->nb_entry_per_page;
$params['limit'] = array((($_page_number-1)*$params['limit']), $params['limit']);
$params['limit'] = [(($_page_number - 1) * $params['limit']), $params['limit']];
# get posts
$_ctx->post_params = $params;
@ -263,16 +269,15 @@ class pubSaba
{
$sentences = explode(',', $p[2]['search']);
$OR = array();
foreach($sentences as $sentence)
{
$AND = array();
$OR = [];
foreach($sentences as $sentence) {
$AND = [];
$words = text::splitWords($sentence);
foreach($words as $word) {
$AND[] = "post_words LIKE '%".$core->con->escape($word)."%'";
$AND[] = "post_words LIKE '%" . $core->con->escape($word) . "%'";
}
if (!empty($AND)) {
$OR[] = " (".implode (' AND ',$AND).") ";
$OR[] = " (" . implode (' AND ', $AND) . ") ";
}
}
if (!empty($OR)) {
@ -291,7 +296,6 @@ class urlSaba extends dcUrlHandlers
if ($e->getCode() == 404) {
$q = explode('/', $args);
if (empty($q)) {
return false;
}
@ -333,22 +337,22 @@ class tplSaba
public static function SabaIf($a, $c)
{
$if = array();
$if = [];
$operator = isset($a['operator']) ? $GLOBALS['core']->tpl->getOperator($a['operator']) : '&&';
if (isset($a['has_search'])) {
$sign = (boolean) $a['has_search'] ? '' : '!';
$if[] = $sign.'isset($_search_count)';
$if[] = $sign . 'isset($_search_count)';
}
if (isset($a['from_error'])) {
$sign = (boolean) $a['from_error'] ? '' : '!';
$if[] = $sign.'isset($_from_error)';
$if[] = $sign . 'isset($_from_error)';
}
return !empty($if) ?
'<?php if('.implode(' '.$operator.' ', $if).') : ?>'.$c.'<?php endif; ?>'
'<?php if(' . implode(' ' . $operator . ' ', $if) . ') : ?>' . $c . '<?php endif; ?>'
: $c;
}
@ -356,71 +360,70 @@ class tplSaba
{
$f = $GLOBALS['core']->tpl->getFilters($a);
return '<?php echo '.sprintf($f,'$core->blog->url.$core->url->getBase("search")').'; ?>';
return '<?php echo ' . sprintf($f, '$core->blog->url.$core->url->getBase("search")') . '; ?>';
}
public static function SabaFormIf($a,$c)
public static function SabaFormIf($a, $c)
{
$if = array();
$if = [];
$operator = isset($a['operator']) ? $GLOBALS['core']->tpl->getOperator($a['operator']) : '&&';
$fl = self::getSabaFormFilters();
foreach($fl as $filter) {
if (isset($a['filter_'.$filter])) {
$sign = (boolean) $a['filter_'.$filter] ? '' : '!';
$if[] = $sign.'tplSaba::isSabaFormFilter(\''.$filter.'\')';
if (isset($a['filter_' . $filter])) {
$sign = (boolean) $a['filter_' . $filter] ? '' : '!';
$if[] = $sign . 'tplSaba::isSabaFormFilter(\'' . $filter . '\')';
}
}
return !empty($if) ?
'<?php if('.implode(' '.$operator.' ',$if).') : ?>'.$c.'<?php endif; ?>'
'<?php if(' . implode(' ' . $operator . ' ', $if) . ') : ?>' . $c . '<?php endif; ?>'
: $c;
}
public static function SabaFormOptions($a)
{
$dis = !empty($a['remove']) ? explode(',', $a['remove']) : array();
$dis = !empty($a['remove']) ? explode(',', $a['remove']) : [];
$res = '';
$li = '<li><label><input name="q_opt[]" type="checkbox" value="%s" %s/> %s</label></li>';
$rs = self::getSabaFormOptions();
foreach($rs as $k => $v) {
if (in_array($v,$dis)) {
if (in_array($v, $dis)) {
continue;
}
$chk = '<?php echo in_array("'.$v.'",$_ctx->saba_options["q_opt"]) ? \'checked="checked" \' : ""; ?>';
$chk = '<?php echo in_array("' . $v . '",$_ctx->saba_options["q_opt"]) ? \'checked="checked" \' : ""; ?>';
$res .= sprintf($li, $v, $chk, html::escapeHTML($k));
}
if (!empty($res)) {
return '<div class="saba_opt_otp"><h3>'.__('Filter options').'</h3><ul>'.$res.'</ul></div>';
return '<div class="saba_opt_otp"><h3>' . __('Filter options') . '</h3><ul>' . $res . '</ul></div>';
}
}
public static function SabaFormOrders($a)
{
$dis = !empty($a['remove']) ? explode(',',$a['remove']) : array();
$dis = !empty($a['remove']) ? explode(',',$a['remove']) : [];
$res = '';
$li = '<li><label><input name="q_order" type="radio" value="%s" %s/> %s</label></li>';
$rs = self::getSabaFormOrders($dis);
foreach($rs as $k => $v) {
if (in_array($v,$dis)) {
if (in_array($v, $dis)) {
continue;
}
$chk = '<?php echo "'.$v.'" == $_ctx->saba_options["q_order"] ? \'checked="checked" \' : ""; ?>';
$chk = '<?php echo "' . $v . '" == $_ctx->saba_options["q_order"] ? \'checked="checked" \' : ""; ?>';
$res .= sprintf($li, $v, $chk, html::escapeHTML($k));
}
if (!empty($res)) {
$chk = '<?php echo !empty($_ctx->saba_options["q_rev"]) ? \'checked="checked" \' : ""; ?>';
$res .= '<li><label><input name="q_rev" type="checkbox" value="1" '.$chk.'/> '.__('Reverse order').'</label></li>';
$res .= '<li><label><input name="q_rev" type="checkbox" value="1" ' . $chk . '/> ' . __('Reverse order') . '</label></li>';
return '<div class="saba_opt_order"><h3>'.__('Filter order').'</h3><ul>'.$res.'</ul></div>';
return '<div class="saba_opt_order"><h3>' . __('Filter order') . '</h3><ul>' . $res . '</ul></div>';
}
}
@ -428,23 +431,22 @@ class tplSaba
{
global $core;
$dis = !empty($a['remove']) ? explode(',', $a['remove']) : array();
$dis = !empty($a['remove']) ? explode(',', $a['remove']) : [];
$res = '';
$li = '<li><label><input name="q_cat[]" type="checkbox" value="%s" %s/> %s</label></li>';
$rs = $core->blog->getCategories();
while ($rs->fetch()) {
if (in_array($rs->cat_id,$dis) || in_array($rs->cat_url,$dis)) {
if (in_array($rs->cat_id, $dis) || in_array($rs->cat_url, $dis)) {
continue;
}
$chk = '<?php echo in_array("'.$rs->cat_id.'",$_ctx->saba_options["q_cat"]) ? \'checked="checked" \' : ""; ?>';
$res .= sprintf($li,$rs->cat_id,$chk,html::escapeHTML($rs->cat_title));
$chk = '<?php echo in_array("' . $rs->cat_id . '",$_ctx->saba_options["q_cat"]) ? \'checked="checked" \' : ""; ?>';
$res .= sprintf($li, $rs->cat_id, $chk, html::escapeHTML($rs->cat_title));
}
if (!empty($res)) {
return '<div class="saba_opt_cat"><h3>'.__('Filter by category').'</h3><ul>'.$res.'</ul></div>';
return '<div class="saba_opt_cat"><h3>' . __('Filter by category') . '</h3><ul>' . $res . '</ul></div>';
}
}
@ -452,23 +454,22 @@ class tplSaba
{
global $core;
$dis = !empty($a['remove']) ? explode(',',$a['remove']) : array();
$dis = !empty($a['remove']) ? explode(',',$a['remove']) : [];
$res = '';
$li = '<li><label><input name="q_type[]" type="checkbox" value="%s" %s/> %s</label></li>';
$li = '<li><label><input name="q_type[]" type="checkbox" value="%s" %s/>%s</label></li>';
$rs = self::getSabaFormTypes();
foreach($rs as $k => $v) {
if (in_array($v,$dis)) {
if (in_array($v, $dis)) {
continue;
}
$chk = '<?php echo in_array("'.$v.'",$_ctx->saba_options["q_type"]) ? \'checked="checked" \' : ""; ?>';
$res .= sprintf($li,$v,$chk,html::escapeHTML($k));
$chk = '<?php echo in_array("' . $v . '",$_ctx->saba_options["q_type"]) ? \'checked="checked" \' : ""; ?>';
$res .= sprintf($li, $v, $chk, html::escapeHTML($k));
}
if (!empty($res)) {
return '<div class="saba_opt_type"><h3>'.__('Filter by type').'</h3><ul>'.$res.'</ul></div>';
return '<div class="saba_opt_type"><h3>' . __('Filter by type') . '</h3><ul>' . $res . '</ul></div>';
}
}
@ -479,13 +480,12 @@ class tplSaba
$rs = self::getSabaFormAges();
foreach($rs as $k => $v) {
$chk = '<?php echo "'.$v.'" == $_ctx->saba_options["q_age"] ? \'checked="checked" \' : ""; ?>';
$chk = '<?php echo "' . $v . '" == $_ctx->saba_options["q_age"] ? \'checked="checked" \' : ""; ?>';
$res .= sprintf($li, $v, $chk, html::escapeHTML($k));
}
if (!empty($res)) {
return '<div class="saba_opt_age"><h3>'.__('Filter by age').'</h3><ul>'.$res.'</ul></div>';
return '<div class="saba_opt_age"><h3>' . __('Filter by age') . '</h3><ul>' . $res . '</ul></div>';
}
}
@ -493,23 +493,22 @@ class tplSaba
{
global $core;
$dis = !empty($a['remove']) ? explode(',',$a['remove']) : array();
$dis = !empty($a['remove']) ? explode(',',$a['remove']) : [];
$res = '';
$li = '<li><label><input name="q_user[]" type="checkbox" value="%s" %s/> %s</label></li>';
$rs = $core->blog->getPostsUsers();
while ($rs->fetch()) {
if (in_array($rs->user_id,$dis)) {
if (in_array($rs->user_id, $dis)) {
continue;
}
$chk = '<?php echo in_array("'.$rs->user_id.'",$_ctx->saba_options["q_user"]) ? \'checked="checked" \' : ""; ?>';
$res .= sprintf($li,$rs->user_id,$chk,html::escapeHTML(dcUtils::getUserCN($rs->user_id,$rs->user_name,$rs->user_firstname, $rs->user_displayname)));
$chk = '<?php echo in_array("' . $rs->user_id . '",$_ctx->saba_options["q_user"]) ? \'checked="checked" \' : ""; ?>';
$res .= sprintf($li, $rs->user_id, $chk, html::escapeHTML(dcUtils::getUserCN($rs->user_id, $rs->user_name, $rs->user_firstname, $rs->user_displayname)));
}
if (!empty($res)) {
return '<div class="saba_opt_user"><h3>'.__('Filter by author').'</h3><ul>'.$res.'</ul></div>';
return '<div class="saba_opt_user"><h3>' . __('Filter by author') . '</h3><ul>' . $res . '</ul></div>';
}
}
@ -522,33 +521,33 @@ class tplSaba
$f = $GLOBALS['core']->tpl->getFilters($attr);
return '<?php echo '.sprintf($f, "ctxSaba::PaginationURL(".$offset.")").'; ?>';
return '<?php echo '.sprintf($f, "ctxSaba::PaginationURL(" . $offset . ")") . '; ?>';
}
public static function getSabaFormFilters()
{
return array(
return [
'options',
'orders',
'ages',
'categories',
'authors',
'types'
);
];
}
public static function getSabaFormOptions()
{
return array(
return [
__('Selected entry') => 'selected',
__('With comments') => 'comment',
__('With trackbacks') => 'trackback'
);
];
}
public static function getSabaFormOrders()
{
return array(
return [
__('Title') => 'title',
__('Selected entry') => 'selected',
__('Author') => 'author',
@ -556,30 +555,31 @@ class tplSaba
__('Update') => 'update',
__('Comments count') => 'comment',
__('Trackbacks count') => 'trackback'
);
];
}
public static function getSabaFormAges()
{
return 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()
{
$know = array(
$know = [
'post' => __('Entry'),
'page' => __('Page'),
'pollsfactory' => __('Poll'),
'eventhandler' => __('Event')
);
];
// todo: add behavior for unknow types
$rs = array();
$rs = [];
$types = $GLOBALS['core']->getPostTypes();
foreach($types as $k => $v) {
@ -597,16 +597,16 @@ class tplSaba
$filters = (string) $GLOBALS['core']->blog->settings->saba->filters;
$filters = @unserialize($filters);
if (!is_array($filters)) {
$filters = array();
$filters = [];
}
return !in_array($f,$filters);
return !in_array($f, $filters);
}
}
class ctxSaba extends context
{
public static function PaginationURL($offset=0)
public static function PaginationURL($offset = 0)
{
$args = $_SERVER['URL_REQUEST_PART'];
@ -614,11 +614,11 @@ class ctxSaba extends context
$args = preg_replace('#(^|/)page/([0-9]+)$#', '', $args);
$url = $GLOBALS['core']->blog->url.$args;
$url = $GLOBALS['core']->blog->url . $args;
if ($n > 1) {
$url = preg_replace('#/$#', '', $url);
$url .= '/page/'.$n;
$url .= '/page/' . $n;
}
$qs = preg_replace('#(^|/)page/([0-9]+)(&?)#', '', $_SERVER['QUERY_STRING']);
@ -626,7 +626,7 @@ class ctxSaba extends context
# If search param
if (!empty($_GET['q'])) {
$s = strpos($url, '?') !== false ? '&amp;' : '?';
$url .= $s.$qs;
$url .= $s . $qs;
}
return $url;

View File

@ -12,10 +12,9 @@
# -- END LICENSE BLOCK ------------------------------------
if (!defined('DC_RC_PATH')) {
return null;
}
if (!empty($_REQUEST['module']) && $_REQUEST['module'] == 'saba') {
$__resources['help']['core_plugins_conf'] = dirname(__FILE__).'/help/help.html';
$__resources['help']['core_plugins_conf'] = dirname(__FILE__) . '/help/help.html';
}