Example #1
0
/**
 * Генерирует ЧПУ ссылки
 *
 * @param    string    $type    Тип ссылки(project, blog и т.д.)
 * @param    integer|array   $data      Параметры для ссылки. Если целое, то id объекта в БД, иначе готовый массив $data (см. внутрь).
 * @return   string             ЧПУ ссылка
 */
function getFriendlyURL($type, $data = NULL)
{
    static $url_cache = array();
    $url = '';
    if (!is_array($data)) {
        $id = intval($data);
        $data = NULL;
    } else {
        $id = intval($data['id']);
    }
    if (!$id) {
        return NULL;
    }
    if ($url_cache[$type][$id]) {
        return $url_cache[$type][$id];
    }
    switch ($type) {
        case 'project':
            if (!$data) {
                require_once $_SERVER['DOCUMENT_ROOT'] . "/classes/projects.php";
                $data = projects::getInfoForFriendlyURL($id);
            }
            if ($data) {
                $name = translit(strtolower(htmlspecialchars_decode($data['name'], ENT_QUOTES)));
                $url = "/projects/{$id}/" . ($name ? "{$name}.html" : "");
            }
            break;
        case 'blog':
            require_once $_SERVER['DOCUMENT_ROOT'] . "/classes/blogs.php";
            $data = blogs::getMsgInfoForFriendlyURL($id);
            if ($data) {
                $name = translit(strtolower(htmlspecialchars_decode($data['name'], ENT_QUOTES)));
                $category = translit(strtolower(htmlspecialchars_decode($data['category'], ENT_QUOTES)));
                $url = "/blogs/{$category}/{$id}/" . ($name ? $name . ".html" : "");
            }
            break;
        case 'blog_group':
            require_once $_SERVER['DOCUMENT_ROOT'] . "/classes/blogs.php";
            $data = blogs::GetGroupName($id);
            if ($data) {
                $category = translit(strtolower(htmlspecialchars_decode($data, ENT_QUOTES)));
                $url = "/blogs/" . ($category ? "{$category}/" : "");
            }
            break;
        case 'article':
            require_once $_SERVER['DOCUMENT_ROOT'] . "/classes/articles.php";
            $data = articles::getInfoForFriendlyURL($id);
            if ($data) {
                $name = translit(strtolower(htmlspecialchars_decode($data['title'], ENT_QUOTES)));
                $url = "/articles/{$id}/" . ($name ? $name . ".html" : "");
            }
            break;
        case 'interview':
            require_once $_SERVER['DOCUMENT_ROOT'] . "/classes/interview.php";
            $data = interview::getInfoForFriendlyURL($id);
            if ($data) {
                $name = translit(strtolower(htmlspecialchars_decode($data['uname'] . ' ' . $data['usurname'] . ' ' . $data['login'], ENT_QUOTES)));
                $url = "/interview/{$id}/" . ($name ? $name . ".html" : "");
            }
            break;
        case 'commune':
            require_once $_SERVER['DOCUMENT_ROOT'] . "/classes/commune.php";
            $data = commune::getMsgInfoForFriendlyURL($id);
            if ($data) {
                $category = translit(strtolower(htmlspecialchars_decode($data['group_link'], ENT_QUOTES)));
                $commune = translit(strtolower(htmlspecialchars_decode($data['commune_name'], ENT_QUOTES)));
                $name = translit(strtolower(htmlspecialchars_decode($data['name'], ENT_QUOTES)));
                $commune_id = $data['commune_id'];
                $url = "/commune/{$category}/{$commune_id}/{$commune}/{$id}/" . ($name ? $name . ".html" : "");
            }
            break;
        case 'commune_group':
            require_once $_SERVER['DOCUMENT_ROOT'] . "/classes/commune.php";
            $data = commune::getGroupInfoForFriendlyURL($id);
            if ($data) {
                $category = translit(strtolower(htmlspecialchars_decode($data['link'], ENT_QUOTES)));
                $url = "/commune/{$category}/";
            }
            break;
        case 'commune_commune':
            require_once $_SERVER['DOCUMENT_ROOT'] . "/classes/commune.php";
            $data = commune::getCommuneInfoForFriendlyURL($id);
            if ($data) {
                $category = translit(strtolower(htmlspecialchars_decode($data['category_link'], ENT_QUOTES)));
                $commune = translit(strtolower(htmlspecialchars_decode($data['name'], ENT_QUOTES)));
                $commune = $commune ? $commune : 'commune';
                $url = "/commune/{$category}/{$id}/{$commune}/";
            }
            break;
    }
    if ($url) {
        $url_cache[$type][$id] = $url;
    }
    return $url;
}