/**
* Get an unique URL for a page
*
* @param string $url The URL to base on.
* @param int $id The id to ignore.
* @param int $parentId The parent for the page to create an url for.
* @param bool $isAction Is this page an action.
*
* @return string
*/
public static function getURL($url, $id = null, $parentId = 0, $isAction = false)
{
$url = (string) $url;
$parentIds = array((int) $parentId);
// 0, 1, 2, 3, 4 are all top levels, so we should place them on the same level
if ($parentId == 0 || $parentId == 1 || $parentId == 2 || $parentId == 3 || $parentId == 4) {
$parentIds = array(0, 1, 2, 3, 4);
}
// get db
$db = BackendModel::getContainer()->get('database');
// no specific id
if ($id === null) {
// no items?
if ((bool) $db->getVar('SELECT 1
FROM pages AS i
INNER JOIN meta AS m ON i.meta_id = m.id
WHERE i.parent_id IN(' . implode(',', $parentIds) . ') AND i.status = ? AND m.url = ?
AND i.language = ?
LIMIT 1', array('active', $url, BL::getWorkingLanguage()))) {
// add a number
$url = BackendModel::addNumber($url);
// recall this method, but with a new URL
return self::getURL($url, null, $parentId, $isAction);
}
} else {
// one item should be ignored
// there are items so, call this method again.
if ((bool) $db->getVar('SELECT 1
FROM pages AS i
INNER JOIN meta AS m ON i.meta_id = m.id
WHERE i.parent_id IN(' . implode(',', $parentIds) . ') AND i.status = ?
AND m.url = ? AND i.id != ? AND i.language = ?
LIMIT 1', array('active', $url, $id, BL::getWorkingLanguage()))) {
// add a number
$url = BackendModel::addNumber($url);
// recall this method, but with a new URL
return self::getURL($url, $id, $parentId, $isAction);
}
}
// get full URL
$fullURL = self::getFullURL($parentId) . '/' . $url;
// get info about parent page
$parentPageInfo = self::get($parentId, null, BL::getWorkingLanguage());
// does the parent have extras?
if ($parentPageInfo['has_extra'] == 'Y' && !$isAction) {
// set locale
FrontendLanguage::setLocale(BL::getWorkingLanguage(), true);
// get all on-site action
$actions = FrontendLanguage::getActions();
// if the new URL conflicts with an action we should rebuild the URL
if (in_array($url, $actions)) {
// add a number
$url = BackendModel::addNumber($url);
// recall this method, but with a new URL
return self::getURL($url, $id, $parentId, $isAction);
}
}
// check if folder exists
if (is_dir(PATH_WWW . '/' . $fullURL) || is_file(PATH_WWW . '/' . $fullURL)) {
// add a number
$url = BackendModel::addNumber($url);
// recall this method, but with a new URL
return self::getURL($url, $id, $parentId, $isAction);
}
// check if it is an application
if (array_key_exists(trim($fullURL, '/'), \ApplicationRouting::getRoutes())) {
// add a number
$url = BackendModel::addNumber($url);
// recall this method, but with a new URL
return self::getURL($url, $id, $parentId, $isAction);
}
// return the unique URL!
return $url;
}