From 0232ff2c5cfe98c8f4206c43abdca5401e134901 Mon Sep 17 00:00:00 2001 From: Paul Mehrer Date: Mon, 8 Feb 2021 12:02:50 +0100 Subject: [PATCH] fix(TB CustomFieldFilter) apply foreignId filter changes for cf filters too Change-Id: Ia26930a1158e9f348d6195af5cdd223fb2b5e41d Reviewed-on: http://gerrit.tine20.com/customers/19065 Reviewed-by: Paul Mehrer Tested-by: Paul Mehrer --- tine20/Tinebase/Model/Filter/CustomField.php | 36 ++++++++++++++++--- tine20/Tinebase/Model/Filter/ForeignId.php | 13 +++++-- .../Tinebase/Model/Filter/ForeignRecord.php | 6 +++- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/tine20/Tinebase/Model/Filter/CustomField.php b/tine20/Tinebase/Model/Filter/CustomField.php index 3fb7095b2bb..18ebd52ddd1 100644 --- a/tine20/Tinebase/Model/Filter/CustomField.php +++ b/tine20/Tinebase/Model/Filter/CustomField.php @@ -93,6 +93,7 @@ class Tinebase_Model_Filter_CustomField extends Tinebase_Model_Filter_Abstract */ protected $_subFilterController = null; + protected $_passThroughFilter = null; /** * get a new single filter action @@ -155,10 +156,19 @@ public function __construct($_fieldOrData, $_operator = NULL, $_value = NULL, ar case 'record': if (is_array($_fieldOrData['value']['value'])) { $modelName = Tinebase_CustomField::getModelNameFromDefinition($this->_cfRecord->definition); - $this->_subFilterController = Tinebase_Core::getApplicationInstance($modelName); - $this->_subFilter = Tinebase_Model_Filter_FilterGroup::getFilterForModel($modelName); - $filterClass = null; - $this->_operators = ['AND', 'OR', 'notDefinedBy:AND', 'notDefinedBy:OR']; + $passThroughData = $_fieldOrData; + $passThroughData['field'] = 'value'; + $passThroughData['value'] = $passThroughData['value']['value']; + $filterGroup = get_class(Tinebase_Model_Filter_FilterGroup::getFilterForModel($modelName)); + if ($filterGroup === Tinebase_Model_Filter_FilterGroup::class) { + $filterGroup = $modelName; + } + $passThroughData['options'] = [ + 'tablename' => $this->_correlationName, + 'controller' => get_class(Tinebase_Core::getApplicationInstance($modelName)), + 'filtergroup' => $filterGroup, + ]; + $this->_passThroughFilter = new Tinebase_Model_Filter_ForeignId($passThroughData); } else { $filterClass = Tinebase_Model_Filter_Id::class; } @@ -185,6 +195,21 @@ public function __construct($_fieldOrData, $_operator = NULL, $_value = NULL, ar parent::__construct($_fieldOrData, $_operator, $_value, $_options); } + + /** + * sets operator + * + * @param string $_operator + * @throws Tinebase_Exception_UnexpectedValue + */ + public function setOperator($_operator) + { + if (null !== $this->_passThroughFilter) { + $this->_operator = $_operator; + } else { + parent::setOperator($_operator); + } + } /** * set options @@ -276,6 +301,9 @@ public function appendFilterSql($_select, $_backend) } } } + if (null !== $this->_passThroughFilter) { + $this->_passThroughFilter->appendFilterSql($_select, $_backend); + } } /** diff --git a/tine20/Tinebase/Model/Filter/ForeignId.php b/tine20/Tinebase/Model/Filter/ForeignId.php index 10b27fa8b8d..5eb2811a320 100644 --- a/tine20/Tinebase/Model/Filter/ForeignId.php +++ b/tine20/Tinebase/Model/Filter/ForeignId.php @@ -68,14 +68,21 @@ public function appendFilterSql($_select, $_backend) if ($this->_valueIsNull) { $_select->where($this->_getQuotedFieldName($_backend) . ' IS NOT NULL'); } elseif (!empty($this->_foreignIds)) { - $_select->where($this->_getQuotedFieldName($_backend) . ' NOT IN (?)', $this->_foreignIds); + $groupSelect = new Tinebase_Backend_Sql_Filter_GroupSelect($_select); + $valueIdentifier = $this->_getQuotedFieldName($_backend); + $groupSelect->orWhere($valueIdentifier . ' IS NULL'); + $groupSelect->orWhere($valueIdentifier . ' NOT IN (?)', $this->_foreignIds); + $groupSelect->appendWhere(Zend_Db_Select::SQL_OR); } } else { if (!$this->_valueIsNull && empty($this->_foreignIds)) { $_select->where('1 = 0'); } else { - $_select->where($this->_getQuotedFieldName($_backend) . ' IN (?)', - empty($this->_foreignIds) ? new Zend_Db_Expr('NULL') : $this->_foreignIds); + if (empty($this->_foreignIds)) { + $_select->where($this->_getQuotedFieldName($_backend) . ' IS NULL'); + } else { + $_select->where($this->_getQuotedFieldName($_backend) . ' IN (?)', $this->_foreignIds); + } } } } diff --git a/tine20/Tinebase/Model/Filter/ForeignRecord.php b/tine20/Tinebase/Model/Filter/ForeignRecord.php index cf635fbaa3b..522ffac1f9f 100644 --- a/tine20/Tinebase/Model/Filter/ForeignRecord.php +++ b/tine20/Tinebase/Model/Filter/ForeignRecord.php @@ -169,11 +169,15 @@ protected function _removePrefixesFromFilterValue(&$value) */ protected function _setFilterGroup() { + $options = $this->_options; + if (isset($options['tablename'])) { + unset($options['tablename']); + } $this->_filterGroup = Tinebase_Model_Filter_FilterGroup::getFilterForModel( $this->_options['filtergroup'], $this->_value, $this->_conditionSubFilter, - $this->_options + $options ); }