add visible redirection option

master
Jean-Christian Paul Denis 2023-04-30 23:44:37 +02:00
parent ab388b136e
commit 39a933e5a1
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
4 changed files with 27 additions and 7 deletions

View File

@ -51,6 +51,7 @@ class Alias
'alias_url', 'alias_url',
'alias_destination', 'alias_destination',
'alias_position', 'alias_position',
'alias_redirect'
]) ])
->where('blog_id = ' . $sql->quote((string) dcCore::app()->blog->id)) ->where('blog_id = ' . $sql->quote((string) dcCore::app()->blog->id))
->order('alias_position ASC') ->order('alias_position ASC')
@ -81,7 +82,7 @@ class Alias
$this->deleteAliases(); $this->deleteAliases();
foreach ($aliases as $k => $v) { foreach ($aliases as $k => $v) {
if (!empty($v['alias_url']) && !empty($v['alias_destination'])) { if (!empty($v['alias_url']) && !empty($v['alias_destination'])) {
$this->createAlias($v['alias_url'], $v['alias_destination'], $k + 1); $this->createAlias($v['alias_url'], $v['alias_destination'], $k + 1, !empty($v['alias_redirect']));
} }
} }
@ -99,8 +100,9 @@ class Alias
* @param string $url The URL * @param string $url The URL
* @param string $destination The destination * @param string $destination The destination
* @param int $position The position * @param int $position The position
* @param bool $position Do redirection
*/ */
public function createAlias(string $url, string $destination, int $position): void public function createAlias(string $url, string $destination, int $position, bool $redirect): void
{ {
// nullsafe PHP < 8.0 // nullsafe PHP < 8.0
if (is_null(dcCore::app()->blog)) { if (is_null(dcCore::app()->blog)) {
@ -122,6 +124,7 @@ class Alias
$cur->setField('alias_url', (string) $url); $cur->setField('alias_url', (string) $url);
$cur->setField('alias_destination', (string) $destination); $cur->setField('alias_destination', (string) $destination);
$cur->setField('alias_position', abs((int) $position)); $cur->setField('alias_position', abs((int) $position));
$cur->setField('alias_redirect', (int) $redirect);
$cur->insert(); $cur->insert();
} }

View File

@ -41,6 +41,7 @@ class Install extends dcNsProcess
->alias_url('varchar', 255, false) ->alias_url('varchar', 255, false)
->alias_destination('varchar', 255, false) ->alias_destination('varchar', 255, false)
->alias_position('smallint', 0, false, 1) ->alias_position('smallint', 0, false, 1)
->alias_redirect('smallint', 0, false, 0)
->primary('pk_alias', 'blog_id', 'alias_url') ->primary('pk_alias', 'blog_id', 'alias_url')

View File

@ -18,6 +18,7 @@ use dcCore;
use dcNsProcess; use dcNsProcess;
use dcPage; use dcPage;
use Dotclear\Helper\Html\Form\{ use Dotclear\Helper\Html\Form\{
Checkbox,
Div, Div,
Form, Form,
Hidden, Hidden,
@ -80,7 +81,7 @@ class Manage extends dcNsProcess
# New alias # New alias
if (isset($_POST['alias_url'])) { if (isset($_POST['alias_url'])) {
try { try {
$alias->createAlias($_POST['alias_url'], $_POST['alias_destination'], count($aliases) + 1); $alias->createAlias($_POST['alias_url'], $_POST['alias_destination'], count($aliases) + 1, !empty($_POST['alias_redirect']));
dcPage::addSuccessNotice(__('Alias successfully created.')); dcPage::addSuccessNotice(__('Alias successfully created.'));
dcCore::app()->adminurl->redirect('admin.plugin.' . My::id()); dcCore::app()->adminurl->redirect('admin.plugin.' . My::id());
} catch (Exception $e) { } catch (Exception $e) {
@ -128,6 +129,10 @@ class Manage extends dcNsProcess
(new Input('alias_destination'))->size(50)->maxlenght(255), (new Input('alias_destination'))->size(50)->maxlenght(255),
]), ]),
(new Note())->class('form-note')->text(sprintf(__('Do not put blog URL "%s" in fields.'), dcCore::app()->blog->url)), (new Note())->class('form-note')->text(sprintf(__('Do not put blog URL "%s" in fields.'), dcCore::app()->blog->url)),
(new Para())->items([
(new Checkbox('alias_redirect', false))->value(1),
(new Label(__('Do visible redirection to destination'), Label::OUTSIDE_LABEL_AFTER))->for('alias_redirect')->class('classic'),
]),
(new Para())->items([ (new Para())->items([
dcCore::app()->formNonce(false), dcCore::app()->formNonce(false),
(new Hidden('part', 'new')), (new Hidden('part', 'new')),
@ -160,18 +165,21 @@ class Manage extends dcNsProcess
'<th class="nowrap" scope="col">' . __('Alias URL') . '</th>' . '<th class="nowrap" scope="col">' . __('Alias URL') . '</th>' .
'<th class="nowrap" scope="col">' . __('Alias destination') . '</th>' . '<th class="nowrap" scope="col">' . __('Alias destination') . '</th>' .
'<th class="nowrap" scope="col">' . __('Alias position') . '</th>' . '<th class="nowrap" scope="col">' . __('Alias position') . '</th>' .
'<th class="nowrap" scope="col">' . __('Redrection') . '</th>' .
'</tr>' . '</tr>' .
'</thead><tbody>'; '</thead><tbody>';
foreach ($aliases as $k => $v) { foreach ($aliases as $k => $v) {
echo echo
'<tr class="line" id="l_' . $k . '">' . '<tr class="line" id="l_' . $k . '">' .
'<td>' . '<td class="minimal">' .
(new Input(['a[' . $k . '][alias_url]']))->size(50)->maxlenght(255)->value(Html::escapeHTML($v['alias_url']))->render() . '</td>' . (new Input(['a[' . $k . '][alias_url]']))->size(50)->maxlenght(255)->value(Html::escapeHTML($v['alias_url']))->render() . '</td>' .
'<td class="maximal">' . '<td class="minimal">' .
(new Input(['a[' . $k . '][alias_destination]']))->size(50)->maxlenght(255)->value(Html::escapeHTML($v['alias_destination']))->render() . '</td>' . (new Input(['a[' . $k . '][alias_destination]']))->size(50)->maxlenght(255)->value(Html::escapeHTML($v['alias_destination']))->render() . '</td>' .
'<td class="minimal">' . '<td class="minimal">' .
(new Number(['a[' . $k . '][alias_position]']))->min(1)->max(count($aliases))->default((int) $v['alias_position'])->class('position')->title(sprintf(__('position of %s'), Html::escapeHTML($v['alias_url'])))->render() . '</td>' . (new Number(['a[' . $k . '][alias_position]']))->min(1)->max(count($aliases))->default((int) $v['alias_position'])->class('position')->title(sprintf(__('position of %s'), Html::escapeHTML($v['alias_url'])))->render() . '</td>' .
'<td class="maximal">' .
(new Checkbox(['a[' . $k . '][alias_redirect]'], (bool) $v['alias_redirect']))->title(sprintf(__('visible redirection to %s'), Html::escapeHTML(dcCore::app()->blog->url . $v['alias_destination'])))->render() . '</td>' .
'</tr>'; '</tr>';
} }

View File

@ -17,6 +17,7 @@ namespace Dotclear\Plugin\alias;
use dcCore; use dcCore;
use dcNsProcess; use dcNsProcess;
use dcUrlHandlers; use dcUrlHandlers;
use Dotclear\Helper\Network\Http;
class Prepend extends dcNsProcess class Prepend extends dcNsProcess
{ {
@ -34,18 +35,21 @@ class Prepend extends dcNsProcess
} }
dcCore::app()->addBehavior('urlHandlerGetArgsDocument', function (dcUrlHandlers $handler): void { dcCore::app()->addBehavior('urlHandlerGetArgsDocument', function (dcUrlHandlers $handler): void {
$found = false; $found = $redir = false;
$type = 'alias'; $type = 'alias';
$part = $args = $_SERVER['URL_REQUEST_PART']; $part = $args = $_SERVER['URL_REQUEST_PART'];
foreach ((new Alias())->getAliases() as $v) { foreach ((new Alias())->getAliases() as $v) {
if (@preg_match('#^/.*/$#', $v['alias_url']) && @preg_match($v['alias_url'], $args)) { if (@preg_match('#^/.*/$#', $v['alias_url']) && @preg_match($v['alias_url'], $args)) {
$part = preg_replace($v['alias_url'], $v['alias_destination'], $args); $part = preg_replace($v['alias_url'], $v['alias_destination'], $args);
$found = true; $found = true;
$redir = !empty($v['alias_redirect']);
break; break;
} elseif ($v['alias_url'] == $args) { } elseif ($v['alias_url'] == $args) {
$part = $v['alias_destination']; $part = $v['alias_destination'];
$found = true; $found = true;
$redir = !empty($v['alias_redirect']);
break; break;
} }
@ -55,7 +59,11 @@ class Prepend extends dcNsProcess
return; return;
} }
dcCore::app()->url->unregister('alias'); if ($redir) {
Http::redirect(dcCore::app()->blog->url . $part);
}
$_SERVER['URL_REQUEST_PART'] = $part;
dcCore::app()->url->getArgs($part, $type, $args); dcCore::app()->url->getArgs($part, $type, $args);
if (!$type) { if (!$type) {