Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple simple backend search from admin UI classic bundle #542

Merged
merged 6 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 48 additions & 41 deletions public/js/pimcore/object/helpers/gridTabAbstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,36 +174,6 @@ pimcore.object.helpers.gridTabAbstract = Class.create({
this.pagingtoolbar.moveFirst();
}.bind(this);

this.searchField = new Ext.form.TextField(
{
name: "query",
width: 200,
hideLabel: true,
enableKeyEvents: true,
value: this.searchFilter,
triggers: {
search: {
weight: 1,
cls: 'x-form-search-trigger',
scope: 'this',
handler: function(field, trigger, e) {
this.searchQuery(field);
}.bind(this)
}
},
listeners: {
"change": function() {
this.saveColumnConfigButton.show();
}.bind(this),
"keydown" : function (field, key) {
if (key.getKey() == key.ENTER) {
this.searchQuery(field);
}
}.bind(this)
}
}
);

this.languageInfo = new Ext.Toolbar.TextItem();

this.toolbarFilterInfo = new Ext.Button({
Expand All @@ -229,7 +199,6 @@ pimcore.object.helpers.gridTabAbstract = Class.create({
}.bind(this)
});

this.store.getProxy().setExtraParam("query", this.searchFilter);

var selectObjectOptions = Ext.create('Ext.data.Store', {
fields: ['name', 'value'],
Expand Down Expand Up @@ -329,18 +298,56 @@ pimcore.object.helpers.gridTabAbstract = Class.create({

this.buildColumnConfigMenu();

let items = [
this.languageInfo, "-",
this.toolbarFilterInfo,
this.clearFilterButton, "->",
this.selectObjectType, "-",
this.checkboxOnlyDirectChildren, "-",
this.exportButton, "-",
this.columnConfigButton,
this.saveColumnConfigButton
];

if(pimcore.helpers.hasSearchImplementation()) {
this.searchField = new Ext.form.TextField(
kingjia90 marked this conversation as resolved.
Show resolved Hide resolved
{
name: "query",
width: 200,
hideLabel: true,
enableKeyEvents: true,
value: this.searchFilter,
triggers: {
search: {
weight: 1,
cls: 'x-form-search-trigger',
scope: 'this',
handler: function (field, trigger, e) {
this.searchQuery(field);
}.bind(this)
}
},
listeners: {
"change": function () {
this.saveColumnConfigButton.show();
}.bind(this),
"keydown": function (field, key) {
if (key.getKey() == key.ENTER) {
this.searchQuery(field);
}
}.bind(this)
}
}
);

this.store.getProxy().setExtraParam("query", this.searchFilter);

items.unshift(this.searchField, "-");
}

var toolbar = new Ext.Toolbar({
scrollable: "x",
items: [this.searchField, "-",
this.languageInfo, "-",
this.toolbarFilterInfo,
this.clearFilterButton, "->",
this.selectObjectType, "-",
this.checkboxOnlyDirectChildren, "-",
this.exportButton, "-",
this.columnConfigButton,
this.saveColumnConfigButton
]
items: items
});

return toolbar;
Expand Down
15 changes: 15 additions & 0 deletions src/Event/AdminEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ class AdminEvents
*/
const OBJECT_LIST_AFTER_LIST_LOAD = 'pimcore.admin.object.list.afterListLoad';

/**
* Allows to implement an additional condition for the object list when the search field within the grid is used.
*
* Subject: \Pimcore\Bundle\AdminBundle\Helper\GridHelperService
* Arguments:
* - query | the fulltext query search terms
* - condition | set the condition for the search
* - list | the data object list
*
* @Event("Symfony\Component\EventDispatcher\GenericEvent")
*
* @var string
*/
const OBJECT_LIST_HANDLE_FULLTEXT_QUERY = 'pimcore.admin.object.list.handleFulltextQuery';

/**
* Fired before the request params are parsed. This event apply to both the folder content preview list and the grid list.
*
Expand Down
24 changes: 23 additions & 1 deletion src/Helper/GridHelperService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,29 @@
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Writer\Exception;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Pimcore\Bundle\AdminBundle\Event\AdminEvents;
use Pimcore\Logger;
use Pimcore\Model;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\Objectbrick;
use Pimcore\Model\User;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @internal
*/
class GridHelperService
{
public function __construct(
private readonly EventDispatcherInterface $eventDispatcher
) {

}

public function getFeatureAndSlugFilters(string $filterJson, ClassDefinition $class, string $requestedLanguage): array
{
$featureJoins = [];
Expand Down Expand Up @@ -621,7 +630,20 @@ public function prepareListingForGrid(array $requestParams, string $requestedLan
if (!empty($requestParams['query'])) {
$query = $this->filterQueryParam($requestParams['query']);
if (!empty($query)) {
$conditionFilters[] = 'oo_id IN (SELECT id FROM search_backend_data WHERE maintype = "object" AND MATCH (`data`,`properties`) AGAINST (' . $list->quote($query) . ' IN BOOLEAN MODE))';
$handleFullTextQueryEvent = new GenericEvent($this, [
'query' => $query,
'condition' => null,
'list' => $list,
]);
$this->eventDispatcher->dispatch(
$handleFullTextQueryEvent,
AdminEvents::OBJECT_LIST_HANDLE_FULLTEXT_QUERY
);

$condition = $handleFullTextQueryEvent->getArgument('condition');
if ($condition !== null) {
$conditionFilters[] = $condition;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Model/GridHelper/GridHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Pimcore\Db;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Tests\Support\Test\ModelTestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;

class GridHelperTest extends ModelTestCase
{
Expand Down Expand Up @@ -65,7 +66,7 @@ public function testAddGridFeatureJoinsWithTwoFilters(): void

$queryBuilder = Db::get()->createQueryBuilder();

$gridHelperService = new GridHelperService();
$gridHelperService = new GridHelperService(new EventDispatcher());
$gridHelperService->addGridFeatureJoins($list, $featureJoins, $class, $featureAndSlugFilters);

$dao = $list->getDao();
Expand Down
Loading