Title.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Block\Adminhtml\Rate;
  7. use Magento\Tax\Controller\RegistryConstants;
  8. /**
  9. * Tax Rate Titles Renderer
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Title extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * @var array
  17. */
  18. protected $_titles;
  19. /**
  20. * @var string
  21. */
  22. protected $_template = 'Magento_Tax::rate/title.phtml';
  23. /**
  24. * @var \Magento\Store\Model\StoreFactory
  25. */
  26. protected $_storeFactory;
  27. /**
  28. * @var \Magento\Framework\Registry
  29. */
  30. protected $_coreRegistry;
  31. /**
  32. * @var \Magento\Tax\Api\TaxRateRepositoryInterface
  33. */
  34. protected $_taxRateRepository;
  35. /**
  36. * Initialize dependencies
  37. *
  38. * @param \Magento\Framework\View\Element\Template\Context $context
  39. * @param \Magento\Store\Model\StoreFactory $storeFactory
  40. * @param \Magento\Framework\Registry $coreRegistry
  41. * @param \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository
  42. * @param array $data
  43. */
  44. public function __construct(
  45. \Magento\Framework\View\Element\Template\Context $context,
  46. \Magento\Store\Model\StoreFactory $storeFactory,
  47. \Magento\Framework\Registry $coreRegistry,
  48. \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository,
  49. array $data = []
  50. ) {
  51. $this->_coreRegistry = $coreRegistry;
  52. $this->_taxRateRepository = $taxRateRepository;
  53. $this->_storeFactory = $storeFactory;
  54. parent::__construct($context, $data);
  55. }
  56. /**
  57. * Return the tax rate titles associated with a store view.
  58. *
  59. * @return array
  60. */
  61. public function getTitles()
  62. {
  63. if ($this->_titles === null) {
  64. $this->_titles = [];
  65. $taxRateId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_TAX_RATE_ID);
  66. $titles = [];
  67. if ($taxRateId) {
  68. $rate = $this->_taxRateRepository->get($taxRateId);
  69. $titles = $rate->getTitles();
  70. }
  71. foreach ($titles as $title) {
  72. $this->_titles[$title->getStoreId()] = $title->getValue();
  73. }
  74. foreach ($this->getStores() as $store) {
  75. if (!isset($this->_titles[$store->getId()])) {
  76. $this->_titles[$store->getId()] = '';
  77. }
  78. }
  79. }
  80. return $this->_titles;
  81. }
  82. /**
  83. * @return mixed
  84. */
  85. public function getStores()
  86. {
  87. $stores = $this->getData('stores');
  88. if ($stores === null) {
  89. $stores = $this->_storeFactory->create()->getResourceCollection()->setLoadDefault(false)->load();
  90. $this->setData('stores', $stores);
  91. }
  92. return $stores;
  93. }
  94. }