add visible redirection option

This commit is contained in:
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_destination',
'alias_position',
'alias_redirect'
])
->where('blog_id = ' . $sql->quote((string) dcCore::app()->blog->id))
->order('alias_position ASC')
@ -81,7 +82,7 @@ class Alias
$this->deleteAliases();
foreach ($aliases as $k => $v) {
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 $destination The destination
* @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
if (is_null(dcCore::app()->blog)) {
@ -122,6 +124,7 @@ class Alias
$cur->setField('alias_url', (string) $url);
$cur->setField('alias_destination', (string) $destination);
$cur->setField('alias_position', abs((int) $position));
$cur->setField('alias_redirect', (int) $redirect);
$cur->insert();
}

View File

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

View File

@ -18,6 +18,7 @@ use dcCore;
use dcNsProcess;
use dcPage;
use Dotclear\Helper\Html\Form\{
Checkbox,
Div,
Form,
Hidden,
@ -80,7 +81,7 @@ class Manage extends dcNsProcess
# New alias
if (isset($_POST['alias_url'])) {
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.'));
dcCore::app()->adminurl->redirect('admin.plugin.' . My::id());
} catch (Exception $e) {
@ -128,6 +129,10 @@ class Manage extends dcNsProcess
(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 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([
dcCore::app()->formNonce(false),
(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 destination') . '</th>' .
'<th class="nowrap" scope="col">' . __('Alias position') . '</th>' .
'<th class="nowrap" scope="col">' . __('Redrection') . '</th>' .
'</tr>' .
'</thead><tbody>';
foreach ($aliases as $k => $v) {
echo
'<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>' .
'<td class="maximal">' .
'<td class="minimal">' .
(new Input(['a[' . $k . '][alias_destination]']))->size(50)->maxlenght(255)->value(Html::escapeHTML($v['alias_destination']))->render() . '</td>' .
'<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>' .
'<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>';
}

View File

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