web/lib/Zend/Navigation/Page.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    12  * obtain it through the world-wide-web, please send an email
    12  * obtain it through the world-wide-web, please send an email
    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  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @license   http://framework.zend.com/license/new-bsd     New BSD License
    18  * @license   http://framework.zend.com/license/new-bsd     New BSD License
    19  * @version    $Id: Page.php 22882 2010-08-22 14:00:16Z freak $
    19  * @version    $Id: Page.php 25125 2012-11-16 15:12:06Z rob $
    20  */
    20  */
    21 
    21 
    22 /**
    22 /**
    23  * @see Zend_Navigation_Container
    23  * @see Zend_Navigation_Container
    24  */
    24  */
    27 /**
    27 /**
    28  * Base class for Zend_Navigation_Page pages
    28  * Base class for Zend_Navigation_Page pages
    29  *
    29  *
    30  * @category  Zend
    30  * @category  Zend
    31  * @package   Zend_Navigation
    31  * @package   Zend_Navigation
    32  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    32  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    33  * @license   http://framework.zend.com/license/new-bsd     New BSD License
    33  * @license   http://framework.zend.com/license/new-bsd     New BSD License
    34  */
    34  */
    35 abstract class Zend_Navigation_Page extends Zend_Navigation_Container
    35 abstract class Zend_Navigation_Page extends Zend_Navigation_Container
    36 {
    36 {
    37     /**
    37     /**
    40      * @var string|null
    40      * @var string|null
    41      */
    41      */
    42     protected $_label;
    42     protected $_label;
    43 
    43 
    44     /**
    44     /**
       
    45      * Fragment identifier (anchor identifier)
       
    46      * 
       
    47      * The fragment identifier (anchor identifier) pointing to an anchor within 
       
    48      * a resource that is subordinate to another, primary resource.
       
    49      * The fragment identifier introduced by a hash mark "#".
       
    50      * Example: http://www.example.org/foo.html#bar ("bar" is the fragment identifier)
       
    51      * 
       
    52      * @link http://www.w3.org/TR/html401/intro/intro.html#fragment-uri
       
    53      * 
       
    54      * @var string|null
       
    55      */
       
    56     protected $_fragment;
       
    57 
       
    58     /**
    45      * Page id
    59      * Page id
    46      *
    60      *
    47      * @var string|null
    61      * @var string|null
    48      */
    62      */
    49     protected $_id;
    63     protected $_id;
    68      * @var string|null
    82      * @var string|null
    69      */
    83      */
    70     protected $_target;
    84     protected $_target;
    71 
    85 
    72     /**
    86     /**
       
    87      * Accessibility key character
       
    88      *
       
    89      * This attribute assigns an access key to an element. An access key is a
       
    90      * single character from the document character set.
       
    91      *
       
    92      * @link http://www.w3.org/TR/html401/interact/forms.html#access-keys
       
    93      *
       
    94      * @var string|null
       
    95      */
       
    96     protected $_accesskey;
       
    97 
       
    98     /**
    73      * Forward links to other pages
    99      * Forward links to other pages
    74      *
   100      *
    75      * @link http://www.w3.org/TR/html4/struct/links.html#h-12.3.1
   101      * @link http://www.w3.org/TR/html4/struct/links.html#h-12.3.1
    76      *
   102      *
    77      * @var array
   103      * @var array
   135      * @var array
   161      * @var array
   136      */
   162      */
   137     protected $_properties = array();
   163     protected $_properties = array();
   138 
   164 
   139     /**
   165     /**
       
   166      * Custom HTML attributes
       
   167      *
       
   168      * @var array
       
   169      */
       
   170     protected $_customHtmlAttribs = array();
       
   171 
       
   172     /**
   140      * The type of page to use when it wasn't set
   173      * The type of page to use when it wasn't set
   141      * 
   174      *
   142      * @var string
   175      * @var string
   143      */
   176      */
   144     protected static $_defaultPageType;
   177     protected static $_defaultPageType;
   145     
   178 
   146     // Initialization:
   179     // Initialization:
   147 
   180 
   148     /**
   181     /**
   149      * Factory for Zend_Navigation_Page classes
   182      * Factory for Zend_Navigation_Page classes
   150      *
   183      *
   189         if (isset($options['type'])) {
   222         if (isset($options['type'])) {
   190             $type = $options['type'];
   223             $type = $options['type'];
   191         } elseif(self::getDefaultPageType()!= null) {
   224         } elseif(self::getDefaultPageType()!= null) {
   192             $type = self::getDefaultPageType();
   225             $type = self::getDefaultPageType();
   193         }
   226         }
   194         
   227 
   195         if(isset($type)) {
   228         if(isset($type)) {
   196             if (is_string($type) && !empty($type)) {
   229             if (is_string($type) && !empty($type)) {
   197                 switch (strtolower($type)) {
   230                 switch (strtolower($type)) {
   198                     case 'mvc':
   231                     case 'mvc':
   199                         $type = 'Zend_Navigation_Page_Mvc';
   232                         $type = 'Zend_Navigation_Page_Mvc';
   220             }
   253             }
   221         }
   254         }
   222 
   255 
   223         $hasUri = isset($options['uri']);
   256         $hasUri = isset($options['uri']);
   224         $hasMvc = isset($options['action']) || isset($options['controller']) ||
   257         $hasMvc = isset($options['action']) || isset($options['controller']) ||
   225                   isset($options['module']) || isset($options['route']);
   258                   isset($options['module']) || isset($options['route']) ||
       
   259                   isset($options['params']);
   226 
   260 
   227         if ($hasMvc) {
   261         if ($hasMvc) {
   228             require_once 'Zend/Navigation/Page/Mvc.php';
   262             require_once 'Zend/Navigation/Page/Mvc.php';
   229             return new Zend_Navigation_Page_Mvc($options);
   263             return new Zend_Navigation_Page_Mvc($options);
   230         } elseif ($hasUri) {
   264         } elseif ($hasUri) {
   231             require_once 'Zend/Navigation/Page/Uri.php';
   265             require_once 'Zend/Navigation/Page/Uri.php';
   232             return new Zend_Navigation_Page_Uri($options);
   266             return new Zend_Navigation_Page_Uri($options);
   233         } else {
   267         } else {
   234             require_once 'Zend/Navigation/Exception.php';
   268             require_once 'Zend/Navigation/Exception.php';
   235             throw new Zend_Navigation_Exception(
   269             
   236                 'Invalid argument: Unable to determine class to instantiate');
   270             $message = 'Invalid argument: Unable to determine class to instantiate';
   237         }
   271             if (isset($options['label'])) {
       
   272                 $message .= ' (Page label: ' . $options['label'] . ')';
       
   273         }
       
   274             
       
   275             throw new Zend_Navigation_Exception($message);
       
   276     }
   238     }
   277     }
   239 
   278 
   240     /**
   279     /**
   241      * Page constructor
   280      * Page constructor
   242      *
   281      *
   328     {
   367     {
   329         return $this->_label;
   368         return $this->_label;
   330     }
   369     }
   331 
   370 
   332     /**
   371     /**
       
   372      * Sets a fragment identifier
       
   373      *
       
   374      * @param  string $fragment   new fragment identifier
       
   375      * @return Zend_Navigation_Page         fluent interface, returns self
       
   376      * @throws Zend_Navigation_Exception    if empty/no string is given
       
   377      */
       
   378     public function setFragment($fragment)
       
   379     {
       
   380         if (null !== $fragment && !is_string($fragment)) {
       
   381             require_once 'Zend/Navigation/Exception.php';
       
   382             throw new Zend_Navigation_Exception(
       
   383                     'Invalid argument: $fragment must be a string or null');
       
   384         }
       
   385  
       
   386         $this->_fragment = $fragment;
       
   387         return $this;
       
   388     }
       
   389     
       
   390      /**
       
   391      * Returns fragment identifier
       
   392      *
       
   393      * @return string|null  fragment identifier
       
   394      */
       
   395     public function getFragment()
       
   396     {
       
   397         return $this->_fragment;
       
   398     }
       
   399 
       
   400     /**
   333      * Sets page id
   401      * Sets page id
   334      *
   402      *
   335      * @param  string|null $id            [optional] id to set. Default is null,
   403      * @param  string|null $id            [optional] id to set. Default is null,
   336      *                                    which sets no id.
   404      *                                    which sets no id.
   337      * @return Zend_Navigation_Page       fluent interface, returns self
   405      * @return Zend_Navigation_Page       fluent interface, returns self
   446      * @return string|null  page target or null
   514      * @return string|null  page target or null
   447      */
   515      */
   448     public function getTarget()
   516     public function getTarget()
   449     {
   517     {
   450         return $this->_target;
   518         return $this->_target;
       
   519     }
       
   520 
       
   521     /**
       
   522      * Sets access key for this page
       
   523      *
       
   524      * @param  string|null $character     [optional] access key to set. Default
       
   525      *                                    is null, which sets no access key.
       
   526      * @return Zend_Navigation_Page       fluent interface, returns self
       
   527      * @throws Zend_Navigation_Exception  if access key is not string or null or
       
   528      *                                    if the string length not equal to one
       
   529      */
       
   530     public function setAccesskey($character = null)
       
   531     {
       
   532         if (null !== $character
       
   533             && (!is_string($character) || 1 != strlen($character)))
       
   534         {
       
   535             require_once 'Zend/Navigation/Exception.php';
       
   536             throw new Zend_Navigation_Exception(
       
   537                 'Invalid argument: $character must be a single character or null'
       
   538             );
       
   539         }
       
   540  
       
   541         $this->_accesskey = $character;
       
   542         return $this;
       
   543     }
       
   544 
       
   545      /**
       
   546      * Returns page access key
       
   547      *
       
   548      * @return string|null  page access key or null
       
   549      */
       
   550     public function getAccesskey()
       
   551     {
       
   552         return $this->_accesskey;
   451     }
   553     }
   452 
   554 
   453     /**
   555     /**
   454      * Sets the page's forward links to other pages
   556      * Sets the page's forward links to other pages
   455      *
   557      *
   572                    $this->_rev[$relation] :
   674                    $this->_rev[$relation] :
   573                    null;
   675                    null;
   574         }
   676         }
   575 
   677 
   576         return $this->_rev;
   678         return $this->_rev;
       
   679     }
       
   680 
       
   681     /**
       
   682      * Sets a single custom HTML attribute
       
   683      *
       
   684      * @param  string      $name            name of the HTML attribute
       
   685      * @param  string|null $value           value for the HTML attribute
       
   686      * @return Zend_Navigation_Page         fluent interface, returns self
       
   687      * @throws Zend_Navigation_Exception    if name is not string or value is
       
   688      *                                      not null or a string
       
   689      */
       
   690     public function setCustomHtmlAttrib($name, $value)
       
   691     {
       
   692         if (!is_string($name)) {
       
   693             require_once 'Zend/Navigation/Exception.php';
       
   694             throw new Zend_Navigation_Exception(
       
   695                 'Invalid argument: $name must be a string'
       
   696             );
       
   697         }
       
   698 
       
   699         if (null !== $value && !is_string($value)) {
       
   700             require_once 'Zend/Navigation/Exception.php';
       
   701             throw new Zend_Navigation_Exception(
       
   702                 'Invalid argument: $value must be a string or null'
       
   703             );
       
   704         }
       
   705 
       
   706         if (null === $value && isset($this->_customHtmlAttribs[$name])) {
       
   707             unset($this->_customHtmlAttribs[$name]);
       
   708         } else {
       
   709             $this->_customHtmlAttribs[$name] = $value;
       
   710         }
       
   711 
       
   712         return $this;
       
   713     }
       
   714 
       
   715     /**
       
   716      * Returns a single custom HTML attributes by name
       
   717      *
       
   718      * @param  string $name                 name of the HTML attribute
       
   719      * @return string|null                  value for the HTML attribute or null
       
   720      * @throws Zend_Navigation_Exception    if name is not string
       
   721      */
       
   722     public function getCustomHtmlAttrib($name)
       
   723     {
       
   724         if (!is_string($name)) {
       
   725             require_once 'Zend/Navigation/Exception.php';
       
   726             throw new Zend_Navigation_Exception(
       
   727                 'Invalid argument: $name must be a string'
       
   728             );
       
   729         }
       
   730 
       
   731         if (isset($this->_customHtmlAttribs[$name])) {
       
   732             return $this->_customHtmlAttribs[$name];
       
   733         }
       
   734 
       
   735         return null;
       
   736     }
       
   737 
       
   738     /**
       
   739      * Sets multiple custom HTML attributes at once
       
   740      *
       
   741      * @param array $attribs        an associative array of html attributes
       
   742      * @return Zend_Navigation_Page fluent interface, returns self
       
   743      */
       
   744     public function setCustomHtmlAttribs(array $attribs)
       
   745     {
       
   746         foreach ($attribs as $key => $value) {
       
   747             $this->setCustomHtmlAttrib($key, $value);
       
   748         }
       
   749         return $this;
       
   750     }
       
   751 
       
   752     /**
       
   753      * Returns all custom HTML attributes as an array
       
   754      *
       
   755      * @return array    an array containing custom HTML attributes
       
   756      */
       
   757     public function getCustomHtmlAttribs()
       
   758     {
       
   759         return $this->_customHtmlAttribs;
       
   760     }
       
   761 
       
   762     /**
       
   763      * Removes a custom HTML attribute from the page
       
   764      *
       
   765      * @param  string $name          name of the custom HTML attribute
       
   766      * @return Zend_Navigation_Page  fluent interface, returns self
       
   767      */
       
   768     public function removeCustomHtmlAttrib($name)
       
   769     {
       
   770         if (!is_string($name)) {
       
   771             require_once 'Zend/Navigation/Exception.php';
       
   772             throw new Zend_Navigation_Exception(
       
   773                 'Invalid argument: $name must be a string'
       
   774             );
       
   775         }
       
   776 
       
   777         if (isset($this->_customHtmlAttribs[$name])) {
       
   778             unset($this->_customHtmlAttribs[$name]);
       
   779         }
       
   780     }
       
   781 
       
   782     /**
       
   783      * Clear all custom HTML attributes
       
   784      *
       
   785      * @return Zend_Navigation_Page fluent interface, returns self
       
   786      */
       
   787     public function clearCustomHtmlAttribs()
       
   788     {
       
   789         $this->_customHtmlAttribs = array();
       
   790 
       
   791         return $this;
   577     }
   792     }
   578 
   793 
   579     /**
   794     /**
   580      * Sets page order to use in parent container
   795      * Sets page order to use in parent container
   581      *
   796      *
   738      *                               considered visible or not. Default is true.
   953      *                               considered visible or not. Default is true.
   739      * @return Zend_Navigation_Page  fluent interface, returns self
   954      * @return Zend_Navigation_Page  fluent interface, returns self
   740      */
   955      */
   741     public function setVisible($visible = true)
   956     public function setVisible($visible = true)
   742     {
   957     {
       
   958         if (is_string($visible) && 'false' == strtolower($visible)) {
       
   959             $visible = false;
       
   960         }
   743         $this->_visible = (bool) $visible;
   961         $this->_visible = (bool) $visible;
   744         return $this;
   962         return $this;
   745     }
   963     }
   746 
   964 
   747     /**
   965     /**
  1088     public function toArray()
  1306     public function toArray()
  1089     {
  1307     {
  1090         return array_merge(
  1308         return array_merge(
  1091             $this->getCustomProperties(),
  1309             $this->getCustomProperties(),
  1092             array(
  1310             array(
  1093                 'label'     => $this->getlabel(),
  1311                 'label'             => $this->getlabel(),
  1094                 'id'        => $this->getId(),
  1312                 'fragment'          => $this->getFragment(),
  1095                 'class'     => $this->getClass(),
  1313                 'id'                => $this->getId(),
  1096                 'title'     => $this->getTitle(),
  1314                 'class'             => $this->getClass(),
  1097                 'target'    => $this->getTarget(),
  1315                 'title'             => $this->getTitle(),
  1098                 'rel'       => $this->getRel(),
  1316                 'target'            => $this->getTarget(),
  1099                 'rev'       => $this->getRev(),
  1317                 'accesskey'         => $this->getAccesskey(),
  1100                 'order'     => $this->getOrder(),
  1318                 'rel'               => $this->getRel(),
  1101                 'resource'  => $this->getResource(),
  1319                 'rev'               => $this->getRev(),
  1102                 'privilege' => $this->getPrivilege(),
  1320                 'customHtmlAttribs' => $this->getCustomHtmlAttribs(),
  1103                 'active'    => $this->isActive(),
  1321                 'order'             => $this->getOrder(),
  1104                 'visible'   => $this->isVisible(),
  1322                 'resource'          => $this->getResource(),
  1105                 'type'      => get_class($this),
  1323                 'privilege'         => $this->getPrivilege(),
  1106                 'pages'     => parent::toArray()
  1324                 'active'            => $this->isActive(),
  1107             ));
  1325                 'visible'           => $this->isVisible(),
       
  1326                 'type'              => get_class($this),
       
  1327                 'pages'             => parent::toArray()
       
  1328             )
       
  1329         );
  1108     }
  1330     }
  1109 
  1331 
  1110     // Internal methods:
  1332     // Internal methods:
  1111 
  1333 
  1112     /**
  1334     /**
  1117      */
  1339      */
  1118     protected static function _normalizePropertyName($property)
  1340     protected static function _normalizePropertyName($property)
  1119     {
  1341     {
  1120         return str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
  1342         return str_replace(' ', '', ucwords(str_replace('_', ' ', $property)));
  1121     }
  1343     }
  1122     
  1344 
  1123     public static function setDefaultPageType($type = null) {
  1345     public static function setDefaultPageType($type = null) {
  1124         if($type !== null && !is_string($type)) {
  1346         if($type !== null && !is_string($type)) {
  1125             throw new Zend_Navigation_Exception(
  1347             throw new Zend_Navigation_Exception(
  1126                 'Cannot set default page type: type is no string but should be'
  1348                 'Cannot set default page type: type is no string but should be'
  1127             );
  1349             );
  1128         }
  1350         }
  1129         
  1351 
  1130         self::$_defaultPageType = $type;
  1352         self::$_defaultPageType = $type;
  1131     }
  1353     }
  1132     
  1354 
  1133     public static function getDefaultPageType() {
  1355     public static function getDefaultPageType() {
  1134         return self::$_defaultPageType;
  1356         return self::$_defaultPageType;
  1135     }
  1357     }
  1136 
  1358 
  1137     // Abstract methods:
  1359     // Abstract methods: