Skip to content

Commit

Permalink
Add related filter (#23)
Browse files Browse the repository at this point in the history
* add support to filter by related field (regex etc depending on field support) f3af2ba
* remove unintended vardump b0eeb30
  • Loading branch information
jonmifsud authored and nitriques committed Jul 24, 2018
1 parent 47ba2e7 commit 2d4ccb9
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion fields/field.association.php
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,11 @@ public function fetchFilterableOperators()
'filter' => ' ',
'help' => __('Find values that are an exact match for the given string.')
),
array(
'title' => 'related:',
'filter' => 'related: ',
'help' => __('Find values by filtering on the associated field, can use regexp.')
),
array(
'filter' => 'sql: NOT NULL',
'title' => 'is not empty',
Expand Down Expand Up @@ -963,7 +968,41 @@ public function buildDSRetrievalSQL($data, &$joins, &$where, $andOperation = fal
{
$field_id = $this->get('id');

if (preg_match('/^sql:\s*/', $data[0], $matches)) {
if (preg_match('/^related:\s*/', $data[0], $matches)) {

$data[0] = preg_replace('/^related: /', null, $data[0]);

$related_field_id = current($this->get('related_field_id'));
$related_field = FieldManager::fetch($related_field_id);

$newJoin = '';
$newWhere = '';

$related_field->buildDSRetrievalSQL($data,$newJoin,$newWhere);

if (empty($newWhere)){
// the filter is empty eg an empty regexp so no need to continue
return true;
}

$relatedTableName = "t{$related_field_id}";
if ($related_field->_key){
$relatedTableName .= '_'.$related_field->_key;
}

// Join the main field
$joins .= " LEFT JOIN
`tbl_entries_data_{$field_id}` AS `t{$field_id}`
ON (`e`.`id` = `t{$field_id}`.`entry_id`)";

// Join the related field
$joins .= " LEFT JOIN
`tbl_entries_data_{$related_field_id}` AS `$relatedTableName`
ON (`t{$field_id}`.`relation_id` = `$relatedTableName`.`entry_id`)";

$where .= $newWhere;

} else if (preg_match('/^sql:\s*/', $data[0], $matches)) {
$data = trim(array_pop(explode(':', $data[0], 2)));

if (strpos($data, "NOT NULL") !== false) {
Expand Down

0 comments on commit 2d4ccb9

Please sign in to comment.