enhancePostContent/index.php

337 lines
10 KiB
PHP
Raw Normal View History

2021-08-23 23:55:52 +00:00
<?php
2021-09-02 18:35:25 +00:00
/**
* @brief enhancePostContent, a plugin for Dotclear 2
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @author Jean-Christian Denis and Contributors
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-23 23:55:52 +00:00
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
2021-08-23 23:55:52 +00:00
}
dcPage::check('contentadmin');
# -- Prepare queries and object --
2021-10-31 22:22:34 +00:00
$_filters = libEPC::getFilters();
$filters_id = $filters_combo = [];
2021-11-01 09:33:43 +00:00
foreach ($_filters as $id => $filter) {
$filters_id[$id] = $filter->name;
2021-10-31 22:22:34 +00:00
$filters_combo[$filter->name] = $id;
2021-08-23 23:55:52 +00:00
}
2021-11-01 09:33:43 +00:00
$action = $_POST['action'] ?? '';
$part = $_REQUEST['part'] ?? key($filters_id);
if (!isset($filters_id[$part])) {
return null;
}
2021-08-23 23:55:52 +00:00
$header = '';
2021-10-31 22:22:34 +00:00
$filter = $_filters[$part];
2022-11-13 20:30:48 +00:00
$records = new epcRecords();
2021-08-23 23:55:52 +00:00
# -- Action --
if (!empty($action)) {
# --BEHAVIOR-- enhancePostContentAdminSave
2022-11-13 20:30:48 +00:00
dcCore::app()->callBehavior('enhancePostContentAdminSave');
2021-08-23 23:55:52 +00:00
}
try {
# Update filter settings
if ($action == 'savefiltersetting') {
# Parse filters options
$f = [
2021-10-23 19:39:47 +00:00
'nocase' => !empty($_POST['filter_nocase']),
'plural' => !empty($_POST['filter_plural']),
2021-11-06 13:46:37 +00:00
'limit' => abs((int) $_POST['filter_limit']),
2021-10-23 19:39:47 +00:00
'style' => (array) $_POST['filter_style'],
'notag' => (string) $_POST['filter_notag'],
'tplValues' => (array) $_POST['filter_tplValues'],
2022-11-13 20:30:48 +00:00
'pubPages' => (array) $_POST['filter_pubPages'],
];
2022-11-13 20:30:48 +00:00
dcCore::app()->blog->settings->addNamespace('enhancePostContent');
dcCore::app()->blog->settings->enhancePostContent->put('enhancePostContent_' . $filter->id(), serialize($f));
2022-11-13 20:30:48 +00:00
dcCore::app()->blog->triggerBlog();
dcPage::addSuccessNotice(
__('Filter successfully updated.')
);
2021-09-04 17:03:02 +00:00
2022-11-13 20:30:48 +00:00
dcCore::app()->adminurl->redirect(
2021-11-01 09:33:43 +00:00
'admin.plugin.enhancePostContent',
['part' => $part],
2021-09-04 17:03:02 +00:00
'#settings'
);
}
# Add new filter record
if ($action == 'savenewrecord'
2021-10-23 19:39:47 +00:00
&& !empty($_POST['new_key'])
&& !empty($_POST['new_value'])
) {
2021-11-01 09:33:43 +00:00
$cur = $records->openCursor();
2021-10-31 22:22:34 +00:00
$cur->epc_filter = $filter->id();
2021-10-23 19:39:47 +00:00
$cur->epc_key = html::escapeHTML($_POST['new_key']);
$cur->epc_value = html::escapeHTML($_POST['new_value']);
2021-09-04 22:43:09 +00:00
if ($records->isRecord($cur->epc_filter, $cur->epc_key)) {
dcPage::addErrorNotice(__('Key already exists for this filter'));
} else {
$records->addRecord($cur);
2022-11-13 20:30:48 +00:00
dcCore::app()->blog->triggerBlog();
2021-09-04 22:43:09 +00:00
dcPage::addSuccessNotice(
__('Filter successfully updated.')
);
}
2022-11-13 20:30:48 +00:00
dcCore::app()->adminurl->redirect(
2021-11-01 09:33:43 +00:00
'admin.plugin.enhancePostContent',
['part' => $part],
2021-09-04 17:03:02 +00:00
'#record'
);
}
# Update filter records
2021-11-01 09:33:43 +00:00
if ($action == 'deleterecords' && $filter->has_list
2022-11-13 20:30:48 +00:00
&& !empty($_POST['epc_id']) && is_array($_POST['epc_id'])
2021-10-23 19:39:47 +00:00
) {
2021-11-01 09:33:43 +00:00
foreach ($_POST['epc_id'] as $id) {
$records->delRecord($id);
}
2022-11-13 20:30:48 +00:00
dcCore::app()->blog->triggerBlog();
dcPage::addSuccessNotice(
__('Filter successfully updated.')
);
if (!empty($_REQUEST['redir'])) {
http::redirect($_REQUEST['redir']);
} else {
2022-11-13 20:30:48 +00:00
dcCore::app()->adminurl->redirect(
2021-11-01 09:33:43 +00:00
'admin.plugin.enhancePostContent',
['part' => $part],
'#record'
2021-09-04 22:43:09 +00:00
);
}
}
2021-11-01 09:33:43 +00:00
} catch (Exception $e) {
2022-11-13 20:30:48 +00:00
dcCore::app()->error->add($e->getMessage());
2021-08-23 23:55:52 +00:00
}
# -- Prepare page --
2021-10-31 22:22:34 +00:00
if ($filter->has_list) {
2022-11-13 20:30:48 +00:00
$sorts = new adminGenericFilter(dcCore::app(), 'epc');
$sorts->add(dcAdminFilters::getPageFilter());
$sorts->add('part', $part);
2021-08-23 23:55:52 +00:00
2021-11-01 09:33:43 +00:00
$params = $sorts->params();
2021-10-31 22:22:34 +00:00
$params['epc_filter'] = $filter->id();
try {
$list = $records->getRecords($params);
$counter = $records->getRecords($params, true);
2022-11-13 20:30:48 +00:00
$pager = new adminEpcList(dcCore::app(), $list, $counter->f(0));
} catch (Exception $e) {
2022-11-13 20:30:48 +00:00
dcCore::app()->error->add($e->getMessage());
}
2022-11-13 20:30:48 +00:00
$header = $sorts->js(dcCore::app()->adminurl->get('admin.plugin.enhancePostContent', ['part' => $part], '&') . '#record');
2021-08-23 23:55:52 +00:00
}
# -- Display page --
# Page headers
2021-08-23 23:55:52 +00:00
echo '
<html><head><title>' . __('Enhance post content') . '</title>' .
dcPage::jsPageTabs() .
2021-10-21 20:22:02 +00:00
dcPage::jsLoad(dcPage::getPF('enhancePostContent/js/index.js')) .
$header .
2021-08-23 23:55:52 +00:00
# --BEHAVIOR-- enhancePostContentAdminHeader
2022-11-13 20:30:48 +00:00
dcCore::app()->callBehavior('enhancePostContentAdminHeader') . '
2021-08-23 23:55:52 +00:00
</head><body>' .
2021-08-23 23:55:52 +00:00
# Page title
dcPage::breadcrumb([
__('Plugins') => '',
__('Enhance post content') => '',
2022-11-13 20:30:48 +00:00
$filter->name => '',
]) .
dcPage::notices() .
2021-08-23 23:55:52 +00:00
# Filters select menu list
2022-11-13 20:30:48 +00:00
'<form method="get" action="' . dcCore::app()->adminurl->get('admin.plugin.enhancePostContent') . '" id="filters_menu">' .
2021-10-28 20:57:04 +00:00
'<p class="anchor-nav"><label for="part" class="classic">' . __('Select filter:') . ' </label>' .
form::combo('part', $filters_combo, $part) . ' ' .
'<input type="submit" value="' . __('Ok') . '" />' .
form::hidden('p', 'enhancePostContent') . '</p>' .
'</form>';
2021-08-23 23:55:52 +00:00
# Filter title and description
echo '
2021-10-31 22:22:34 +00:00
<h3>' . $filter->name . '</h3>
<p>' . $filter->help . '</p>';
# Filter settings
echo '
<div class="multi-part" id="setting" title="' . __('Settings') . '">
2022-11-13 20:30:48 +00:00
<form method="post" action="' . dcCore::app()->adminurl->get('admin.plugin.enhancePostContent') . '#setting">
<div class="two-boxes odd">
<h4>' . __('Pages to be filtered') . '</h4>';
2021-11-01 09:33:43 +00:00
foreach (libEPC::blogAllowedPubPages() as $k => $v) {
echo '
<p><label for="filter_pubPages' . $v . '">' .
form::checkbox(
['filter_pubPages[]', 'filter_pubPages' . $v],
$v,
2021-10-31 22:22:34 +00:00
in_array($v, $filter->pubPages)
) .
__($k) . '</label></p>';
}
echo '
</div><div class="two-boxes even">
<h4>' . __('Filtering') . '</h4>
<p><label for="filter_nocase">' .
2021-10-31 22:22:34 +00:00
form::checkbox('filter_nocase', '1', $filter->nocase) .
__('Case insensitive') . '</label></p>
<p><label for="filter_plural">' .
2021-10-31 22:22:34 +00:00
form::checkbox('filter_plural', '1', $filter->plural) .
__('Also use the plural') . '</label></p>
<p><label for="filter_limit">' .
__('Limit the number of replacement to:') . '</label>' .
2021-11-06 13:46:37 +00:00
form::number('filter_limit', ['min' => 0, 'max' => 99, 'default' => (int) $filter->limit]) . '
</p>
<p class="form-note">' . __('Leave it blank or set it to 0 for no limit') . '</p>
</div><div class="two-boxes odd">
<h4>' . __('Contents to be filtered') . '</h4>';
2021-11-01 09:33:43 +00:00
foreach (libEPC::blogAllowedTplValues() as $k => $v) {
echo '
<p><label for="filter_tplValues' . $v . '">' .
form::checkbox(
['filter_tplValues[]', 'filter_tplValues' . $v],
$v,
2021-10-31 22:22:34 +00:00
in_array($v, $filter->tplValues)
) .
__($k) . '</label></p>';
}
echo '
</div><div class="two-boxes even">
<h4>' . __('Style') . '</h4>';
2021-11-01 09:33:43 +00:00
foreach ($filter->class as $k => $v) {
echo '
<p><label for="filter_style' . $k . '">' .
sprintf(__('Class "%s":'), $v) . '</label>' .
form::field(
2021-11-01 09:33:43 +00:00
['filter_style[]', 'filter_style' . $k],
60,
255,
2021-10-31 22:22:34 +00:00
html::escapeHTML($filter->style[$k])
) .
'</p>';
}
echo '
2021-10-31 22:22:34 +00:00
<p class="form-note">' . sprintf(__('The inserted HTML tag looks like: %s'), html::escapeHTML(str_replace('%s', '...', $filter->replace))) . '</p>
<p><label for="filter_notag">' . __('Ignore HTML tags:') . '</label>' .
2021-10-31 22:22:34 +00:00
form::field('filter_notag', 60, 255, html::escapeHTML($filter->notag)) . '
</p>
<p class="form-note">' . __('This is the list of HTML tags where content will be ignored.') . ' ' .
2021-10-31 22:22:34 +00:00
('' != $filter->htmltag ? '' : sprintf(__('Tag "%s" always be ignored.'), $filter->htmltag)) . '</p>
</div>
<div class="clear">
<p>' .
2022-11-13 20:30:48 +00:00
dcCore::app()->formNonce() .
form::hidden(['action'], 'savefiltersetting') .
form::hidden(['part'], $part) . '
<input type="submit" name="save" value="' . __('Save') . '" />
</p>
</div>
</form>
</div>';
# Filter records list
2022-11-13 20:30:48 +00:00
if ($filter->has_list && isset($sorts) && isset($pager)) {
$pager_url = dcCore::app()->adminurl->get('admin.plugin.enhancePostContent', array_diff_key($sorts->values(true), ['page' => ''])) . '&page=%s#record';
echo '
<div class="multi-part" id="record" title="' . __('Records') . '">';
$sorts->display(['admin.plugin.enhancePostContent', '#record'], form::hidden('p', 'enhancePostContent') . form::hidden('part', $part));
2021-11-01 09:33:43 +00:00
$pager->display(
$sorts,
$pager_url,
2022-11-13 20:30:48 +00:00
'<form action="' . dcCore::app()->adminurl->get('admin.plugin.enhancePostContent') . '#record" method="post" id="form-records">' .
'%s' .
'<div class="two-cols">' .
'<p class="col checkboxes-helpers"></p>' .
2021-11-01 09:33:43 +00:00
'<p class="col right">' .
form::hidden('action', 'deleterecords') .
'<input id="del-action" type="submit" name="save" value="' . __('Delete selected records') . '" /></p>' .
2022-11-13 20:30:48 +00:00
dcCore::app()->adminurl->getHiddenFormFields('admin.plugin.enhancePostContent', array_merge(['p' => 'enhancePostContent'], $sorts->values(true))) .
form::hidden('redir', dcCore::app()->adminurl->get('admin.plugin.enhancePostContent', $sorts->values(true))) .
dcCore::app()->formNonce() .
'</div>' .
'</form>'
);
echo '</div>';
# New record
echo '
<div class="multi-part" id="newrecord" title="' . __('New record') . '">
2022-11-13 20:30:48 +00:00
<form action="' . dcCore::app()->adminurl->get('admin.plugin.enhancePostContent') . '#record" method="post" id="form-create">' .
'<p><label for="new_key">' . __('Key:') . '</label>' .
2021-11-01 09:33:43 +00:00
form::field('new_key', 60, 255, ['extra_html' => 'required']) .
'</p>' .
'<p><label for="new_value">' . __('Value:') . '</label>' .
2021-11-01 09:33:43 +00:00
form::field('new_value', 60, 255, ['extra_html' => 'required']) .
'</p>
<p class="clear">' .
form::hidden(['action'], 'savenewrecord') .
form::hidden(['part'], $part) .
2022-11-13 20:30:48 +00:00
dcCore::app()->formNonce() . '
<input id="new-action" type="submit" name="save" value="' . __('Save') . '" />
</p>
</form>
</div>';
2021-08-23 23:55:52 +00:00
}
# --BEHAVIOR-- enhancePostContentAdminPage
2022-11-13 20:30:48 +00:00
dcCore::app()->callBehavior('enhancePostContentAdminPage');
2021-08-23 23:55:52 +00:00
dcPage::helpBlock('enhancePostContent');
2021-11-01 09:33:43 +00:00
echo '</body></html>';