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@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_Catalog
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Widget to display catalog link
29: *
30: * @category Mage
31: * @package Mage_Catalog
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34:
35: class Mage_Catalog_Block_Widget_Link
36: extends Mage_Core_Block_Html_Link
37: implements Mage_Widget_Block_Interface
38: {
39: /**
40: * Entity model name which must be used to retrieve entity specific data.
41: * @var null|Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract
42: */
43: protected $_entityResource = null;
44:
45: /**
46: * Prepared href attribute
47: *
48: * @var string
49: */
50: protected $_href;
51:
52: /**
53: * Prepared anchor text
54: *
55: * @var string
56: */
57: protected $_anchorText;
58:
59: /**
60: * Prepare url using passed id path and return it
61: * or return false if path was not found in url rewrites.
62: *
63: * @return string|false
64: */
65: public function getHref()
66: {
67: if (!$this->_href) {
68:
69: if($this->hasStoreId()) {
70: $store = Mage::app()->getStore($this->getStoreId());
71: } else {
72: $store = Mage::app()->getStore();
73: }
74:
75: /* @var $store Mage_Core_Model_Store */
76: $href = "";
77: if ($this->getData('id_path')) {
78: /* @var $urlRewriteResource Mage_Core_Model_Mysql4_Url_Rewrite */
79: $urlRewriteResource = Mage::getResourceSingleton('core/url_rewrite');
80: $href = $urlRewriteResource->getRequestPathByIdPath($this->getData('id_path'), $store);
81: if (!$href) {
82: return false;
83: }
84: }
85:
86: $this->_href = $store->getUrl('', array('_direct' => $href));
87: }
88:
89: if(strpos($this->_href, "___store") === false){
90: $symbol = (strpos($this->_href, "?") === false) ? "?" : "&";
91: $this->_href = $this->_href . $symbol . "___store=" . $store->getCode();
92: }
93:
94: return $this->_href;
95: }
96:
97: /**
98: * Prepare anchor text using passed text as parameter.
99: * If anchor text was not specified get entity name from DB.
100: *
101: * @return string
102: */
103: public function getAnchorText()
104: {
105: if (!$this->_anchorText && $this->_entityResource) {
106: if (!$this->getData('anchor_text')) {
107: $idPath = explode('/', $this->_getData('id_path'));
108: if (isset($idPath[1])) {
109: $id = $idPath[1];
110: if ($id) {
111: $this->_anchorText = $this->_entityResource->getAttributeRawValue($id, 'name', Mage::app()->getStore());
112: }
113: }
114: } else {
115: $this->_anchorText = $this->getData('anchor_text');
116: }
117: }
118:
119: return $this->_anchorText;
120: }
121:
122: /**
123: * Render block HTML
124: * or return empty string if url can't be prepared
125: *
126: * @return string
127: */
128: protected function _toHtml()
129: {
130: if ($this->getHref()) {
131: return parent::_toHtml();
132: }
133: return '';
134: }
135: }
136: