6059cdafc0d6cf61d5fc07f927c66069df69ba95
[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         /** @noinspection PhpUndefinedMethodInspection */
23         parent::_resolveRecords($_records);
24
25         if ('adb_list_doc' === $this->_config->name) {
26             $this->_listDocResolveRecords($_records);
27             return;
28         }
29
30         /** @var Addressbook_Model_List $record */
31         foreach ($_records as $record) {
32             $memberRolesBackend = Addressbook_Controller_List::getInstance()->getMemberRolesBackend();
33             $filter = new Addressbook_Model_ListMemberRoleFilter(array(
34                 array('field' => 'list_id',      'operator' => 'equals', 'value' => $record->getId())
35             ));
36
37             $members = array();
38             $listRoles = array();
39             /** @var Addressbook_Model_ListMemberRole $listMemberRole */
40             foreach($memberRolesBackend->search($filter) as $listMemberRole)
41             {
42                 if (!isset($members[$listMemberRole->contact_id])) {
43                     $members[$listMemberRole->contact_id] = array();
44                 }
45                 $members[$listMemberRole->contact_id][] = $listMemberRole->list_role_id;
46                 $listRoles[$listMemberRole->list_role_id] = true;
47             }
48
49             if (count($listRoles) > 0) {
50                 $listRoles = Addressbook_Controller_ListRole::getInstance()->getMultiple(array_keys($listRoles));
51             }
52
53             $memberRecords = array();
54             if (is_array($record->members)) {
55                 $memberRecords = Addressbook_Controller_Contact::getInstance()->getMultiple($record->members);
56             }
57
58             $str = '';
59             /** @var Addressbook_Model_Contact $contact */
60             foreach($memberRecords as $contact) {
61                 $str .= ($str===''?'':', ') . $contact->n_fn;
62                 if (isset($members[$contact->getId()])) {
63                     $str .= ' (';
64                     $first = true;
65                     foreach($members[$contact->getId()] as $listRole) {
66                         /** @var Addressbook_Model_ListRole $listRole */
67                         if (null !== ($listRole = $listRoles->getById($listRole))) {
68                             $str .= (true===$first?'':', ') . $listRole->name;
69                             $first = false;
70                         }
71                     }
72                     $str .= ')';
73                 }
74             }
75
76             $record->memberroles = $str;
77         }
78     }
79
80     /**
81      * resolve records and prepare for export (set user timezone, ...)
82      *
83      * @param Tinebase_Record_RecordSet $_records
84      */
85     protected function _listDocResolveRecords(Tinebase_Record_RecordSet $_records)
86     {
87         $memberRolesBackend = Addressbook_Controller_List::getInstance()->getMemberRolesBackend();
88
89         /** @var Addressbook_Model_List $record */
90         foreach ($_records as $record) {
91             $filter = new Addressbook_Model_ListMemberRoleFilter(array(
92                 array('field' => 'list_id',      'operator' => 'equals', 'value' => $record->getId())
93             ));
94
95             $members = array();
96             $listRoles = array();
97             /** @var Addressbook_Model_ListMemberRole $listMemberRole */
98             foreach($memberRolesBackend->search($filter) as $listMemberRole)
99             {
100                 if (!isset($members[$listMemberRole->contact_id])) {
101                     $members[$listMemberRole->contact_id] = array();
102                 }
103                 $members[$listMemberRole->contact_id][] = $listMemberRole->list_role_id;
104                 $listRoles[$listMemberRole->list_role_id] = true;
105             }
106
107             if (count($listRoles) > 0) {
108                 $listRolesResultSet = Addressbook_Controller_ListRole::getInstance()->getMultiple(array_keys($listRoles));
109             }
110
111             $memberRecords = array();
112             if (is_array($record->members)) {
113                 $memberRecords = Addressbook_Controller_Contact::getInstance()->getMultiple($record->members);
114             }
115             $newMembers = array();
116
117             /** @var Addressbook_Model_Contact $contact */
118             foreach($memberRecords as $contact) {
119                 $newMembers[$contact->getId()] = $contact->toArray();
120                 if (isset($members[$contact->getId()])) {
121                     $str = '';
122                     $first = true;
123                     foreach($members[$contact->getId()] as $listRoleId) {
124                         /** @var Addressbook_Model_ListRole $listRole */
125                         if (null !== ($listRole = $listRolesResultSet->getById($listRoleId))) {
126                             $str .= (true===$first?'':', ') . $listRole->name;
127                             $first = false;
128                             if (true === $listRoles[$listRoleId]) {
129                                 $listRoles[$listRoleId] = array(
130                                     'name'      => $listRole->name,
131                                     'members'   => $contact->n_fn
132                                 );
133                             } else {
134                                 $listRoles[$listRoleId]['members'] .= ', ' . $contact->n_fn;
135                             }
136                         }
137                     }
138                     $newMembers[$contact->getId()]['memberroles'] = $str;
139                 } else {
140                     $newMembers[$contact->getId()]['memberroles'] = '';
141                 }
142             }
143
144             $record->members = $newMembers;
145             $record->memberroles = $listRoles;
146         }
147     }
148 }