source: branches/2.5/prototype/modules/calendar/schedules.php @ 7955

Revision 7955, 10.7 KB checked in by douglas, 7 years ago (diff)

Ticket #3355 - ExpressoCalendar? não funciona a contento em diferentes banco de dados

Line 
1<?php
2/**
3 * Created by JetBrains PhpStorm.
4 * User: Adriano
5 * Date: 28/12/12
6 * Time: 18:36
7 * To change this template use File | Settings | File Templates.
8 */
9if(!defined('ROOTPATH'))
10    define('ROOTPATH', dirname(__FILE__).'/../..');
11
12require_once ROOTPATH.'/api/controller.php';
13require_once ROOTPATH.'/modules/calendar/constants.php';
14require_once ROOTPATH.'/modules/calendar/interceptors/DBMapping.php';
15
16use prototype\api\Config as Config;
17
18class Schedule{
19
20    function findEventsRange( $start, $end, $calendars, $timezones ){
21
22        $sql =
23            ' SELECT calendar_object.id as id ,calendar_object.cal_uid as "uid", calendar_object.type_id as "type", '
24                .'calendar_object.dtstart as "startTime", calendar_object.summary as "summary", '
25                .'calendar_object.description as "description", calendar_object.dtend as "endTime" , '
26                .'calendar_object.priority as "priority", calendar_object.due as "due", '
27                .'calendar_object.percentage as "percentage", calendar_object.status as "status", '
28                .'calendar_object.location as "location", calendar_object.allday as "allDay", '
29                .'calendar_object.transp as "transparent", calendar_object.class_id as "class", '
30                .'calendar_object.repeat as "repeat", calendar_object.range_start as "rangeStart", '
31                .'calendar_object.range_end as "rangeEnd", calendar_object.last_update as "lastUpdate", '
32                .'calendar_object.dtstamp as "dtstamp", calendar_object.sequence as "sequence", '
33                .'count(calendar_task_to_activity_object.id) as "tasks", '
34                .'calendar_object.tzid as "timezone" ,calendar_to_calendar_object.calendar_id as '
35                .'calendar FROM calendar_object left join calendar_task_to_activity_object on ( calendar_object.id = calendar_task_to_activity_object.calendar_object_activity_id  ), calendar_to_calendar_object '
36                .'WHERE ( calendar_to_calendar_object.calendar_id IN (\'' . implode('\',\'', $calendars) . '\')) '
37                .'AND calendar_to_calendar_object.calendar_object_id = calendar_object.id '
38                .'AND calendar_object.id NOT IN(select calendar_object_task_id from calendar_task_to_activity_object where owner = \'' . Config::me('uidNumber') . '\') ';
39
40        $ids = array();
41        $occ = array();
42
43        if ($occurrences = DBMapping::checkOccurrences($start, $end, $calendars))
44            foreach ($occurrences as $id => $occurrence) {
45                $ids[] = $id;
46                $occ[] = $occurrence;
47            }
48
49        $where =
50            ' AND ((range_end >= \'' . $start . '\' AND range_end <= \'' . $end . '\') OR '
51                .'(range_start >= \'' . $start . '\' AND range_start <= \'' . $end . '\') OR '
52                .'(range_start <= \'' . $start . '\' AND range_end >= \'' . $end . '\')) '
53                .(!empty($ids) ? ' ' .'AND calendar_object.id NOT IN (\'' . implode('\',\'', $ids) . '\') ' : ' ')
54                .'AND calendar_object.dtstart NOT IN (SELECT calendar_repeat_occurrence.occurrence from calendar_repeat_occurrence, '
55                .'calendar_repeat where (calendar_repeat_occurrence.repeat_id = calendar_repeat.id) '
56                .'AND (calendar_repeat.object_id = calendar_object.id)) '
57                .'group by
58                    calendar_object.id, calendar_object.cal_uid, calendar_object.type_id,
59                    calendar_object.dtstart, calendar_object.summary, calendar_object.description,
60                    calendar_object.dtend, calendar_object.priority, calendar_object.due, calendar_object.percentage,
61                    calendar_object.status, calendar_object.location, calendar_object.allday, calendar_object.transp,
62                    calendar_object.class_id, calendar_object.repeat, calendar_object.range_start, calendar_object.range_end,
63                    calendar_object.last_update, calendar_object.dtstamp, calendar_object.sequence,
64                    calendar_object.tzid, calendar_to_calendar_object.calendar_id
65                ORDER BY
66                    calendar_object.dtstart';
67
68        $params = Controller::service('PostgreSQL')->execResultSql($sql.$where);
69        $params = array_merge($params, $occ);
70
71        return $this->normalizeEvents( $params, $timezones );
72    }
73
74    function findEventsSearch( $summary, $description, $calendars, $timezones, $limit, $offset ){
75
76        $sql = ' SELECT calendar_object.id as id ,calendar_object.cal_uid as "uid", calendar_object.type_id as "type", '
77            .'calendar_object.dtstart as "startTime", calendar_object.summary as "summary", '
78            .'calendar_object.description as "description", calendar_object.dtend as "endTime" , '
79            .'calendar_object.priority as "priority", calendar_object.due as "due", '
80            .'calendar_object.percentage as "percentage", calendar_object.status as "status", '
81            .'calendar_object.location as "location", calendar_object.allday as "allDay", '
82            .'calendar_object.transp as "transparent", calendar_object.class_id as "class", '
83            .'calendar_object.repeat as "repeat", calendar_object.range_start as "rangeStart", '
84            .'calendar_object.range_end as "rangeEnd", calendar_object.last_update as "lastUpdate", '
85            .'calendar_object.dtstamp as "dtstamp", calendar_object.sequence as "sequence", '
86            .'count(calendar_task_to_activity_object.id) as "tasks", '
87            .'calendar_object.tzid as "timezone" ,calendar_to_calendar_object.calendar_id as '
88            .'calendar FROM calendar_object left join calendar_task_to_activity_object on ( calendar_object.id = calendar_task_to_activity_object.calendar_object_activity_id  ), calendar_to_calendar_object '
89            .'WHERE ( calendar_to_calendar_object.calendar_id IN (\'' . implode('\',\'', $calendars) . '\')) '
90            .'AND calendar_to_calendar_object.calendar_object_id = calendar_object.id '
91            .'AND calendar_object.id NOT IN(select calendar_object_task_id from calendar_task_to_activity_object where owner = \'' . Config::me('uidNumber') . '\') ';
92
93
94            $where = 'AND (((upper("summary") like upper(\'%'.$summary.'%\') OR upper("description") like upper(\'%'.$description.'%\'))))
95                group by
96                    calendar_object.id, calendar_object.cal_uid, calendar_object.type_id,
97                    calendar_object.dtstart, calendar_object.summary, calendar_object.description,
98                    calendar_object.dtend, calendar_object.priority, calendar_object.due, calendar_object.percentage,
99                    calendar_object.status, calendar_object.location, calendar_object.allday, calendar_object.transp,
100                    calendar_object.class_id, calendar_object.repeat, calendar_object.range_start, calendar_object.range_end,
101                    calendar_object.last_update, calendar_object.dtstamp, calendar_object.sequence,
102                    calendar_object.tzid, calendar_to_calendar_object.calendar_id
103                ORDER BY
104                    dtstart LIMIT '.$limit.'  OFFSET '.$offset.' ';
105 
106            $params = Controller::service('PostgreSQL')->execResultSql($sql.$where);
107
108
109
110        return $this->normalizeEvents( $params, $timezones );
111    }
112
113    function normalizeEvents( &$result, $timezones ){
114
115        $date = new DateTime('now', new DateTimeZone('UTC'));
116        $DayLigth = array();
117
118        foreach ($result as $i => $v) {
119
120            $currentTimezone = (isset($v['calendar']) && isset($timezones[$v['calendar']])) ? $timezones[$v['calendar']] : $v['timezone'];
121
122            $date->setTimestamp((int) ($v['startTime'] / 1000));
123            $date->setTimezone( new DateTimeZone( $v['timezone'] ));
124            $DayLigth['event']['startTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
125
126            $date->setTimezone( new DateTimeZone($currentTimezone));
127            $DayLigth['calendar']['startTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
128
129            $date->setTimestamp((int) ($v['endTime'] / 1000));
130            $date->setTimezone( new DateTimeZone($currentTimezone));
131            $DayLigth['event']['endTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
132
133            if(isset($v['due']) && $v['due'] != '0'){
134                $date->setTimestamp((int) ($v['due'] / 1000));
135                $DayLigth['event']['due'] = ($date->getTimestamp() + $date->getOffset()).'000';
136            }else{
137                $DayLigth['event']['due'] = $v['due'];
138            }
139
140            $date->setTimezone( new DateTimeZone($currentTimezone));
141            $DayLigth['calendar']['endTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
142
143            $result[$i]['DayLigth'] = $DayLigth;
144
145            if(isset( $v['occurrences'] ) && count( $v['occurrences'] ) > 0){
146
147                foreach( $result[$i]['occurrences'] as &$o){
148
149                    $date->setTimestamp((int) ($o / 1000));
150                    $o = ($date->getTimestamp() + $date->getOffset()).'000';
151
152                }
153            }
154
155            $attend = Controller::read(array('concept' => 'participant'), null, array('filter' => array('AND', array('=','schedulable',$v['id']), array('=','user', Config::me('uidNumber'))  )));
156
157            if(count($attend) > 0 && !empty($attend)){
158
159                if(array_key_exists(0, $attend))
160                    $attend = $attend[0];
161
162                $result[$i]['editable'] = (strstr($attend['acl'],"w") || strstr($attend['acl'],"o") || $attend['isOrganizer'] == '1') ? 1 : 0;
163
164            }else{
165
166               $result[$i]['editable'] = $v['type'] == '2' ? 0 : 2;
167
168            }
169            if( $v['type'] == 2 && $v['tasks'] > 0)
170                $result[$i]['type'] = 3;
171        }
172
173        return $this->toUtf8( $result );
174    }
175
176    function srtToUtf8($data) {
177        return mb_convert_encoding($data, 'UTF-8', 'UTF-8 , ISO-8859-1');
178    }
179
180    function toUtf8($data) {
181        if (is_array($data)) {
182            $return = array();
183            foreach ($data as $i => $v)
184                $return[$this->srtToUtf8($i)] = (is_array($v)) ? $this->toUtf8($v) : $this->srtToUtf8($v);
185
186            return $return;
187        }else
188            return $this->srtToUtf8($data);
189    }
190
191}
192
193$params = $_GET;
194$schedule = new Schedule();
195
196if(isset( $params['rangeStart'] ))
197    $events = $schedule->findEventsRange( $params['rangeStart'], $params['rangeEnd'], $params['calendar'], $params['timezones'] );
198else
199    $events = $schedule->findEventsSearch( $params['summary'], $params['description'], $params['calendar'], $params['timezones'], $params['limit'], $params['offset'] );
200
201echo json_encode( $events );
202?>
Note: See TracBrowser for help on using the repository browser.