#5024: allow to attach external files to records
[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 ? $_events : new Tinebase_Record_RecordSet('Calendar_Model_Event', array($_events));
72         self::resolveMultipleIdFields($events, array(
73             'Addressbook_Model_Contact' => array(
74                 'options' => array('ignoreAcl' => TRUE),
75                 'fields'  => array('organizer'),
76             )
77         ));
78     }
79     
80     /**
81      * converts Tinebase_Record_RecordSet to external format
82      * 
83      * @param Tinebase_Record_RecordSet         $_records
84      * @param Tinebase_Model_Filter_FilterGroup $_filter
85      * @param Tinebase_Model_Pagination         $_pagination
86      *
87      * @return mixed
88      */
89     public function fromTine20RecordSet(Tinebase_Record_RecordSet $_records, $_filter = NULL, $_pagination = NULL)
90     {
91         if (count($_records) == 0) {
92             return array();
93         }
94
95         Tinebase_Notes::getInstance()->getMultipleNotesOfRecords($_records);
96         Tinebase_Tags::getInstance()->getMultipleTagsOfRecords($_records);
97         Tinebase_FileSystem_RecordAttachments::getInstance()->getMultipleAttachmentsOfRecords($_records);
98         
99         Calendar_Model_Attender::resolveAttendee($_records->attendee, TRUE, $_records);
100         Calendar_Convert_Event_Json::resolveRrule($_records);
101         Calendar_Controller_Event::getInstance()->getAlarms($_records);
102         
103         self::resolveMultipleIdFields($_records, array(
104             'Addressbook_Model_Contact' => array(
105                 'options' => array('ignoreAcl' => TRUE),
106                 'fields'  => array('organizer'),
107             ),
108             'recursive' => array('attachments' => 'Tinebase_Model_Tree_Node')
109         ));
110         
111         Calendar_Model_Rrule::mergeAndRemoveNonMatchingRecurrences($_records, $_filter);
112         $_records->sortByPagination($_pagination);
113         
114         Tinebase_Frontend_Json_Abstract::resolveContainerTagsUsers($_records, array('container_id'));
115
116         $_records->setTimezone(Tinebase_Core::get(Tinebase_Core::USERTIMEZONE));
117         $_records->convertDates = true;
118
119         $eventsData = $_records->toArray();
120         
121         foreach ($eventsData as $idx => $eventData) {
122             if (! array_key_exists(Tinebase_Model_Grants::GRANT_READ, $eventData) || ! $eventData[Tinebase_Model_Grants::GRANT_READ]) {
123                 $eventsData[$idx] = array_intersect_key($eventsData[$idx], array_flip(array(
124                     'id',
125                     'dtstart',
126                     'dtend',
127                     'transp',
128                     'is_all_day_event',
129                 )));
130             }
131         }
132
133         return $eventsData;
134     }
135 }