Columns.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Ui\Component\Listing;
  7. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  8. use Magento\Customer\Ui\Component\ColumnFactory;
  9. use Magento\Customer\Api\Data\AttributeMetadataInterface as AttributeMetadata;
  10. use Magento\Framework\View\Element\UiComponentInterface;
  11. use Magento\Customer\Ui\Component\Listing\Column\InlineEditUpdater;
  12. use Magento\Customer\Api\CustomerMetadataInterface;
  13. class Columns extends \Magento\Ui\Component\Listing\Columns
  14. {
  15. /**
  16. * @var int
  17. */
  18. protected $columnSortOrder;
  19. /**
  20. * @var \Magento\Customer\Ui\Component\Listing\AttributeRepository
  21. */
  22. protected $attributeRepository;
  23. /**
  24. * @var \Magento\Customer\Ui\Component\Listing\Column\InlineEditUpdater
  25. */
  26. protected $inlineEditUpdater;
  27. /**
  28. * @var array
  29. */
  30. protected $filterMap = [
  31. 'default' => 'text',
  32. 'select' => 'select',
  33. 'boolean' => 'select',
  34. 'multiselect' => 'select',
  35. 'date' => 'dateRange',
  36. ];
  37. /**
  38. * @param ContextInterface $context
  39. * @param ColumnFactory $columnFactory
  40. * @param AttributeRepository $attributeRepository
  41. * @param InlineEditUpdater $inlineEditor
  42. * @param array $components
  43. * @param array $data
  44. */
  45. public function __construct(
  46. ContextInterface $context,
  47. ColumnFactory $columnFactory,
  48. AttributeRepository $attributeRepository,
  49. InlineEditUpdater $inlineEditor,
  50. array $components = [],
  51. array $data = []
  52. ) {
  53. parent::__construct($context, $components, $data);
  54. $this->columnFactory = $columnFactory;
  55. $this->attributeRepository = $attributeRepository;
  56. $this->inlineEditUpdater = $inlineEditor;
  57. }
  58. /**
  59. * @return int
  60. */
  61. protected function getDefaultSortOrder()
  62. {
  63. $max = 0;
  64. foreach ($this->components as $component) {
  65. $config = $component->getData('config');
  66. if (isset($config['sortOrder']) && $config['sortOrder'] > $max) {
  67. $max = $config['sortOrder'];
  68. }
  69. }
  70. return ++$max;
  71. }
  72. /**
  73. * Update actions column sort order
  74. *
  75. * @return void
  76. */
  77. protected function updateActionColumnSortOrder()
  78. {
  79. if (isset($this->components['actions'])) {
  80. $component = $this->components['actions'];
  81. $component->setData(
  82. 'config',
  83. array_merge($component->getData('config'), ['sortOrder' => ++$this->columnSortOrder])
  84. );
  85. }
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function prepare()
  91. {
  92. $this->columnSortOrder = $this->getDefaultSortOrder();
  93. foreach ($this->attributeRepository->getList() as $newAttributeCode => $attributeData) {
  94. if (isset($this->components[$newAttributeCode])) {
  95. $this->updateColumn($attributeData, $newAttributeCode);
  96. } elseif (!$attributeData[AttributeMetadata::BACKEND_TYPE] != 'static'
  97. && $attributeData[AttributeMetadata::IS_USED_IN_GRID]
  98. ) {
  99. $this->addColumn($attributeData, $newAttributeCode);
  100. }
  101. }
  102. $this->updateActionColumnSortOrder();
  103. parent::prepare();
  104. }
  105. /**
  106. * @param array $attributeData
  107. * @param string $columnName
  108. * @return void
  109. */
  110. public function addColumn(array $attributeData, $columnName)
  111. {
  112. $config['sortOrder'] = ++$this->columnSortOrder;
  113. if ($attributeData[AttributeMetadata::IS_FILTERABLE_IN_GRID]) {
  114. $config['filter'] = $this->getFilterType($attributeData[AttributeMetadata::FRONTEND_INPUT]);
  115. }
  116. $column = $this->columnFactory->create($attributeData, $columnName, $this->getContext(), $config);
  117. $column->prepare();
  118. $this->addComponent($attributeData[AttributeMetadata::ATTRIBUTE_CODE], $column);
  119. }
  120. /**
  121. * @param array $attributeData
  122. * @param string $newAttributeCode
  123. * @return void
  124. */
  125. public function updateColumn(array $attributeData, $newAttributeCode)
  126. {
  127. $component = $this->components[$attributeData[AttributeMetadata::ATTRIBUTE_CODE]];
  128. $this->addOptions($component, $attributeData);
  129. if ($attributeData[AttributeMetadata::BACKEND_TYPE] != 'static') {
  130. if ($attributeData[AttributeMetadata::IS_USED_IN_GRID]) {
  131. $config = array_merge(
  132. $component->getData('config'),
  133. [
  134. 'name' => $newAttributeCode,
  135. 'dataType' => $attributeData[AttributeMetadata::BACKEND_TYPE],
  136. 'visible' => (bool)$attributeData[AttributeMetadata::IS_VISIBLE_IN_GRID]
  137. ]
  138. );
  139. if ($attributeData[AttributeMetadata::IS_FILTERABLE_IN_GRID]) {
  140. $config['filter'] = $this->getFilterType($attributeData[AttributeMetadata::FRONTEND_INPUT]);
  141. }
  142. $component->setData('config', $config);
  143. }
  144. } else {
  145. if ($attributeData['entity_type_code'] == CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER
  146. && !empty($component->getData('config')['editor'])
  147. ) {
  148. $this->inlineEditUpdater->applyEditing(
  149. $component,
  150. $attributeData[AttributeMetadata::FRONTEND_INPUT],
  151. $attributeData[AttributeMetadata::VALIDATION_RULES],
  152. $attributeData[AttributeMetadata::REQUIRED]
  153. );
  154. }
  155. $component->setData(
  156. 'config',
  157. array_merge(
  158. $component->getData('config'),
  159. ['visible' => (bool)$attributeData[AttributeMetadata::IS_VISIBLE_IN_GRID]]
  160. )
  161. );
  162. }
  163. }
  164. /**
  165. * Add options to component
  166. *
  167. * @param UiComponentInterface $component
  168. * @param array $attributeData
  169. * @return void
  170. */
  171. public function addOptions(UiComponentInterface $component, array $attributeData)
  172. {
  173. $config = $component->getData('config');
  174. if (count($attributeData[AttributeMetadata::OPTIONS]) && !isset($config[AttributeMetadata::OPTIONS])) {
  175. $component->setData(
  176. 'config',
  177. array_merge($config, [AttributeMetadata::OPTIONS => $attributeData[AttributeMetadata::OPTIONS]])
  178. );
  179. }
  180. }
  181. /**
  182. * Retrieve filter type by $frontendInput
  183. *
  184. * @param string $frontendInput
  185. * @return string
  186. */
  187. protected function getFilterType($frontendInput)
  188. {
  189. return isset($this->filterMap[$frontendInput]) ? $this->filterMap[$frontendInput] : $this->filterMap['default'];
  190. }
  191. }