AbstractDataProvider.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\DataProvider;
  7. use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
  8. use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
  9. /**
  10. * @api
  11. * @since 100.0.2
  12. */
  13. abstract class AbstractDataProvider implements DataProviderInterface, \Countable
  14. {
  15. /**
  16. * Data Provider name
  17. *
  18. * @var string
  19. */
  20. protected $name;
  21. /**
  22. * Data Provider Primary Identifier name
  23. *
  24. * @var string
  25. */
  26. protected $primaryFieldName;
  27. /**
  28. * Data Provider Request Parameter Identifier name
  29. *
  30. * @var string
  31. */
  32. protected $requestFieldName;
  33. /**
  34. * @var array
  35. */
  36. protected $meta = [];
  37. /**
  38. * Provider configuration data
  39. *
  40. * @var array
  41. */
  42. protected $data = [];
  43. /**
  44. * @var AbstractCollection
  45. */
  46. protected $collection;
  47. /**
  48. * @param string $name
  49. * @param string $primaryFieldName
  50. * @param string $requestFieldName
  51. * @param array $meta
  52. * @param array $data
  53. */
  54. public function __construct(
  55. $name,
  56. $primaryFieldName,
  57. $requestFieldName,
  58. array $meta = [],
  59. array $data = []
  60. ) {
  61. $this->name = $name;
  62. $this->primaryFieldName = $primaryFieldName;
  63. $this->requestFieldName = $requestFieldName;
  64. $this->meta = $meta;
  65. $this->data = $data;
  66. }
  67. /**
  68. * @return AbstractCollection
  69. */
  70. public function getCollection()
  71. {
  72. return $this->collection;
  73. }
  74. /**
  75. * Get Data Provider name
  76. *
  77. * @return string
  78. */
  79. public function getName()
  80. {
  81. return $this->name;
  82. }
  83. /**
  84. * Get primary field name
  85. *
  86. * @return string
  87. */
  88. public function getPrimaryFieldName()
  89. {
  90. return $this->primaryFieldName;
  91. }
  92. /**
  93. * Get field name in request
  94. *
  95. * @return string
  96. */
  97. public function getRequestFieldName()
  98. {
  99. return $this->requestFieldName;
  100. }
  101. /**
  102. * @return array
  103. */
  104. public function getMeta()
  105. {
  106. return $this->meta;
  107. }
  108. /**
  109. * Get field Set meta info
  110. *
  111. * @param string $fieldSetName
  112. * @return array
  113. */
  114. public function getFieldSetMetaInfo($fieldSetName)
  115. {
  116. return isset($this->meta[$fieldSetName]) ? $this->meta[$fieldSetName] : [];
  117. }
  118. /**
  119. * @param string $fieldSetName
  120. * @return array
  121. */
  122. public function getFieldsMetaInfo($fieldSetName)
  123. {
  124. return isset($this->meta[$fieldSetName]['children']) ? $this->meta[$fieldSetName]['children'] : [];
  125. }
  126. /**
  127. * @param string $fieldSetName
  128. * @param string $fieldName
  129. * @return array
  130. */
  131. public function getFieldMetaInfo($fieldSetName, $fieldName)
  132. {
  133. return isset($this->meta[$fieldSetName]['children'][$fieldName])
  134. ? $this->meta[$fieldSetName]['children'][$fieldName]
  135. : [];
  136. }
  137. /**
  138. * @inheritdoc
  139. */
  140. public function addFilter(\Magento\Framework\Api\Filter $filter)
  141. {
  142. $this->getCollection()->addFieldToFilter(
  143. $filter->getField(),
  144. [$filter->getConditionType() => $filter->getValue()]
  145. );
  146. }
  147. /**
  148. * Returns search criteria
  149. *
  150. * @return null
  151. */
  152. public function getSearchCriteria()
  153. {
  154. //TODO: Technical dept, should be implemented as part of SearchAPI support for Catalog Grids
  155. return null;
  156. }
  157. /**
  158. * Returns SearchResult
  159. *
  160. * @return \Magento\Framework\Api\Search\SearchResultInterface
  161. */
  162. public function getSearchResult()
  163. {
  164. //TODO: Technical dept, should be implemented as part of SearchAPI support for Catalog Grids
  165. return $this->getCollection();
  166. }
  167. /**
  168. * Add field to select
  169. *
  170. * @param string|array $field
  171. * @param string|null $alias
  172. * @return void
  173. */
  174. public function addField($field, $alias = null)
  175. {
  176. $this->getCollection()->addFieldToSelect($field, $alias);
  177. }
  178. /**
  179. * self::setOrder() alias
  180. *
  181. * @param string $field
  182. * @param string $direction
  183. * @return void
  184. */
  185. public function addOrder($field, $direction)
  186. {
  187. $this->getCollection()->addOrder($field, $direction);
  188. }
  189. /**
  190. * Set Query limit
  191. *
  192. * @param int $offset
  193. * @param int $size
  194. * @return void
  195. */
  196. public function setLimit($offset, $size)
  197. {
  198. $this->getCollection()->setPageSize($size);
  199. $this->getCollection()->setCurPage($offset);
  200. }
  201. /**
  202. * Removes field from select
  203. *
  204. * @param string|null $field
  205. * @param bool $isAlias Alias identifier
  206. * @return void
  207. */
  208. public function removeField($field, $isAlias = false)
  209. {
  210. $this->getCollection()->removeFieldFromSelect($field, $isAlias);
  211. }
  212. /**
  213. * Removes all fields from select
  214. *
  215. * @return void
  216. */
  217. public function removeAllFields()
  218. {
  219. $this->getCollection()->removeAllFieldsFromSelect();
  220. }
  221. /**
  222. * Get data
  223. *
  224. * @return array
  225. */
  226. public function getData()
  227. {
  228. return $this->getCollection()->toArray();
  229. }
  230. /**
  231. * Retrieve count of loaded items
  232. *
  233. * @return int
  234. */
  235. public function count()
  236. {
  237. return $this->getCollection()->count();
  238. }
  239. /**
  240. * Get config data
  241. *
  242. * @return mixed
  243. */
  244. public function getConfigData()
  245. {
  246. return isset($this->data['config']) ? $this->data['config'] : [];
  247. }
  248. /**
  249. * Set data
  250. *
  251. * @param mixed $config
  252. * @return void
  253. */
  254. public function setConfigData($config)
  255. {
  256. $this->data['config'] = $config;
  257. }
  258. /**
  259. * Retrieve all ids from collection
  260. *
  261. * @return int[]
  262. * @since 101.0.0
  263. */
  264. public function getAllIds()
  265. {
  266. return $this->getCollection()->getAllIds();
  267. }
  268. }