Merge branch '2014.11-develop' into 2014.09
[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
72             ? $_events : new Tinebase_Record_RecordSet('Calendar_Model_Event', array($_events));
73         
74         self::resolveMultipleIdFields($events, array(
75             'Addressbook_Model_Contact' => array(
76                 'options' => array('ignoreAcl' => TRUE),
77                 'fields'  => array('organizer'),
78             )
79         ));
80     }
81     
82     /**
83      * converts Tinebase_Record_RecordSet to external format
84      * 
85      * @param Tinebase_Record_RecordSet         $_records
86      * @param Tinebase_Model_Filter_FilterGroup $_filter
87      * @param Tinebase_Model_Pagination         $_pagination
88      *
89      * @return mixed
90      */
91     public function fromTine20RecordSet(Tinebase_Record_RecordSet $_records = NULL, $_filter = NULL, $_pagination = NULL)
92     {
93         if (count($_records) == 0) {
94             return array();
95         }
96
97         Tinebase_Notes::getInstance()->getMultipleNotesOfRecords($_records);
98         Tinebase_Tags::getInstance()->getMultipleTagsOfRecords($_records);
99         if (Setup_Controller::getInstance()->isFilesystemAvailable()) {
100             Tinebase_FileSystem_RecordAttachments::getInstance()->getMultipleAttachmentsOfRecords($_records);
101         }
102         
103         Calendar_Model_Attender::resolveAttendee($_records->attendee, TRUE, $_records);
104         Calendar_Convert_Event_Json::resolveRrule($_records);
105         Calendar_Controller_Event::getInstance()->getAlarms($_records);
106         
107         self::resolveMultipleIdFields($_records, array(
108             'Addressbook_Model_Contact' => array(
109                 'options' => array('ignoreAcl' => TRUE),
110                 'fields'  => array('organizer'),
111             ),
112             'recursive' => array('attachments' => 'Tinebase_Model_Tree_Node')
113         ));
114         
115         Calendar_Model_Rrule::mergeAndRemoveNonMatchingRecurrences($_records, $_filter);
116         $_records->sortByPagination($_pagination);
117         
118         Tinebase_Frontend_Json_Abstract::resolveContainersAndTags($_records, array('container_id'));
119
120         $_records->setTimezone(Tinebase_Core::getUserTimezone());
121         $_records->convertDates = true;
122
123         $eventsData = $_records->toArray();
124         
125         foreach ($eventsData as $idx => $eventData) {
126             if (! (isset($eventData[Tinebase_Model_Grants::GRANT_READ]) || 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 }