PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Backend/Block/Widget/Tabs.php

https://github.com/FiveDigital/magento2
PHP | 403 lines | 258 code | 38 blank | 107 comment | 49 complexity | b30a20c9e0d1ab8edb86d3b6485cfc5d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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_Backend
  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. * Tabs block
  28. *
  29. * @category Mage
  30. * @package Mage_Backend
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Backend_Block_Widget_Tabs extends Mage_Backend_Block_Widget
  34. {
  35. /**
  36. * tabs structure
  37. *
  38. * @var array
  39. */
  40. protected $_tabs = array();
  41. /**
  42. * Active tab key
  43. *
  44. * @var string
  45. */
  46. protected $_activeTab = null;
  47. /**
  48. * Destination HTML element id
  49. *
  50. * @var string
  51. */
  52. protected $_destElementId = 'content';
  53. protected function _construct()
  54. {
  55. $this->setTemplate('Mage_Backend::widget/tabs.phtml');
  56. }
  57. /**
  58. * retrieve destination html element id
  59. *
  60. * @return string
  61. */
  62. public function getDestElementId()
  63. {
  64. return $this->_destElementId;
  65. }
  66. public function setDestElementId($elementId)
  67. {
  68. $this->_destElementId = $elementId;
  69. return $this;
  70. }
  71. /**
  72. * Add new tab after another
  73. *
  74. * @param string $tabId new tab Id
  75. * @param array|Varien_Object $tab
  76. * @param string $afterTabId
  77. * @return Mage_Backend_Block_Widget_Tabs
  78. */
  79. public function addTabAfter($tabId, $tab, $afterTabId)
  80. {
  81. $this->addTab($tabId, $tab);
  82. $this->_tabs[$tabId]->setAfter($afterTabId);
  83. }
  84. /**
  85. * Add new tab
  86. *
  87. * @param string $tabId
  88. * @param array|Varien_Object|string $tab
  89. * @return Mage_Backend_Block_Widget_Tabs
  90. */
  91. public function addTab($tabId, $tab)
  92. {
  93. if (is_array($tab)) {
  94. $this->_tabs[$tabId] = new Varien_Object($tab);
  95. }
  96. elseif ($tab instanceof Varien_Object) {
  97. $this->_tabs[$tabId] = $tab;
  98. if (!$this->_tabs[$tabId]->hasTabId()) {
  99. $this->_tabs[$tabId]->setTabId($tabId);
  100. }
  101. }
  102. elseif (is_string($tab)) {
  103. if (strpos($tab, '_Block_')) {
  104. $this->_tabs[$tabId] = $this->getLayout()->createBlock(
  105. $tab,
  106. $this->getNameInLayout() . '_tab_' . $tabId
  107. );
  108. }
  109. elseif ($this->getChildBlock($tab)) {
  110. $this->_tabs[$tabId] = $this->getChildBlock($tab);
  111. }
  112. else {
  113. $this->_tabs[$tabId] = null;
  114. }
  115. if (!($this->_tabs[$tabId] instanceof Mage_Backend_Block_Widget_Tab_Interface)) {
  116. throw new Exception(Mage::helper('Mage_Backend_Helper_Data')->__('Wrong tab configuration.'));
  117. }
  118. }
  119. else {
  120. throw new Exception(Mage::helper('Mage_Backend_Helper_Data')->__('Wrong tab configuration.'));
  121. }
  122. if (is_null($this->_tabs[$tabId]->getUrl())) {
  123. $this->_tabs[$tabId]->setUrl('#');
  124. }
  125. if (!$this->_tabs[$tabId]->getTitle()) {
  126. $this->_tabs[$tabId]->setTitle($this->_tabs[$tabId]->getLabel());
  127. }
  128. $this->_tabs[$tabId]->setId($tabId);
  129. $this->_tabs[$tabId]->setTabId($tabId);
  130. if (is_null($this->_activeTab)) $this->_activeTab = $tabId;
  131. if (true === $this->_tabs[$tabId]->getActive()) $this->setActiveTab($tabId);
  132. return $this;
  133. }
  134. public function getActiveTabId()
  135. {
  136. return $this->getTabId($this->_tabs[$this->_activeTab]);
  137. }
  138. /**
  139. * Set Active Tab
  140. * Tab has to be not hidden and can show
  141. *
  142. * @param string $tabId
  143. * @return Mage_Backend_Block_Widget_Tabs
  144. */
  145. public function setActiveTab($tabId)
  146. {
  147. if (isset($this->_tabs[$tabId]) && $this->canShowTab($this->_tabs[$tabId])
  148. && !$this->getTabIsHidden($this->_tabs[$tabId])) {
  149. $this->_activeTab = $tabId;
  150. if (!(is_null($this->_activeTab)) && ($tabId !== $this->_activeTab)) {
  151. foreach ($this->_tabs as $id => $tab) {
  152. $tab->setActive($id === $tabId);
  153. }
  154. }
  155. }
  156. return $this;
  157. }
  158. /**
  159. * Set Active Tab
  160. *
  161. * @param string $tabId
  162. * @return Mage_Backend_Block_Widget_Tabs
  163. */
  164. protected function _setActiveTab($tabId)
  165. {
  166. foreach ($this->_tabs as $id => $tab) {
  167. if ($this->getTabId($tab) == $tabId) {
  168. $this->_activeTab = $id;
  169. $tab->setActive(true);
  170. return $this;
  171. }
  172. }
  173. return $this;
  174. }
  175. protected function _beforeToHtml()
  176. {
  177. if ($activeTab = $this->getRequest()->getParam('active_tab')) {
  178. $this->setActiveTab($activeTab);
  179. } elseif ($activeTabId = Mage::getSingleton('Mage_Backend_Model_Auth_Session')->getActiveTabId()) {
  180. $this->_setActiveTab($activeTabId);
  181. }
  182. $_new = array();
  183. foreach( $this->_tabs as $key => $tab ) {
  184. foreach( $this->_tabs as $k => $t ) {
  185. if( $t->getAfter() == $key ) {
  186. $_new[$key] = $tab;
  187. $_new[$k] = $t;
  188. } else {
  189. if( !$tab->getAfter() || !in_array($tab->getAfter(), array_keys($this->_tabs)) ) {
  190. $_new[$key] = $tab;
  191. }
  192. }
  193. }
  194. }
  195. $this->_tabs = $_new;
  196. unset($_new);
  197. $this->assign('tabs', $this->_tabs);
  198. return parent::_beforeToHtml();
  199. }
  200. public function getJsObjectName()
  201. {
  202. return $this->getId() . 'JsTabs';
  203. }
  204. public function getTabsIds()
  205. {
  206. if (empty($this->_tabs))
  207. return array();
  208. return array_keys($this->_tabs);
  209. }
  210. public function getTabId($tab, $withPrefix = true)
  211. {
  212. if ($tab instanceof Mage_Backend_Block_Widget_Tab_Interface) {
  213. return ($withPrefix ? $this->getId().'_' : '').$tab->getTabId();
  214. }
  215. return ($withPrefix ? $this->getId().'_' : '').$tab->getId();
  216. }
  217. public function canShowTab($tab)
  218. {
  219. if ($tab instanceof Mage_Backend_Block_Widget_Tab_Interface) {
  220. return $tab->canShowTab();
  221. }
  222. return true;
  223. }
  224. public function getTabIsHidden($tab)
  225. {
  226. if ($tab instanceof Mage_Backend_Block_Widget_Tab_Interface) {
  227. return $tab->isHidden();
  228. }
  229. return $tab->getIsHidden();
  230. }
  231. public function getTabUrl($tab)
  232. {
  233. if ($tab instanceof Mage_Backend_Block_Widget_Tab_Interface) {
  234. if (method_exists($tab, 'getTabUrl')) {
  235. return $tab->getTabUrl();
  236. }
  237. return '#';
  238. }
  239. if (!is_null($tab->getUrl())) {
  240. return $tab->getUrl();
  241. }
  242. return '#';
  243. }
  244. public function getTabTitle($tab)
  245. {
  246. if ($tab instanceof Mage_Backend_Block_Widget_Tab_Interface) {
  247. return $tab->getTabTitle();
  248. }
  249. return $tab->getTitle();
  250. }
  251. public function getTabClass($tab)
  252. {
  253. if ($tab instanceof Mage_Backend_Block_Widget_Tab_Interface) {
  254. if (method_exists($tab, 'getTabClass')) {
  255. return $tab->getTabClass();
  256. }
  257. return '';
  258. }
  259. return $tab->getClass();
  260. }
  261. public function getTabLabel($tab)
  262. {
  263. if ($tab instanceof Mage_Backend_Block_Widget_Tab_Interface) {
  264. return $tab->getTabLabel();
  265. }
  266. return $tab->getLabel();
  267. }
  268. public function getTabContent($tab)
  269. {
  270. if ($tab instanceof Mage_Backend_Block_Widget_Tab_Interface) {
  271. if ($tab->getSkipGenerateContent()) {
  272. return '';
  273. }
  274. return $tab->toHtml();
  275. }
  276. return $tab->getContent();
  277. }
  278. /**
  279. * Mark tabs as dependant of each other
  280. * Arbitrary number of tabs can be specified, but at least two
  281. *
  282. * @param string $tabOneId
  283. * @param string $tabTwoId
  284. * @param string $tabNId...
  285. */
  286. public function bindShadowTabs($tabOneId, $tabTwoId)
  287. {
  288. $tabs = array();
  289. $args = func_get_args();
  290. if ((!empty($args)) && (count($args) > 1)) {
  291. foreach ($args as $tabId) {
  292. if (isset($this->_tabs[$tabId])) {
  293. $tabs[$tabId] = $tabId;
  294. }
  295. }
  296. $blockId = $this->getId();
  297. foreach ($tabs as $tabId) {
  298. foreach ($tabs as $tabToId) {
  299. if ($tabId !== $tabToId) {
  300. if (!$this->_tabs[$tabToId]->getData('shadow_tabs')) {
  301. $this->_tabs[$tabToId]->setData('shadow_tabs', array());
  302. }
  303. $this->_tabs[$tabToId]->setData('shadow_tabs', array_merge(
  304. $this->_tabs[$tabToId]->getData('shadow_tabs'),
  305. array($blockId . '_' . $tabId)
  306. ));
  307. }
  308. }
  309. }
  310. }
  311. }
  312. /**
  313. * Obtain shadow tabs information
  314. *
  315. * @param bool $asJson
  316. * @return array|string
  317. */
  318. public function getAllShadowTabs($asJson = true)
  319. {
  320. $result = array();
  321. if (!empty($this->_tabs)) {
  322. $blockId = $this->getId();
  323. foreach (array_keys($this->_tabs) as $tabId) {
  324. if ($this->_tabs[$tabId]->getData('shadow_tabs')) {
  325. $result[$blockId . '_' . $tabId] = $this->_tabs[$tabId]->getData('shadow_tabs');
  326. }
  327. }
  328. }
  329. if ($asJson) {
  330. return Mage::helper('Mage_Core_Helper_Data')->jsonEncode($result);
  331. }
  332. return $result;
  333. }
  334. /**
  335. * Set tab property by tab's identifier
  336. *
  337. * @param string $tab
  338. * @param string $key
  339. * @param mixed $value
  340. * @return Mage_Backend_Block_Widget_Tabs
  341. */
  342. public function setTabData($tab, $key, $value)
  343. {
  344. if (isset($this->_tabs[$tab]) && $this->_tabs[$tab] instanceof Varien_Object) {
  345. if ($key == 'url') {
  346. $value = $this->getUrl($value, array('_current' => true, '_use_rewrite' => true));
  347. }
  348. $this->_tabs[$tab]->setData($key, $value);
  349. }
  350. return $this;
  351. }
  352. /**
  353. * Removes tab with passed id from tabs block
  354. *
  355. * @param string $tabId
  356. * @return Mage_Backend_Block_Widget_Tabs
  357. */
  358. public function removeTab($tabId)
  359. {
  360. if (isset($this->_tabs[$tabId])) {
  361. unset($this->_tabs[$tabId]);
  362. }
  363. return $this;
  364. }
  365. }