Catalog.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Model\Search;
  7. use Magento\Search\Model\QueryFactory;
  8. /**
  9. * Search model for backend search
  10. */
  11. class Catalog extends \Magento\Framework\DataObject
  12. {
  13. /**
  14. * Catalog search data
  15. *
  16. * @var \Magento\Search\Model\QueryFactory
  17. */
  18. protected $queryFactory = null;
  19. /**
  20. * Magento string lib
  21. *
  22. * @var \Magento\Framework\Stdlib\StringUtils
  23. */
  24. protected $string;
  25. /**
  26. * Adminhtml data
  27. *
  28. * @var \Magento\Backend\Helper\Data
  29. */
  30. protected $_adminhtmlData = null;
  31. /**
  32. * @param \Magento\Backend\Helper\Data $adminhtmlData
  33. * @param \Magento\Framework\Stdlib\StringUtils $string
  34. * @param QueryFactory $queryFactory
  35. */
  36. public function __construct(
  37. \Magento\Backend\Helper\Data $adminhtmlData,
  38. \Magento\Framework\Stdlib\StringUtils $string,
  39. QueryFactory $queryFactory
  40. ) {
  41. $this->_adminhtmlData = $adminhtmlData;
  42. $this->string = $string;
  43. $this->queryFactory = $queryFactory;
  44. }
  45. /**
  46. * Load search results
  47. *
  48. * @return $this
  49. */
  50. public function load()
  51. {
  52. $result = [];
  53. if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) {
  54. $this->setResults($result);
  55. return $this;
  56. }
  57. $collection = $this->queryFactory->get()
  58. ->getSearchCollection()
  59. ->addAttributeToSelect('name')
  60. ->addAttributeToSelect('description')
  61. ->addBackendSearchFilter($this->getQuery())
  62. ->setCurPage($this->getStart())
  63. ->setPageSize($this->getLimit())
  64. ->load();
  65. foreach ($collection as $product) {
  66. $description = strip_tags($product->getDescription());
  67. $result[] = [
  68. 'id' => 'product/1/' . $product->getId(),
  69. 'type' => __('Product'),
  70. 'name' => $product->getName(),
  71. 'description' => $this->string->substr($description, 0, 30),
  72. 'url' => $this->_adminhtmlData->getUrl('catalog/product/edit', ['id' => $product->getId()]),
  73. ];
  74. }
  75. $this->setResults($result);
  76. return $this;
  77. }
  78. }