jablonka.czprosek.czf

freenet-router

Subversion Repositories:
[/] [trunk/] [freenet-router/] [var/] [www/] [freenet-router/] [Framework/] [Environment/] [LinkBuilder.php] - Rev 2

Compare with Previous - Blame - Download


<?php

namespace Phem\Environment;

/**
 * Description of LinkBuilder
 *
 * @author Vejvis
 */
class LinkBuilder
{

    public function link($params)
    {
        //EnvironmentManager::getLogger()->append("LinkBuilder -> link()", $params);
        if (is_array($params))
        {
            return $this->linkFromArray($params);
        }
        else
        {
            return $this->linkFomString($params);
        }
    }
    
    public function linkToResource($pathFromResources)
    {
        if (defined("RESOURCES_SUBDOMAIN"))
        {
            return "http://".RESOURCES_SUBDOMAIN."/".$pathFromResources;
        }
        else
        {
            return SITE_SUBDIR . DS . RESOURCES_SYMLINK . DS . $pathFromResources;
        }
    }
                    
    public function externalLink($params)
    {
        return "http://" . $_SERVER["SERVER_NAME"] . $this->link($params);
    }
    
    public function frontendLink($params)
    {
        if(is_array($params))
        {
            return "http://".SROVNAVAC_ADMIN_DEFAULT_SITE . ".povinne-ruceni.tv" .$this->linkRewriteFromArray($params);
        }
        else
        {
            return "http://".SROVNAVAC_ADMIN_DEFAULT_SITE . ".povinne-ruceni.tv" .$this->linkRewriteFromString($params);
        }
    }

    private function linkRewriteFromArray($params)
    {
            $p = "";

            $controller = (array_key_exists("controller", $params) ? $params["controller"] : null);
            unset($params["controller"]);

            $task = (array_key_exists("task", $params) ? $params["task"] : null);
            unset($params["task"]);

            $product = (array_key_exists("product", $params) ? $params["product"] : null);
            unset($params["product"]);

            $uuid = (array_key_exists("uuid", $params) ? $params["uuid"] : null);
            unset($params["uuid"]);

            foreach ($params as $key => $value)
            {
                $p.=$key . "=" . $value . "&";
            }

            $link = "/srovnani";
            if ($controller != null)
                $link.="/$controller";

            if ($task != null)
                $link.="/$task";

            if ($product != null)
                $link.="/$product";
            else if ($uuid != null)
                $link.="/$uuid";

            if ($p != "")
                $link.= "?" . substr($p, 0, strlen($p) - 1);

            return $link;
    }

    private function linkNormalFromArray($params)
    {
        $p = "";

        foreach ($params as $key => $value)
        {
            $p.=$key . "=" . $value . "&";
        }

        $link = "index.php";

        if ($p != "")
            $link.= "?" . substr($p, 0, strlen($p) - 1);

        return $link;
    }

    private function linkFromArray($params)
    {

        if (REWRITE_LINKS)
        {

                return $this->linkRewriteFromArray($params);
        }
        else
        {
                return $this->linkNormalFromArray($params);
        }
    }

    private function linkRewriteFromString($link)
    {
        //pozice dvojtečky
        $p1 = strpos($link, ":");
        $p2 = strpos($link, "=");

        if (!$p1)
        {
            $odd = "=";
        }
        else if (!$p2)
        {
            $odd = ":";
        }
        else if ($p2 < $p1)
        {
            $odd = "=";
        }
        else
        {
            $odd = ":";
        }


        if ($link == "")
            return $this->linkRewriteFromArray(array());
        $link = explode(",", $link);
        $params = array();
        foreach ($link as $l)
        {
            $l = explode($odd, $l);
            $params[$l[0]] = $l[1];
        }
        return $this->linkRewriteFromArray($params);
    }

    private function linkFomString($link)
    {
        //pozice dvojtečky
        $p1 = strpos($link, ":");
        $p2 = strpos($link, "=");

        if (!$p1)
        {
            $odd = "=";
        }
        else if (!$p2)
        {
            $odd = ":";
        }
        else if ($p2 < $p1)
        {
            $odd = "=";
        }
        else
        {
            $odd = ":";
        }


        if ($link == "")
            return $this->linkFromArray(array());
        $link = explode(",", $link);
        $params = array();
        foreach ($link as $l)
        {
            $l = explode($odd, $l);
            $params[$l[0]] = $l[1];
        }
        return $this->linkFromArray($params);
    }
    
    public function setUrlParameter($url, $key, $value)
    {
        $parseUrl = parse_url($url);
        parse_str($parseUrl['query'], $query);
        $query[$key] = $value;
        return $parseUrl['path'].'?'.http_build_query($query);
    }
    
    public function navigate($controller,$task)
    {
        return $this->link(array("controller"=>$controller,"task"=>$task));
    }

}

Powered by WebSVN 2.2.1