Skip to content

Commit

Permalink
Merge branch 'pu/pm/TbForeignRecordsFilterEmptyValueFixed' into '2024…
Browse files Browse the repository at this point in the history
….11'

tweak(TB ForeignRecordsFilter) not/definedBy empty value fixed

See merge request tine20/tine20!4751
  • Loading branch information
paulmhh committed Jan 15, 2024
2 parents a17c158 + 2a92116 commit 3a602de
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
37 changes: 36 additions & 1 deletion tests/tine20/Sales/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,42 @@ protected function setUp(): void
$this->_backend->setNumberPrefix();
$this->_backend->setNumberZerofill();
}


public function testEvalDimFilter()
{
$cc = Tinebase_Controller_EvaluationDimension::getInstance()->getAll()->find(Tinebase_Model_EvaluationDimension::FLD_NAME, Tinebase_Model_EvaluationDimension::COST_CENTER);
$cc->{Tinebase_Model_EvaluationDimension::FLD_ITEMS} =
new Tinebase_Record_RecordSet(Tinebase_Model_EvaluationDimensionItem::class, [
new Tinebase_Model_EvaluationDimensionItem([
Tinebase_Model_EvaluationDimensionItem::FLD_NAME => 'foo',
Tinebase_Model_EvaluationDimensionItem::FLD_NUMBER => '1',
], true),
new Tinebase_Model_EvaluationDimensionItem([
Tinebase_Model_EvaluationDimensionItem::FLD_NAME => 'bar',
Tinebase_Model_EvaluationDimensionItem::FLD_NUMBER => '2',
'divisions' => new Tinebase_Record_RecordSet(Sales_Model_DivisionEvalDimensionItem::class, [
new Sales_Model_DivisionEvalDimensionItem([
Sales_Model_DivisionEvalDimensionItem::FLD_DIVISION_ID => Sales_Config::getInstance()->{Sales_Config::DEFAULT_DIVISION},
], true),
]),
], true),
]);
$cc = Tinebase_Controller_EvaluationDimension::getInstance()->update($cc);
$foo = $cc->{Tinebase_Model_EvaluationDimension::FLD_ITEMS}->find(Tinebase_Model_EvaluationDimensionItem::FLD_NAME, 'foo');
$bar = $cc->{Tinebase_Model_EvaluationDimension::FLD_ITEMS}->find(Tinebase_Model_EvaluationDimensionItem::FLD_NAME, 'bar');

$result = Tinebase_Controller_EvaluationDimensionItem::getInstance()->search(Tinebase_Model_Filter_FilterGroup::getFilterForModel(Tinebase_Model_EvaluationDimensionItem::class, [
['field' => 'divisions', 'operator' => 'definedBy', 'value' => null],
['field' => 'divisions', 'operator' => 'definedBy', 'value' => [
['field' => 'division_id', 'operator' => 'in', 'value' => [Sales_Config::getInstance()->{Sales_Config::DEFAULT_DIVISION}]]
]],
], Tinebase_Model_Filter_FilterGroup::CONDITION_OR));

$this->assertGreaterThanOrEqual(2, $result->count());
$this->assertInstanceOf(Tinebase_Model_EvaluationDimensionItem::class, $result->getById($foo->getId()));
$this->assertInstanceOf(Tinebase_Model_EvaluationDimensionItem::class, $result->getById($bar->getId()));
}

/**
* tests for the costcenter controller
*/
Expand Down
3 changes: 3 additions & 0 deletions tine20/Addressbook/Model/ListMemberFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ protected function _setOptions(array $_options)
*/
protected function _setFilterGroup()
{
if ($this->_valueIsNull) {
return;
}
if ($this->_field === 'contact') {
$this->_options['filtergroup'] = Addressbook_Model_Contact::class;
} else {
Expand Down
26 changes: 25 additions & 1 deletion tine20/Tinebase/Model/Filter/ForeignRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ protected function _setOptions(array $_options)
parent::_setOptions($_options);
}

public function setValue($_value)
{
parent::setValue($_value);
if ($this->_valueIsNull) {
$this->_setFilterGroup();
}
}

/**
* appends sql to given select statement
*
Expand Down Expand Up @@ -101,7 +109,23 @@ public function appendFilterSql($_select, $_backend)
$this->_field = 'id';

try {
parent::appendFilterSql($_select, $_backend);
if ($this->_valueIsNull) {
if (strpos($this->_operator, 'not') === 0) {
if (empty($this->_foreignIds)) {
$_select->where('1 = 0');
} else {
$_select->where($this->_getQuotedFieldName($_backend) . ' IN (?)', $this->_foreignIds);
}
} else {
if (empty($this->_foreignIds)) {
$_select->where('1 = 1');
} else {
$_select->where($this->_getQuotedFieldName($_backend) . ' NOT IN (?)', $this->_foreignIds);
}
}
} else {
parent::appendFilterSql($_select, $_backend);
}
} finally {
$this->_field = $orgField;
}
Expand Down

0 comments on commit 3a602de

Please sign in to comment.