Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
models/DataObject/ClassDefinition/Data/Extension/RelationFilterConditionParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* @category pimcore | ||
* @date 25/06/2021 | ||
* @author Filip Szenborn <fszenborn@divante.co> | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Pimcore\Model\DataObject\ClassDefinition\Data\Extension; | ||
|
||
/** | ||
* Trait RelationFilterConditionParser | ||
* @package Pimcore\Model\DataObject\ClassDefinition\Data\Extension | ||
*/ | ||
trait RelationFilterConditionParser | ||
{ | ||
/** | ||
* Parses filter value of a relation field and creates the filter condition | ||
* @param $value | ||
* @param $operator | ||
* @param $name | ||
* @return string | ||
*/ | ||
public function getRelationFilterCondition($value, $operator, $name) | ||
{ | ||
if ($operator == '=') { | ||
$value = "'%," . $value . ",%'"; | ||
return '`' . $name . '` LIKE ' . $value . ' '; | ||
} elseif ($operator == 'LIKE') { | ||
$result = $name . " IS NULL"; | ||
$values = explode(',', (string)$value ?? ''); | ||
if (is_array($values) && !empty($values)) { | ||
$fieldConditions = []; | ||
foreach ($values as $value) { | ||
if (empty($value)) { | ||
continue; | ||
} | ||
$fieldConditions[] = '`' . $name . "` LIKE '%" . $value . "%' "; | ||
} | ||
if (!empty($fieldConditions)) { | ||
$result = '(' . implode(' AND ', $fieldConditions) . ')'; | ||
} | ||
} | ||
return $result; | ||
} | ||
} | ||
} |