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 class Mage_Adminhtml_Block_Widget_Tabs extends Mage_Adminhtml_Block_Widget
00035 {
00036
00037
00038
00039
00040
00041 protected $_tabs = array();
00042
00043
00044
00045
00046
00047
00048 protected $_activeTab = null;
00049
00050
00051
00052
00053
00054
00055 protected $_destElementId = 'content';
00056
00057 protected function _construct()
00058 {
00059 $this->setTemplate('widget/tabs.phtml');
00060 }
00061
00062
00063
00064
00065
00066
00067 public function getDestElementId()
00068 {
00069 return $this->_destElementId;
00070 }
00071
00072 public function setDestElementId($elementId)
00073 {
00074 $this->_destElementId = $elementId;
00075 return $this;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085 public function addTab($tabId, $tab)
00086 {
00087 if (is_array($tab)) {
00088 $this->_tabs[$tabId] = new Varien_Object($tab);
00089 }
00090 elseif ($tab instanceof Varien_Object) {
00091 $this->_tabs[$tabId] = $tab;
00092 if (!$this->_tabs[$tabId]->hasTabId()) {
00093 $this->_tabs[$tabId]->setTabId($tabId);
00094 }
00095 }
00096 elseif (is_string($tab)) {
00097 if (strpos($tab, '/')) {
00098 $this->_tabs[$tabId] = $this->getLayout()->createBlock($tab);
00099 }
00100 elseif ($this->getChild($tab)) {
00101 $this->_tabs[$tabId] = $this->getChild($tab);
00102 }
00103 else {
00104 $this->_tabs[$tabId] = null;
00105 }
00106
00107 if (!($this->_tabs[$tabId] instanceof Mage_Adminhtml_Block_Widget_Tab_Interface)) {
00108 throw new Exception(Mage::helper('adminhtml')->__('Wrong tab configuration'));
00109 }
00110 $this->_tabs[$tabId]->setTabId($tabId);
00111
00112 if (is_null($this->_activeTab)) $this->_activeTab = $tabId;
00113 return $this;
00114 }
00115 else {
00116 throw new Exception(Mage::helper('adminhtml')->__('Wrong tab configuration'));
00117 }
00118
00119 if (is_null($this->_tabs[$tabId]->getUrl())) {
00120 $this->_tabs[$tabId]->setUrl('#');
00121 }
00122
00123 if (!$this->_tabs[$tabId]->getTitle()) {
00124 $this->_tabs[$tabId]->setTitle($this->_tabs[$tabId]->getLabel());
00125 }
00126
00127 $this->_tabs[$tabId]->setId($tabId);
00128
00129 if (is_null($this->_activeTab)) $this->_activeTab = $tabId;
00130 if (true === $this->_tabs[$tabId]->getActive()) $this->setActiveTab($tabId);
00131
00132 return $this;
00133 }
00134
00135 public function getActiveTabId()
00136 {
00137 return $this->getTabId($this->_tabs[$this->_activeTab]);
00138 }
00139
00140
00141
00142
00143
00144
00145
00146 public function setActiveTab($tabId)
00147 {
00148 if (isset($this->_tabs[$tabId])) {
00149 $this->_activeTab = $tabId;
00150 if (!(is_null($this->_activeTab)) && ($tabId !== $this->_activeTab)) {
00151 foreach ($this->_tabs as $id => $tab) {
00152 $tab->setActive($id === $tabId);
00153 }
00154 }
00155 }
00156 return $this;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 protected function _setActiveTab($tabId)
00166 {
00167 foreach ($this->_tabs as $id => $tab) {
00168 if ($this->getTabId($tab) == $tabId) {
00169 $this->_activeTab = $id;
00170 $tab->setActive(true);
00171 return $this;
00172 }
00173 }
00174 return $this;
00175 }
00176
00177 protected function _beforeToHtml()
00178 {
00179 if ($activeTab = $this->getRequest()->getParam('active_tab')) {
00180 $this->setActiveTab($activeTab);
00181 } elseif ($activeTabId = Mage::getSingleton('admin/session')->getActiveTabId()) {
00182 $this->_setActiveTab($activeTabId);
00183 }
00184
00185 $_new = array();
00186 foreach( $this->_tabs as $key => $tab ) {
00187 foreach( $this->_tabs as $k => $t ) {
00188 if( $t->getAfter() == $key ) {
00189 $_new[$key] = $tab;
00190 $_new[$k] = $t;
00191 } else {
00192 if( !$tab->getAfter() || !in_array($tab->getAfter(), array_keys($this->_tabs)) ) {
00193 $_new[$key] = $tab;
00194 }
00195 }
00196 }
00197 }
00198
00199 $this->_tabs = $_new;
00200 unset($_new);
00201
00202 $this->assign('tabs', $this->_tabs);
00203 return parent::_beforeToHtml();
00204 }
00205
00206 public function getJsObjectName()
00207 {
00208 return $this->getId() . 'JsTabs';
00209 }
00210
00211 public function getTabsIds()
00212 {
00213 if (empty($this->_tabs))
00214 return array();
00215 return array_keys($this->_tabs);
00216 }
00217
00218 public function getTabId($tab, $withPrefix = true)
00219 {
00220 if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
00221 return ($withPrefix ? $this->getId().'_' : '').$tab->getTabId();
00222 }
00223 return ($withPrefix ? $this->getId().'_' : '').$tab->getId();
00224 }
00225
00226 public function canShowTab($tab)
00227 {
00228 if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
00229 return $tab->canShowTab();
00230 }
00231 return true;
00232 }
00233
00234 public function getTabIsHidden($tab)
00235 {
00236 if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
00237 return $tab->isHidden();
00238 }
00239 return $tab->getIsHidden();
00240 }
00241
00242 public function getTabUrl($tab)
00243 {
00244 if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
00245 if (method_exists($tab, 'getTabUrl')) {
00246 return $tab->getTabUrl();
00247 }
00248 return '#';
00249 }
00250 if (!is_null($tab->getUrl())) {
00251 return $tab->getUrl();
00252 }
00253 return '#';
00254 }
00255
00256 public function getTabTitle($tab)
00257 {
00258 if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
00259 return $tab->getTabTitle();
00260 }
00261 return $tab->getTitle();
00262 }
00263
00264 public function getTabClass($tab)
00265 {
00266 if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
00267 if (method_exists($tab, 'getTabClass')) {
00268 return $tab->getTabClass();
00269 }
00270 return '';
00271 }
00272 return $tab->getClass();
00273 }
00274
00275
00276 public function getTabLabel($tab)
00277 {
00278 if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
00279 return $tab->getTabLabel();
00280 }
00281 return $tab->getLabel();
00282 }
00283
00284 public function getTabContent($tab)
00285 {
00286 if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
00287 if ($tab->getSkipGenerateContent()) {
00288 return '';
00289 }
00290 return $tab->toHtml();
00291 }
00292 return $tab->getContent();
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 public function bindShadowTabs($tabOneId, $tabTwoId)
00304 {
00305 $tabs = array();
00306 $args = func_get_args();
00307 if ((!empty($args)) && (count($args) > 1)) {
00308 foreach ($args as $tabId) {
00309 if (isset($this->_tabs[$tabId])) {
00310 $tabs[$tabId] = $tabId;
00311 }
00312 }
00313 $blockId = $this->getId();
00314 foreach ($tabs as $tabId) {
00315 foreach ($tabs as $tabToId) {
00316 if ($tabId !== $tabToId) {
00317 if (!$this->_tabs[$tabToId]->getData('shadow_tabs')) {
00318 $this->_tabs[$tabToId]->setData('shadow_tabs', array());
00319 }
00320 $this->_tabs[$tabToId]->setData('shadow_tabs', array_merge(
00321 $this->_tabs[$tabToId]->getData('shadow_tabs'),
00322 array($blockId . '_' . $tabId)
00323 ));
00324 }
00325 }
00326 }
00327 }
00328 }
00329
00330
00331
00332
00333
00334
00335
00336 public function getAllShadowTabs($asJson = true)
00337 {
00338 $result = array();
00339 if (!empty($this->_tabs)) {
00340 $blockId = $this->getId();
00341 foreach (array_keys($this->_tabs) as $tabId) {
00342 if ($this->_tabs[$tabId]->getData('shadow_tabs')) {
00343 $result[$blockId . '_' . $tabId] = $this->_tabs[$tabId]->getData('shadow_tabs');
00344 }
00345 }
00346 }
00347 if ($asJson) {
00348 return Zend_Json::encode($result);
00349 }
00350 return $result;
00351 }
00352 }