Skip to content

Commit

Permalink
Implemented feature request #59 (search for duplicate customers)
Browse files Browse the repository at this point in the history
  • Loading branch information
deltatag committed Jan 18, 2018
1 parent a333243 commit bd1c28b
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 112 deletions.
113 changes: 9 additions & 104 deletions src/Controller/Admin/CustomersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,13 @@
use CustomerManagementFrameworkBundle\CustomerList\Exporter\AbstractExporter;
use CustomerManagementFrameworkBundle\CustomerList\Exporter\ExporterInterface;
use CustomerManagementFrameworkBundle\CustomerList\ExporterManagerInterface;
use CustomerManagementFrameworkBundle\CustomerList\Filter\CustomerSegment as CustomerSegmentFilter;
use CustomerManagementFrameworkBundle\Listing\Filter\Permission as PermissionFilter;
use CustomerManagementFrameworkBundle\CustomerList\SearchHelper;
use CustomerManagementFrameworkBundle\CustomerList\Filter\Exception\SearchQueryException;
use CustomerManagementFrameworkBundle\CustomerList\Filter\SearchQuery;
use CustomerManagementFrameworkBundle\CustomerProvider\CustomerProviderInterface;
use CustomerManagementFrameworkBundle\Helper\Objects;
use CustomerManagementFrameworkBundle\Listing\Filter;
use CustomerManagementFrameworkBundle\Listing\FilterHandler;
use CustomerManagementFrameworkBundle\Model\CustomerInterface;
use CustomerManagementFrameworkBundle\Model\CustomerView\FilterDefinition;
use CustomerManagementFrameworkBundle\Model\CustomerSegmentInterface;
use CustomerManagementFrameworkBundle\SegmentManager\SegmentManagerInterface;
use Pimcore\Db;
use Pimcore\Db\ZendCompatibility\QueryBuilder;
use Pimcore\Model\DataObject\AbstractObject;
Expand Down Expand Up @@ -102,7 +97,7 @@ public function listAction(Request $request)
'errors' => $errors,
'paginator' => $paginator,
'customerView' => $customerView,
'searchBarFields' => $this->getConfiguredSearchBarFields(),
'searchBarFields' => $this->getSearchHelper()->getConfiguredSearchBarFields(),
'request' => $request,
'clearUrlParams' => $this->clearUrlParams,
'filterDefinitions' => $this->getFilterDefinitions(),
Expand All @@ -120,7 +115,7 @@ public function listAction(Request $request)
*/
public function detailAction(Request $request)
{
$customer = $this->getCustomerProvider()->getById((int)$request->get('id'));
$customer = $this->getSearchHelper()->getCustomerProvider()->getById((int)$request->get('id'));
if($customer && $customer instanceof CustomerInterface) {
$customerView = \Pimcore::getContainer()->get('cmf.customer_view');
if(!$customerView->hasDetailView($customer)) {
Expand Down Expand Up @@ -378,7 +373,7 @@ public function loadSegmentGroups()
{
if(is_null($this->segmentGroups)) {
/** @var CustomerSegmentGroup\Listing $segmentGroups */
$segmentGroups = $this->getSegmentManager()->getSegmentGroups();
$segmentGroups = $this->getSearchHelper()->getSegmentManager()->getSegmentGroups();
$segmentGroups->addConditionParam('showAsFilter = 1');
// sort by filterSortOrder high to low
$segmentGroups->setOrderKey('filterSortOrder IS NULL, filterSortOrder DESC', false);
Expand All @@ -395,72 +390,14 @@ public function loadSegmentGroups()
protected function buildListing(array $filters = [])
{
/** @var Listing|Listing\Concrete $listing */
$listing = $this->getCustomerProvider()->getList();
$listing = $this->getSearchHelper()->getCustomerProvider()->getList();
$listing
->setOrderKey('o_id')
->setOrder('ASC');
$this->addListingFilters($listing, $filters);
$this->getSearchHelper()->addListingFilters($listing, $filters, $this->getAdminUser());

return $listing;
}

/**
* @param Listing\Concrete $listing
* @param array $filters
* @throws \Exception
*/
protected function addListingFilters(Listing\Concrete $listing, array $filters = [])
{
$handler = new FilterHandler($listing);

$filterProperties = \Pimcore::getContainer()->getParameter('pimcore_customer_management_framework.customer_list.filter_properties');

$equalsProperties = isset($filterProperties['equals']) ? $filterProperties['equals'] : [];
$searchProperties = isset($filterProperties['search']) ? $filterProperties['search'] : [];

foreach($equalsProperties as $property => $databaseField) {
if(array_key_exists($property, $filters)) {
$handler->addFilter(new Filter\Equals($databaseField, $filters[$property]));
}
}

foreach($searchProperties as $property => $databaseFields) {
if(array_key_exists($property, $filters)
&& !empty($filters[$property])
&& is_string($filters[$property])) {
$handler->addFilter(new SearchQuery($databaseFields, $filters[$property]));
}
}

if(array_key_exists('segments', $filters)) {
foreach($filters['segments'] as $groupId => $segmentIds) {
$segmentGroup = null;
if($groupId !== 'default') {
/** @var \Pimcore\Model\DataObject\CustomerSegmentGroup $segmentGroup */
$segmentGroup = $this->getSegmentManager()->getSegmentGroupById($groupId);
if(!$segmentGroup) {
throw new \Exception(sprintf('Segment group %d was not found', $groupId));
}
}
$segments = [];
foreach($segmentIds as $segmentId) {
$segment = $this->getSegmentManager()->getSegmentById($segmentId);
if(!$segment) {
throw new \Exception(sprintf('Segment %d was not found', $segmentId));
}
$segments[] = $segment;
}
$handler->addFilter(new CustomerSegmentFilter($segments, $segmentGroup));
}
}

// add permission filter for non admin
if(!$this->getAdminUser()->isAdmin()) {
// only show customers which the user can access
$handler->addFilter(new PermissionFilter($this->getAdminUser()));
}
}

/**
* Fetch filters and set them on view
*
Expand Down Expand Up @@ -530,26 +467,6 @@ protected function fetchPrefilteredSegment(Request $request)
return null;
}

/**
* @return array
*/
protected function getConfiguredSearchBarFields()
{
$filterProperties = \Pimcore::getContainer()->getParameter('pimcore_customer_management_framework.customer_list.filter_properties');
$searchProperties = $filterProperties['search'];

$searchBarFields = [];
if(isset($searchProperties['search'])) {
$searchProperties = $searchProperties['search'];

if(is_array($searchProperties) && count($searchProperties) > 0) {
$searchBarFields = array_values($searchProperties);
}
}

return $searchBarFields;
}

/**
* Fetch all filter definitions available for current user
*
Expand Down Expand Up @@ -688,22 +605,10 @@ protected function addFilterDefinitionSegments(Request $request, array $filters)
}

/**
* @return SegmentManagerInterface
*/
protected function getSegmentManager()
{
/** @noinspection MissingService */
/** @noinspection PhpIncompatibleReturnTypeInspection */
return \Pimcore::getContainer()->get('cmf.segment_manager');
}

/**
* @return CustomerProviderInterface
* @return SearchHelper
*/
protected function getCustomerProvider()
protected function getSearchHelper()
{
/** @noinspection MissingService */
/** @noinspection PhpIncompatibleReturnTypeInspection */
return \Pimcore::getContainer()->get('cmf.customer_provider');
return \Pimcore::getContainer()->get(SearchHelper::class);
}
}
43 changes: 39 additions & 4 deletions src/Controller/Admin/DuplicatesController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Pimcore
*
Expand All @@ -16,7 +15,10 @@
namespace CustomerManagementFrameworkBundle\Controller\Admin;

use CustomerManagementFrameworkBundle\Controller\Admin;
use CustomerManagementFrameworkBundle\CustomerList\SearchHelper;
use CustomerManagementFrameworkBundle\DuplicatesIndex\DuplicatesIndexInterface;
use CustomerManagementFrameworkBundle\Factory;
use Pimcore\Model\DataObject\Listing\Concrete;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -33,14 +35,35 @@ public function init()

/**
* @param Request $request
* @param DuplicatesIndexInterface $duplicatesIndex
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Exception
* @Route("/list")
*/
public function listAction(Request $request)
public function listAction(Request $request, DuplicatesIndexInterface $duplicatesIndex)
{
$paginator = \Pimcore::getContainer()->get('cmf.customer_duplicates_index')->getPotentialDuplicates(
// fetch all filters
$filters = $request->get('filter', []);
// check if filters exist
$customerList = null;
if(!empty($filters)) {
// build customer listing
/** @var Concrete $listing */
$customerList = $this->getSearchHelper()->getCustomerProvider()->getList();
$customerList
->setOrderKey('o_id')
->setOrder('ASC');

/** @noinspection PhpUnhandledExceptionInspection */
$this->getSearchHelper()->addListingFilters($customerList, $filters, $this->getAdminUser());
}


$paginator = $duplicatesIndex->getPotentialDuplicates(
$request->get('page', 1),
50,
$request->get('declined')
$request->get('declined'),
$customerList
);

return $this->render(
Expand All @@ -49,6 +72,8 @@ public function listAction(Request $request)
'paginator' => $paginator,
'duplicates' => $paginator->getCurrentItems(),
'duplicatesView' => \Pimcore::getContainer()->get('cmf.customer_duplicates_view'),
'searchBarFields' => $this->getSearchHelper()->getConfiguredSearchBarFields(),
'filters' => $filters,
]
);
}
Expand All @@ -65,10 +90,12 @@ public function falsePositivesAction()
/**
* @param Request $request
* @Route("/decline/{id}")
* @return JsonResponse
*/
public function declineAction(Request $request)
{
try {
/** @noinspection MissingService */
\Pimcore::getContainer()->get('cmf.customer_duplicates_index')->declinePotentialDuplicate(
$request->get('id')
);
Expand All @@ -78,4 +105,12 @@ public function declineAction(Request $request)
return new JsonResponse(['success' => false, 'msg' => $e->getMessage()]);
}
}

/**
* @return SearchHelper
*/
protected function getSearchHelper()
{
return \Pimcore::getContainer()->get(SearchHelper::class);
}
}
Loading

0 comments on commit bd1c28b

Please sign in to comment.