1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Page_Block_Template_Links extends Mage_Core_Block_Template
36: {
37: 38: 39: 40: 41:
42: protected $_links = array();
43:
44: 45: 46: 47: 48:
49: protected $_cacheKeyInfo = null;
50:
51: 52: 53: 54:
55: protected function _construct()
56: {
57: $this->setTemplate('page/template/links.phtml');
58: }
59:
60: 61: 62: 63: 64:
65: public function getLinks()
66: {
67: return $this->_links;
68: }
69:
70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84:
85: public function addLink($label, $url='', $title='', $prepare=false, $urlParams=array(),
86: $position=null, $liParams=null, $aParams=null, $beforeText='', $afterText='')
87: {
88: if (is_null($label) || false===$label) {
89: return $this;
90: }
91: $link = new Varien_Object(array(
92: 'label' => $label,
93: 'url' => ($prepare ? $this->getUrl($url, (is_array($urlParams) ? $urlParams : array())) : $url),
94: 'title' => $title,
95: 'li_params' => $this->_prepareParams($liParams),
96: 'a_params' => $this->_prepareParams($aParams),
97: 'before_text' => $beforeText,
98: 'after_text' => $afterText,
99: ));
100:
101: $this->_links[$this->_getNewPosition($position)] = $link;
102: if (intval($position) > 0) {
103: ksort($this->_links);
104: }
105:
106: return $this;
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function addLinkBlock($blockName)
116: {
117: $block = $this->getLayout()->getBlock($blockName);
118: if ($block) {
119: $this->_links[$this->_getNewPosition((int)$block->getPosition())] = $block;
120: }
121: return $this;
122: }
123:
124: 125: 126: 127: 128: 129:
130: public function removeLinkBlock($blockName)
131: {
132: foreach ($this->_links as $key => $link) {
133: if ($link instanceof Mage_Core_Block_Abstract && $link->getNameInLayout() == $blockName) {
134: unset($this->_links[$key]);
135: }
136: }
137: return $this;
138: }
139:
140: 141: 142: 143: 144: 145:
146: public function removeLinkByUrl($url)
147: {
148: foreach ($this->_links as $k => $v) {
149: if ($v->getUrl() == $url) {
150: unset($this->_links[$k]);
151: }
152: }
153:
154: return $this;
155: }
156:
157: 158: 159: 160: 161: 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:
180: return $this->_cacheKeyInfo;
181: }
182:
183: 184: 185: 186: 187: 188:
189: protected function _prepareParams($params)
190: {
191: if (is_string($params)) {
192: return $params;
193: } elseif (is_array($params)) {
194: $result = '';
195: foreach ($params as $key=>$value) {
196: $result .= ' ' . $key . '="' . addslashes($value) . '"';
197: }
198: return $result;
199: }
200: return '';
201: }
202:
203: 204: 205: 206: 207:
208: protected function _beforeToHtml()
209: {
210: if (!empty($this->_links)) {
211: reset($this->_links);
212: $this->_links[key($this->_links)]->setIsFirst(true);
213: end($this->_links);
214: $this->_links[key($this->_links)]->setIsLast(true);
215: }
216: return parent::_beforeToHtml();
217: }
218:
219: 220: 221: 222: 223: 224:
225: protected function _getNewPosition($position = 0)
226: {
227: if (intval($position) > 0) {
228: while (isset($this->_links[$position])) {
229: $position++;
230: }
231: } else {
232: $position = 0;
233: foreach ($this->_links as $k=>$v) {
234: $position = $k;
235: }
236: $position += 10;
237: }
238: return $position;
239: }
240:
241: }
242: