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