MetadataProvider.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Model\Export;
  7. use Magento\Framework\Api\Search\DocumentInterface;
  8. use Magento\Framework\View\Element\UiComponentInterface;
  9. use Magento\Ui\Component\Filters;
  10. use Magento\Ui\Component\Filters\Type\Select;
  11. use Magento\Ui\Component\Listing\Columns;
  12. use Magento\Ui\Component\MassAction\Filter;
  13. use Magento\Framework\Locale\ResolverInterface;
  14. use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
  15. /**
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class MetadataProvider
  19. {
  20. /**
  21. * @var Filter
  22. */
  23. protected $filter;
  24. /**
  25. * @var array
  26. */
  27. protected $columns;
  28. /**
  29. * @var TimezoneInterface
  30. */
  31. protected $localeDate;
  32. /**
  33. * @var string
  34. */
  35. protected $locale;
  36. /**
  37. * @var string
  38. */
  39. protected $dateFormat;
  40. /**
  41. * @var array
  42. */
  43. protected $data;
  44. /**
  45. * @param Filter $filter
  46. * @param TimezoneInterface $localeDate
  47. * @param ResolverInterface $localeResolver
  48. * @param string $dateFormat
  49. * @param array $data
  50. */
  51. public function __construct(
  52. Filter $filter,
  53. TimezoneInterface $localeDate,
  54. ResolverInterface $localeResolver,
  55. $dateFormat = 'M j, Y h:i:s A',
  56. array $data = []
  57. ) {
  58. $this->filter = $filter;
  59. $this->localeDate = $localeDate;
  60. $this->locale = $localeResolver->getLocale();
  61. $this->dateFormat = $dateFormat;
  62. $this->data = $data;
  63. }
  64. /**
  65. * Returns Columns component
  66. *
  67. * @param UiComponentInterface $component
  68. * @return UiComponentInterface
  69. * @throws \Exception
  70. */
  71. protected function getColumnsComponent(UiComponentInterface $component)
  72. {
  73. foreach ($component->getChildComponents() as $childComponent) {
  74. if ($childComponent instanceof Columns) {
  75. return $childComponent;
  76. }
  77. }
  78. throw new \Exception('No columns found');
  79. }
  80. /**
  81. * Returns columns list
  82. *
  83. * @param UiComponentInterface $component
  84. * @return UiComponentInterface[]
  85. */
  86. protected function getColumns(UiComponentInterface $component)
  87. {
  88. if (!isset($this->columns[$component->getName()])) {
  89. $columns = $this->getColumnsComponent($component);
  90. foreach ($columns->getChildComponents() as $column) {
  91. if ($column->getData('config/label') && $column->getData('config/dataType') !== 'actions') {
  92. $this->columns[$component->getName()][$column->getName()] = $column;
  93. }
  94. }
  95. }
  96. return $this->columns[$component->getName()];
  97. }
  98. /**
  99. * Retrieve Headers row array for Export
  100. *
  101. * @param UiComponentInterface $component
  102. * @return string[]
  103. */
  104. public function getHeaders(UiComponentInterface $component)
  105. {
  106. $row = [];
  107. foreach ($this->getColumns($component) as $column) {
  108. $row[] = $column->getData('config/label');
  109. }
  110. array_walk($row, function (&$header) {
  111. if (mb_strpos($header, 'ID') === 0) {
  112. $header = '"' . $header . '"';
  113. }
  114. });
  115. return $row;
  116. }
  117. /**
  118. * Returns DB fields list
  119. *
  120. * @param UiComponentInterface $component
  121. * @return array
  122. */
  123. public function getFields(UiComponentInterface $component)
  124. {
  125. $row = [];
  126. foreach ($this->getColumns($component) as $column) {
  127. $row[] = $column->getName();
  128. }
  129. return $row;
  130. }
  131. /**
  132. * Returns row data
  133. *
  134. * @param DocumentInterface $document
  135. * @param array $fields
  136. * @param array $options
  137. * @return array
  138. */
  139. public function getRowData(DocumentInterface $document, $fields, $options)
  140. {
  141. $row = [];
  142. foreach ($fields as $column) {
  143. if (isset($options[$column])) {
  144. $key = $document->getCustomAttribute($column)->getValue();
  145. if (isset($options[$column][$key])) {
  146. $row[] = $options[$column][$key];
  147. } else {
  148. $row[] = '';
  149. }
  150. } else {
  151. $row[] = $document->getCustomAttribute($column)->getValue();
  152. }
  153. }
  154. return $row;
  155. }
  156. /**
  157. * Returns complex option
  158. *
  159. * @param array $list
  160. * @param string $label
  161. * @param array $output
  162. * @return void
  163. */
  164. protected function getComplexLabel($list, $label, &$output)
  165. {
  166. foreach ($list as $item) {
  167. if (!is_array($item['value'])) {
  168. $output[$item['value']] = $label . $item['label'];
  169. } else {
  170. $this->getComplexLabel($item['value'], $label . $item['label'], $output);
  171. }
  172. }
  173. }
  174. /**
  175. * Returns array of Select options
  176. *
  177. * @param Select $filter
  178. * @return array
  179. */
  180. protected function getFilterOptions(Select $filter)
  181. {
  182. $options = [];
  183. foreach ($filter->getData('config/options') as $option) {
  184. if (!is_array($option['value'])) {
  185. $options[$option['value']] = $option['label'];
  186. } else {
  187. $this->getComplexLabel(
  188. $option['value'],
  189. $option['label'],
  190. $options
  191. );
  192. }
  193. }
  194. return $options;
  195. }
  196. /**
  197. * Returns Filters with options
  198. *
  199. * @return array
  200. */
  201. public function getOptions()
  202. {
  203. $options = [];
  204. $component = $this->filter->getComponent();
  205. $childComponents = $component->getChildComponents();
  206. $listingTop = $childComponents['listing_top'];
  207. foreach ($listingTop->getChildComponents() as $child) {
  208. if ($child instanceof Filters) {
  209. foreach ($child->getChildComponents() as $filter) {
  210. if ($filter instanceof Select) {
  211. $options[$filter->getName()] = $this->getFilterOptions($filter);
  212. }
  213. }
  214. }
  215. }
  216. return $options;
  217. }
  218. /**
  219. * Convert document date(UTC) fields to default scope specified
  220. *
  221. * @param \Magento\Framework\Api\Search\DocumentInterface $document
  222. * @param string $componentName
  223. * @return void
  224. */
  225. public function convertDate($document, $componentName)
  226. {
  227. if (!isset($this->data[$componentName])) {
  228. return;
  229. }
  230. foreach ($this->data[$componentName] as $field) {
  231. $fieldValue = $document->getData($field);
  232. if (!$fieldValue) {
  233. continue;
  234. }
  235. $convertedDate = $this->localeDate->date(
  236. new \DateTime($fieldValue, new \DateTimeZone('UTC')),
  237. $this->locale,
  238. true
  239. );
  240. $document->setData($field, $convertedDate->format($this->dateFormat));
  241. }
  242. }
  243. }