blog->settings->addNamespace('saba'); 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'] = [ '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; $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'; # retreive _GET $qs = $_SERVER['QUERY_STRING']; $qs = preg_replace('#(^|/)page/([0-9]+)#', '', $qs); parse_str($qs, $get); $params = [ 'sql'=>'', 'post_type' => [] ]; # 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"; } } # 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'; } # 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) . ') '; } } #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( ['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); } # 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; } # Ajouter la condition "ou" à la recherche public static function corePostSearch($core, $p) { $sentences = explode(',', $p[2]['search']); $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; if ($e->getCode() == 404) { $q = explode('/', $args); if (empty($q)) { return false; } # Clean URI $_GET['q'] = implode('%20', $q); $_SERVER['QUERY_STRING'] = ''; # Claim comes from 404 $GLOBALS['_from_error'] = true; # Serve saba self::serveDocument('saba_search.html'); return true; } } public static function saba($args) { $_ctx =& $GLOBALS['_ctx']; $core =& $GLOBALS['core']; self::serveDocument('saba_search.html'); } } class tplSaba { 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 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)'; } if (isset($a['from_error'])) { $sign = (boolean) $a['from_error'] ? '' : '!'; $if[] = $sign . 'isset($_from_error)'; } return !empty($if) ? '' . $c . '' : $c; } public static function SabaURL($a) { $f = $GLOBALS['core']->tpl->getFilters($a); return 'blog->url.$core->url->getBase("search")') . '; ?>'; } public static function SabaFormIf($a, $c) { $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 . '\')'; } } return !empty($if) ? '' . $c . '' : $c; } public static function SabaFormOptions($a) { $dis = !empty($a['remove']) ? explode(',', $a['remove']) : []; $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)); } if (!empty($res)) { return '