|
| 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 | +/** |
| 8 | + * Search model for backend search |
| 9 | + * |
| 10 | + * @method Category setQuery(string $query) |
| 11 | + * @method string|null getQuery() |
| 12 | + * @method bool hasQuery() |
| 13 | + * @method Category setStart(int $startPosition) |
| 14 | + * @method int|null getStart() |
| 15 | + * @method bool hasStart() |
| 16 | + * @method Category setLimit(int $limit) |
| 17 | + * @method int|null getLimit() |
| 18 | + * @method bool hasLimit() |
| 19 | + * @method Category setResults(array $results) |
| 20 | + * @method array getResults() |
| 21 | + * @api |
| 22 | + */ |
| 23 | +class Category extends \Magento\Framework\DataObject |
| 24 | +{ |
| 25 | + /** |
| 26 | + * Adminhtml data |
| 27 | + * |
| 28 | + * @var \Magento\Backend\Helper\Data |
| 29 | + */ |
| 30 | + protected $_adminhtmlData = null; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var \Magento\Catalog\Api\CategoryListInterface |
| 34 | + */ |
| 35 | + protected $categoryRepository; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var \Magento\Framework\Api\SearchCriteriaBuilder |
| 39 | + */ |
| 40 | + protected $searchCriteriaBuilder; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var \Magento\Framework\Api\FilterBuilder |
| 44 | + */ |
| 45 | + protected $filterBuilder; |
| 46 | + |
| 47 | + /** |
| 48 | + * Magento string lib |
| 49 | + * |
| 50 | + * @var \Magento\Framework\Stdlib\StringUtils |
| 51 | + */ |
| 52 | + protected $string; |
| 53 | + |
| 54 | + /** |
| 55 | + * Initialize dependencies. |
| 56 | + * |
| 57 | + * @param \Magento\Backend\Helper\Data $adminhtmlData |
| 58 | + * @param \Magento\Catalog\Api\CategoryListInterface $categoryRepository |
| 59 | + * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder |
| 60 | + * @param \Magento\Framework\Api\FilterBuilder $filterBuilder |
| 61 | + * @param \Magento\Framework\Stdlib\StringUtils $string |
| 62 | + */ |
| 63 | + public function __construct( |
| 64 | + \Magento\Backend\Helper\Data $adminhtmlData, |
| 65 | + \Magento\Catalog\Api\CategoryListInterface $categoryRepository, |
| 66 | + \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, |
| 67 | + \Magento\Framework\Api\FilterBuilder $filterBuilder, |
| 68 | + \Magento\Framework\Stdlib\StringUtils $string |
| 69 | + ) |
| 70 | + { |
| 71 | + $this->_adminhtmlData = $adminhtmlData; |
| 72 | + $this->categoryRepository = $categoryRepository; |
| 73 | + $this->searchCriteriaBuilder = $searchCriteriaBuilder; |
| 74 | + $this->filterBuilder = $filterBuilder; |
| 75 | + $this->string = $string; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Load search results |
| 80 | + * |
| 81 | + * @return $this |
| 82 | + */ |
| 83 | + public function load() |
| 84 | + { |
| 85 | + $result = []; |
| 86 | + if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) { |
| 87 | + $this->setResults($result); |
| 88 | + return $this; |
| 89 | + } |
| 90 | + |
| 91 | + $this->searchCriteriaBuilder->setCurrentPage($this->getStart()); |
| 92 | + $this->searchCriteriaBuilder->setPageSize($this->getLimit()); |
| 93 | + $searchFields = ['name']; |
| 94 | + |
| 95 | + $filters = []; |
| 96 | + foreach ($searchFields as $field) { |
| 97 | + $filters[] = $this->filterBuilder |
| 98 | + ->setField($field) |
| 99 | + ->setConditionType('like') |
| 100 | + ->setValue('%' . $this->getQuery() . '%') |
| 101 | + ->create(); |
| 102 | + } |
| 103 | + $this->searchCriteriaBuilder->addFilters($filters); |
| 104 | + |
| 105 | + $searchCriteria = $this->searchCriteriaBuilder->create(); |
| 106 | + $searchResults = $this->categoryRepository->getList($searchCriteria); |
| 107 | + |
| 108 | + foreach ($searchResults->getItems() as $category) { |
| 109 | + $description = strip_tags($category->getDescription()); |
| 110 | + $result[] = [ |
| 111 | + 'id' => 'category/1/' . $category->getId(), |
| 112 | + 'type' => __('Category'), |
| 113 | + 'name' => $category->getName(), |
| 114 | + 'description' => $this->string->substr($description, 0, 30), |
| 115 | + 'url' => $this->_adminhtmlData->getUrl('catalog/category/edit', ['id' => $category->getId()]), |
| 116 | + ]; |
| 117 | + } |
| 118 | + $this->setResults($result); |
| 119 | + return $this; |
| 120 | + } |
| 121 | +} |
0 commit comments