web/lib/Zend/Navigation/Page/Mvc.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    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-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 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 25213 2013-01-11 08:19:09Z frosch $
    20  * @version    $Id$
    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-2012 Zend Technologies USA Inc. (http://www.zend.com)
    47  * @copyright  Copyright (c) 2005-2015 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     /**
    96 
    96 
    97     /**
    97     /**
    98      * Whether href should be encoded when assembling URL
    98      * Whether href should be encoded when assembling URL
    99      *
    99      *
   100      * @see getHref()
   100      * @see getHref()
   101      * @var bool 
   101      * @var bool
   102      */
   102      */
   103     protected $_encodeUrl = true;
   103     protected $_encodeUrl = true;
   104 
   104 
   105     /**
   105     /**
   106      * Whether this page should be considered active
   106      * Whether this page should be considered active
   170                 }
   170                 }
   171             }
   171             }
   172 
   172 
   173             $myParams = $this->_params;
   173             $myParams = $this->_params;
   174 
   174 
   175             if ($this->_route) {
   175             if ($this->_route
       
   176                 && method_exists($front->getRouter(), 'getRoute')
       
   177             ) {
   176                 $route = $front->getRouter()->getRoute($this->_route);
   178                 $route = $front->getRouter()->getRoute($this->_route);
   177                 if(method_exists($route, 'getDefaults')) {
   179                 if (method_exists($route, 'getDefaults')) {
   178                     $myParams = array_merge($route->getDefaults(), $myParams);
   180                     $myParams = array_merge($route->getDefaults(), $myParams);
   179                 }
   181                 }
   180             }
   182             }
   181 
   183 
   182             if (null !== $this->_module) {
   184             if (null !== $this->_module) {
   183                 $myParams['module'] = $this->_module;
   185                 $myParams['module'] = $this->_module;
   184             } elseif(!array_key_exists('module', $myParams)) {
   186             } elseif (!array_key_exists('module', $myParams)) {
   185                 $myParams['module'] = $front->getDefaultModule();
   187                 $myParams['module'] = $front->getDefaultModule();
   186             }
   188             }
   187 
   189 
   188             if (null !== $this->_controller) {
   190             if (null !== $this->_controller) {
   189                 $myParams['controller'] = $this->_controller;
   191                 $myParams['controller'] = $this->_controller;
   190             } elseif(!array_key_exists('controller', $myParams)) {
   192             } elseif (!array_key_exists('controller', $myParams)) {
   191                 $myParams['controller'] = $front->getDefaultControllerName();
   193                 $myParams['controller'] = $front->getDefaultControllerName();
   192             }
   194             }
   193 
   195 
   194             if (null !== $this->_action) {
   196             if (null !== $this->_action) {
   195                 $myParams['action'] = $this->_action;
   197                 $myParams['action'] = $this->_action;
   196             } elseif(!array_key_exists('action', $myParams)) {
   198             } elseif (!array_key_exists('action', $myParams)) {
   197                 $myParams['action'] = $front->getDefaultAction();
   199                 $myParams['action'] = $front->getDefaultAction();
   198             }
   200             }
   199 
   201 
   200             foreach($myParams as $key => $value) {
   202             foreach ($myParams as $key => $value) {
   201                 if(null === $value) {
   203                 if (null === $value) {
   202                     unset($myParams[$key]);
   204                     unset($myParams[$key]);
   203                 }
   205                 }
   204             }
   206             }
   205 
   207 
   206             if (count(array_intersect_assoc($reqParams, $myParams)) ==
   208             if (count(array_intersect_assoc($reqParams, $myParams)) ==
   207                 count($myParams)) {
   209                 count($myParams)
       
   210             ) {
   208                 $this->_active = true;
   211                 $this->_active = true;
       
   212 
   209                 return true;
   213                 return true;
   210             }
   214             }
   211             
   215 
   212             $this->_active = false;
   216             $this->_active = false;
   213         }
   217         }
   214 
   218 
   215         return parent::isActive($recursive);
   219         return parent::isActive($recursive);
   216     }
   220     }
   246 
   250 
   247         if ($param = $this->getAction()) {
   251         if ($param = $this->getAction()) {
   248             $params['action'] = $param;
   252             $params['action'] = $param;
   249         }
   253         }
   250 
   254 
   251         $url = self::$_urlHelper->url($params,
   255         $url = self::$_urlHelper->url(
   252                                       $this->getRoute(),
   256             $params,
   253                                       $this->getResetParams(),
   257             $this->getRoute(),
   254                                       $this->getEncodeUrl());
   258             $this->getResetParams(),
       
   259             $this->getEncodeUrl()
       
   260         );
   255 
   261 
   256         // Use scheme?
   262         // Use scheme?
   257         $scheme = $this->getScheme();
   263         $scheme = $this->getScheme();
   258         if (null !== $scheme) {
   264         if (null !== $scheme) {
   259             if (null === self::$_schemeHelper) {
   265             if (null === self::$_schemeHelper) {
   263 
   269 
   264             $url = self::$_schemeHelper->setScheme($scheme)->serverUrl($url);
   270             $url = self::$_schemeHelper->setScheme($scheme)->serverUrl($url);
   265         }
   271         }
   266 
   272 
   267         // Add the fragment identifier if it is set
   273         // Add the fragment identifier if it is set
   268         $fragment = $this->getFragment();       
   274         $fragment = $this->getFragment();
   269         if (null !== $fragment) {
   275         if (null !== $fragment) {
   270             $url .= '#' . $fragment;
   276             $url .= '#' . $fragment;
   271         }         
   277         }
   272         
   278 
   273         return $this->_hrefCache = $url;
   279         return $this->_hrefCache = $url;
   274     }
   280     }
   275 
   281 
   276     /**
   282     /**
   277      * Sets action name to use when assembling URL
   283      * Sets action name to use when assembling URL
   285     public function setAction($action)
   291     public function setAction($action)
   286     {
   292     {
   287         if (null !== $action && !is_string($action)) {
   293         if (null !== $action && !is_string($action)) {
   288             require_once 'Zend/Navigation/Exception.php';
   294             require_once 'Zend/Navigation/Exception.php';
   289             throw new Zend_Navigation_Exception(
   295             throw new Zend_Navigation_Exception(
   290                     'Invalid argument: $action must be a string or null');
   296                 'Invalid argument: $action must be a string or null'
   291         }
   297             );
   292 
   298         }
   293         $this->_action = $action;
   299 
       
   300         $this->_action    = $action;
   294         $this->_hrefCache = null;
   301         $this->_hrefCache = null;
       
   302 
   295         return $this;
   303         return $this;
   296     }
   304     }
   297 
   305 
   298     /**
   306     /**
   299      * Returns action name to use when assembling URL
   307      * Returns action name to use when assembling URL
   319     public function setController($controller)
   327     public function setController($controller)
   320     {
   328     {
   321         if (null !== $controller && !is_string($controller)) {
   329         if (null !== $controller && !is_string($controller)) {
   322             require_once 'Zend/Navigation/Exception.php';
   330             require_once 'Zend/Navigation/Exception.php';
   323             throw new Zend_Navigation_Exception(
   331             throw new Zend_Navigation_Exception(
   324                     'Invalid argument: $controller must be a string or null');
   332                 'Invalid argument: $controller must be a string or null'
       
   333             );
   325         }
   334         }
   326 
   335 
   327         $this->_controller = $controller;
   336         $this->_controller = $controller;
   328         $this->_hrefCache = null;
   337         $this->_hrefCache  = null;
       
   338 
   329         return $this;
   339         return $this;
   330     }
   340     }
   331 
   341 
   332     /**
   342     /**
   333      * Returns controller name to use when assembling URL
   343      * Returns controller name to use when assembling URL
   353     public function setModule($module)
   363     public function setModule($module)
   354     {
   364     {
   355         if (null !== $module && !is_string($module)) {
   365         if (null !== $module && !is_string($module)) {
   356             require_once 'Zend/Navigation/Exception.php';
   366             require_once 'Zend/Navigation/Exception.php';
   357             throw new Zend_Navigation_Exception(
   367             throw new Zend_Navigation_Exception(
   358                     'Invalid argument: $module must be a string or null');
   368                 'Invalid argument: $module must be a string or null'
   359         }
   369             );
   360 
   370         }
   361         $this->_module = $module;
   371 
       
   372         $this->_module    = $module;
   362         $this->_hrefCache = null;
   373         $this->_hrefCache = null;
       
   374 
   363         return $this;
   375         return $this;
   364     }
   376     }
   365 
   377 
   366     /**
   378     /**
   367      * Returns module name to use when assembling URL
   379      * Returns module name to use when assembling URL
   378     /**
   390     /**
   379      * Set multiple parameters (to use when assembling URL) at once
   391      * Set multiple parameters (to use when assembling URL) at once
   380      *
   392      *
   381      * URL options passed to the url action helper for assembling URLs.
   393      * URL options passed to the url action helper for assembling URLs.
   382      * Overwrites any previously set parameters!
   394      * Overwrites any previously set parameters!
   383      * 
   395      *
   384      * @see getHref()
   396      * @see getHref()
   385      *
   397      *
   386      * @param  array|null $params           [optional] paramters as array
   398      * @param  array|null $params           [optional] paramters as array
   387      *                                      ('name' => 'value'). Default is null
   399      *                                      ('name' => 'value'). Default is null
   388      *                                      which clears all params.
   400      *                                      which clears all params.
   389      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
   401      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
   390      */
   402      */
   391     public function setParams(array $params = null)
   403     public function setParams(array $params = null)
   392     {
   404     {
   393         $this->clearParams();
   405         $this->clearParams();
   394         
   406 
   395         if (is_array($params)) {
   407         if (is_array($params)) {
   396             $this->addParams($params);
   408             $this->addParams($params);
   397         }
   409         }
   398         
   410 
   399         return $this;
   411         return $this;
   400     }
   412     }
   401     
   413 
   402     /**
   414     /**
   403      * Set parameter (to use when assembling URL)
   415      * Set parameter (to use when assembling URL)
   404      * 
   416      *
   405      * URL option passed to the url action helper for assembling URLs.
   417      * URL option passed to the url action helper for assembling URLs.
   406      *
   418      *
   407      * @see getHref()
   419      * @see getHref()
   408      *
   420      *
   409      * @param  string $name                 parameter name
   421      * @param  string $name                 parameter name
   410      * @param  mixed $value                 parameter value
   422      * @param  mixed $value                 parameter value
   411      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
   423      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
   412      */
   424      */
   413     public function setParam($name, $value)
   425     public function setParam($name, $value)
   414     {
   426     {
   415         $name = (string) $name;
   427         $name                 = (string)$name;
   416         $this->_params[$name] = $value;
   428         $this->_params[$name] = $value;
   417 
   429 
   418         $this->_hrefCache = null;
   430         $this->_hrefCache = null;
       
   431 
   419         return $this;
   432         return $this;
   420     }
   433     }
   421 
   434 
   422     /**
   435     /**
   423      * Add multiple parameters (to use when assembling URL) at once
   436      * Add multiple parameters (to use when assembling URL) at once
   424      * 
   437      *
   425      * URL options passed to the url action helper for assembling URLs.
   438      * URL options passed to the url action helper for assembling URLs.
   426      * 
   439      *
   427      * @see getHref()
   440      * @see getHref()
   428      *
   441      *
   429      * @param  array $params                paramters as array ('name' => 'value')
   442      * @param  array $params                paramters as array ('name' => 'value')
   430      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
   443      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
   431      */
   444      */
   438         return $this;
   451         return $this;
   439     }
   452     }
   440 
   453 
   441     /**
   454     /**
   442      * Remove parameter (to use when assembling URL)
   455      * Remove parameter (to use when assembling URL)
   443      * 
   456      *
   444      * @see getHref()
   457      * @see getHref()
   445      *
   458      *
   446      * @param  string $name
   459      * @param  string $name
   447      * @return bool
   460      * @return bool
   448      */
   461      */
   449     public function removeParam($name)
   462     public function removeParam($name)
   450     {             
   463     {
   451         if (array_key_exists($name, $this->_params)) {
   464         if (array_key_exists($name, $this->_params)) {
   452             unset($this->_params[$name]);
   465             unset($this->_params[$name]);
   453 
   466 
   454             $this->_hrefCache = null;
   467             $this->_hrefCache = null;
   455             return true;
   468             return true;
   458         return false;
   471         return false;
   459     }
   472     }
   460 
   473 
   461     /**
   474     /**
   462      * Clear all parameters (to use when assembling URL)
   475      * Clear all parameters (to use when assembling URL)
   463      * 
   476      *
   464      * @see getHref()
   477      * @see getHref()
   465      *
   478      *
   466      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
   479      * @return Zend_Navigation_Page_Mvc     fluent interface, returns self
   467      */
   480      */
   468     public function clearParams()
   481     public function clearParams()
   470         $this->_params = array();
   483         $this->_params = array();
   471 
   484 
   472         $this->_hrefCache = null;
   485         $this->_hrefCache = null;
   473         return $this;
   486         return $this;
   474     }
   487     }
   475     
   488 
   476     /**
   489     /**
   477      * Retrieve all parameters (to use when assembling URL)
   490      * Retrieve all parameters (to use when assembling URL)
   478      * 
   491      *
   479      * @see getHref()
   492      * @see getHref()
   480      *
   493      *
   481      * @return array                        parameters as array ('name' => 'value')
   494      * @return array parameters as array ('name' => 'value')
   482      */
   495      */
   483     public function getParams()
   496     public function getParams()
   484     {
   497     {
   485         return $this->_params;
   498         return $this->_params;
   486     }
   499     }
   487 
   500 
   488     /**
   501     /**
   489      * Retrieve a single parameter (to use when assembling URL)
   502      * Retrieve a single parameter (to use when assembling URL)
   490      * 
   503      *
   491      * @see getHref()
   504      * @see getHref()
   492      *
   505      *
   493      * @param  string $name                 parameter name
   506      * @param  string $name parameter name
   494      * @return mixed
   507      * @return mixed
   495      */
   508      */
   496     public function getParam($name)
   509     public function getParam($name)
   497     {
   510     {
   498         $name = (string) $name;
   511         $name = (string) $name;
   516     public function setRoute($route)
   529     public function setRoute($route)
   517     {
   530     {
   518         if (null !== $route && (!is_string($route) || strlen($route) < 1)) {
   531         if (null !== $route && (!is_string($route) || strlen($route) < 1)) {
   519             require_once 'Zend/Navigation/Exception.php';
   532             require_once 'Zend/Navigation/Exception.php';
   520             throw new Zend_Navigation_Exception(
   533             throw new Zend_Navigation_Exception(
   521                  'Invalid argument: $route must be a non-empty string or null');
   534                 'Invalid argument: $route must be a non-empty string or null'
   522         }
   535             );
   523 
   536         }
   524         $this->_route = $route;
   537 
       
   538         $this->_route     = $route;
   525         $this->_hrefCache = null;
   539         $this->_hrefCache = null;
       
   540 
   526         return $this;
   541         return $this;
   527     }
   542     }
   528 
   543 
   529     /**
   544     /**
   530      * Returns route name to use when assembling URL
   545      * Returns route name to use when assembling URL
   548      * @return Zend_Navigation_Page_Mvc  fluent interface, returns self
   563      * @return Zend_Navigation_Page_Mvc  fluent interface, returns self
   549      */
   564      */
   550     public function setResetParams($resetParams)
   565     public function setResetParams($resetParams)
   551     {
   566     {
   552         $this->_resetParams = (bool) $resetParams;
   567         $this->_resetParams = (bool) $resetParams;
   553         $this->_hrefCache = null;
   568         $this->_hrefCache   = null;
       
   569 
   554         return $this;
   570         return $this;
   555     }
   571     }
   556 
   572 
   557     /**
   573     /**
   558      * Returns whether params should be reset when assembling URL
   574      * Returns whether params should be reset when assembling URL
   566         return $this->_resetParams;
   582         return $this->_resetParams;
   567     }
   583     }
   568 
   584 
   569     /**
   585     /**
   570      * Sets whether href should be encoded when assembling URL
   586      * Sets whether href should be encoded when assembling URL
   571      * 
   587      *
   572      * @see getHref()
   588      * @see getHref()
   573      *
   589      *
   574      * @param bool $resetParams         whether href should be encoded when
   590      * @param $encodeUrl
   575      *                                  assembling URL
       
   576      * @return Zend_Navigation_Page_Mvc fluent interface, returns self
   591      * @return Zend_Navigation_Page_Mvc fluent interface, returns self
   577      */
   592      */
   578     public function setEncodeUrl($encodeUrl)
   593     public function setEncodeUrl($encodeUrl)
   579     {
   594     {
   580         $this->_encodeUrl = (bool) $encodeUrl;
   595         $this->_encodeUrl = (bool) $encodeUrl;
   581         $this->_hrefCache = null;
   596         $this->_hrefCache = null;
   582         
   597 
   583         return $this;
   598         return $this;
   584     }
   599     }
   585     
   600 
   586     /**
   601     /**
   587      * Returns whether herf should be encoded when assembling URL
   602      * Returns whether herf should be encoded when assembling URL
   588      * 
   603      *
   589      * @see getHref()
   604      * @see getHref()
   590      *
   605      *
   591      * @return bool whether herf should be encoded when assembling URL 
   606      * @return bool whether herf should be encoded when assembling URL
   592      */
   607      */
   593     public function getEncodeUrl()
   608     public function getEncodeUrl()
   594     {
   609     {
   595         return $this->_encodeUrl;
   610         return $this->_encodeUrl;
   596     }
   611     }
   599      * Sets scheme to use when assembling URL
   614      * Sets scheme to use when assembling URL
   600      *
   615      *
   601      * @see getHref()
   616      * @see getHref()
   602      *
   617      *
   603      * @param  string|null $scheme        scheme
   618      * @param  string|null $scheme        scheme
       
   619      * @throws Zend_Navigation_Exception
   604      * @return Zend_Navigation_Page_Mvc   fluent interface, returns self
   620      * @return Zend_Navigation_Page_Mvc   fluent interface, returns self
   605      */
   621      */
   606     public function setScheme($scheme)
   622     public function setScheme($scheme)
   607     {
   623     {
   608         if (null !== $scheme && !is_string($scheme)) {
   624         if (null !== $scheme && !is_string($scheme)) {
   664     public function toArray()
   680     public function toArray()
   665     {
   681     {
   666         return array_merge(
   682         return array_merge(
   667             parent::toArray(),
   683             parent::toArray(),
   668             array(
   684             array(
   669                 'action'       => $this->getAction(),
   685                  'action'       => $this->getAction(),
   670                 'controller'   => $this->getController(),
   686                  'controller'   => $this->getController(),
   671                 'module'       => $this->getModule(),
   687                  'module'       => $this->getModule(),
   672                 'params'       => $this->getParams(),
   688                  'params'       => $this->getParams(),
   673                 'route'        => $this->getRoute(),
   689                  'route'        => $this->getRoute(),
   674                 'reset_params' => $this->getResetParams(),
   690                  'reset_params' => $this->getResetParams(),
   675                 'encodeUrl'    => $this->getEncodeUrl(),
   691                  'encodeUrl'    => $this->getEncodeUrl(),
   676                 'scheme'       => $this->getScheme(),
   692                  'scheme'       => $this->getScheme(),
   677             )
   693             )
   678         );
   694         );
   679     }
   695     }
   680 }
   696 }