DataProvider.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\UiComponent\DataProvider;
  7. use Magento\Framework\Api\FilterBuilder;
  8. use Magento\Framework\Api\Search\ReportingInterface;
  9. use Magento\Framework\Api\Search\SearchCriteria;
  10. use Magento\Framework\Api\Search\SearchCriteriaBuilder;
  11. use Magento\Framework\Api\Search\SearchResultInterface;
  12. use Magento\Framework\App\RequestInterface;
  13. /**
  14. * Class DataProvider
  15. */
  16. class DataProvider implements DataProviderInterface
  17. {
  18. /**
  19. * Data Provider name
  20. *
  21. * @var string
  22. */
  23. protected $name;
  24. /**
  25. * Data Provider Primary Identifier name
  26. *
  27. * @var string
  28. */
  29. protected $primaryFieldName;
  30. /**
  31. * Data Provider Request Parameter Identifier name
  32. *
  33. * @var string
  34. */
  35. protected $requestFieldName;
  36. /**
  37. * @var array
  38. */
  39. protected $meta = [];
  40. /**
  41. * Provider configuration data
  42. *
  43. * @var array
  44. */
  45. protected $data = [];
  46. /**
  47. * @var ReportingInterface
  48. */
  49. protected $reporting;
  50. /**
  51. * @var FilterBuilder
  52. */
  53. protected $filterBuilder;
  54. /**
  55. * @var SearchCriteriaBuilder
  56. */
  57. protected $searchCriteriaBuilder;
  58. /**
  59. * @var RequestInterface
  60. */
  61. protected $request;
  62. /**
  63. * @var SearchCriteria
  64. */
  65. protected $searchCriteria;
  66. /**
  67. * @param string $name
  68. * @param string $primaryFieldName
  69. * @param string $requestFieldName
  70. * @param ReportingInterface $reporting
  71. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  72. * @param RequestInterface $request
  73. * @param FilterBuilder $filterBuilder
  74. * @param array $meta
  75. * @param array $data
  76. */
  77. public function __construct(
  78. $name,
  79. $primaryFieldName,
  80. $requestFieldName,
  81. ReportingInterface $reporting,
  82. SearchCriteriaBuilder $searchCriteriaBuilder,
  83. RequestInterface $request,
  84. FilterBuilder $filterBuilder,
  85. array $meta = [],
  86. array $data = []
  87. ) {
  88. $this->request = $request;
  89. $this->filterBuilder = $filterBuilder;
  90. $this->name = $name;
  91. $this->primaryFieldName = $primaryFieldName;
  92. $this->requestFieldName = $requestFieldName;
  93. $this->reporting = $reporting;
  94. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  95. $this->meta = $meta;
  96. $this->data = $data;
  97. $this->prepareUpdateUrl();
  98. }
  99. /**
  100. * @return void
  101. */
  102. protected function prepareUpdateUrl()
  103. {
  104. if (!isset($this->data['config']['filter_url_params'])) {
  105. return;
  106. }
  107. foreach ($this->data['config']['filter_url_params'] as $paramName => $paramValue) {
  108. if ('*' == $paramValue) {
  109. $paramValue = $this->request->getParam($paramName);
  110. }
  111. if ($paramValue) {
  112. $this->data['config']['update_url'] = sprintf(
  113. '%s%s/%s/',
  114. $this->data['config']['update_url'],
  115. $paramName,
  116. $paramValue
  117. );
  118. $this->addFilter(
  119. $this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('eq')->create()
  120. );
  121. }
  122. }
  123. }
  124. /**
  125. * Get Data Provider name
  126. *
  127. * @return string
  128. */
  129. public function getName()
  130. {
  131. return $this->name;
  132. }
  133. /**
  134. * Get primary field name
  135. *
  136. * @return string
  137. */
  138. public function getPrimaryFieldName()
  139. {
  140. return $this->primaryFieldName;
  141. }
  142. /**
  143. * Get field name in request
  144. *
  145. * @return string
  146. */
  147. public function getRequestFieldName()
  148. {
  149. return $this->requestFieldName;
  150. }
  151. /**
  152. * @return array
  153. */
  154. public function getMeta()
  155. {
  156. return $this->meta;
  157. }
  158. /**
  159. * Get field Set meta info
  160. *
  161. * @param string $fieldSetName
  162. * @return array
  163. */
  164. public function getFieldSetMetaInfo($fieldSetName)
  165. {
  166. return $this->meta[$fieldSetName] ?? [];
  167. }
  168. /**
  169. * @param string $fieldSetName
  170. * @return array
  171. */
  172. public function getFieldsMetaInfo($fieldSetName)
  173. {
  174. return $this->meta[$fieldSetName]['children'] ?? [];
  175. }
  176. /**
  177. * @param string $fieldSetName
  178. * @param string $fieldName
  179. * @return array
  180. */
  181. public function getFieldMetaInfo($fieldSetName, $fieldName)
  182. {
  183. return $this->meta[$fieldSetName]['children'][$fieldName] ?? [];
  184. }
  185. /**
  186. * @inheritdoc
  187. */
  188. public function addFilter(\Magento\Framework\Api\Filter $filter)
  189. {
  190. $this->searchCriteriaBuilder->addFilter($filter);
  191. }
  192. /**
  193. * self::setOrder() alias
  194. *
  195. * @param string $field
  196. * @param string $direction
  197. * @return void
  198. */
  199. public function addOrder($field, $direction)
  200. {
  201. $this->searchCriteriaBuilder->addSortOrder($field, $direction);
  202. }
  203. /**
  204. * Set Query limit
  205. *
  206. * @param int $offset
  207. * @param int $size
  208. * @return void
  209. */
  210. public function setLimit($offset, $size)
  211. {
  212. $this->searchCriteriaBuilder->setPageSize($size);
  213. $this->searchCriteriaBuilder->setCurrentPage($offset);
  214. }
  215. /**
  216. * @param SearchResultInterface $searchResult
  217. * @return array
  218. */
  219. protected function searchResultToOutput(SearchResultInterface $searchResult)
  220. {
  221. $arrItems = [];
  222. $arrItems['items'] = [];
  223. foreach ($searchResult->getItems() as $item) {
  224. $itemData = [];
  225. foreach ($item->getCustomAttributes() as $attribute) {
  226. $itemData[$attribute->getAttributeCode()] = $attribute->getValue();
  227. }
  228. $arrItems['items'][] = $itemData;
  229. }
  230. $arrItems['totalRecords'] = $searchResult->getTotalCount();
  231. return $arrItems;
  232. }
  233. /**
  234. * Returns search criteria
  235. *
  236. * @return \Magento\Framework\Api\Search\SearchCriteria
  237. */
  238. public function getSearchCriteria()
  239. {
  240. if (!$this->searchCriteria) {
  241. $this->searchCriteria = $this->searchCriteriaBuilder->create();
  242. $this->searchCriteria->setRequestName($this->name);
  243. }
  244. return $this->searchCriteria;
  245. }
  246. /**
  247. * Get data
  248. *
  249. * @return array
  250. */
  251. public function getData()
  252. {
  253. return $this->searchResultToOutput($this->getSearchResult());
  254. }
  255. /**
  256. * Get config data
  257. *
  258. * @return array
  259. */
  260. public function getConfigData()
  261. {
  262. return $this->data['config'] ?? [];
  263. }
  264. /**
  265. * Set data
  266. *
  267. * @param mixed $config
  268. * @return void
  269. */
  270. public function setConfigData($config)
  271. {
  272. $this->data['config'] = $config;
  273. }
  274. /**
  275. * Returns Search result
  276. *
  277. * @return SearchResultInterface
  278. */
  279. public function getSearchResult()
  280. {
  281. return $this->reporting->search($this->getSearchCriteria());
  282. }
  283. }