diff --git a/_prepend.php b/_prepend.php index 9f614c3..5161d91 100644 --- a/_prepend.php +++ b/_prepend.php @@ -17,5 +17,6 @@ if (!defined('DC_RC_PATH')) { $d = dirname(__FILE__) . '/inc/'; -$__autoload['libEPC'] = $d . 'lib.epc.php'; -$__autoload['epcRecords'] = $d . 'lib.epc.records.php'; \ No newline at end of file +$__autoload['libEPC'] = $d . 'lib.epc.php'; +$__autoload['epcRecords'] = $d . 'lib.epc.records.php'; +$__autoload['adminEpcList'] = $d . 'lib.epc.pager.php'; \ No newline at end of file diff --git a/inc/lib.epc.pager.php b/inc/lib.epc.pager.php new file mode 100644 index 0000000..aa06d6f --- /dev/null +++ b/inc/lib.epc.pager.php @@ -0,0 +1,76 @@ +rs->isEmpty()) { + if ($filter->show()) { + echo '

' . __('No record matches the filter') . '

'; + } else { + echo '

' . __('No record') . '

'; + } + } else { + $pager = new dcPager($filter->page, $this->rs_count, $filter->nb, 10); + $pager->base_url = $pager_url; + + $epc_id = []; + if (isset($_REQUEST['epc_id'])) { + foreach ($_REQUEST['epc_id'] as $v) { + $epc_id[(integer) $v] = true; + } + } + + $cols = [ + 'key' => '' . __('Key') . '', + 'value' => '' . __('Value') . '', + 'date' => '' . __('Date') . '' + ]; + + $html_block = + '
' . + '' . implode($cols) . '%s
' . + ($filter->show() ? + sprintf(__('List of %s records matching the filter.'), $this->rs_count) : + sprintf(__('List of %s records.'), $this->rs_count) + ). '
%s
'; + + if ($enclose_block) { + $html_block = sprintf($enclose_block, $html_block); + } + $blocks = explode('%s', $html_block); + + echo $pager->getLinks() . $blocks[0]; + + while ($this->rs->fetch()) { + echo $this->line(isset($epc_id[$this->rs->epc_id])); + } + + echo $blocks[1] . $blocks[2] . $pager->getLinks(); + } + } + + private function line($checked) + { + $cols = [ + 'check' => '' . form::checkbox(['epc_id[]'], $this->rs->epc_id, ['checked' => $checked]) . '', + 'key' => '' . html::escapeHTML($this->rs->epc_key) . '', + 'value' => '' . html::escapeHTML($this->rs->epc_value) . '', + 'date' => '' . dt::dt2str(__('%Y-%m-%d %H:%M'), $this->rs->epc_upddt) . '' + ]; + + return + '' . + implode($cols) . + ''; + } +} \ No newline at end of file diff --git a/inc/lib.epc.php b/inc/lib.epc.php index f621321..0d6919d 100644 --- a/inc/lib.epc.php +++ b/inc/lib.epc.php @@ -328,6 +328,7 @@ class libEPC if (isset($opt[$name]['pubPages'])) { $filters[$name]['pubPages'] = (array) $opt[$name]['pubPages']; } + $filters[$name]['name'] = $name; } $core->callBehavior('enhancePostContentBlogFilters', $filters); diff --git a/index.php b/index.php index ba046f8..1dab43f 100644 --- a/index.php +++ b/index.php @@ -21,14 +21,22 @@ dcPage::check('contentadmin'); # -- Prepare queries and object -- $_filters = libEPC::blogFilters(); -$filters_id = array(); + +$filters_id = $filters_combo = []; foreach($_filters as $name => $filter) { $filters_id[$filter['id']] = $name; + $filters_combo[__($name)] = $filter['id']; } -$action = isset($_POST['action']) ? $_POST['action'] : ''; -$default_part = isset($_REQUEST['part']) ? $_REQUEST['part'] : key($filters_id); +$action = $_POST['action'] ?? ''; +$part = $_REQUEST['part'] ?? key($filters_id); +if (!isset($filters_id[$part])) { + return null; +} + +$header = ''; +$filter = $_filters[$filters_id[$part]]; $records = new epcRecords($core); # -- Action -- @@ -40,11 +48,8 @@ if (!empty($action)) { try { # Update filter settings - if ($action == 'savefiltersetting' - && isset($filters_id[$default_part]) - ) { + if ($action == 'savefiltersetting') { # Parse filters options - $name = $filters_id[$default_part]; $f = [ 'nocase' => !empty($_POST['filter_nocase']), 'plural' => !empty($_POST['filter_plural']), @@ -56,7 +61,7 @@ try { ]; $core->blog->settings->addNamespace('enhancePostContent'); - $core->blog->settings->enhancePostContent->put('enhancePostContent_' . $name, serialize($f)); + $core->blog->settings->enhancePostContent->put('enhancePostContent_' . $filter['name'], serialize($f)); $core->blog->triggerBlog(); @@ -66,19 +71,18 @@ try { $core->adminurl->redirect( 'admin.plugin.enhancePostContent', - ['part' => $default_part], + ['part' => $part], '#settings' ); } # Add new filter record if ($action == 'savenewrecord' - && isset($filters_id[$default_part]) && !empty($_POST['new_key']) && !empty($_POST['new_value']) ) { $cur = $records->openCursor(); - $cur->epc_filter = $filters_id[$default_part]; + $cur->epc_filter = $filter['name']; $cur->epc_key = html::escapeHTML($_POST['new_key']); $cur->epc_value = html::escapeHTML($_POST['new_value']); @@ -95,55 +99,34 @@ try { } $core->adminurl->redirect( 'admin.plugin.enhancePostContent', - ['part' => $default_part], + ['part' => $part], '#record' ); } # Update filter records - $error = false; - if ($action == 'saveupdaterecords' - && isset($filters_id[$default_part]) - && $_filters[$filters_id[$default_part]]['has_list'] + if ($action == 'deleterecords' && $filter['has_list'] + && !empty($_POST['epc_id']) && is_array($_POST['epc_id']) ) { - foreach($_POST['epc_id'] as $k => $id) { - $k = abs((integer) $k); - $id = abs((integer) $id); - - if (empty($_POST['epc_key'][$k]) - || empty($_POST['epc_value'][$k]) - ) { - $records->delRecord($id); - } elseif ($_POST['epc_key'][$k] != $_POST['epc_old_key'][$k] - || $_POST['epc_value'][$k] != $_POST['epc_old_value'][$k] - ) { - $cur = $records->openCursor(); - $cur->epc_filter = $filters_id[$default_part]; - $cur->epc_key = html::escapeHTML($_POST['epc_key'][$k]); - $cur->epc_value = html::escapeHTML($_POST['epc_value'][$k]); - - if ($records->isRecord($cur->epc_filter, $cur->epc_key, $id)) { - dcPage::addErrorNotice(__('Key already exists for this filter')); - $error = true; - } else { - $records->updRecord($id, $cur); - } - } + foreach($_POST['epc_id'] as $id) { + $records->delRecord($id); } $core->blog->triggerBlog(); - $redir = !empty($_REQUEST['redir']) ? - $_REQUEST['redir'] : - $core->adminurl->get('admin.plugin.enhancePostContent', ['part' => $default_part]) . '#record'; - if (!$error) { - dcPage::addSuccessNotice( - __('Filter successfully updated.') + dcPage::addSuccessNotice( + __('Filter successfully updated.') + ); + + if (!empty($_REQUEST['redir'])) { + http::redirect($_REQUEST['redir']); + } else { + $core->adminurl->redirect( + 'admin.plugin.enhancePostContent', + ['part' => $part], + '#record' ); } - http::redirect( - $redir - ); } } catch(Exception $e) { $core->error->add($e->getMessage()); @@ -151,267 +134,201 @@ try { # -- Prepare page -- -$breadcrumb = [html::escapeHTML($core->blog->name) => '', __('Enhance post content') => '', __('Filters') => '']; +if ($filter['has_list']) { + $sorts = new adminGenericFilter($core, 'epc'); + $sorts->add(dcAdminFilters::getPageFilter()); + $sorts->add('part', $part); -$filters_combo = []; -foreach($filters_id as $id => $name) { - if ($default_part == $id) { - $breadcrumb[__($filters_id[$default_part])] = ''; + $params = $sorts->params(); + $params['epc_filter'] = $filter['name']; + + try { + $list = $records->getRecords($params); + $counter = $records->getRecords($params, true); + $pager = new adminEpcList($core, $list, $counter->f(0)); + + } catch (Exception $e) { + $core->error->add($e->getMessage()); } - $filters_combo[__($name)] = $id; + + $header = $sorts->js($core->adminurl->get('admin.plugin.enhancePostContent', ['part' => $part], '&').'#record'); } # -- Display page -- -# Headers +# Page headers echo ' ' . __('Enhance post content') . '' . //dcPage::jsLoad('js/_posts_list.js') . dcPage::jsToolbar() . dcPage::jsPageTabs() . dcPage::jsLoad(dcPage::getPF('enhancePostContent/js/index.js')) . +$header . # --BEHAVIOR-- enhancePostContentAdminHeader $core->callBehavior('enhancePostContentAdminHeader', $core) . ' ' . -# Title -dcPage::breadcrumb($breadcrumb) . +# Page title +dcPage::breadcrumb([ + __('Plugins') => '', + __('Enhance post content') => '', + __($filter['name']) => '' +]) . dcPage::notices() . -# Filters list -'
' . +# Filters select menu list +'' . '

' . - form::combo('part', $filters_combo, $default_part) . ' ' . -$core->formNonce() . -'

' . +form::combo('part', $filters_combo, $part) . ' ' . +'' . +form::hidden('p', 'enhancePostContent') . '

' . '
'; -# Filter content -if (isset($filters_id[$default_part])) { - $name = $filters_id[$default_part]; - $filter = $_filters[$name]; +# Filter title and description +echo ' +

' . __($filter['name']) . '

+

' . $filter['help'] . '

'; - # Filter title and description +# Filter settings +echo ' +
+
+ +
+

' . __('Pages to be filtered') . '

'; + +foreach(libEPC::blogAllowedPubPages() as $k => $v) { echo ' -

' . __($filters_id[$default_part]) . '

-

' . $filter['help'] . '

'; +

'; +} - # Filter settings +echo ' +
+

' . __('Filtering') . '

+ +

+ +

+ +

' . +form::number('filter_limit', ['min' => 0, 'max' => 99, 'default' => (integer) $filter['limit']]) . ' +

+

' . __('Leave it blank or set it to 0 for no limit') . '

+ +
+

' . __('Contents to be filtered') . '

'; + +foreach(libEPC::blogAllowedTplValues() as $k => $v) { echo ' -
-
'; +

'; +} - echo - '
-

' . __('Pages to be filtered') . '

'; +echo ' +
+

' . __('Style') . '

'; - foreach(libEPC::blogAllowedPubPages() as $k => $v) { - echo ' -

'; - } +foreach($filter['class'] as $k => $v) { + echo ' +

' . + form::field( + ['filter_style[]', 'filter_style'.$k], + 60, + 255, + html::escapeHTML($filter['style'][$k]) + ) . + '

'; +} - echo - '
'; +echo ' +

' . sprintf(__('The inserted HTML tag looks like: %s'), html::escapeHTML(str_replace('%s', '...', $filter['replace']))) . '

- echo - '
-

' . __('Filtering') . '

+

' . +form::field('filter_notag', 60, 255, html::escapeHTML($filter['notag'])) . ' +

+

' . __('This is the list of HTML tags where content will be ignored.') . ' ' . +(empty($filter['htmltag']) ? '' : sprintf(__('Tag "%s" always be ignored.'), $filter['htmltag'])) . '

+
+
+

' . +$core->formNonce() . +form::hidden(['action'], 'savefiltersetting') . +form::hidden(['part'], $part) . ' + +

+
-

+ +
'; -

- -

' . - form::field('filter_limit', 4, 10, html::escapeHTML($filter['limit'])) . ' -

-

' . __('Leave it blank or set it to 0 for no limit') . '

- -
'; - - echo - '
-

' . __('Contents to be filtered') . '

'; - - foreach(libEPC::blogAllowedTplValues() as $k => $v) { - echo ' -

'; - } - - echo - '
'; - - echo - '
-

' . __('Style') . '

'; - - foreach($filter['class'] as $k => $v) { - echo ' -

' . - form::field( - ['filter_style[]', 'filter_style'.$k], - 60, - 255, - html::escapeHTML($filter['style'][$k]) - ) . - '

'; - } +# Filter records list +if ($filter['has_list']) { + $pager_url = $core->adminurl->get('admin.plugin.enhancePostContent', array_diff_key($sorts->values(true), ['page' => ''])).'&page=%s#record'; echo ' -

' . sprintf(__('The inserted HTML tag looks like: %s'), html::escapeHTML(str_replace('%s', '...', $filter['replace']))) . '

+
'; -

' . - form::field('filter_notag', 60, 255, html::escapeHTML($filter['notag'])) . ' + $sorts->display(['admin.plugin.enhancePostContent', '#record'], form::hidden('p', 'enhancePostContent') . form::hidden('part', $part)); + + $pager->display($sorts, $pager_url, + '

' . + '%s' . + + '
' . + '

' . + + '

' . + form::hidden('action', 'deleterecords') . + '

' . + $core->adminurl->getHiddenFormFields('admin.plugin.enhancePostContent', array_merge(['p' => 'enhancePostContent'], $sorts->values(true))) . + form::hidden('redir', $core->adminurl->get('admin.plugin.enhancePostContent', $sorts->values(true))) . + $core->formNonce() . + '
' . + '
' + ); + + echo '
'; + + # New record + echo ' +
+
' . + + '

' . + form::field('new_key', 60, 255, ['extra_html' => 'required']) . + '

' . + + '

' . + form::field('new_value', 60, 255, ['extra_html' => 'required']) . + '

+ +

' . + form::hidden(['action'], 'savenewrecord') . + form::hidden(['part'], $part) . + $core->formNonce() . ' +

-

' . __('This is the list of HTML tags where content will be ignored.') . ' ' . - (empty($filter['htmltag']) ? '' : sprintf(__('Tag "%s" always be ignored.'), $filter['htmltag'])) . '

- -
'; - - echo '
-
-

' . - $core->formNonce() . - form::hidden(['action'], 'savefiltersetting') . ' - -

-
-
'; - - # Filter records list - if ($filter['has_list']) { - $sorts = new adminGenericFilter($core, 'epc'); - $sorts->add(dcAdminFilters::getPageFilter()); - - $params = $sorts->params(); - $params['epc_filter'] = $name; - - try { - $list = $records->getRecords($params); - $counter = $records->getRecords($params, true); - - $pager_url = $p_url . - '&nb=' . $sorts->nb . - '&sortby=%s' . - '&order=%s' . - '&page=%s' . - '&part=' . $default_part . - '#record'; - - $pager = new dcPager($sorts->page, $counter->f(0), $sorts->nb, 10); - $pager->base_url = sprintf($pager_url, $sorts->sortby, $sorts->order, '%s'); - } catch (Exception $e) { - $core->error->add($e->getMessage()); - } - - echo ' -
'; - - if ($core->error->flag() || $list->isEmpty()) { - echo '

' . __('No record') . '

'; - } else { - echo ' -
' . - - $pager->getLinks() . ' - -
- - '; - - $lines = [ - __('Key') => 'epc_key', - __('Value') => 'epc_value', - __('Date') => 'epc_date' - ]; - foreach($lines as $k => $v) { - echo ''; - } - - echo ' - - '; - - while($list->fetch()) { - echo ' - - - - - '; - } - - echo ' - -
' .$k . '
' . - form::hidden(['epc_id[]'], $list->epc_id) . - form::hidden(['epc_old_key[]'], html::escapeHTML($list->epc_key)) . - form::hidden(['epc_old_value[]'], html::escapeHTML($list->epc_value)) . - form::field(['epc_key[]'], 30, 225, html::escapeHTML($list->epc_key), '') . '' . - form::field(['epc_value[]'], 90, 225, html::escapeHTML($list->epc_value), '') . '' . - dt::dt2str(__('%Y-%m-%d %H:%M'), $list->epc_upddt,$core->auth->getInfo('user_tz')) . '
-

' . __('In order to remove a record, leave empty its key or value.') . '

' . - - $pager->getLinks() . ' - -
-

' . - $core->formNonce() . - form::hidden(['redir'], sprintf($pager_url, $sorts->sortby, $sorts->order, $sorts->page)) . - form::hidden(['action'], 'saveupdaterecords') . ' - -

-
- -
'; - } - - echo '
'; - - # New record - echo ' -
-
' . - - '

' . - form::field('new_key', 60, 255) . - '

' . - - '

' . - form::field('new_value', 60, 255) . - '

- -

' . - form::hidden(['action'], 'savenewrecord') . - $core->formNonce() . ' - -

-
-
'; - } } # --BEHAVIOR-- enhancePostContentAdminPage diff --git a/js/index.js b/js/index.js index 70df43a..b10641c 100644 --- a/js/index.js +++ b/js/index.js @@ -2,7 +2,12 @@ 'use strict'; $(function () { - $('#part').on('change', function () { - this.form.submit(); + $('#filters_menu input[type=submit]').hide(); + $('#filters_menu #part').on('change', function () {this.form.submit();}); + + $('.checkboxes-helpers').each(function () { + dotclear.checkboxesHelpers(this, undefined, '#form-records td input[type=checkbox]', '#form-records #del-action'); }); + $('#form-records td input[type=checkbox]').enableShiftClick(); + dotclear.condSubmit('#form-records td input[type=checkbox]', '#form-records #del-action'); }); \ No newline at end of file