From b5e2a9132664ac9bc3e00ec75c5e10a753152ea1 Mon Sep 17 00:00:00 2001
From: Paul Mehrer
Date: Wed, 20 Sep 2023 18:07:17 +0200
Subject: [PATCH] tweak(Adb Contact) make groups property writeable)
---
tests/tine20/Addressbook/ControllerTest.php | 60 ++++++++++++++++++++-
tine20/Addressbook/Controller/Contact.php | 47 ++++++++++++++++
2 files changed, 106 insertions(+), 1 deletion(-)
diff --git a/tests/tine20/Addressbook/ControllerTest.php b/tests/tine20/Addressbook/ControllerTest.php
index 52fce1dd9b..58a279edea 100644
--- a/tests/tine20/Addressbook/ControllerTest.php
+++ b/tests/tine20/Addressbook/ControllerTest.php
@@ -360,7 +360,65 @@ public function testSearchContactCS()
['field' => 'adr_one_street', 'operator' => 'contains', 'value' => 'IsVeryS']
], '', [Tinebase_Model_Filter_Text::CASE_SENSITIVE => true]))->count(), 'cs search did not work');
}
-
+
+ public function testAddRemoveGroups()
+ {
+ $listCtrl = Addressbook_Controller_List::getInstance();
+ $sharedContainer = $this->_getTestContainer(Addressbook_Config::APP_NAME, Addressbook_Model_List::class, true);
+ $grants = Tinebase_Container::getInstance()->getGrantsOfContainer($sharedContainer->getId());
+ $grants->find('account_type', 'anyone')->adminGrant = true;
+ Tinebase_Container::getInstance()->setGrants($sharedContainer->getId(), $grants);
+ $publicList = $listCtrl->create(new Addressbook_Model_List([
+ 'name' => 'unittest list public',
+ 'container_id' => $sharedContainer->getId(),]));
+ $publicList2 = $listCtrl->create(new Addressbook_Model_List([
+ 'name' => 'unittest list public2',
+ 'container_id' => $sharedContainer->getId(),]));
+
+ $privateContainer = $this->_getTestContainer(Addressbook_Config::APP_NAME, Addressbook_Model_List::class);
+ $privateList = $listCtrl->create(new Addressbook_Model_List([
+ 'name' => 'unittest list private',
+ 'container_id' => $privateContainer->getId(),
+ ]));
+
+ $container = $this->_getTestContainer(Addressbook_Config::APP_NAME, Addressbook_Model_Contact::class, true);
+ $grants = Tinebase_Container::getInstance()->getGrantsOfContainer($container->getId());
+ $grants->find('account_type', 'anyone')->adminGrant = true;
+ Tinebase_Container::getInstance()->setGrants($container->getId(), $grants);
+ $contact = $this->_instance->create(new Addressbook_Model_Contact([
+ 'container_id' => $container->getId(),
+ 'n_family' => 'unittest',
+ ]));
+
+ $listCtrl->addListMember($publicList->getId(), $contact->getId());
+ $listCtrl->addListMember($privateList->getId(), $contact->getId());
+
+ Tinebase_Core::setUser($this->_personas['sclever']);
+
+ $contact = $this->_instance->get($contact->getId());
+
+ $groups = $contact->groups->getArrayOfIds();
+ $this->assertContains($publicList->getId(), $groups);
+ $this->assertNotContains($privateList->getId(), $groups);
+
+ unset($groups[array_search($publicList->getId(), $groups)]);
+ $groups[] = $publicList2->getId();
+ $contact->groups = $groups;
+
+ $contact = $this->_instance->update($contact);
+ $groups = $contact->groups->getArrayOfIds();
+ $this->assertContains($publicList2->getId(), $groups);
+ $this->assertNotContains($publicList->getId(), $groups);
+ $this->assertNotContains($privateList->getId(), $groups);
+
+ Tinebase_Core::setUser($this->_originalTestUser);
+ $contact = $this->_instance->get($contact->getId());
+ $groups = $contact->groups->getArrayOfIds();
+ $this->assertContains($publicList2->getId(), $groups);
+ $this->assertNotContains($publicList->getId(), $groups);
+ $this->assertContains($privateList->getId(), $groups);
+ }
+
/**
* test remove image
*/
diff --git a/tine20/Addressbook/Controller/Contact.php b/tine20/Addressbook/Controller/Contact.php
index f2f8b4c528..66bce2f0ce 100644
--- a/tine20/Addressbook/Controller/Contact.php
+++ b/tine20/Addressbook/Controller/Contact.php
@@ -310,6 +310,53 @@ public function updateUserProfile($_userProfile)
return $userProfile;
}
+ /**
+ * set relations / tags / alarms
+ *
+ * @param Tinebase_Record_Interface $updatedRecord the just updated record
+ * @param Tinebase_Record_Interface $record the update record
+ * @param Tinebase_Record_Interface $currentRecord the original record if one exists
+ * @param boolean $returnUpdatedRelatedData
+ * @param boolean $isCreate
+ * @return Tinebase_Record_Interface
+ * @throws Setup_Exception
+ * @throws Tinebase_Exception_InvalidArgument
+ * @throws Tinebase_Exception_Record_DefinitionFailure
+ * @throws Tinebase_Exception_Record_NotAllowed
+ */
+ protected function _setRelatedData(Tinebase_Record_Interface $updatedRecord, Tinebase_Record_Interface $record, Tinebase_Record_Interface $currentRecord = null, $returnUpdatedRelatedData = false, $isCreate = false)
+ {
+ if (is_array($groups = $record->groups)) {
+ foreach ($groups as &$group) {
+ if ($group['id'] ?? false) {
+ $group = $group['id'];
+ }
+ }
+ } elseif ($groups instanceof Tinebase_Record_RecordSet) {
+ $groups = $groups->getArrayOfIds();
+ } else {
+ return parent::_setRelatedData($updatedRecord, $record, $currentRecord, $returnUpdatedRelatedData, $isCreate);
+ }
+
+ if ($currentRecord) {
+ $toAdd = array_diff($groups, $currentRecord->groups->getArrayOfIds());
+ $toDelete = array_diff($currentRecord->groups->getArrayOfIds(), $groups);
+ } else {
+ $toAdd = $groups;
+ $toDelete = [];
+ }
+
+ foreach ($toAdd as $groupId) {
+ Addressbook_Controller_List::getInstance()->addListMember($groupId, $updatedRecord->getId());
+ }
+ foreach ($toDelete as $groupId) {
+ Addressbook_Controller_List::getInstance()->removeListMember($groupId, $updatedRecord->getId());
+ }
+ $updatedRecord->groups = $groups;
+
+ return parent::_setRelatedData($updatedRecord, $record, $currentRecord, $returnUpdatedRelatedData, $isCreate);
+ }
+
/**
* inspect update of one record (after update)
*