From 67220d278303c784e3c32aab9fc03ca994d7110c Mon Sep 17 00:00:00 2001 From: Roelf Date: Tue, 2 Jul 2019 10:01:06 +0200 Subject: [PATCH] dispatch event to add search conditions (#12) * added-event-dispatch-when-creating-query-conditions * DI * more-beautyfixes --- .gitignore | 1 + Controller/DefaultController.php | 5 ++- Event/HreftypeaheadSearchEvent.php | 66 ++++++++++++++++++++++++++++++ README.md | 34 +++++++++++++++ Resources/config/services.yml | 3 ++ Service/SearchBuilder.php | 38 +++++++++++++++-- Service/SearchService.php | 36 +++++++++++----- 7 files changed, 167 insertions(+), 16 deletions(-) create mode 100644 .gitignore create mode 100644 Event/HreftypeaheadSearchEvent.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php index e0f8406..4830d0b 100644 --- a/Controller/DefaultController.php +++ b/Controller/DefaultController.php @@ -26,7 +26,7 @@ class DefaultController extends AdminController /** * @Route("/find") */ - public function findAction(Request $request) + public function findAction(Request $request, SearchBuilder $searchBuilder) { $sourceId = $request->get('sourceId'); $sourceClassName = $request->get('className'); @@ -77,7 +77,7 @@ public function findAction(Request $request) $filter = $request->get('filter') ? \Zend_Json::decode($request->get('filter')) : null; $considerChildTags = $request->get('considerChildTags') === 'true'; $sortingSettings = \Pimcore\Admin\Helper\QueryParams::extractSortingSettings($request->request->all()); - $searchService = SearchBuilder::create() + $searchService = $searchBuilder->create() ->withUser($this->getAdminUser()) ->withTypes(['object']) ->withSubTypes(['object']) @@ -87,6 +87,7 @@ public function findAction(Request $request) ->withLimit((int) $request->get('limit')) ->withFields( $request->get('fields')) ->withFilter($filter) + ->withSourceObject($source) ->withTagIds( $request->get('tagIds')) ->withConsiderChildTags($considerChildTags) ->withSortSettings($sortingSettings) diff --git a/Event/HreftypeaheadSearchEvent.php b/Event/HreftypeaheadSearchEvent.php new file mode 100644 index 0000000..d245831 --- /dev/null +++ b/Event/HreftypeaheadSearchEvent.php @@ -0,0 +1,66 @@ +sourceObject = $sourceObject; + $this->conditions = &$conditions; + } + + /** + * @return DataObject\Concrete + */ + public function getSourceObject() + { + return $this->sourceObject; + } + + /** + * @return string[] + */ + public function getConditions() + { + return $this->conditions; + } + + public function setConditions(array $conditions) + { + $this->conditions = $conditions; + } + + public function addCondition($condition) + { + $this->conditions[] = $condition; + } + +} diff --git a/README.md b/README.md index cb61357..98a6f5e 100644 --- a/README.md +++ b/README.md @@ -26,5 +26,39 @@ composer require youwe/pimcore-href-typeahead
A controller containing autocomplete source
+### Events + +When you want to add some extra filters to the search done by the href typeahead, you can use a listener. +An example: + +Listener config (ie. in src/AppBundle/Resources/config/services.yml): + + AppBundle\EventListener\HreftypeaheadSearchListener: + tags: + - { name: kernel.event_listener, event: hreftypeahead.search, method: onSearch } + +Listener code (ie. in AppBundle\EventListener\HreftypeaheadSearchListener): + + class HreftypeaheadSearchListener + { + + /* @var HreftypeaheadSearchEvent */ + protected $e; + + public function __construct() + { + } + + public function onSearch(HreftypeaheadSearchEvent $e) + { + $this->e = $e; + + $e->addCondition('(0 = 0)'); // just an example + + } + + ...... + + ### Limitations * It only supports one object linked in href diff --git a/Resources/config/services.yml b/Resources/config/services.yml index af03061..ff443a2 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -16,6 +16,9 @@ services: public: true tags: ['controller.service_arguments'] + PimcoreHrefTypeaheadBundle\Service\SearchBuilder: ~ + PimcoreHrefTypeaheadBundle\Service\SearchService: ~ + # add more services, or override services that need manual wiring # PimcoreHrefTypeaheadBundle\ExampleClass: # arguments: diff --git a/Service/SearchBuilder.php b/Service/SearchBuilder.php index 10c7648..499a88e 100644 --- a/Service/SearchBuilder.php +++ b/Service/SearchBuilder.php @@ -4,6 +4,7 @@ namespace PimcoreHrefTypeaheadBundle\Service; +use Pimcore\Model\DataObject; use Pimcore\Model\User; class SearchBuilder @@ -32,13 +33,24 @@ class SearchBuilder private $considerChildTags; /** @var array */ private $sortSettings; + /** @var DataObject\Concrete */ + private $sourceObject; + + /* @var SearchService */ + private $searchService; + + public function __construct(SearchService $searchService) + { + $this->searchService = $searchService; + } /** * @return SearchBuilder */ - public static function create() + public function create() { - return new self(); +// return new self(); + return $this; } /** @@ -46,7 +58,8 @@ public static function create() */ public function build() { - return new SearchService($this); + $this->searchService->fromBuilder($this); + return $this->searchService; } /** @@ -220,6 +233,25 @@ public function withFilter($filter) return $this; } + /** + * @return DataObject\Concrete + */ + public function getSourceObject() + { + return $this->sourceObject; + } + + /** + * @param Dataobject\Conrete + * @return SearchBuilder + */ + public function withSourceObject(DataObject\Concrete $sourceObject) + { + $this->sourceObject = $sourceObject; + + return $this; + } + /** * @return array */ diff --git a/Service/SearchService.php b/Service/SearchService.php index 1647a5a..615ff3d 100644 --- a/Service/SearchService.php +++ b/Service/SearchService.php @@ -9,6 +9,8 @@ use Pimcore\Model\Search\Backend; use Pimcore\Model\Search\Backend\Data; use Pimcore\Model\User; +use PimcoreHrefTypeaheadBundle\Event\HreftypeaheadSearchEvent; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Class SearchService @@ -17,31 +19,40 @@ class SearchService { - /** @var array */ + /** @var array */ private $types; - /** @var array */ + /** @var array */ private $subTypes; - /** @var User */ + /** @var User */ private $user; - /** @var array */ + /** @var array */ private $classNames; - /** @var string */ + /** @var string */ private $query; - /** @var int */ + /** @var int */ private $start; - /** @var int */ + /** @var int */ private $limit; - /** @var array */ + /** @var array */ private $fields; /** @var array */ private $filter; - /** @var array */ + /** @var DataObject\Concrete */ + private $sourceObject; + /** @var array */ private $tagIds; - /** @var bool */ + /** @var bool */ private $considerChildTags; private $sortSettings; - public function __construct(SearchBuilder $searchBuilder) + /* @var EventDispatcherInterface */ + private $dispatcher; + + public function __construct(EventDispatcherInterface $dispatcher) { + $this->dispatcher = $dispatcher; + } + + public function fromBuilder(SearchBuilder $searchBuilder) { if ($searchBuilder->getUser() === null || !$searchBuilder->getUser() instanceof User) { throw new \InvalidArgumentException('Missing user or invalid, please review what we passed to SearchBuilder class'); @@ -65,6 +76,7 @@ public function __construct(SearchBuilder $searchBuilder) $this->tagIds = $searchBuilder->getTagIds(); $this->considerChildTags = $searchBuilder->isConsiderChildTags(); $this->sortSettings = $searchBuilder->getSortSettings(); + $this->sourceObject = $searchBuilder->getSourceObject(); // Force subtype to obj, var, folder when all objects are allowed /** @noinspection NotOptimalIfConditionsInspection */ @@ -202,6 +214,8 @@ public function getListingObject() //filtering for tags $conditionParts = $this->appendTagConditions($conditionParts); + $this->dispatcher->dispatch('hreftypeahead.search', new HreftypeaheadSearchEvent($this->sourceObject, $conditionParts)); + if (count($conditionParts) > 0) { $condition = implode(' AND ', $conditionParts); $searcherList->setCondition($condition);