Skip to content

Commit

Permalink
Add search by company fields for list view/API (#8352)
Browse files Browse the repository at this point in the history
* Add search by company field for list view/API

* Fix travis issues

* Fix travis issue

* Add wildcard to search

Co-authored-by: Dennis Ameling <dennis@dennisameling.com>
Co-authored-by: Ruth Cheesley <ruth.cheesley@acquia.com>
  • Loading branch information
3 people committed Mar 29, 2021
1 parent 489b00d commit cf0cd31
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 3 deletions.
6 changes: 6 additions & 0 deletions app/bundles/LeadBundle/Assets/js/lead.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
//LeadBundle
Mautic.companyOnLoad = function (container, response) {

if (mQuery(container + ' #list-search').length) {
Mautic.activateSearchAutocomplete('list-search', 'lead.company');
}
}
Mautic.leadOnLoad = function (container, response) {
Mautic.addKeyboardShortcut('a', 'Quick add a New Contact', function(e) {
if(mQuery('a.quickadd').length) {
Expand Down
63 changes: 61 additions & 2 deletions app/bundles/LeadBundle/Entity/CompanyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Doctrine\ORM\QueryBuilder;
use Mautic\CoreBundle\Entity\CommonRepository;
use Mautic\LeadBundle\Event\CompanyBuildSearchEvent;
use Mautic\LeadBundle\LeadEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Class CompanyRepository.
Expand All @@ -22,6 +25,29 @@ class CompanyRepository extends CommonRepository implements CustomFieldRepositor
{
use CustomFieldRepositoryTrait;

/**
* @var array
*/
private $availableSearchFields = [];

/**
* @var EventDispatcherInterface|null
*/
private $dispatcher;

/**
* Used by search functions to search using aliases as commands.
*/
public function setAvailableSearchFields(array $fields)
{
$this->availableSearchFields = $fields;
}

public function setDispatcher(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -156,15 +182,48 @@ protected function addCatchAllWhereClause($q, $filter)
*/
protected function addSearchCommandWhereClause($q, $filter)
{
return $this->addStandardSearchCommandWhereClause($q, $filter);
list($expr, $parameters) = $this->addStandardSearchCommandWhereClause($q, $filter);
$unique = $this->generateRandomParameterName();
$returnParameter = true;
$command = $filter->command;

if (in_array($command, $this->availableSearchFields)) {
$expr = $q->expr()->like($this->getTableAlias().".$command", ":$unique");
}

if ($this->dispatcher) {
$event = new CompanyBuildSearchEvent($filter->string, $filter->command, $unique, $filter->not, $q);
$this->dispatcher->dispatch(LeadEvents::COMPANY_BUILD_SEARCH_COMMANDS, $event);
if ($event->isSearchDone()) {
$returnParameter = $event->getReturnParameters();
$filter->strict = $event->getStrict();
$expr = $event->getSubQuery();
$parameters = array_merge($parameters, $event->getParameters());
}
}

if ($returnParameter) {
$string = ($filter->strict) ? $filter->string : "%{$filter->string}%";
$parameters[$unique] = $string;
}

return [
$expr,
$parameters,
];
}

/**
* {@inheritdoc}
*/
public function getSearchCommands()
{
return $this->getStandardSearchCommands();
$commands = $this->getStandardSearchCommands();
if (!empty($this->availableSearchFields)) {
$commands = array_merge($commands, $this->availableSearchFields);
}

return $commands;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions app/bundles/LeadBundle/Event/CompanyBuildSearchEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* @copyright 2020 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace Mautic\LeadBundle\Event;

class CompanyBuildSearchEvent extends LeadBuildSearchEvent
{
}
10 changes: 10 additions & 0 deletions app/bundles/LeadBundle/LeadEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ final class LeadEvents
*/
const LEAD_BUILD_SEARCH_COMMANDS = 'mautic.lead_build_search_commands';

/**
* The mautic.company_build_search_commands event is dispatched when the search commands are built.
*
* The event listener receives a
* Mautic\LeadBundle\Event\CompanyBuildSearchEvent instance.
*
* @var string
*/
const COMPANY_BUILD_SEARCH_COMMANDS = 'mautic.company_build_search_commands';

/**
* The mautic.current_lead_changed event is dispatched when the current lead is changed to another such as when
* a new lead is created from a form submit. This gives opportunity to update session data if applicable.
Expand Down
21 changes: 20 additions & 1 deletion app/bundles/LeadBundle/Model/CompanyModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class CompanyModel extends CommonFormModel implements AjaxLookupModelInterface
*/
private $fields = [];

/**
* @var bool
*/
private $repoSetup = false;

/**
* CompanyModel constructor.
*/
Expand Down Expand Up @@ -112,7 +117,21 @@ public function saveEntities($entities, $unlock = true)
*/
public function getRepository()
{
return $this->em->getRepository('MauticLeadBundle:Company');
$repo = $this->em->getRepository('MauticLeadBundle:Company');
if (!$this->repoSetup) {
$this->repoSetup = true;
$repo->setDispatcher($this->dispatcher);
//set the point trigger model in order to get the color code for the lead
$fields = $this->leadFieldModel->getFieldList(true, true, ['isPublished' => true, 'object' => 'company']);

$searchFields = [];
foreach ($fields as $groupFields) {
$searchFields = array_merge($searchFields, array_keys($groupFields));
}
$repo->setAvailableSearchFields($searchFields);
}

return $repo;
}

/**
Expand Down

0 comments on commit cf0cd31

Please sign in to comment.