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: class Mage_Adminhtml_Block_Widget_Tabs extends Mage_Adminhtml_Block_Widget
35: {
36: 37: 38: 39: 40:
41: protected $_tabs = array();
42:
43: 44: 45: 46: 47:
48: protected $_activeTab = null;
49:
50: 51: 52: 53: 54:
55: protected $_destElementId = 'content';
56:
57: protected function _construct()
58: {
59: $this->setTemplate('widget/tabs.phtml');
60: }
61:
62: 63: 64: 65: 66:
67: public function getDestElementId()
68: {
69: return $this->_destElementId;
70: }
71:
72: public function setDestElementId($elementId)
73: {
74: $this->_destElementId = $elementId;
75: return $this;
76: }
77:
78: 79: 80: 81: 82: 83: 84: 85:
86: public function addTabAfter($tabId, $tab, $afterTabId)
87: {
88: $this->addTab($tabId, $tab);
89: $this->_tabs[$tabId]->setAfter($afterTabId);
90: }
91:
92: 93: 94: 95: 96: 97: 98:
99: public function addTab($tabId, $tab)
100: {
101: if (is_array($tab)) {
102: $this->_tabs[$tabId] = new Varien_Object($tab);
103: }
104: elseif ($tab instanceof Varien_Object) {
105: $this->_tabs[$tabId] = $tab;
106: if (!$this->_tabs[$tabId]->hasTabId()) {
107: $this->_tabs[$tabId]->setTabId($tabId);
108: }
109: }
110: elseif (is_string($tab)) {
111: if (strpos($tab, '/')) {
112: $this->_tabs[$tabId] = $this->getLayout()->createBlock($tab);
113: }
114: elseif ($this->getChild($tab)) {
115: $this->_tabs[$tabId] = $this->getChild($tab);
116: }
117: else {
118: $this->_tabs[$tabId] = null;
119: }
120:
121: if (!($this->_tabs[$tabId] instanceof Mage_Adminhtml_Block_Widget_Tab_Interface)) {
122: throw new Exception(Mage::helper('adminhtml')->__('Wrong tab configuration.'));
123: }
124: }
125: else {
126: throw new Exception(Mage::helper('adminhtml')->__('Wrong tab configuration.'));
127: }
128:
129: if (is_null($this->_tabs[$tabId]->getUrl())) {
130: $this->_tabs[$tabId]->setUrl('#');
131: }
132:
133: if (!$this->_tabs[$tabId]->getTitle()) {
134: $this->_tabs[$tabId]->setTitle($this->_tabs[$tabId]->getLabel());
135: }
136:
137: $this->_tabs[$tabId]->setId($tabId);
138: $this->_tabs[$tabId]->setTabId($tabId);
139:
140: if (is_null($this->_activeTab)) $this->_activeTab = $tabId;
141: if (true === $this->_tabs[$tabId]->getActive()) $this->setActiveTab($tabId);
142:
143: return $this;
144: }
145:
146: public function getActiveTabId()
147: {
148: return $this->getTabId($this->_tabs[$this->_activeTab]);
149: }
150:
151: 152: 153: 154: 155: 156: 157:
158: public function setActiveTab($tabId)
159: {
160: if (isset($this->_tabs[$tabId]) && $this->canShowTab($this->_tabs[$tabId])
161: && !$this->getTabIsHidden($this->_tabs[$tabId])) {
162: $this->_activeTab = $tabId;
163: if (!(is_null($this->_activeTab)) && ($tabId !== $this->_activeTab)) {
164: foreach ($this->_tabs as $id => $tab) {
165: $tab->setActive($id === $tabId);
166: }
167: }
168: }
169: return $this;
170: }
171:
172: 173: 174: 175: 176: 177:
178: protected function _setActiveTab($tabId)
179: {
180: foreach ($this->_tabs as $id => $tab) {
181: if ($this->getTabId($tab) == $tabId) {
182: $this->_activeTab = $id;
183: $tab->setActive(true);
184: return $this;
185: }
186: }
187: return $this;
188: }
189:
190: protected function _beforeToHtml()
191: {
192: if ($activeTab = $this->getRequest()->getParam('active_tab')) {
193: $this->setActiveTab($activeTab);
194: } elseif ($activeTabId = Mage::getSingleton('admin/session')->getActiveTabId()) {
195: $this->_setActiveTab($activeTabId);
196: }
197:
198: $_new = array();
199: foreach( $this->_tabs as $key => $tab ) {
200: foreach( $this->_tabs as $k => $t ) {
201: if( $t->getAfter() == $key ) {
202: $_new[$key] = $tab;
203: $_new[$k] = $t;
204: } else {
205: if( !$tab->getAfter() || !in_array($tab->getAfter(), array_keys($this->_tabs)) ) {
206: $_new[$key] = $tab;
207: }
208: }
209: }
210: }
211:
212: $this->_tabs = $_new;
213: unset($_new);
214:
215: $this->assign('tabs', $this->_tabs);
216: return parent::_beforeToHtml();
217: }
218:
219: public function getJsObjectName()
220: {
221: return $this->getId() . 'JsTabs';
222: }
223:
224: public function getTabsIds()
225: {
226: if (empty($this->_tabs))
227: return array();
228: return array_keys($this->_tabs);
229: }
230:
231: public function getTabId($tab, $withPrefix = true)
232: {
233: if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
234: return ($withPrefix ? $this->getId().'_' : '').$tab->getTabId();
235: }
236: return ($withPrefix ? $this->getId().'_' : '').$tab->getId();
237: }
238:
239: public function canShowTab($tab)
240: {
241: if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
242: return $tab->canShowTab();
243: }
244: return true;
245: }
246:
247: public function getTabIsHidden($tab)
248: {
249: if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
250: return $tab->isHidden();
251: }
252: return $tab->getIsHidden();
253: }
254:
255: public function getTabUrl($tab)
256: {
257: if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
258: if (method_exists($tab, 'getTabUrl')) {
259: return $tab->getTabUrl();
260: }
261: return '#';
262: }
263: if (!is_null($tab->getUrl())) {
264: return $tab->getUrl();
265: }
266: return '#';
267: }
268:
269: public function getTabTitle($tab)
270: {
271: if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
272: return $tab->getTabTitle();
273: }
274: return $tab->getTitle();
275: }
276:
277: public function getTabClass($tab)
278: {
279: if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
280: if (method_exists($tab, 'getTabClass')) {
281: return $tab->getTabClass();
282: }
283: return '';
284: }
285: return $tab->getClass();
286: }
287:
288:
289: public function getTabLabel($tab)
290: {
291: if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
292: return $tab->getTabLabel();
293: }
294: return $tab->getLabel();
295: }
296:
297: public function getTabContent($tab)
298: {
299: if ($tab instanceof Mage_Adminhtml_Block_Widget_Tab_Interface) {
300: if ($tab->getSkipGenerateContent()) {
301: return '';
302: }
303: return $tab->toHtml();
304: }
305: return $tab->getContent();
306: }
307:
308: 309: 310: 311: 312: 313: 314: 315:
316: public function bindShadowTabs($tabOneId, $tabTwoId)
317: {
318: $tabs = array();
319: $args = func_get_args();
320: if ((!empty($args)) && (count($args) > 1)) {
321: foreach ($args as $tabId) {
322: if (isset($this->_tabs[$tabId])) {
323: $tabs[$tabId] = $tabId;
324: }
325: }
326: $blockId = $this->getId();
327: foreach ($tabs as $tabId) {
328: foreach ($tabs as $tabToId) {
329: if ($tabId !== $tabToId) {
330: if (!$this->_tabs[$tabToId]->getData('shadow_tabs')) {
331: $this->_tabs[$tabToId]->setData('shadow_tabs', array());
332: }
333: $this->_tabs[$tabToId]->setData('shadow_tabs', array_merge(
334: $this->_tabs[$tabToId]->getData('shadow_tabs'),
335: array($blockId . '_' . $tabId)
336: ));
337: }
338: }
339: }
340: }
341: }
342:
343: 344: 345: 346: 347: 348:
349: public function getAllShadowTabs($asJson = true)
350: {
351: $result = array();
352: if (!empty($this->_tabs)) {
353: $blockId = $this->getId();
354: foreach (array_keys($this->_tabs) as $tabId) {
355: if ($this->_tabs[$tabId]->getData('shadow_tabs')) {
356: $result[$blockId . '_' . $tabId] = $this->_tabs[$tabId]->getData('shadow_tabs');
357: }
358: }
359: }
360: if ($asJson) {
361: return Mage::helper('core')->jsonEncode($result);
362: }
363: return $result;
364: }
365:
366: 367: 368: 369: 370: 371: 372: 373:
374: public function setTabData($tab, $key, $value)
375: {
376: if (isset($this->_tabs[$tab]) && $this->_tabs[$tab] instanceof Varien_Object) {
377: if ($key == 'url') {
378: $value = $this->getUrl($value, array('_current' => true, '_use_rewrite' => true));
379: }
380: $this->_tabs[$tab]->setData($key, $value);
381: }
382:
383: return $this;
384: }
385:
386: 387: 388: 389: 390: 391:
392: public function removeTab($tabId)
393: {
394: if (isset($this->_tabs[$tabId])) {
395: unset($this->_tabs[$tabId]);
396: }
397: return $this;
398: }
399: }
400: