Customer.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Model\Search;
  7. /**
  8. * Search Customer Model
  9. *
  10. * @method Customer setQuery(string $query)
  11. * @method string|null getQuery()
  12. * @method bool hasQuery()
  13. * @method Customer setStart(int $startPosition)
  14. * @method int|null getStart()
  15. * @method bool hasStart()
  16. * @method Customer setLimit(int $limit)
  17. * @method int|null getLimit()
  18. * @method bool hasLimit()
  19. * @method Customer setResults(array $results)
  20. * @method array getResults()
  21. * @api
  22. * @since 100.0.2
  23. */
  24. class Customer extends \Magento\Framework\DataObject
  25. {
  26. /**
  27. * Adminhtml data
  28. *
  29. * @var \Magento\Backend\Helper\Data
  30. */
  31. protected $_adminhtmlData = null;
  32. /**
  33. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  34. */
  35. protected $customerRepository;
  36. /**
  37. * @var \Magento\Framework\Api\SearchCriteriaBuilder
  38. */
  39. protected $searchCriteriaBuilder;
  40. /**
  41. * @var \Magento\Framework\Api\FilterBuilder
  42. */
  43. protected $filterBuilder;
  44. /**
  45. * @var \Magento\Customer\Helper\View
  46. */
  47. protected $_customerViewHelper;
  48. /**
  49. * Initialize dependencies.
  50. *
  51. * @param \Magento\Backend\Helper\Data $adminhtmlData
  52. * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
  53. * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
  54. * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
  55. * @param \Magento\Customer\Helper\View $customerViewHelper
  56. */
  57. public function __construct(
  58. \Magento\Backend\Helper\Data $adminhtmlData,
  59. \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
  60. \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
  61. \Magento\Framework\Api\FilterBuilder $filterBuilder,
  62. \Magento\Customer\Helper\View $customerViewHelper
  63. ) {
  64. $this->_adminhtmlData = $adminhtmlData;
  65. $this->customerRepository = $customerRepository;
  66. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  67. $this->filterBuilder = $filterBuilder;
  68. $this->_customerViewHelper = $customerViewHelper;
  69. }
  70. /**
  71. * Load search results
  72. *
  73. * @return $this
  74. */
  75. public function load()
  76. {
  77. $result = [];
  78. if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) {
  79. $this->setResults($result);
  80. return $this;
  81. }
  82. $this->searchCriteriaBuilder->setCurrentPage($this->getStart());
  83. $this->searchCriteriaBuilder->setPageSize($this->getLimit());
  84. $searchFields = ['firstname', 'lastname', 'billing_company'];
  85. $filters = [];
  86. foreach ($searchFields as $field) {
  87. $filters[] = $this->filterBuilder
  88. ->setField($field)
  89. ->setConditionType('like')
  90. ->setValue($this->getQuery() . '%')
  91. ->create();
  92. }
  93. $this->searchCriteriaBuilder->addFilters($filters);
  94. $searchCriteria = $this->searchCriteriaBuilder->create();
  95. $searchResults = $this->customerRepository->getList($searchCriteria);
  96. foreach ($searchResults->getItems() as $customer) {
  97. $customerAddresses = $customer->getAddresses();
  98. /** Look for a company name defined in default billing address */
  99. $company = null;
  100. foreach ($customerAddresses as $customerAddress) {
  101. if ($customerAddress->getId() == $customer->getDefaultBilling()) {
  102. $company = $customerAddress->getCompany();
  103. break;
  104. }
  105. }
  106. $result[] = [
  107. 'id' => 'customer/1/' . $customer->getId(),
  108. 'type' => __('Customer'),
  109. 'name' => $this->_customerViewHelper->getCustomerName($customer),
  110. 'description' => $company,
  111. 'url' => $this->_adminhtmlData->getUrl('customer/index/edit', ['id' => $customer->getId()]),
  112. ];
  113. }
  114. $this->setResults($result);
  115. return $this;
  116. }
  117. }