From 94f7e4da7e8317284920e80f385420f0b7f305ad Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Thu, 17 Nov 2022 22:41:04 +0100 Subject: [PATCH] prepare to DC 2.24 --- _admin.php | 27 +++--- _define.php | 23 +++-- _public.php | 192 ++++++++++++++++++------------------- _widgets.php | 83 +++++++--------- locales/fr/main.lang.php | 56 ----------- locales/fr/public.lang.php | 81 ---------------- locales/fr/resources.php | 9 +- 7 files changed, 156 insertions(+), 315 deletions(-) delete mode 100644 locales/fr/main.lang.php delete mode 100644 locales/fr/public.lang.php diff --git a/_admin.php b/_admin.php index 9cab066..04019f7 100644 --- a/_admin.php +++ b/_admin.php @@ -1,32 +1,31 @@ blog->settings->addNamespace('saba'); +dcCore::app()->blog->settings->addNamespace('saba'); # widget -require_once dirname(__FILE__) . '/_widgets.php'; +require_once __DIR__ . '/_widgets.php'; # behaviors -$core->addBehavior( - 'adminBlogPreferencesForm', +dcCore::app()->addBehavior( + 'adminBlogPreferencesFormV2', ['adminSaba', 'adminBlogPreferencesForm'] ); -$core->addBehavior( +dcCore::app()->addBehavior( 'adminBeforeBlogSettingsUpdate', ['adminSaba', 'adminBeforeBlogSettingsUpdate'] ); @@ -34,19 +33,19 @@ $core->addBehavior( # add settings to admin blog pref page class adminSaba { - public static function adminBlogPreferencesForm($core, $blog_settings) + public static function adminBlogPreferencesForm($blog_settings) { echo '
' . '

' . __('Search Across Blog Archive') . '

' . '

' . '

' . '

' . - __("This suggests visitors some posts on page 404.") . + __('This suggests visitors some posts on page 404.') . '

' . '
'; } @@ -56,4 +55,4 @@ class adminSaba $blog_settings->saba->put('active', !empty($_POST['saba_active'])); $blog_settings->saba->put('error', !empty($_POST['saba_error'])); } -} \ No newline at end of file +} diff --git a/_define.php b/_define.php index 7c78113..58ef1b0 100644 --- a/_define.php +++ b/_define.php @@ -1,16 +1,15 @@ registerModule( 'saba', 'Search across blog archive', 'Jean-Christian Denis and Contributors', - '2021.09.10', + '2022.11.12', [ - 'requires' => [['core', '2.19']], - 'permissions' => 'admin', - 'type' => 'plugin', - 'support' => 'https://github.com/JcDenis/saba', - 'details' => 'https://plugins.dotaddict.org/dc2/details/saba', - 'repository' => 'https://raw.githubusercontent.com/JcDenis/saba/master/dcstore.xml' + 'requires' => [['core', '2.24']], + 'permissions' => dcAuth::PERMISSION_ADMIN, + 'type' => 'plugin', + 'support' => 'https://github.com/JcDenis/saba', + 'details' => 'https://plugins.dotaddict.org/dc2/details/saba', + 'repository' => 'https://raw.githubusercontent.com/JcDenis/saba/master/dcstore.xml', ] -); \ No newline at end of file +); diff --git a/_public.php b/_public.php index 3070a32..3d0f077 100644 --- a/_public.php +++ b/_public.php @@ -1,56 +1,55 @@ blog->settings->addNamespace('saba'); +dcCore::app()->blog->settings->addNamespace('saba'); -if (!$core->blog->settings->saba->active) { +if (!dcCore::app()->blog->settings->saba->active) { return null; } # translation -l10n::set(dirname(__FILE__) . '/locales/' . $_lang . '/public'); +l10n::set(__DIR__ . '/locales/' . dcCore::app()->lang . '/public'); # widget -require_once dirname(__FILE__) . '/_widgets.php'; +require_once __DIR__ . '/_widgets.php'; # template path -$core->tpl->setPath( - $core->tpl->getPath(), - dirname(__FILE__) . '/default-templates/' +dcCore::app()->tpl->setPath( + dcCore::app()->tpl->getPath(), + __DIR__ . '/default-templates/' ); # behavior -$core->addBehavior( +dcCore::app()->addBehavior( 'templateCustomSortByAlias', ['pubSaba', 'templateCustomSortByAlias'] ); -$core->addBehavior( +dcCore::app()->addBehavior( 'urlHandlerBeforeGetData', ['pubSaba', 'urlHandlerBeforeGetData'] ); -$core->addBehavior( +dcCore::app()->addBehavior( 'coreBlogBeforeGetPosts', ['pubSaba', 'coreBlogBeforeGetPosts'] ); # url -if ($core->blog->settings->saba->error) { - $core->url->registerError(['urlSaba', 'error']); +if (dcCore::app()->blog->settings->saba->error) { + dcCore::app()->url->registerError(['urlSaba', 'error']); } class pubSaba @@ -58,54 +57,52 @@ 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' + '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 = tplSaba::getSabaDefaultPostsOptions(); if (!empty($_GET['q']) && 1 < strlen($_GET['q'])) { - # search string $params = new ArrayObject(['search' => rawurldecode($_GET['q'])]); - $options = self::getPostsParams($params); + $options = self::getPostsParams($params); $options['q'] = rawurldecode($_GET['q']); # count - $GLOBALS['_search'] = rawurldecode($_GET['q']); - if ($GLOBALS['_search']) { - $GLOBALS['_search_count'] = $core->blog->getPosts($params, true)->f(0); + 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 - $_page_number = !isset($GLOBALS['_page_number']) ? 1 : $GLOBALS['_page_number']; - $params['limit'] = $_ctx->nb_entry_per_page; + $_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 - $_ctx->post_params = $params; - $_ctx->posts = $core->blog->getPosts($params); + dcCore::app()->ctx->post_params = $params; + dcCore::app()->ctx->posts = dcCore::app()->blog->getPosts($params); unset($params); } - $_ctx->saba_options = $options; + dcCore::app()->ctx->saba_options = $options; } public static function getPostsParams($params) { - global $core; - if (!isset($params['sql'])) { $params['sql'] = ''; } @@ -118,35 +115,34 @@ class pubSaba parse_str($qs, $get); # search string - $options = tplSaba::getSabaDefaultPostsOptions(); + $options = tplSaba::getSabaDefaultPostsOptions(); $options['q'] = $params['search']; # options if (!empty($get['q_opt'])) { - if (in_array('selected', $get['q_opt'])) { - $options['q_opt'][] = 'selected'; + $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 "; + $params['sql'] = 'AND nb_comment > 0 '; } if (in_array('trackback', $get['q_opt'])) { $options['q_opt'][] = 'trackback'; - $params['sql'] = "AND nb_trackback > 0"; + $params['sql'] = 'AND nb_trackback > 0'; } } # categories if (!empty($get['q_cat'])) { - $cats = array(); - foreach($get['q_cat'] as $v) { - $v = abs((integer) $v); + $cats = []; + foreach ($get['q_cat'] as $v) { + $v = abs((int) $v); if (!$v) { continue; } - $cats[] = "C.cat_id = '" . $v . "'"; + $cats[] = "C.cat_id = '" . $v . "'"; $options['q_cat'][] = $v; } if (!empty($cats)) { @@ -156,12 +152,12 @@ class pubSaba # post types if (!empty($get['q_type'])) { - $types = $core->getPostTypes(); - foreach($get['q_type'] as $v) { + $types = dcCore::app()->getPostTypes(); + foreach ($get['q_type'] as $v) { if (!$types[$v]) { continue; } - $options['q_type'][] = $v; + $options['q_type'][] = $v; $params['post_type'][] = $v; } } else { @@ -171,8 +167,8 @@ class pubSaba # age $ages = tplSaba::getSabaFormAges(); if (!empty($get['q_age']) && in_array($get['q_age'], $ages)) { - $age = explode(',', $get['q_age']); - $ts = time(); + $age = explode(',', $get['q_age']); + $ts = time(); $options['q_age'] = $get['q_age']; if ($age[0]) { @@ -187,9 +183,9 @@ class pubSaba # user if (!empty($get['q_user'])) { - $users = array(); - foreach($get['q_user'] as $v) { - $users[] = "U.user_id = '" . $core->con->escape($v) . "'"; + $users = []; + foreach ($get['q_user'] as $v) { + $users[] = "U.user_id = '" . dcCore::app()->con->escape($v) . "'"; $options['q_user'][] = $v; } if (!empty($users)) { @@ -201,25 +197,23 @@ class pubSaba $sort = 'desc'; if (!empty($get['q_rev'])) { $options['q_rev'] = '1'; - $sort = 'asc'; + $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], + $params['order'] = dcCore::app()->tpl->getSortByStr( + new ArrayObject(['sortby' => $get['q_order'], 'order' => $sort]), 'post' ); //?! post_type } + return $options; } # Ajouter la condition "ou" ŕ la recherche public static function coreBlogBeforeGetPosts($p) { - global $core; - if (empty($p['search'])) { return; } @@ -227,19 +221,19 @@ class pubSaba self::getPostsParams($p); $OR = []; - foreach(explode(',', $p['search']) as $sentence) { - $AND = []; + foreach (explode(',', $p['search']) as $sentence) { + $AND = []; $words = text::splitWords($sentence); - foreach($words as $word) { - $AND[] = "post_words LIKE '%" . $core->con->escape($word) . "%'"; + foreach ($words as $word) { + $AND[] = "post_words LIKE '%" . dcCore::app()->con->escape($word) . "%'"; } if (!empty($AND)) { - $OR[] = " (" . implode (' AND ', $AND) . ") "; + $OR[] = ' (' . implode(' AND ', $AND) . ') '; } } if (!empty($OR)) { $p['search'] = ''; - $p['sql'] .= "AND (" . implode (' OR ', $OR) . ") "; + $p['sql'] .= 'AND (' . implode(' OR ', $OR) . ') '; } } } @@ -248,8 +242,6 @@ class urlSaba extends dcUrlHandlers { public static function error($args, $type, $e) { - global $core, $_ctx; - if ($e->getCode() == 404) { $q = explode('/', $args); if (empty($q)) { @@ -257,7 +249,7 @@ class urlSaba extends dcUrlHandlers } # Clean URI - $_GET['q'] = implode('%20', $q); + $_GET['q'] = implode('%20', $q); $_SERVER['QUERY_STRING'] = ''; # Claim comes from 404 @@ -276,70 +268,70 @@ class tplSaba public static function getSabaDefaultPostsOptions() { return [ - 'q'=> '', - 'q_opt' => [], - 'q_cat' => [], - 'q_age' => '0,0', - 'q_user'=> [], - 'q_order'=> 'date', - 'q_rev' => '0', - 'q_type'=> [] + 'q' => '', + 'q_opt' => [], + 'q_cat' => [], + 'q_age' => '0,0', + 'q_user' => [], + 'q_order' => 'date', + 'q_rev' => '0', + 'q_type' => [], ]; } public static function getSabaFormOptions() { return [ - __('Selected entry') => 'selected', - __('With comments') => 'comment', - __('With trackbacks') => 'trackback' + __('Selected entry') => 'selected', + __('With comments') => 'comment', + __('With trackbacks') => 'trackback', ]; } public static function getSabaFormOrders() { return [ - __('Title') => 'title', - __('Selected entry') => 'selected', - __('Author') => 'author', - __('Date') => 'date', - __('Update') => 'update', - __('Comments count') => 'comment', - __('Trackbacks count') => 'trackback' + __('Title') => 'title', + __('Selected entry') => 'selected', + __('Author') => 'author', + __('Date') => 'date', + __('Update') => 'update', + __('Comments count') => 'comment', + __('Trackbacks count') => 'trackback', ]; } public static function getSabaFormAges() { return [ - __('All') => '0,0', - __('Less than a month') => '0,2592000', - __('From 1 to 6 month') => '2592000,15552000', + __('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' + __('More than a year') => '31536000,0', ]; } public static function getSabaFormTypes() { $know = [ - 'post' => __('Entry'), - 'page' => __('Page'), + 'post' => __('Entry'), + 'page' => __('Page'), 'pollsfactory' => __('Poll'), - 'eventhandler' => __('Event') + 'eventhandler' => __('Event'), ]; // todo: add behavior for unknow types - $rs = []; - $types = $GLOBALS['core']->getPostTypes(); + $rs = []; + $types = dcCore::app()->getPostTypes(); - foreach($types as $k => $v) { + foreach ($types as $k => $v) { if (!$v['public_url']) { continue; } - $rs[isset($know[$k]) ? $know[$k] : __($k)] = $k; + $rs[$know[$k] ?? __($k)] = $k; } return $rs; } -} \ No newline at end of file +} diff --git a/_widgets.php b/_widgets.php index 0d524ba..d9a5d48 100644 --- a/_widgets.php +++ b/_widgets.php @@ -1,33 +1,30 @@ addBehavior('initWidgets', ['sabaWidget', 'setWidget']); +dcCore::app()->addBehavior('initWidgets', ['sabaWidget', 'setWidget']); class sabaWidget { public static function setWidget($w) { - global $core; - $w ->create( 'saba', __('Advanced search'), - array('sabaWidget', 'getWidget'), + ['sabaWidget', 'getWidget'], null, __('Add more search options on public side') ) @@ -79,15 +76,13 @@ class sabaWidget public static function getWidget($w) { - global $core, $_ctx; + dcCore::app()->blog->settings->addNamespace('saba'); - $core->blog->settings->addNamespace('saba'); - - if (!$core->blog->settings->saba->active) { + if (!dcCore::app()->blog->settings->saba->active) { return; } - if (!$core->blog->settings->saba->error && $core->url->type == '404') { + if (!dcCore::app()->blog->settings->saba->error && dcCore::app()->url->type == '404') { return; } @@ -95,12 +90,11 @@ class sabaWidget return; } - $q = $_ctx->saba_otpion ?? ''; + $q = dcCore::app()->ctx->saba_otpion ?? ''; # title and search - $res = - ($w->title ? $w->renderTitle('') : '') . - '
' . + $res = ($w->title ? $w->renderTitle('') : '') . + '' . '

placeholder ? 'placeholder="' . html::escapeHTML($w->placeholder) . '"' : '') . @@ -108,22 +102,20 @@ class sabaWidget '

' ; # advenced search only on search page - if ($core->url->type == 'search') { - + if (dcCore::app()->url->type == 'search') { # order if (!$w->saba_filter_orders) { $ct = ''; - foreach(tplSaba::getSabaFormOrders() as $k => $v) { - $ct .= - '
  • '; } if (!empty($ct)) { - $ct .= '
  • '; $res .= $w->renderTitle(__('Filter order')) . sprintf('', $ct); } @@ -134,14 +126,13 @@ class sabaWidget $ct = ''; $rm = explode(',', $w->saba_remove_options); - foreach(tplSaba::getSabaFormOptions() as $k => $v) { + foreach (tplSaba::getSabaFormOptions() as $k => $v) { if (in_array($v, $rm)) { continue; } - $ct .= - '
  • '; } if (!empty($ct)) { @@ -153,11 +144,10 @@ class sabaWidget if (!$w->saba_filter_ages) { $ct = ''; - foreach(tplSaba::getSabaFormAges() as $k => $v) { - $ct .= - '
  • '; } if (!empty($ct)) { @@ -170,14 +160,13 @@ class sabaWidget $ct = ''; $rm = explode(',', $w->saba_remove_types); - foreach(tplSaba::getSabaFormTypes() as $k => $v) { + foreach (tplSaba::getSabaFormTypes() as $k => $v) { if (in_array($v, $rm)) { continue; } - $ct .= - '
  • '; } if (!empty($ct)) { @@ -189,16 +178,15 @@ class sabaWidget if (!$w->saba_filter_categories) { $ct = ''; $rm = explode(',', $w->saba_remove_categories); - $rs = $core->blog->getCategories(); + $rs = dcCore::app()->blog->getCategories(); while ($rs->fetch()) { if (in_array($rs->cat_id, $rm) || in_array($rs->cat_url, $rm)) { - continue; + continue; } - $ct .= - '
  • '; } if (!empty($ct)) { @@ -210,16 +198,15 @@ class sabaWidget if (!$w->saba_filter_authors) { $ct = ''; $rm = explode(',', $w->saba_remove_authors); - $rs = $core->blog->getPostsUsers(); + $rs = dcCore::app()->blog->getPostsUsers(); while ($rs->fetch()) { if (in_array($rs->user_id, $rm)) { - continue; + continue; } - $ct .= - '
  • '; } if (!empty($ct)) { @@ -232,4 +219,4 @@ class sabaWidget return $w->renderDiv($w->content_only, $w->class, 'id="search"', $res); } -} \ No newline at end of file +} diff --git a/locales/fr/main.lang.php b/locales/fr/main.lang.php deleted file mode 100644 index d02bf13..0000000 --- a/locales/fr/main.lang.php +++ /dev/null @@ -1,56 +0,0 @@ -resources['help']['core_plugins_conf'] = __DIR__ . '/help/help.html'; +}