PageRenderTime 27ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-backend/Block/Widget/Tabs.php

https://gitlab.com/daigiangaitu91/magento
PHP | 484 lines | 341 code | 26 blank | 117 comment | 30 complexity | 528918ca5a7e09e4b5b32eaccda32350 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2015 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget;
  7. use Magento\Backend\Block\Widget\Tab\TabInterface;
  8. /**
  9. * Tabs block
  10. * @SuppressWarnings(PHPMD.NumberOfChildren)
  11. */
  12. class Tabs extends \Magento\Backend\Block\Widget
  13. {
  14. /**
  15. * Tabs structure
  16. *
  17. * @var array
  18. */
  19. protected $_tabs = [];
  20. /**
  21. * Active tab key
  22. *
  23. * @var string
  24. */
  25. protected $_activeTab = null;
  26. /**
  27. * Destination HTML element id
  28. *
  29. * @var string
  30. */
  31. protected $_destElementId = 'content';
  32. /** @var string */
  33. protected $_template = 'Magento_Backend::widget/tabs.phtml';
  34. /**
  35. * @var \Magento\Backend\Model\Auth\Session
  36. */
  37. protected $_authSession;
  38. /**
  39. * @var \Magento\Framework\Json\EncoderInterface
  40. */
  41. private $_jsonEncoder;
  42. /**
  43. * @param \Magento\Backend\Block\Template\Context $context
  44. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  45. * @param \Magento\Backend\Model\Auth\Session $authSession
  46. * @param array $data
  47. */
  48. public function __construct(
  49. \Magento\Backend\Block\Template\Context $context,
  50. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  51. \Magento\Backend\Model\Auth\Session $authSession,
  52. array $data = []
  53. ) {
  54. $this->_authSession = $authSession;
  55. parent::__construct($context, $data);
  56. $this->_jsonEncoder = $jsonEncoder;
  57. }
  58. /**
  59. * Retrieve destination html element id
  60. *
  61. * @return string
  62. */
  63. public function getDestElementId()
  64. {
  65. return $this->_destElementId;
  66. }
  67. /**
  68. * Set destination element id
  69. *
  70. * @param string $elementId
  71. * @return $this
  72. */
  73. public function setDestElementId($elementId)
  74. {
  75. $this->_destElementId = $elementId;
  76. return $this;
  77. }
  78. /**
  79. * Add new tab after another
  80. *
  81. * @param string $tabId new tab Id
  82. * @param array|\Magento\Framework\DataObject $tab
  83. * @param string $afterTabId
  84. * @return void
  85. */
  86. public function addTabAfter($tabId, $tab, $afterTabId)
  87. {
  88. $this->addTab($tabId, $tab);
  89. $this->_tabs[$tabId]->setAfter($afterTabId);
  90. }
  91. /**
  92. * Add new tab
  93. *
  94. * @param string $tabId
  95. * @param array|\Magento\Framework\DataObject|string $tab
  96. * @return $this
  97. * @throws \Exception
  98. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  99. */
  100. public function addTab($tabId, $tab)
  101. {
  102. if (empty($tabId)) {
  103. throw new \Exception(__('Please correct the tab configuration and try again. Tab Id should be not empry'));
  104. }
  105. if (is_array($tab)) {
  106. $this->_tabs[$tabId] = new \Magento\Framework\DataObject($tab);
  107. } elseif ($tab instanceof \Magento\Framework\DataObject) {
  108. $this->_tabs[$tabId] = $tab;
  109. if (!$this->_tabs[$tabId]->hasTabId()) {
  110. $this->_tabs[$tabId]->setTabId($tabId);
  111. }
  112. } elseif (is_string($tab)) {
  113. $this->_addTabByName($tab, $tabId);
  114. if (!$this->_tabs[$tabId] instanceof TabInterface) {
  115. unset($this->_tabs[$tabId]);
  116. return $this;
  117. }
  118. } else {
  119. throw new \Exception(__('Please correct the tab configuration and try again.'));
  120. }
  121. if ($this->_tabs[$tabId]->getUrl() === null) {
  122. $this->_tabs[$tabId]->setUrl('#');
  123. }
  124. if (!$this->_tabs[$tabId]->getTitle()) {
  125. $this->_tabs[$tabId]->setTitle($this->_tabs[$tabId]->getLabel());
  126. }
  127. $this->_tabs[$tabId]->setId($tabId);
  128. $this->_tabs[$tabId]->setTabId($tabId);
  129. if ($this->_activeTab === null) {
  130. $this->_activeTab = $tabId;
  131. }
  132. if (true === $this->_tabs[$tabId]->getActive()) {
  133. $this->setActiveTab($tabId);
  134. }
  135. return $this;
  136. }
  137. /**
  138. * Add tab by tab block name
  139. *
  140. * @param string $tab
  141. * @param string $tabId
  142. * @return void
  143. * @throws \Exception
  144. */
  145. protected function _addTabByName($tab, $tabId)
  146. {
  147. if (strpos($tab, '\Block\\')) {
  148. $this->_tabs[$tabId] = $this->getLayout()->createBlock($tab, $this->getNameInLayout() . '_tab_' . $tabId);
  149. } elseif ($this->getChildBlock($tab)) {
  150. $this->_tabs[$tabId] = $this->getChildBlock($tab);
  151. } else {
  152. $this->_tabs[$tabId] = null;
  153. }
  154. if ($this->_tabs[$tabId] !== null && !$this->_tabs[$tabId] instanceof TabInterface) {
  155. throw new \Exception(__('Please correct the tab configuration and try again.'));
  156. }
  157. }
  158. /**
  159. * @return string
  160. */
  161. public function getActiveTabId()
  162. {
  163. return $this->getTabId($this->_tabs[$this->_activeTab]);
  164. }
  165. /**
  166. * Set Active Tab
  167. * Tab has to be not hidden and can show
  168. *
  169. * @param string $tabId
  170. * @return $this
  171. */
  172. public function setActiveTab($tabId)
  173. {
  174. if (isset(
  175. $this->_tabs[$tabId]
  176. ) && $this->canShowTab(
  177. $this->_tabs[$tabId]
  178. ) && !$this->getTabIsHidden(
  179. $this->_tabs[$tabId]
  180. )
  181. ) {
  182. $this->_activeTab = $tabId;
  183. if ($this->_activeTab !== null && $tabId !== $this->_activeTab) {
  184. foreach ($this->_tabs as $id => $tab) {
  185. $tab->setActive($id === $tabId);
  186. }
  187. }
  188. }
  189. return $this;
  190. }
  191. /**
  192. * Set Active Tab
  193. *
  194. * @param string $tabId
  195. * @return $this
  196. */
  197. protected function _setActiveTab($tabId)
  198. {
  199. foreach ($this->_tabs as $id => $tab) {
  200. if ($this->getTabId($tab) == $tabId) {
  201. $this->_activeTab = $id;
  202. $tab->setActive(true);
  203. return $this;
  204. }
  205. }
  206. return $this;
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. protected function _beforeToHtml()
  212. {
  213. if ($activeTab = $this->getRequest()->getParam('active_tab')) {
  214. $this->setActiveTab($activeTab);
  215. } elseif ($activeTabId = $this->_authSession->getActiveTabId()) {
  216. $this->_setActiveTab($activeTabId);
  217. }
  218. $_new = [];
  219. foreach ($this->_tabs as $key => $tab) {
  220. foreach ($this->_tabs as $k => $t) {
  221. if ($t->getAfter() == $key) {
  222. $_new[$key] = $tab;
  223. $_new[$k] = $t;
  224. } else {
  225. if (!$tab->getAfter() || !in_array($tab->getAfter(), array_keys($this->_tabs))) {
  226. $_new[$key] = $tab;
  227. }
  228. }
  229. }
  230. }
  231. $this->_tabs = $_new;
  232. unset($_new);
  233. $this->assign('tabs', $this->_tabs);
  234. return parent::_beforeToHtml();
  235. }
  236. /**
  237. * @return string
  238. */
  239. public function getJsObjectName()
  240. {
  241. return $this->getId() . 'JsTabs';
  242. }
  243. /**
  244. * @return string[]
  245. */
  246. public function getTabsIds()
  247. {
  248. if (empty($this->_tabs)) {
  249. return [];
  250. }
  251. return array_keys($this->_tabs);
  252. }
  253. /**
  254. * @param \Magento\Framework\DataObject|TabInterface $tab
  255. * @param bool $withPrefix
  256. * @return string
  257. */
  258. public function getTabId($tab, $withPrefix = true)
  259. {
  260. if ($tab instanceof TabInterface) {
  261. return ($withPrefix ? $this->getId() . '_' : '') . $tab->getTabId();
  262. }
  263. return ($withPrefix ? $this->getId() . '_' : '') . $tab->getId();
  264. }
  265. /**
  266. * @param \Magento\Framework\DataObject|TabInterface $tab
  267. * @return bool
  268. */
  269. public function canShowTab($tab)
  270. {
  271. if ($tab instanceof TabInterface) {
  272. return $tab->canShowTab();
  273. }
  274. return true;
  275. }
  276. /**
  277. * @param \Magento\Framework\DataObject|TabInterface $tab
  278. * @return bool
  279. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  280. */
  281. public function getTabIsHidden($tab)
  282. {
  283. if ($tab instanceof TabInterface) {
  284. return $tab->isHidden();
  285. }
  286. return $tab->getIsHidden();
  287. }
  288. /**
  289. * @param \Magento\Framework\DataObject|TabInterface $tab
  290. * @return string
  291. */
  292. public function getTabUrl($tab)
  293. {
  294. if ($tab instanceof TabInterface) {
  295. if (method_exists($tab, 'getTabUrl')) {
  296. return $tab->getTabUrl();
  297. }
  298. return '#';
  299. }
  300. if ($tab->getUrl() !== null) {
  301. return $tab->getUrl();
  302. }
  303. return '#';
  304. }
  305. /**
  306. * @param \Magento\Framework\DataObject|TabInterface $tab
  307. * @return string
  308. */
  309. public function getTabTitle($tab)
  310. {
  311. if ($tab instanceof TabInterface) {
  312. return $tab->getTabTitle();
  313. }
  314. return $tab->getTitle();
  315. }
  316. /**
  317. * @param \Magento\Framework\DataObject|TabInterface $tab
  318. * @return string
  319. */
  320. public function getTabClass($tab)
  321. {
  322. if ($tab instanceof TabInterface) {
  323. if (method_exists($tab, 'getTabClass')) {
  324. return $tab->getTabClass();
  325. }
  326. return '';
  327. }
  328. return $tab->getClass();
  329. }
  330. /**
  331. * @param \Magento\Framework\DataObject|TabInterface $tab
  332. * @return string
  333. */
  334. public function getTabLabel($tab)
  335. {
  336. if ($tab instanceof TabInterface) {
  337. return $tab->getTabLabel();
  338. }
  339. return $tab->getLabel();
  340. }
  341. /**
  342. * @param \Magento\Framework\DataObject|TabInterface $tab
  343. * @return string
  344. */
  345. public function getTabContent($tab)
  346. {
  347. if ($tab instanceof TabInterface) {
  348. if ($tab->getSkipGenerateContent()) {
  349. return '';
  350. }
  351. return $tab->toHtml();
  352. }
  353. return $tab->getContent();
  354. }
  355. /**
  356. * Mark tabs as dependant of each other
  357. * Arbitrary number of tabs can be specified, but at least two
  358. *
  359. * @param string $tabOneId
  360. * @param string $tabTwoId
  361. * @return void
  362. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  363. */
  364. public function bindShadowTabs($tabOneId, $tabTwoId)
  365. {
  366. $tabs = [];
  367. $args = func_get_args();
  368. if (!empty($args) && count($args) > 1) {
  369. foreach ($args as $tabId) {
  370. if (isset($this->_tabs[$tabId])) {
  371. $tabs[$tabId] = $tabId;
  372. }
  373. }
  374. $blockId = $this->getId();
  375. foreach ($tabs as $tabId) {
  376. foreach ($tabs as $tabToId) {
  377. if ($tabId !== $tabToId) {
  378. if (!$this->_tabs[$tabToId]->getData('shadow_tabs')) {
  379. $this->_tabs[$tabToId]->setData('shadow_tabs', []);
  380. }
  381. $this->_tabs[$tabToId]->setData(
  382. 'shadow_tabs',
  383. array_merge($this->_tabs[$tabToId]->getData('shadow_tabs'), [$blockId . '_' . $tabId])
  384. );
  385. }
  386. }
  387. }
  388. }
  389. }
  390. /**
  391. * Obtain shadow tabs information
  392. *
  393. * @param bool $asJson
  394. * @return array|string
  395. */
  396. public function getAllShadowTabs($asJson = true)
  397. {
  398. $result = [];
  399. if (!empty($this->_tabs)) {
  400. $blockId = $this->getId();
  401. foreach (array_keys($this->_tabs) as $tabId) {
  402. if ($this->_tabs[$tabId]->getData('shadow_tabs')) {
  403. $result[$blockId . '_' . $tabId] = $this->_tabs[$tabId]->getData('shadow_tabs');
  404. }
  405. }
  406. }
  407. if ($asJson) {
  408. return $this->_jsonEncoder->encode($result);
  409. }
  410. return $result;
  411. }
  412. /**
  413. * Set tab property by tab's identifier
  414. *
  415. * @param string $tab
  416. * @param string $key
  417. * @param mixed $value
  418. * @return $this
  419. */
  420. public function setTabData($tab, $key, $value)
  421. {
  422. if (isset($this->_tabs[$tab]) && $this->_tabs[$tab] instanceof \Magento\Framework\DataObject) {
  423. if ($key == 'url') {
  424. $value = $this->getUrl($value, ['_current' => true, '_use_rewrite' => true]);
  425. }
  426. $this->_tabs[$tab]->setData($key, $value);
  427. }
  428. return $this;
  429. }
  430. /**
  431. * Removes tab with passed id from tabs block
  432. *
  433. * @param string $tabId
  434. * @return $this
  435. */
  436. public function removeTab($tabId)
  437. {
  438. if (isset($this->_tabs[$tabId])) {
  439. unset($this->_tabs[$tabId]);
  440. }
  441. return $this;
  442. }
  443. }