PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 271 lines | 130 code | 23 blank | 118 comment | 18 complexity | 209d295ad695628faf975c1366e3f70d MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Page
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  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->_addIntoPosition($link, $position);
  95. return $this;
  96. }
  97. /**
  98. * Add link into collection
  99. *
  100. * @param Varien_Object $link
  101. * @param int $position
  102. * @return Mage_Page_Block_Template_Links
  103. */
  104. protected function _addIntoPosition($link, $position)
  105. {
  106. $this->_links[$this->_getNewPosition($position)] = $link;
  107. if (intval($position) > 0) {
  108. ksort($this->_links);
  109. }
  110. return $this;
  111. }
  112. /**
  113. * Add block to link list
  114. *
  115. * @param string $blockName
  116. * @return Mage_Page_Block_Template_Links
  117. */
  118. public function addLinkBlock($blockName)
  119. {
  120. $block = $this->getLayout()->getBlock($blockName);
  121. if ($block) {
  122. $position = (int)$block->getPosition();
  123. $this->_addIntoPosition($block, $position);
  124. }
  125. return $this;
  126. }
  127. /**
  128. * Remove Link block by blockName
  129. *
  130. * @param string $blockName
  131. * @return Mage_Page_Block_Template_Links
  132. */
  133. public function removeLinkBlock($blockName)
  134. {
  135. foreach ($this->_links as $key => $link) {
  136. if ($link instanceof Mage_Core_Block_Abstract && $link->getNameInLayout() == $blockName) {
  137. unset($this->_links[$key]);
  138. }
  139. }
  140. return $this;
  141. }
  142. /**
  143. * Removes link by url
  144. *
  145. * @param string $url
  146. * @return Mage_Page_Block_Template_Links
  147. */
  148. public function removeLinkByUrl($url)
  149. {
  150. foreach ($this->_links as $k => $v) {
  151. if ($v->getUrl() == $url) {
  152. unset($this->_links[$k]);
  153. }
  154. }
  155. return $this;
  156. }
  157. /**
  158. * Get cache key informative items
  159. * Provide string array key to share specific info item with FPC placeholder
  160. *
  161. * @return array
  162. */
  163. public function getCacheKeyInfo()
  164. {
  165. if (is_null($this->_cacheKeyInfo)) {
  166. $links = array();
  167. if (!empty($this->_links)) {
  168. foreach ($this->_links as $position => $link) {
  169. if ($link instanceof Varien_Object) {
  170. $links[$position] = $link->getData();
  171. }
  172. }
  173. }
  174. $this->_cacheKeyInfo = parent::getCacheKeyInfo() + array(
  175. 'links' => base64_encode(serialize($links)),
  176. 'name' => $this->getNameInLayout()
  177. );
  178. }
  179. return $this->_cacheKeyInfo;
  180. }
  181. /**
  182. * Prepare tag attributes
  183. *
  184. * @param string|array $params
  185. * @return string
  186. */
  187. protected function _prepareParams($params)
  188. {
  189. if (is_string($params)) {
  190. return $params;
  191. } elseif (is_array($params)) {
  192. $result = '';
  193. foreach ($params as $key=>$value) {
  194. $result .= ' ' . $key . '="' . addslashes($value) . '"';
  195. }
  196. return $result;
  197. }
  198. return '';
  199. }
  200. /**
  201. * Set first/last
  202. *
  203. * @return Mage_Page_Block_Template_Links
  204. */
  205. protected function _beforeToHtml()
  206. {
  207. if (!empty($this->_links)) {
  208. reset($this->_links);
  209. $this->_links[key($this->_links)]->setIsFirst(true);
  210. end($this->_links);
  211. $this->_links[key($this->_links)]->setIsLast(true);
  212. }
  213. return parent::_beforeToHtml();
  214. }
  215. /**
  216. * Return new link position in list
  217. *
  218. * @param int $position
  219. * @return int
  220. */
  221. protected function _getNewPosition($position = 0)
  222. {
  223. if (intval($position) > 0) {
  224. while (isset($this->_links[$position])) {
  225. $position++;
  226. }
  227. } else {
  228. $position = 0;
  229. foreach ($this->_links as $k=>$v) {
  230. $position = $k;
  231. }
  232. $position += 10;
  233. }
  234. return $position;
  235. }
  236. /**
  237. * Get tags array for saving cache
  238. *
  239. * @return array
  240. */
  241. public function getCacheTags()
  242. {
  243. if (Mage::getSingleton('customer/session')->isLoggedIn()) {
  244. $this->addModelTags(Mage::getSingleton('customer/session')->getCustomer());
  245. }
  246. return parent::getCacheTags();
  247. }
  248. }