diff --git a/CHANGELOG.md b/CHANGELOG.md index c6bbf5e..b93c0d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 5b5c2d1..4def6cd 100644 --- a/README.md +++ b/README.md @@ -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 @@ -26,4 +26,10 @@ 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. \ No newline at end of file +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) \ No newline at end of file diff --git a/_define.php b/_define.php index 49c5af3..8e3b37a 100644 --- a/_define.php +++ b/_define.php @@ -12,25 +12,19 @@ # -- END LICENSE BLOCK ------------------------------------ if (!defined('DC_RC_PATH')) { - - return null; + return null; } $this->registerModule( - /* Name */ - "saba", - /* Description*/ - "Search across blog's archive", - /* Author */ - "Jean-Christian Denis", - /* Version */ - '2021.08.20.3', - /* Properies */ - array( - 'permissions' => 'admin', - 'type' => 'plugin', - 'dc_min' => '2.6', - 'support' => 'http://jcd.lv/q=saba', - 'details' => 'http://plugins.dotaddict.org/dc2/details/saba' - ) -); + 'saba', + 'Search across blog archive', + 'Jean-Christian Denis and Contributors', + '2021.08.20.5', + [ + 'permissions' => 'admin', + 'type' => 'plugin', + 'dc_min' => '2.18', + 'support' => 'https://github.com/JcDenis/saba', + 'details' => 'https://plugins.dotaddict.org/dc2/details/saba' + ] +); \ No newline at end of file diff --git a/_public.php b/_public.php index 11b6107..f62f3a8 100644 --- a/_public.php +++ b/_public.php @@ -12,623 +12,623 @@ # -- END LICENSE BLOCK ------------------------------------ if (!defined('DC_RC_PATH')) { - - return null; + return null; } # Admin behaviors $core->blog->settings->addNamespace('saba'); -if ($core->blog->settings->saba->active) { - - # Register saba handler - $core->url->register( - 'search', - 'search', - '^search(/.+)?$', - array('urlSaba', 'saba') - ); - - # Add saba templates path - $core->tpl->setPath( - $core->tpl->getPath(), - dirname(__FILE__).'/default-templates/' - ); - - # templates - $core->tpl->addBlock( - 'SabaIf', - array('tplSaba', 'SabaIf') - ); - $core->tpl->addBlock( - 'SabaEntries', - array('tplSaba', 'SabaEntries') - ); - $core->tpl->addBlock( - 'SabaFormIf', - array('tplSaba', 'SabaFormIf') - ); - $core->tpl->addValue( - 'SabaFormSearch', - array('tplSaba', 'SabaFormSearch') - ); - $core->tpl->addValue( - 'SabaFormOptions', - array('tplSaba', 'SabaFormOptions') - ); - $core->tpl->addValue( - 'SabaFormCategories', - array('tplSaba', 'SabaFormCategories') - ); - $core->tpl->addValue( - 'SabaFormTypes', - array('tplSaba', 'SabaFormTypes') - ); - $core->tpl->addValue( - 'SabaFormAges', - array('tplSaba', 'SabaFormAges') - ); - $core->tpl->addValue( - 'SabaFormOrders', - array('tplSaba', 'SabaFormOrders') - ); - $core->tpl->addValue( - 'SabaFormAuthors', - array('tplSaba', 'SabaFormAuthors') - ); - $core->tpl->addValue( - 'SabaPaginationURL', - array('tplSaba', 'SabaPaginationURL') - ); - $core->tpl->addValue( - 'SabaURL', - array('tplSaba','SabaURL') - ); - - # behavior - $core->addBehavior( - 'templateCustomSortByAlias', - array('pubSaba', 'templateCustomSortByAlias') - ); - $core->addBehavior( - 'urlHandlerBeforeGetData', - array('pubSaba', 'urlHandlerBeforeGetData') - ); - $core->addBehavior( - 'corePostSearch', - array('pubSaba', 'corePostSearch') - ); - $core->url->registerError(array('urlSaba','error')); +if (!$core->blog->settings->saba->active) { + return null; } +# Register saba handler +$core->url->register( + 'search', + 'search', + '^search(/.+)?$', + ['urlSaba', 'saba'] +); + +# Add saba templates path +$core->tpl->setPath( + $core->tpl->getPath(), + dirname(__FILE__) . '/default-templates/' +); + +# templates +$core->tpl->addBlock( + 'SabaIf', + ['tplSaba', 'SabaIf'] +); +$core->tpl->addBlock( + 'SabaEntries', + ['tplSaba', 'SabaEntries'] +); +$core->tpl->addBlock( + 'SabaFormIf', + ['tplSaba', 'SabaFormIf'] +); +$core->tpl->addValue( + 'SabaFormSearch', + ['tplSaba', 'SabaFormSearch'] +); +$core->tpl->addValue( + 'SabaFormOptions', + ['tplSaba', 'SabaFormOptions'] +); +$core->tpl->addValue( + 'SabaFormCategories', + ['tplSaba', 'SabaFormCategories'] +); +$core->tpl->addValue( + 'SabaFormTypes', + ['tplSaba', 'SabaFormTypes'] +); +$core->tpl->addValue( + 'SabaFormAges', + ['tplSaba', 'SabaFormAges'] +); +$core->tpl->addValue( + 'SabaFormOrders', + ['tplSaba', 'SabaFormOrders'] +); +$core->tpl->addValue( + 'SabaFormAuthors', + ['tplSaba', 'SabaFormAuthors'] +); +$core->tpl->addValue( + 'SabaPaginationURL', + ['tplSaba', 'SabaPaginationURL'] +); +$core->tpl->addValue( + 'SabaURL', + ['tplSaba', 'SabaURL'] +); + +# behavior +$core->addBehavior( + 'templateCustomSortByAlias', + ['pubSaba', 'templateCustomSortByAlias'] +); +$core->addBehavior( + 'urlHandlerBeforeGetData', + ['pubSaba', 'urlHandlerBeforeGetData'] +); +$core->addBehavior( + 'corePostSearch', + ['pubSaba', 'corePostSearch'] +); + +# url +$core->url->registerError(['urlSaba', 'error']); + class pubSaba { - public static function templateCustomSortByAlias($alias) - { - $alias['post'] = array( - '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 templateCustomSortByAlias($alias) + { + $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($_ctx) - { - global $core; + public static function urlHandlerBeforeGetData($_ctx) + { + global $core; - $options = array( - 'q' => '', - 'q_opt' => array(), - 'q_cat' => array(), - 'q_age' => '0,0', - 'q_user' => array(), - 'q_order' => 'date', - 'q_rev' => '0', - 'q_type' => array() - ); + $options = [ + 'q'=> '', + 'q_opt' => [], + 'q_cat' => [], + 'q_age' => '0,0', + 'q_user'=> [], + 'q_order'=> 'date', + 'q_rev' => '0', + 'q_type'=> [] + ]; - if (!empty($_GET['q']) && 1 < strlen($_GET['q'])) { - # move to saba - $_ctx->current_tpl = null; - $_ctx->current_tpl = 'saba_search.html'; + if (!empty($_GET['q']) && 1 < strlen($_GET['q'])) { + # move to saba + $_ctx->current_tpl = null; + $_ctx->current_tpl = 'saba_search.html'; - # retreive _GET - $qs = $_SERVER['QUERY_STRING']; - $qs = preg_replace('#(^|/)page/([0-9]+)#', '', $qs); - parse_str($qs, $get); + # retreive _GET + $qs = $_SERVER['QUERY_STRING']; + $qs = preg_replace('#(^|/)page/([0-9]+)#', '', $qs); + parse_str($qs, $get); - $params = array( - 'sql'=>'', - 'post_type' => array() - ); + $params = [ + 'sql'=>'', + 'post_type' => [] + ]; - # search string - $params['search'] = rawurldecode($_GET['q']); - $options['q'] = rawurldecode($_GET['q']); + # search string + $params['search'] = rawurldecode($_GET['q']); + $options['q'] = rawurldecode($_GET['q']); - # 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"; - } - } + # 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"; + } + } - # categories - if (!empty($get['q_cat'])) { - $cats = array(); - foreach($get['q_cat'] as $v) { - $v = abs((integer) $v); - if (!$v) { - continue; - } - $cats[] = "C.cat_id = '".$v."'"; - $options['q_cat'][] = $v; - } - if (!empty($cats)) { - $params['sql'] .= 'AND ('.implode(' OR ', $cats).') '; - } - } + # categories + if (!empty($get['q_cat'])) { + $cats = array(); + foreach($get['q_cat'] as $v) { + $v = abs((integer) $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'])) { - $types = $core->getPostTypes(); - foreach($get['q_type'] as $v) { - if (!$types[$v]) { - continue; - } - $options['q_type'][] = $v; - $params['post_type'][] = $v; - } - } else { - $params['post_type'][] = 'post'; - } + # post types + if (!empty($get['q_type'])) { + $types = $core->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 = 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']; - - 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])."' "; - } - } + # 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']; + + 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'])) { - $users = array(); - foreach($get['q_user'] as $v) { - $users[] = "U.user_id = '".$core->con->escape($v)."'"; - $options['q_user'][] = $v; - } - if (!empty($users)) { - $params['sql'] .= 'AND ('.implode(' OR ', $users).') '; - } - } + # user + if (!empty($get['q_user'])) { + $users = array(); + foreach($get['q_user'] as $v) { + $users[] = "U.user_id = '" . $core->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)) { + #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'] = $core->tpl->getSortByStr( - array('sortby'=>$get['q_order'], 'order'=>$sort),'post'); //?! post_type - } + $options['q_order'] = $get['q_order']; + $params['order'] = $core->tpl->getSortByStr( + ['sortby' => $get['q_order'], 'order' => $sort], + 'post' + ); //?! post_type + } - # count - $GLOBALS['_search'] = rawurldecode($_GET['q']); - if ($GLOBALS['_search']) { - $GLOBALS['_search_count'] = $core->blog->getPosts($params, true)->f(0); - } + # count + $GLOBALS['_search'] = rawurldecode($_GET['q']); + if ($GLOBALS['_search']) { + $GLOBALS['_search_count'] = $core->blog->getPosts($params, true)->f(0); + } - # 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']); + # pagintaion + $_page_number = !isset($GLOBALS['_page_number']) ? 1 : $GLOBALS['_page_number']; + $params['limit'] = $_ctx->nb_entry_per_page; + $params['limit'] = [(($_page_number - 1) * $params['limit']), $params['limit']]; - # get posts - $_ctx->post_params = $params; - $_ctx->posts = $core->blog->getPosts($params); - unset($params); - } - $_ctx->saba_options = $options; - } + # get posts + $_ctx->post_params = $params; + $_ctx->posts = $core->blog->getPosts($params); + unset($params); + } + $_ctx->saba_options = $options; + } - # Ajouter la condition "ou" à la recherche - public static function corePostSearch($core, $p) - { - $sentences = explode(',', $p[2]['search']); + # Ajouter la condition "ou" à la recherche + public static function corePostSearch($core, $p) + { + $sentences = explode(',', $p[2]['search']); - $OR = array(); - foreach($sentences as $sentence) - { - $AND = array(); - $words = text::splitWords($sentence); - foreach($words as $word) { - $AND[] = "post_words LIKE '%".$core->con->escape($word)."%'"; - } - if (!empty($AND)) { - $OR[] = " (".implode (' AND ',$AND).") "; - } - } - if (!empty($OR)) { - $p[0] = ''; - $p[2]['sql'] = (isset($p[2]['sql']) ? $p[2]['sql'] : '') . "AND (" . implode (' OR ', $OR) . ") "; - } - } + $OR = []; + foreach($sentences as $sentence) { + $AND = []; + $words = text::splitWords($sentence); + foreach($words as $word) { + $AND[] = "post_words LIKE '%" . $core->con->escape($word) . "%'"; + } + if (!empty($AND)) { + $OR[] = " (" . implode (' AND ', $AND) . ") "; + } + } + if (!empty($OR)) { + $p[0] = ''; + $p[2]['sql'] = (isset($p[2]['sql']) ? $p[2]['sql'] : '') . "AND (" . implode (' OR ', $OR) . ") "; + } + } } class urlSaba extends dcUrlHandlers { - public static function error($args, $type, $e) - { - global $core, $_ctx; + public static function error($args, $type, $e) + { + global $core, $_ctx; - if ($e->getCode() == 404) { - $q = explode('/', $args); - if (empty($q)) { + if ($e->getCode() == 404) { + $q = explode('/', $args); + if (empty($q)) { + return false; + } - return false; - } + # Clean URI + $_GET['q'] = implode('%20', $q); + $_SERVER['QUERY_STRING'] = ''; - # Clean URI - $_GET['q'] = implode('%20', $q); - $_SERVER['QUERY_STRING'] = ''; + # Claim comes from 404 + $GLOBALS['_from_error'] = true; - # Claim comes from 404 - $GLOBALS['_from_error'] = true; + # Serve saba + self::serveDocument('saba_search.html'); - # Serve saba - self::serveDocument('saba_search.html'); + return true; + } + } + + public static function saba($args) + { + $_ctx =& $GLOBALS['_ctx']; + $core =& $GLOBALS['core']; - return true; - } - } - - public static function saba($args) - { - $_ctx =& $GLOBALS['_ctx']; - $core =& $GLOBALS['core']; - - self::serveDocument('saba_search.html'); - } + self::serveDocument('saba_search.html'); + } } class tplSaba { - public static function SabaEntries($a, $c) - { - return - 'exists("posts")) : while ($_ctx->posts->fetch()) : ?>'.$c.''; - } + public static function SabaEntries($a, $c) + { + return + 'exists("posts")) : while ($_ctx->posts->fetch()) : ?>'.$c.''; + } - public static function SabaFormSearch($a) - { - return 'saba_options["q"]); ?>'; - } + public static function SabaFormSearch($a) + { + return 'saba_options["q"]); ?>'; + } - public static function SabaIf($a, $c) - { - $if = array(); + public static function SabaIf($a, $c) + { + $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)'; - } + $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 (isset($a['from_error'])) { - $sign = (boolean) $a['from_error'] ? '' : '!'; - $if[] = $sign.'isset($_from_error)'; - } + if (isset($a['from_error'])) { + $sign = (boolean) $a['from_error'] ? '' : '!'; + $if[] = $sign . 'isset($_from_error)'; + } - return !empty($if) ? - ''.$c.'' - : $c; - } + return !empty($if) ? + '' . $c . '' + : $c; + } - public static function SabaURL($a) - { - $f = $GLOBALS['core']->tpl->getFilters($a); + public static function SabaURL($a) + { + $f = $GLOBALS['core']->tpl->getFilters($a); - return 'blog->url.$core->url->getBase("search")').'; ?>'; - } + return 'blog->url.$core->url->getBase("search")') . '; ?>'; + } - public static function SabaFormIf($a,$c) - { - $if = array(); + public static function SabaFormIf($a, $c) + { + $if = []; - $operator = isset($a['operator']) ? $GLOBALS['core']->tpl->getOperator($a['operator']) : '&&'; + $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.'\')'; - } - } + $fl = self::getSabaFormFilters(); + foreach($fl as $filter) { + if (isset($a['filter_' . $filter])) { + $sign = (boolean) $a['filter_' . $filter] ? '' : '!'; + $if[] = $sign . 'tplSaba::isSabaFormFilter(\'' . $filter . '\')'; + } + } - return !empty($if) ? - ''.$c.'' - : $c; - } + return !empty($if) ? + '' . $c . '' + : $c; + } - public static function SabaFormOptions($a) - { - $dis = !empty($a['remove']) ? explode(',', $a['remove']) : array(); + public static function SabaFormOptions($a) + { + $dis = !empty($a['remove']) ? explode(',', $a['remove']) : []; - $res = ''; - $li = '
'; + $res = ''; + $li = ''; - $rs = self::getSabaFormOptions(); - foreach($rs as $k => $v) { - if (in_array($v,$dis)) { - continue; - } - $chk = 'saba_options["q_opt"]) ? \'checked="checked" \' : ""; ?>'; - $res .= sprintf($li, $v, $chk, html::escapeHTML($k)); - } + $rs = self::getSabaFormOptions(); + foreach($rs as $k => $v) { + if (in_array($v, $dis)) { + continue; + } + $chk = 'saba_options["q_opt"]) ? \'checked="checked" \' : ""; ?>'; + $res .= sprintf($li, $v, $chk, html::escapeHTML($k)); + } - if (!empty($res)) { + if (!empty($res)) { + return '