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

[Bug]: Use parameters for joins in Customer Segment (#549) #557

Merged
merged 18 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:

- name: "Upload baseline file"
if: ${{ failure() }}
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: phpstan-baseline.neon
path: phpstan-baseline.neon
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"require": {
"php": "~8.1.0 || ~8.2.0",
"doctrine/dbal": "^3.4.2",
"doctrine/dbal": "^3.6.0",
"doctrine/migrations": "^3.0.2",
"dragonmantank/cron-expression": "^3.0.2",
"drewm/mailchimp-api": "^2.2",
Expand Down
20 changes: 13 additions & 7 deletions src/CustomerList/Filter/CustomerSegment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CustomerManagementFrameworkBundle\Listing\Filter\OnCreateQueryFilterInterface;
use CustomerManagementFrameworkBundle\Service\MariaDb;
use Doctrine\DBAL\Query\QueryBuilder;
use InvalidArgumentException;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Listing as CoreListing;

Expand Down Expand Up @@ -69,7 +70,7 @@ public function __construct(array $segments, DataObject\CustomerSegmentGroup $se
{
$this->identifier = $this->buildIdentifier($segmentGroup);
$this->segmentGroup = $segmentGroup;
$this->type = $type;
$this->type = $type === self::OPERATOR_AND ? self::OPERATOR_AND : self::OPERATOR_OR;

foreach ($segments as $segment) {
$this->addCustomerSegment($segment);
Expand Down Expand Up @@ -119,7 +120,7 @@ protected function addCustomerSegment(DataObject\CustomerSegment $segment)
{
if ($segment->getGroup() && null !== $this->segmentGroup) {
if ($segment->getGroup()->getId() !== $this->segmentGroup->getId()) {
throw new \InvalidArgumentException('Segment does not belong to the defined segment group');
throw new InvalidArgumentException('Segment does not belong to the defined segment group');
}
}

Expand Down Expand Up @@ -187,6 +188,7 @@ protected function applyAndQuery(CoreListing\Concrete $listing, QueryBuilder $qu
*
* @param string $joinName
* @param int|array $conditionValue
*
*/
protected function addJoin(
CoreListing\Concrete $listing,
Expand All @@ -212,20 +214,24 @@ protected function addJoin(
);

$condition = $baseCondition;

if ($this->type === self::OPERATOR_OR) {
// must match any of the passed IDs
$valueKeys = array_keys($conditionValue);
$isNumericArray = $valueKeys == array_filter($valueKeys, 'is_numeric');
if (!$isNumericArray) {
$valueKeys = [0];
}

$condition .= sprintf(
' AND %1$s.dest_id IN (%2$s)',
$joinName,
implode(',', $conditionValue)
implode(',', $valueKeys)
);
} else {
// runs an extra join for every ID - all joins must match
$condition .= sprintf(
' AND %1$s.dest_id = %2$s',
' AND %1$s.dest_id = %2$d',
$joinName,
$conditionValue
is_numeric($conditionValue) ? $conditionValue : 0
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,13 @@ private function extractNamingScheme(CustomerInterface $customer, $namingScheme)
foreach ($namingScheme as $i => $namingSchemeItem) {
preg_match_all('/{([a-zA-Z0-9]*)}/', $namingSchemeItem, $matchedPlaceholder);

if (sizeof($matchedPlaceholder)) {
foreach ($matchedPlaceholder[0] as $j => $placeholder) {
$field = $matchedPlaceholder[1][$j];

$getter = 'get'.ucfirst($field);
if (method_exists($customer, $getter)) {
$value = (string)$customer->$getter();
$namingScheme[$i] = str_replace($placeholder, $value, $namingScheme[$i]);
}
foreach ($matchedPlaceholder[0] as $j => $placeholder) {
$field = $matchedPlaceholder[1][$j];

$getter = 'get'.ucfirst($field);
if (method_exists($customer, $getter)) {
$value = (string)$customer->$getter();
$namingScheme[$i] = str_replace($placeholder, $value, $namingScheme[$i]);
}
}
$namingScheme[$i] = trim($namingScheme[$i]) ?: '--';
Expand Down