00001 <?php
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 class Mage_Page_Block_Template_Links extends Mage_Core_Block_Template
00036 {
00037 
00038 
00039 
00040 
00041 
00042 
00043     protected $_links = array();
00044 
00045 
00046 
00047 
00048 
00049     protected function _construct()
00050     {
00051         $this->setTemplate('page/template/links.phtml');
00052     }
00053 
00054 
00055 
00056 
00057 
00058 
00059     public function getLinks()
00060     {
00061         return $this->_links;
00062     }
00063 
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079     public function addLink($label, $url='', $title='', $prepare=false, $urlParams=array(),
00080         $position=null, $liParams=null, $aParams=null, $beforeText='', $afterText='')
00081     {
00082         if (is_null($label) || false===$label) {
00083             return $this;
00084         }
00085         $link = new Varien_Object(array(
00086             'label'         => $label,
00087             'url'           => ($prepare ? $this->getUrl($url, (is_array($urlParams) ? $urlParams : array())) : $url),
00088             'title'         => $title,
00089             'li_params'     => $this->_prepareParams($liParams),
00090             'a_params'      => $this->_prepareParams($aParams),
00091             'before_text'   => $beforeText,
00092             'after_text'    => $afterText,
00093         ));
00094 
00095         if (intval($position) > 0) {
00096             while (isset($this->_links[$position])) {
00097                 $position++;
00098             }
00099             $this->_links[$position] = $link;
00100             ksort($this->_links);
00101         } else {
00102             $position = 0;
00103             foreach ($this->_links as $k=>$v) {
00104                 $position = $k;
00105             }
00106             $this->_links[$position+10] = $link;
00107         }
00108 
00109         return $this;
00110     }
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118     public function removeLinkByUrl($url)
00119     {
00120         foreach ($this->_links as $k => $v) {
00121             if ($v->getUrl() == $url) {
00122                 unset($this->_links[$k]);
00123             }
00124         }
00125 
00126         return $this;
00127     }
00128 
00129 
00130 
00131 
00132 
00133 
00134 
00135     protected function _prepareParams($params)
00136     {
00137         if (is_string($params)) {
00138             return $params;
00139         } elseif (is_array($params)) {
00140             $result = '';
00141             foreach ($params as $key=>$value) {
00142                 $result .= ' ' . $key . '="' . addslashes($value) . '"';
00143             }
00144             return $result;
00145         }
00146         return '';
00147     }
00148 
00149 
00150 
00151 
00152 
00153 
00154     protected function _beforeToHtml()
00155     {
00156         if (!empty($this->_links)) {
00157             reset($this->_links);
00158             $this->_links[key($this->_links)]->setIsFirst(true);
00159             end($this->_links);
00160             $this->_links[key($this->_links)]->setIsLast(true);
00161         }
00162         return parent::_beforeToHtml();
00163     }
00164 
00165 }