Addressbook - Export - Lists - resolve some data
authorPaul Mehrer <p.mehrer@metaways.de>
Fri, 4 Aug 2017 13:36:45 +0000 (15:36 +0200)
committerPaul Mehrer <p.mehrer@metaways.de>
Fri, 4 Aug 2017 14:42:08 +0000 (16:42 +0200)
Change-Id: Idcbc512f46c999f369a6f8c5f69ac2ba5362cead
Reviewed-on: http://gerrit.tine20.com/customers/5398
Tested-by: Jenkins CI (http://ci.tine20.com/) <tine20-jenkins@metaways.de>
Reviewed-by: Paul Mehrer <p.mehrer@metaways.de>
Tested-by: Paul Mehrer <p.mehrer@metaways.de>
tine20/Addressbook/Export/List/Trait.php

index b8c0c0e..6059cda 100644 (file)
@@ -19,8 +19,14 @@ trait Addressbook_Export_List_Trait
      */
     protected function _resolveRecords(Tinebase_Record_RecordSet $_records)
     {
+        /** @noinspection PhpUndefinedMethodInspection */
         parent::_resolveRecords($_records);
 
+        if ('adb_list_doc' === $this->_config->name) {
+            $this->_listDocResolveRecords($_records);
+            return;
+        }
+
         /** @var Addressbook_Model_List $record */
         foreach ($_records as $record) {
             $memberRolesBackend = Addressbook_Controller_List::getInstance()->getMemberRolesBackend();
@@ -70,4 +76,73 @@ trait Addressbook_Export_List_Trait
             $record->memberroles = $str;
         }
     }
+
+    /**
+     * resolve records and prepare for export (set user timezone, ...)
+     *
+     * @param Tinebase_Record_RecordSet $_records
+     */
+    protected function _listDocResolveRecords(Tinebase_Record_RecordSet $_records)
+    {
+        $memberRolesBackend = Addressbook_Controller_List::getInstance()->getMemberRolesBackend();
+
+        /** @var Addressbook_Model_List $record */
+        foreach ($_records as $record) {
+            $filter = new Addressbook_Model_ListMemberRoleFilter(array(
+                array('field' => 'list_id',      'operator' => 'equals', 'value' => $record->getId())
+            ));
+
+            $members = array();
+            $listRoles = array();
+            /** @var Addressbook_Model_ListMemberRole $listMemberRole */
+            foreach($memberRolesBackend->search($filter) as $listMemberRole)
+            {
+                if (!isset($members[$listMemberRole->contact_id])) {
+                    $members[$listMemberRole->contact_id] = array();
+                }
+                $members[$listMemberRole->contact_id][] = $listMemberRole->list_role_id;
+                $listRoles[$listMemberRole->list_role_id] = true;
+            }
+
+            if (count($listRoles) > 0) {
+                $listRolesResultSet = Addressbook_Controller_ListRole::getInstance()->getMultiple(array_keys($listRoles));
+            }
+
+            $memberRecords = array();
+            if (is_array($record->members)) {
+                $memberRecords = Addressbook_Controller_Contact::getInstance()->getMultiple($record->members);
+            }
+            $newMembers = array();
+
+            /** @var Addressbook_Model_Contact $contact */
+            foreach($memberRecords as $contact) {
+                $newMembers[$contact->getId()] = $contact->toArray();
+                if (isset($members[$contact->getId()])) {
+                    $str = '';
+                    $first = true;
+                    foreach($members[$contact->getId()] as $listRoleId) {
+                        /** @var Addressbook_Model_ListRole $listRole */
+                        if (null !== ($listRole = $listRolesResultSet->getById($listRoleId))) {
+                            $str .= (true===$first?'':', ') . $listRole->name;
+                            $first = false;
+                            if (true === $listRoles[$listRoleId]) {
+                                $listRoles[$listRoleId] = array(
+                                    'name'      => $listRole->name,
+                                    'members'   => $contact->n_fn
+                                );
+                            } else {
+                                $listRoles[$listRoleId]['members'] .= ', ' . $contact->n_fn;
+                            }
+                        }
+                    }
+                    $newMembers[$contact->getId()]['memberroles'] = $str;
+                } else {
+                    $newMembers[$contact->getId()]['memberroles'] = '';
+                }
+            }
+
+            $record->members = $newMembers;
+            $record->memberroles = $listRoles;
+        }
+    }
 }
\ No newline at end of file