<?php
goweb::loadClass('Zend_View_Helper_Url');
/**
 * Goweb Sitemanager 5 Framework
 *
 * Helper para a criação dos URL
 *
 * @version    5.0.14
 * @category   Goweb
 * @package    Goweb_View_Helper_Link
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 *
 *
 */

class Goweb_View_Helper_Link extends Zend_View_Helper_Url{
    
    public function Link($path, $query = null) {
        $urlBase = Zend_Controller_Front::getInstance()->getBaseUrl();
        $locale = Zend_Controller_Front::getInstance()->getRequest()->getParam('@locale');
        if (strlen($urlBase) > 0 && strpos($path, $urlBase) !== false){
            $path = substr($path, strlen($urlBase));
        }
        if (strstr($path, '?')) {
            $path = substr($path, 0, strpos($path, '?'));
        }
        $path = explode('/', trim($path, '/'));
        $startCount=0;
        $params = array();
        if (goweb::registry('session')->langs && in_array($path[0], goweb::registry('session')->langs)){
            $params['lang'] = $path[0];
            if ($locale){
                $params['@locale'] = $path[0];
            }
            $startCount=1; 
        }
        if(strpos($query, 'lang=') !== false){
            $querys = explode('&', $query);
            foreach ($querys as $items){
                $item = explode('=', $items);
                if (isset($item['0'] ) && $item['0'] == 'lang'){
                    $params['lang'] = $item['1'];
                    if ($locale){
                        $params['@locale'] = $item['1'];
                    }
                    $query = str_replace($item['0'] . '=' . $item['1'], '', $query);
                }
                
            }
            if (strlen($query) == 0) $query = null;
        }
        $controller = isset($path[$startCount]) ? $path[$startCount] : null;
        $action     = isset($path[$startCount + 1]) ? $path[$startCount + 1] : null;
        if (!strlen($controller)) {
           $controller = 'index';
           $action = 'index';
        }
        $params['controller'] = $controller;
        $params['action'] = $action;
        for ($i=$startCount + 2; $i<sizeof($path); $i=$i+2) {
            $params[$path[$i]] = isset($path[$i+1]) ? $path[$i+1] : '';
        }
        if (!isset($params['lang']) && count(goweb::registry('session')->langs)>1){
            $params['lang'] = goweb::registry('session')->lang;
            if ($locale){
                $params['@locale'] = goweb::registry('session')->lang;
            }
        }
        $return = $this->url($params,null,true);
        if (!is_null($query)){
            if (strpos('?', $return) === false){
                $return .= '?';
            }else if (substr($return,-1) !== '&'){
                $return .= '&amp;';
            }
            $return .= str_replace(array('&amp;', '&'), '&amp;', $query);
        }
        if (goweb::registry('config')->url->rewrite && strpos($return, '/index.php') !== false){
            $return = str_replace('/index.php', '', $return);
        }
        return $return;
    }
    
}
