Options.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Ui\Component\MassAction\Group;
  7. use Magento\Framework\Phrase;
  8. use Magento\Framework\UrlInterface;
  9. use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
  10. /**
  11. * Class Options
  12. */
  13. class Options implements \JsonSerializable
  14. {
  15. /**
  16. * @var array
  17. */
  18. protected $options;
  19. /**
  20. * @var CollectionFactory
  21. */
  22. protected $collectionFactory;
  23. /**
  24. * Additional options params
  25. *
  26. * @var array
  27. */
  28. protected $data;
  29. /**
  30. * @var UrlInterface
  31. */
  32. protected $urlBuilder;
  33. /**
  34. * Base URL for subactions
  35. *
  36. * @var string
  37. */
  38. protected $urlPath;
  39. /**
  40. * Param name for subactions
  41. *
  42. * @var string
  43. */
  44. protected $paramName;
  45. /**
  46. * Additional params for subactions
  47. *
  48. * @var array
  49. */
  50. protected $additionalData = [];
  51. /**
  52. * Constructor
  53. *
  54. * @param CollectionFactory $collectionFactory
  55. * @param UrlInterface $urlBuilder
  56. * @param array $data
  57. */
  58. public function __construct(
  59. CollectionFactory $collectionFactory,
  60. UrlInterface $urlBuilder,
  61. array $data = []
  62. ) {
  63. $this->collectionFactory = $collectionFactory;
  64. $this->data = $data;
  65. $this->urlBuilder = $urlBuilder;
  66. }
  67. /**
  68. * Get action options
  69. *
  70. * @return array
  71. */
  72. public function jsonSerialize()
  73. {
  74. if ($this->options === null) {
  75. $options = $this->collectionFactory->create()->setRealGroupsFilter()->toOptionArray();
  76. $this->prepareData();
  77. foreach ($options as $optionCode) {
  78. $this->options[$optionCode['value']] = [
  79. 'type' => 'customer_group_' . $optionCode['value'],
  80. 'label' => __($optionCode['label']),
  81. ];
  82. if ($this->urlPath && $this->paramName) {
  83. $this->options[$optionCode['value']]['url'] = $this->urlBuilder->getUrl(
  84. $this->urlPath,
  85. [$this->paramName => $optionCode['value']]
  86. );
  87. }
  88. $this->options[$optionCode['value']] = array_merge_recursive(
  89. $this->options[$optionCode['value']],
  90. $this->additionalData
  91. );
  92. }
  93. $this->options = array_values($this->options);
  94. }
  95. return $this->options;
  96. }
  97. /**
  98. * Prepare addition data for subactions
  99. *
  100. * @return void
  101. */
  102. protected function prepareData()
  103. {
  104. foreach ($this->data as $key => $value) {
  105. switch ($key) {
  106. case 'urlPath':
  107. $this->urlPath = $value;
  108. break;
  109. case 'paramName':
  110. $this->paramName = $value;
  111. break;
  112. case 'confirm':
  113. foreach ($value as $messageName => $message) {
  114. $this->additionalData[$key][$messageName] = (string) new Phrase($message);
  115. }
  116. break;
  117. default:
  118. $this->additionalData[$key] = $value;
  119. break;
  120. }
  121. }
  122. }
  123. }