alias/inc/class.dc.aliases.php

112 lines
3.2 KiB
PHP
Raw Normal View History

2022-11-27 12:57:51 +00:00
<?php
2022-11-27 13:51:46 +00:00
/**
* @brief alias, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Olivier Meunier and contributors
*
2022-11-28 21:52:27 +00:00
* @copyright Jean-Christian Denis
2022-11-27 13:51:46 +00:00
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return null;
}
2022-11-27 12:57:51 +00:00
class dcAliases
{
2022-11-27 13:51:46 +00:00
protected $aliases;
public function __construct()
{
}
public function getAliases()
{
if (is_array($this->aliases)) {
return $this->aliases;
}
$this->aliases = [];
$sql = 'SELECT alias_url, alias_destination, alias_position ' .
2022-11-30 21:51:55 +00:00
'FROM ' . dcCore::app()->prefix . initAlias::ALIAS_TABLE_NAME . ' ' .
2022-11-27 13:51:46 +00:00
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
'ORDER BY alias_position ASC ';
$this->aliases = dcCore::app()->con->select($sql)->rows();
return $this->aliases;
}
public function updateAliases($aliases)
{
usort($aliases, [$this,'sortCallback']);
foreach ($aliases as $v) {
if (!isset($v['alias_url']) || !isset($v['alias_destination'])) {
throw new Exception(__('Invalid aliases definitions'));
}
}
dcCore::app()->con->begin();
try {
$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);
}
}
dcCore::app()->con->commit();
} catch (Exception $e) {
dcCore::app()->con->rollback();
throw $e;
}
}
public function createAlias($url, $destination, $position)
{
if (!$url) {
throw new Exception(__('Alias URL is empty.'));
}
if (!$destination) {
throw new Exception(__('Alias destination is empty.'));
}
2022-11-30 21:51:55 +00:00
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . initAlias::ALIAS_TABLE_NAME);
2022-11-27 13:51:46 +00:00
$cur->blog_id = (string) dcCore::app()->blog->id;
$cur->alias_url = (string) $url;
$cur->alias_destination = (string) $destination;
$cur->alias_position = abs((int) $position);
$cur->insert();
}
public function deleteAlias($url)
{
dcCore::app()->con->execute(
2022-11-30 21:51:55 +00:00
'DELETE FROM ' . dcCore::app()->prefix . initAlias::ALIAS_TABLE_NAME . ' ' .
2022-11-27 13:51:46 +00:00
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
"AND alias_url = '" . dcCore::app()->con->escape($url) . "' "
);
}
public function deleteAliases()
{
dcCore::app()->con->execute(
2022-11-30 21:51:55 +00:00
'DELETE FROM ' . dcCore::app()->prefix . initAlias::ALIAS_TABLE_NAME . ' ' .
2022-11-27 13:51:46 +00:00
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' "
);
}
protected function sortCallback($a, $b)
{
if ($a['alias_position'] == $b['alias_position']) {
return 0;
}
return $a['alias_position'] < $b['alias_position'] ? -1 : 1;
}
2022-11-27 12:57:51 +00:00
}