PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Page/Block/Template/Links.php

https://bitbucket.org/kdms/sh-magento
PHP | 241 lines | 117 code | 18 blank | 106 comment | 17 complexity | b681c5d7a65dd4c3711021b87d013429 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Page
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Simple links list block
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Page_Block_Template_Links extends Mage_Core_Block_Template
  34. {
  35. /**
  36. * All links
  37. *
  38. * @var array
  39. */
  40. protected $_links = array();
  41. /**
  42. * Cache key info
  43. *
  44. * @var null|array
  45. */
  46. protected $_cacheKeyInfo = null;
  47. /**
  48. * Set default template
  49. *
  50. */
  51. protected function _construct()
  52. {
  53. $this->setTemplate('page/template/links.phtml');
  54. }
  55. /**
  56. * Get all links
  57. *
  58. * @return array
  59. */
  60. public function getLinks()
  61. {
  62. return $this->_links;
  63. }
  64. /**
  65. * Add link to the list
  66. *
  67. * @param string $label
  68. * @param string $url
  69. * @param string $title
  70. * @param boolean $prepare
  71. * @param array $urlParams
  72. * @param int $position
  73. * @param string|array $liParams
  74. * @param string|array $aParams
  75. * @param string $beforeText
  76. * @param string $afterText
  77. * @return Mage_Page_Block_Template_Links
  78. */
  79. public function addLink($label, $url='', $title='', $prepare=false, $urlParams=array(),
  80. $position=null, $liParams=null, $aParams=null, $beforeText='', $afterText='')
  81. {
  82. if (is_null($label) || false===$label) {
  83. return $this;
  84. }
  85. $link = new Varien_Object(array(
  86. 'label' => $label,
  87. 'url' => ($prepare ? $this->getUrl($url, (is_array($urlParams) ? $urlParams : array())) : $url),
  88. 'title' => $title,
  89. 'li_params' => $this->_prepareParams($liParams),
  90. 'a_params' => $this->_prepareParams($aParams),
  91. 'before_text' => $beforeText,
  92. 'after_text' => $afterText,
  93. ));
  94. $this->_links[$this->_getNewPosition($position)] = $link;
  95. if (intval($position) > 0) {
  96. ksort($this->_links);
  97. }
  98. return $this;
  99. }
  100. /**
  101. * Add block to link list
  102. *
  103. * @param string $blockName
  104. * @return Mage_Page_Block_Template_Links
  105. */
  106. public function addLinkBlock($blockName)
  107. {
  108. $block = $this->getLayout()->getBlock($blockName);
  109. if ($block) {
  110. $this->_links[$this->_getNewPosition((int)$block->getPosition())] = $block;
  111. }
  112. return $this;
  113. }
  114. /**
  115. * Remove Link block by blockName
  116. *
  117. * @param string $blockName
  118. * @return Mage_Page_Block_Template_Links
  119. */
  120. public function removeLinkBlock($blockName)
  121. {
  122. foreach ($this->_links as $key => $link) {
  123. if ($link instanceof Mage_Core_Block_Abstract && $link->getNameInLayout() == $blockName) {
  124. unset($this->_links[$key]);
  125. }
  126. }
  127. return $this;
  128. }
  129. /**
  130. * Removes link by url
  131. *
  132. * @param string $url
  133. * @return Mage_Page_Block_Template_Links
  134. */
  135. public function removeLinkByUrl($url)
  136. {
  137. foreach ($this->_links as $k => $v) {
  138. if ($v->getUrl() == $url) {
  139. unset($this->_links[$k]);
  140. }
  141. }
  142. return $this;
  143. }
  144. /**
  145. * Get cache key informative items
  146. * Provide string array key to share specific info item with FPC placeholder
  147. *
  148. * @return array
  149. */
  150. public function getCacheKeyInfo()
  151. {
  152. if (is_null($this->_cacheKeyInfo)) {
  153. $links = array();
  154. if (!empty($this->_links)) {
  155. foreach ($this->_links as $position => $link) {
  156. if ($link instanceof Varien_Object) {
  157. $links[$position] = $link->getData();
  158. }
  159. }
  160. }
  161. $this->_cacheKeyInfo = parent::getCacheKeyInfo() + array(
  162. 'links' => base64_encode(serialize($links)),
  163. 'name' => $this->getNameInLayout()
  164. );
  165. }
  166. return $this->_cacheKeyInfo;
  167. }
  168. /**
  169. * Prepare tag attributes
  170. *
  171. * @param string|array $params
  172. * @return string
  173. */
  174. protected function _prepareParams($params)
  175. {
  176. if (is_string($params)) {
  177. return $params;
  178. } elseif (is_array($params)) {
  179. $result = '';
  180. foreach ($params as $key=>$value) {
  181. $result .= ' ' . $key . '="' . addslashes($value) . '"';
  182. }
  183. return $result;
  184. }
  185. return '';
  186. }
  187. /**
  188. * Set first/last
  189. *
  190. * @return Mage_Page_Block_Template_Links
  191. */
  192. protected function _beforeToHtml()
  193. {
  194. if (!empty($this->_links)) {
  195. reset($this->_links);
  196. $this->_links[key($this->_links)]->setIsFirst(true);
  197. end($this->_links);
  198. $this->_links[key($this->_links)]->setIsLast(true);
  199. }
  200. return parent::_beforeToHtml();
  201. }
  202. /**
  203. * Return new link position in list
  204. *
  205. * @param int $position
  206. * @return int
  207. */
  208. protected function _getNewPosition($position = 0)
  209. {
  210. if (intval($position) > 0) {
  211. while (isset($this->_links[$position])) {
  212. $position++;
  213. }
  214. } else {
  215. $position = 0;
  216. foreach ($this->_links as $k=>$v) {
  217. $position = $k;
  218. }
  219. $position += 10;
  220. }
  221. return $position;
  222. }
  223. }