Skip to content

Commit

Permalink
dispatch event to add search conditions (#12)
Browse files Browse the repository at this point in the history
* added-event-dispatch-when-creating-query-conditions
* DI
* more-beautyfixes
  • Loading branch information
Sunohara666 authored Jul 2, 2019
1 parent f5c4969 commit 67220d2
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
5 changes: 3 additions & 2 deletions Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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'])
Expand All @@ -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)
Expand Down
66 changes: 66 additions & 0 deletions Event/HreftypeaheadSearchEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/

namespace PimcoreHrefTypeaheadBundle\Event;

use Pimcore\Model\DataObject;
use Symfony\Component\EventDispatcher\Event;

class HreftypeaheadSearchEvent extends Event
{
/** @var DataObject\Conrete */
private $sourceObject;

/** @var array */
private $conditions;

/**
* HreftypeaheadSearchEvent constructor.
*
* @param DataObject\Concrete $sourceObject
* @param string[] $conditions
*/
public function __construct(DataObject\Concrete $sourceObject, array &$conditions)
{
$this->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;
}

}
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,39 @@ composer require youwe/pimcore-href-typeahead
<dd>A controller containing autocomplete source</dd>
</dl>

### 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
3 changes: 3 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
38 changes: 35 additions & 3 deletions Service/SearchBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace PimcoreHrefTypeaheadBundle\Service;


use Pimcore\Model\DataObject;
use Pimcore\Model\User;

class SearchBuilder
Expand Down Expand Up @@ -32,21 +33,33 @@ 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;
}

/**
* @return SearchService
*/
public function build()
{
return new SearchService($this);
$this->searchService->fromBuilder($this);
return $this->searchService;
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down
36 changes: 25 additions & 11 deletions Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand All @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 67220d2

Please sign in to comment.