Export Addressbook List - fix definition, export members and roles
[tine20] / tine20 / Addressbook / Export / List / Trait.php
1 <?php
2 /**
3  * Addressbook List Export trait
4  *
5  * @package     Addressbook
6  * @subpackage  Export
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Paul Mehrer <p.mehrer@metaways.de>
9  * @copyright   Copyright (c) 2017-2017 Metaways Infosystems GmbH (http://www.metaways.de)
10  *
11  */
12
13 trait Addressbook_Export_List_Trait
14 {
15     /**
16      * resolve records and prepare for export (set user timezone, ...)
17      *
18      * @param Tinebase_Record_RecordSet $_records
19      */
20     protected function _resolveRecords(Tinebase_Record_RecordSet $_records)
21     {
22         parent::_resolveRecords($_records);
23
24         /** @var Addressbook_Model_List $record */
25         foreach ($_records as $record) {
26             $memberRolesBackend = Addressbook_Controller_List::getInstance()->getMemberRolesBackend();
27             $filter = new Addressbook_Model_ListMemberRoleFilter(array(
28                 array('field' => 'list_id',      'operator' => 'equals', 'value' => $record->getId())
29             ));
30
31             $members = array();
32             $listRoles = array();
33             /** @var Addressbook_Model_ListMemberRole $listMemberRole */
34             foreach($memberRolesBackend->search($filter) as $listMemberRole)
35             {
36                 if (!isset($members[$listMemberRole->contact_id])) {
37                     $members[$listMemberRole->contact_id] = array();
38                 }
39                 $members[$listMemberRole->contact_id][] = $listMemberRole->list_role_id;
40                 $listRoles[$listMemberRole->list_role_id] = true;
41             }
42
43             if (count($listRoles) > 0) {
44                 $listRoles = Addressbook_Controller_ListRole::getInstance()->getMultiple(array_keys($listRoles));
45             }
46
47             $memberRecords = array();
48             if (is_array($record->members)) {
49                 $memberRecords = Addressbook_Controller_Contact::getInstance()->getMultiple($record->members);
50             }
51
52             $str = '';
53             /** @var Addressbook_Model_Contact $contact */
54             foreach($memberRecords as $contact) {
55                 $str .= ($str===''?'':', ') . $contact->n_fn;
56                 if (isset($members[$contact->getId()])) {
57                     $str .= ' (';
58                     $first = true;
59                     foreach($members[$contact->getId()] as $listRole) {
60                         /** @var Addressbook_Model_ListRole $listRole */
61                         if (null !== ($listRole = $listRoles->getById($listRole))) {
62                             $str .= (true===$first?'':', ') . $listRole->name;
63                             $first = false;
64                         }
65                     }
66                     $str .= ')';
67                 }
68             }
69
70             $record->memberroles = $str;
71         }
72     }
73 }