Ejemplo n.º 1
0
    /**
     * Retrieve a unique URL for a profile based on the display name.
     *
     * @param string $displayName The display name to base on.
     * @param int[optional] $id The id of the profile to ignore.
     * @return string
     */
    public static function getUrl($displayName, $id = null)
    {
        // decode specialchars
        $displayName = SpoonFilter::htmlspecialcharsDecode((string) $displayName);
        // urlise
        $url = (string) SpoonFilter::urlise($displayName);
        // get db
        $db = FrontendModel::getDB();
        // new item
        if ($id === null) {
            // get number of profiles with this URL
            $number = (int) $db->getVar('SELECT COUNT(p.id)
				 FROM profiles AS p
				 WHERE p.url = ?', (string) $url);
            // already exists
            if ($number != 0) {
                // add number
                $url = FrontendModel::addNumber($url);
                // try again
                return self::getURL($url);
            }
        } else {
            // get number of profiles with this URL
            $number = (int) $db->getVar('SELECT COUNT(p.id)
				 FROM profiles AS p
				 WHERE p.url = ? AND p.id != ?', array((string) $url, (int) $id));
            // already exists
            if ($number != 0) {
                // add number
                $url = FrontendModel::addNumber($url);
                // try again
                return self::getURL($url, $id);
            }
        }
        return $url;
    }