b35130250aeea3351392d14c4dd80b2c0c58f516
[tine20] / tine20 / Addressbook / Export / List / Xls.php
1 <?php
2 /**
3  * Addressbook List Xls generation class
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 /**
14  * Addressbook List Xls generation class
15  *
16  * @package     Addressbook
17  * @subpackage  Export
18  *
19  */
20 class Addressbook_Export_List_Xls extends Tinebase_Export_Xls
21 {
22     protected $_defaultExportname = 'adb_list_xls';
23
24     /**
25      * resolve records and prepare for export (set user timezone, ...)
26      *
27      * @param Tinebase_Record_RecordSet $_records
28      */
29     protected function _resolveRecords(Tinebase_Record_RecordSet $_records)
30     {
31         parent::_resolveRecords($_records);
32
33         /** @var Addressbook_Model_List $record */
34         foreach ($_records as $record) {
35             $memberRolesBackend = Addressbook_Controller_List::getInstance()->getMemberRolesBackend();
36             $filter = new Addressbook_Model_ListMemberRoleFilter(array(
37                 array('field' => 'list_id',      'operator' => 'equals', 'value' => $record->getId())
38             ));
39
40             $members = array();
41             $listRoles = array();
42             /** @var Addressbook_Model_ListMemberRole $listMemberRole */
43             foreach($memberRolesBackend->search($filter) as $listMemberRole)
44             {
45                 if (!isset($members[$listMemberRole->contact_id])) {
46                     $members[$listMemberRole->contact_id] = array();
47                 }
48                 $members[$listMemberRole->contact_id][] = $listMemberRole->list_role_id;
49                 $listRoles[$listMemberRole->list_role_id] = true;
50             }
51
52             if (count($listRoles) > 0) {
53                 $listRoles = Addressbook_Controller_ListRole::getInstance()->getMultiple(array_keys($listRoles));
54             }
55
56             $memberRecords = array();
57             if (is_array($record->members)) {
58                 $memberRecords = Addressbook_Controller_Contact::getInstance()->getMultiple($record->members);
59             }
60
61             $str = '';
62             /** @var Addressbook_Model_Contact $contact */
63             foreach($memberRecords as $contact) {
64                 $str .= ($str===''?'':', ') . $contact->n_fn;
65                 if (isset($members[$contact->getId()])) {
66                     $str .= ' (';
67                     $first = true;
68                     foreach($members[$contact->getId()] as $listRole) {
69                         /** @var Addressbook_Model_ListRole $listRole */
70                         if (null !== ($listRole = $listRoles->getById($listRole))) {
71                             $str .= (true===$first?'':', ') . $listRole->name;
72                             $first = false;
73                         }
74                     }
75                     $str .= ')';
76                 }
77             }
78
79             $record->memberroles = $str;
80         }
81     }
82 }