web/lib/Zend/Navigation/Page/Mvc.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Navigation
    16  * @package    Zend_Navigation
    17  * @subpackage Page
    17  * @subpackage Page
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Mvc.php 20096 2010-01-06 02:05:09Z bkarwin $
    20  * @version    $Id: Mvc.php 25213 2013-01-11 08:19:09Z frosch $
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_Navigation_Page
    24  * @see Zend_Navigation_Page
    25  */
    25  */
    42  * name and route params to assemble the href
    42  * name and route params to assemble the href
    43  *
    43  *
    44  * @category   Zend
    44  * @category   Zend
    45  * @package    Zend_Navigation
    45  * @package    Zend_Navigation
    46  * @subpackage Page
    46  * @subpackage Page
    47  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    47  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    48  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    48  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    49  */
    49  */
    50 class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
    50 class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
    51 {
    51 {
    52     /**
    52     /**
    93      * @var bool
    93      * @var bool
    94      */
    94      */
    95     protected $_resetParams = true;
    95     protected $_resetParams = true;
    96 
    96 
    97     /**
    97     /**
       
    98      * Whether href should be encoded when assembling URL
       
    99      *
       
   100      * @see getHref()
       
   101      * @var bool 
       
   102      */
       
   103     protected $_encodeUrl = true;
       
   104 
       
   105     /**
       
   106      * Whether this page should be considered active
       
   107      *
       
   108      * @var bool
       
   109      */
       
   110     protected $_active = null;
       
   111 
       
   112     /**
       
   113      * Scheme to use when assembling URL
       
   114      *
       
   115      * @see getHref()
       
   116      * @var string
       
   117      */
       
   118     protected $_scheme;
       
   119 
       
   120     /**
    98      * Cached href
   121      * Cached href
    99      *
   122      *
   100      * The use of this variable minimizes execution time when getHref() is
   123      * The use of this variable minimizes execution time when getHref() is
   101      * called more than once during the lifetime of a request. If a property
   124      * called more than once during the lifetime of a request. If a property
   102      * is updated, the cache is invalidated.
   125      * is updated, the cache is invalidated.
   110      *
   133      *
   111      * @see getHref()
   134      * @see getHref()
   112      * @var Zend_Controller_Action_Helper_Url
   135      * @var Zend_Controller_Action_Helper_Url
   113      */
   136      */
   114     protected static $_urlHelper = null;
   137     protected static $_urlHelper = null;
       
   138 
       
   139     /**
       
   140      * View helper for assembling URLs with schemes
       
   141      *
       
   142      * @see getHref()
       
   143      * @var Zend_View_Helper_ServerUrl
       
   144      */
       
   145     protected static $_schemeHelper = null;
   115 
   146 
   116     // Accessors:
   147     // Accessors:
   117 
   148 
   118     /**
   149     /**
   119      * Returns whether page should be considered active or not
   150      * Returns whether page should be considered active or not
   126      *                          false.
   157      *                          false.
   127      * @return bool             whether page should be considered active or not
   158      * @return bool             whether page should be considered active or not
   128      */
   159      */
   129     public function isActive($recursive = false)
   160     public function isActive($recursive = false)
   130     {
   161     {
   131         if (!$this->_active) {
   162         if (null === $this->_active) {
   132             $front = Zend_Controller_Front::getInstance();
   163             $front     = Zend_Controller_Front::getInstance();
   133             $reqParams = $front->getRequest()->getParams();
   164             $request   = $front->getRequest();
   134 
   165             $reqParams = array();
   135             if (!array_key_exists('module', $reqParams)) {
   166             if ($request) {
   136                 $reqParams['module'] = $front->getDefaultModule();
   167                 $reqParams = $request->getParams();
       
   168                 if (!array_key_exists('module', $reqParams)) {
       
   169                     $reqParams['module'] = $front->getDefaultModule();
       
   170                 }
   137             }
   171             }
   138 
   172 
   139             $myParams = $this->_params;
   173             $myParams = $this->_params;
       
   174 
       
   175             if ($this->_route) {
       
   176                 $route = $front->getRouter()->getRoute($this->_route);
       
   177                 if(method_exists($route, 'getDefaults')) {
       
   178                     $myParams = array_merge($route->getDefaults(), $myParams);
       
   179                 }
       
   180             }
   140 
   181 
   141             if (null !== $this->_module) {
   182             if (null !== $this->_module) {
   142                 $myParams['module'] = $this->_module;
   183                 $myParams['module'] = $this->_module;
   143             } else {
   184             } elseif(!array_key_exists('module', $myParams)) {
   144                 $myParams['module'] = $front->getDefaultModule();
   185                 $myParams['module'] = $front->getDefaultModule();
   145             }
   186             }
   146 
   187 
   147             if (null !== $this->_controller) {
   188             if (null !== $this->_controller) {
   148                 $myParams['controller'] = $this->_controller;
   189                 $myParams['controller'] = $this->_controller;
   149             } else {
   190             } elseif(!array_key_exists('controller', $myParams)) {
   150                 $myParams['controller'] = $front->getDefaultControllerName();
   191                 $myParams['controller'] = $front->getDefaultControllerName();
   151             }
   192             }
   152 
   193 
   153             if (null !== $this->_action) {
   194             if (null !== $this->_action) {
   154                 $myParams['action'] = $this->_action;
   195                 $myParams['action'] = $this->_action;
   155             } else {
   196             } elseif(!array_key_exists('action', $myParams)) {
   156                 $myParams['action'] = $front->getDefaultAction();
   197                 $myParams['action'] = $front->getDefaultAction();
       
   198             }
       
   199 
       
   200             foreach($myParams as $key => $value) {
       
   201                 if(null === $value) {
       
   202                     unset($myParams[$key]);
       
   203                 }
   157             }
   204             }
   158 
   205 
   159             if (count(array_intersect_assoc($reqParams, $myParams)) ==
   206             if (count(array_intersect_assoc($reqParams, $myParams)) ==
   160                 count($myParams)) {
   207                 count($myParams)) {
   161                 $this->_active = true;
   208                 $this->_active = true;
   162                 return true;
   209                 return true;
   163             }
   210             }
       
   211             
       
   212             $this->_active = false;
   164         }
   213         }
   165 
   214 
   166         return parent::isActive($recursive);
   215         return parent::isActive($recursive);
   167     }
   216     }
   168 
   217 
   199             $params['action'] = $param;
   248             $params['action'] = $param;
   200         }
   249         }
   201 
   250 
   202         $url = self::$_urlHelper->url($params,
   251         $url = self::$_urlHelper->url($params,
   203                                       $this->getRoute(),
   252                                       $this->getRoute(),
   204                                       $this->getResetParams());
   253                                       $this->getResetParams(),
   205 
   254                                       $this->getEncodeUrl());
       
   255 
       
   256         // Use scheme?
       
   257         $scheme = $this->getScheme();
       
   258         if (null !== $scheme) {
       
   259             if (null === self::$_schemeHelper) {
       
   260                 require_once 'Zend/View/Helper/ServerUrl.php';
       
   261                 self::$_schemeHelper = new Zend_View_Helper_ServerUrl();
       
   262             }
       
   263 
       
   264             $url = self::$_schemeHelper->setScheme($scheme)->serverUrl($url);
       
   265         }
       
   266 
       
   267         // Add the fragment identifier if it is set
       
   268         $fragment = $this->getFragment();       
       
   269         if (null !== $fragment) {
       
   270             $url .= '#' . $fragment;
       
   271         }         
       
   272         
   206         return $this->_hrefCache = $url;
   273         return $this->_hrefCache = $url;
   207     }
   274     }
   208 
   275 
   209     /**
   276     /**
   210      * Sets action name to use when assembling URL
   277      * Sets action name to use when assembling URL
   307     {
   374     {
   308         return $this->_module;
   375         return $this->_module;
   309     }
   376     }
   310 
   377 
   311     /**
   378     /**
   312      * Sets params to use when assembling URL
   379      * Set multiple parameters (to use when assembling URL) at once
   313      *
   380      *
   314      * @see getHref()
   381      * URL options passed to the url action helper for assembling URLs.
   315      *
   382      * Overwrites any previously set parameters!
   316      * @param  array|null $params        [optional] page params. Default is null
   383      * 
   317      *                                   which sets no params.
   384      * @see getHref()
   318      * @return Zend_Navigation_Page_Mvc  fluent interface, returns self
   385      *
       
   386      * @param  array|null $params           [optional] paramters as array
       
   387      *                                      ('name' => 'value'). Default is null
       
   388      *                                      which clears all params.
       
   389      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
   319      */
   390      */
   320     public function setParams(array $params = null)
   391     public function setParams(array $params = null)
   321     {
   392     {
   322         if (null === $params) {
   393         $this->clearParams();
   323             $this->_params = array();
   394         
   324         } else {
   395         if (is_array($params)) {
   325             // TODO: do this more intelligently?
   396             $this->addParams($params);
   326             $this->_params = $params;
   397         }
   327         }
   398         
   328 
   399         return $this;
   329         $this->_hrefCache = null;
   400     }
   330         return $this;
   401     
   331     }
   402     /**
   332 
   403      * Set parameter (to use when assembling URL)
   333     /**
   404      * 
   334      * Returns params to use when assembling URL
   405      * URL option passed to the url action helper for assembling URLs.
   335      *
   406      *
   336      * @see getHref()
   407      * @see getHref()
   337      *
   408      *
   338      * @return array  page params
   409      * @param  string $name                 parameter name
       
   410      * @param  mixed $value                 parameter value
       
   411      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
       
   412      */
       
   413     public function setParam($name, $value)
       
   414     {
       
   415         $name = (string) $name;
       
   416         $this->_params[$name] = $value;
       
   417 
       
   418         $this->_hrefCache = null;
       
   419         return $this;
       
   420     }
       
   421 
       
   422     /**
       
   423      * Add multiple parameters (to use when assembling URL) at once
       
   424      * 
       
   425      * URL options passed to the url action helper for assembling URLs.
       
   426      * 
       
   427      * @see getHref()
       
   428      *
       
   429      * @param  array $params                paramters as array ('name' => 'value')
       
   430      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
       
   431      */
       
   432     public function addParams(array $params)
       
   433     {
       
   434         foreach ($params as $name => $value) {
       
   435             $this->setParam($name, $value);
       
   436         }
       
   437 
       
   438         return $this;
       
   439     }
       
   440 
       
   441     /**
       
   442      * Remove parameter (to use when assembling URL)
       
   443      * 
       
   444      * @see getHref()
       
   445      *
       
   446      * @param  string $name
       
   447      * @return bool
       
   448      */
       
   449     public function removeParam($name)
       
   450     {             
       
   451         if (array_key_exists($name, $this->_params)) {
       
   452             unset($this->_params[$name]);
       
   453 
       
   454             $this->_hrefCache = null;
       
   455             return true;
       
   456         }
       
   457 
       
   458         return false;
       
   459     }
       
   460 
       
   461     /**
       
   462      * Clear all parameters (to use when assembling URL)
       
   463      * 
       
   464      * @see getHref()
       
   465      *
       
   466      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
       
   467      */
       
   468     public function clearParams()
       
   469     {
       
   470         $this->_params = array();
       
   471 
       
   472         $this->_hrefCache = null;
       
   473         return $this;
       
   474     }
       
   475     
       
   476     /**
       
   477      * Retrieve all parameters (to use when assembling URL)
       
   478      * 
       
   479      * @see getHref()
       
   480      *
       
   481      * @return array                        parameters as array ('name' => 'value')
   339      */
   482      */
   340     public function getParams()
   483     public function getParams()
   341     {
   484     {
   342         return $this->_params;
   485         return $this->_params;
       
   486     }
       
   487 
       
   488     /**
       
   489      * Retrieve a single parameter (to use when assembling URL)
       
   490      * 
       
   491      * @see getHref()
       
   492      *
       
   493      * @param  string $name                 parameter name
       
   494      * @return mixed
       
   495      */
       
   496     public function getParam($name)
       
   497     {
       
   498         $name = (string) $name;
       
   499 
       
   500         if (!array_key_exists($name, $this->_params)) {
       
   501             return null;
       
   502         }
       
   503 
       
   504         return $this->_params[$name];
   343     }
   505     }
   344 
   506 
   345     /**
   507     /**
   346      * Sets route name to use when assembling URL
   508      * Sets route name to use when assembling URL
   347      *
   509      *
   403     {
   565     {
   404         return $this->_resetParams;
   566         return $this->_resetParams;
   405     }
   567     }
   406 
   568 
   407     /**
   569     /**
       
   570      * Sets whether href should be encoded when assembling URL
       
   571      * 
       
   572      * @see getHref()
       
   573      *
       
   574      * @param bool $resetParams         whether href should be encoded when
       
   575      *                                  assembling URL
       
   576      * @return Zend_Navigation_Page_Mvc fluent interface, returns self
       
   577      */
       
   578     public function setEncodeUrl($encodeUrl)
       
   579     {
       
   580         $this->_encodeUrl = (bool) $encodeUrl;
       
   581         $this->_hrefCache = null;
       
   582         
       
   583         return $this;
       
   584     }
       
   585     
       
   586     /**
       
   587      * Returns whether herf should be encoded when assembling URL
       
   588      * 
       
   589      * @see getHref()
       
   590      *
       
   591      * @return bool whether herf should be encoded when assembling URL 
       
   592      */
       
   593     public function getEncodeUrl()
       
   594     {
       
   595         return $this->_encodeUrl;
       
   596     }
       
   597 
       
   598     /**
       
   599      * Sets scheme to use when assembling URL
       
   600      *
       
   601      * @see getHref()
       
   602      *
       
   603      * @param  string|null $scheme        scheme
       
   604      * @return Zend_Navigation_Page_Mvc   fluent interface, returns self
       
   605      */
       
   606     public function setScheme($scheme)
       
   607     {
       
   608         if (null !== $scheme && !is_string($scheme)) {
       
   609             require_once 'Zend/Navigation/Exception.php';
       
   610             throw new Zend_Navigation_Exception(
       
   611                 'Invalid argument: $scheme must be a string or null'
       
   612             );
       
   613         }
       
   614 
       
   615         $this->_scheme = $scheme;
       
   616         return $this;
       
   617     }
       
   618 
       
   619     /**
       
   620      * Returns scheme to use when assembling URL
       
   621      *
       
   622      * @see getHref()
       
   623      *
       
   624      * @return string|null  scheme or null
       
   625      */
       
   626     public function getScheme()
       
   627     {
       
   628         return $this->_scheme;
       
   629     }
       
   630 
       
   631     /**
   408      * Sets action helper for assembling URLs
   632      * Sets action helper for assembling URLs
   409      *
   633      *
   410      * @see getHref()
   634      * @see getHref()
   411      *
   635      *
   412      * @param  Zend_Controller_Action_Helper_Url $uh  URL helper
   636      * @param  Zend_Controller_Action_Helper_Url $uh  URL helper
   413      * @return void
   637      * @return void
   414      */
   638      */
   415     public static function setUrlHelper(Zend_Controller_Action_Helper_Url $uh)
   639     public static function setUrlHelper(Zend_Controller_Action_Helper_Url $uh)
   416     {
   640     {
   417         self::$_urlHelper = $uh;
   641         self::$_urlHelper = $uh;
       
   642     }
       
   643 
       
   644     /**
       
   645      * Sets view helper for assembling URLs with schemes
       
   646      *
       
   647      * @see getHref()
       
   648      *
       
   649      * @param  Zend_View_Helper_ServerUrl $sh   scheme helper
       
   650      * @return void
       
   651      */
       
   652     public static function setSchemeHelper(Zend_View_Helper_ServerUrl $sh)
       
   653     {
       
   654         self::$_schemeHelper = $sh;
   418     }
   655     }
   419 
   656 
   420     // Public methods:
   657     // Public methods:
   421 
   658 
   422     /**
   659     /**
   432                 'action'       => $this->getAction(),
   669                 'action'       => $this->getAction(),
   433                 'controller'   => $this->getController(),
   670                 'controller'   => $this->getController(),
   434                 'module'       => $this->getModule(),
   671                 'module'       => $this->getModule(),
   435                 'params'       => $this->getParams(),
   672                 'params'       => $this->getParams(),
   436                 'route'        => $this->getRoute(),
   673                 'route'        => $this->getRoute(),
   437                 'reset_params' => $this->getResetParams()
   674                 'reset_params' => $this->getResetParams(),
   438             ));
   675                 'encodeUrl'    => $this->getEncodeUrl(),
       
   676                 'scheme'       => $this->getScheme(),
       
   677             )
       
   678         );
   439     }
   679     }
   440 }
   680 }