3bfeb0b90c193e6ebacb3bde07ce49dc05ac8c2f
[tine20] / tine20 / Calendar / Convert / Event / Json.php
1 <?php
2 /**
3  * convert functions for records from/to json (array) format
4  * 
5  * @package     Calendar
6  * @subpackage  Convert
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  * @copyright   Copyright (c) 2011 Metaways Infosystems GmbH (http://www.metaways.de)
10  */
11
12 /**
13  * convert functions for records from/to json (array) format
14  *
15  * @package     Calendar
16  * @subpackage  Convert
17  */
18 class Calendar_Convert_Event_Json extends Tinebase_Convert_Json
19 {
20     /**
21     * converts Tinebase_Record_Abstract to external format
22     *
23     * @param  Tinebase_Record_Abstract $_record
24     * @return mixed
25     */
26     public function fromTine20Model(Tinebase_Record_Abstract $_record)
27     {
28         self::resolveRelatedData($_record);
29         return parent::fromTine20Model($_record);
30     }
31     
32     /**
33      * resolve related event data: attendee, rrule and organizer
34      * 
35      * @param Calendar_Model_Event $_record
36      */
37     static public function resolveRelatedData($_record)
38     {
39         if (! $_record instanceof Calendar_Model_Event) {
40             return;
41         }
42         
43         Calendar_Model_Attender::resolveAttendee($_record->attendee, TRUE, $_record);
44         self::resolveRrule($_record);
45         self::resolveOrganizer($_record);
46     }
47     
48     /**
49     * resolves rrule of given event(s)
50     *
51     * @param Tinebase_Record_RecordSet|Calendar_Model_Event $_events
52     */
53     static public function resolveRrule($_events)
54     {
55         $events = $_events instanceof Tinebase_Record_RecordSet ? $_events : array($_events);
56     
57         foreach ($events as $event) {
58             if ($event->rrule) {
59                 $event->rrule = Calendar_Model_Rrule::getRruleFromString($event->rrule);
60             }
61         }
62     }
63     
64     /**
65     * resolves organizer of given event
66     *
67     * @param Tinebase_Record_RecordSet|Calendar_Model_Event $_events
68     */
69     static public function resolveOrganizer($_events)
70     {
71         $events = $_events instanceof Tinebase_Record_RecordSet || is_array($_events) ? $_events : array($_events);
72     
73         $organizerIds = array();
74         foreach ($events as $event) {
75             if ($event->organizer) {
76                 $organizerIds[] = $event->organizer;
77             }
78         }
79     
80         $organizers = Addressbook_Controller_Contact::getInstance()->getMultiple(array_unique($organizerIds), TRUE);
81     
82         foreach ($events as $event) {
83             if ($event->organizer && is_scalar($event->organizer)) {
84                 $idx = $organizers->getIndexById($event->organizer);
85                 if ($idx !== FALSE) {
86                     $event->organizer = $organizers[$idx];
87                 }
88             }
89         }
90     }
91     
92     /**
93      * converts Tinebase_Record_RecordSet to external format
94      * 
95      * @param Tinebase_Record_RecordSet         $_records
96      * @param Tinebase_Model_Filter_FilterGroup $_filter
97      * @param Tinebase_Model_Pagination         $_pagination
98      *
99      * @return mixed
100      */
101     public function fromTine20RecordSet(Tinebase_Record_RecordSet $_records, $_filter = NULL, $_pagination = NULL)
102     {
103         if (count($_records) == 0) {
104             return array();
105         }
106
107         Tinebase_Notes::getInstance()->getMultipleNotesOfRecords($_records);
108         Tinebase_Tags::getInstance()->getMultipleTagsOfRecords($_records);
109         
110         Calendar_Model_Attender::resolveAttendee($_records->attendee, TRUE, $_records);
111         Calendar_Convert_Event_Json::resolveOrganizer($_records);
112         Calendar_Convert_Event_Json::resolveRrule($_records);
113         Calendar_Controller_Event::getInstance()->getAlarms($_records);
114         
115         Calendar_Model_Rrule::mergeAndRemoveNonMatchingRecurrences($_records, $_filter);
116         $_records->sortByPagination($_pagination);
117         
118         Tinebase_Frontend_Json_Abstract::resolveContainerTagsUsers($_records, array('container_id'));
119
120         $_records->setTimezone(Tinebase_Core::get(Tinebase_Core::USERTIMEZONE));
121         $_records->convertDates = true;
122
123         $eventsData = $_records->toArray();
124         
125         foreach ($eventsData as $idx => $eventData) {
126             if (! array_key_exists(Tinebase_Model_Grants::GRANT_READ, $eventData) || ! $eventData[Tinebase_Model_Grants::GRANT_READ]) {
127                 $eventsData[$idx] = array_intersect_key($eventsData[$idx], array_flip(array(
128                     'id', 
129                     'dtstart', 
130                     'dtend',
131                     'transp',
132                     'is_all_day_event',
133                 )));
134             }
135         }
136
137         return $eventsData;
138     }
139 }