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

Revision 7805, 9.2 KB checked in by douglas, 7 years ago (diff)

Ticket #3328 - Lista de eventos e tarefas do ExpressoCalendar? não está ordenada adequadamente

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)) group by calendar_object.id, calendar_to_calendar_object.calendar_id ORDER BY calendar_object.dtstart';
57
58        $params = Controller::service('PostgreSQL')->execResultSql($sql.$where);
59        $params = array_merge($params, $occ);
60
61        return $this->normalizeEvents( $params, $timezones );
62    }
63
64    function findEventsSearch( $summary, $description, $calendars, $timezones, $limit, $offset ){
65
66        $sql = ' SELECT calendar_object.id as id ,calendar_object.cal_uid as "uid", calendar_object.type_id as "type", '
67            .'calendar_object.dtstart as "startTime", calendar_object.summary as "summary", '
68            .'calendar_object.description as "description", calendar_object.dtend as "endTime" , '
69            .'calendar_object.priority as "priority", calendar_object.due as "due", '
70            .'calendar_object.percentage as "percentage", calendar_object.status as "status", '
71            .'calendar_object.location as "location", calendar_object.allday as "allDay", '
72            .'calendar_object.transp as "transparent", calendar_object.class_id as "class", '
73            .'calendar_object.repeat as "repeat", calendar_object.range_start as "rangeStart", '
74            .'calendar_object.range_end as "rangeEnd", calendar_object.last_update as "lastUpdate", '
75            .'calendar_object.dtstamp as "dtstamp", calendar_object.sequence as "sequence", '
76            .'count(calendar_task_to_activity_object.id) as "tasks", '
77            .'calendar_object.tzid as "timezone" ,calendar_to_calendar_object.calendar_id as '
78            .'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 '
79            .'WHERE ( calendar_to_calendar_object.calendar_id IN (\'' . implode('\',\'', $calendars) . '\')) '
80            .'AND calendar_to_calendar_object.calendar_object_id = calendar_object.id '
81            .'AND calendar_object.id NOT IN(select calendar_object_task_id from calendar_task_to_activity_object where owner = \'' . Config::me('uidNumber') . '\') ';
82
83
84            $where = 'AND (((upper("summary") like upper(\'%'.$summary.'%\') OR upper("description") like upper(\'%'.$description.'%\')))) group by calendar_object.id, calendar_to_calendar_object.calendar_id ORDER BY dtstart LIMIT '.$limit.'  OFFSET '.$offset.' ';
85            $params = Controller::service('PostgreSQL')->execResultSql($sql.$where);
86
87
88
89        return $this->normalizeEvents( $params, $timezones );
90    }
91
92    function normalizeEvents( &$result, $timezones ){
93
94        $date = new DateTime('now', new DateTimeZone('UTC'));
95        $DayLigth = array();
96
97        foreach ($result as $i => $v) {
98
99            $currentTimezone = (isset($v['calendar']) && isset($timezones[$v['calendar']])) ? $timezones[$v['calendar']] : $v['timezone'];
100
101            $date->setTimestamp((int) ($v['startTime'] / 1000));
102            $date->setTimezone( new DateTimeZone( $v['timezone'] ));
103            $DayLigth['event']['startTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
104
105            $date->setTimezone( new DateTimeZone($currentTimezone));
106            $DayLigth['calendar']['startTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
107
108            $date->setTimestamp((int) ($v['endTime'] / 1000));
109            $date->setTimezone( new DateTimeZone($currentTimezone));
110            $DayLigth['event']['endTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
111
112            if(isset($v['due']) && $v['due'] != '0'){
113                $date->setTimestamp((int) ($v['due'] / 1000));
114                $DayLigth['event']['due'] = ($date->getTimestamp() + $date->getOffset()).'000';
115            }else{
116                $DayLigth['event']['due'] = $v['due'];
117            }
118
119            $date->setTimezone( new DateTimeZone($currentTimezone));
120            $DayLigth['calendar']['endTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
121
122            $result[$i]['DayLigth'] = $DayLigth;
123
124            if(isset( $v['occurrences'] ) && count( $v['occurrences'] ) > 0){
125
126                foreach( $result[$i]['occurrences'] as &$o){
127
128                    $date->setTimestamp((int) ($o / 1000));
129                    $o = ($date->getTimestamp() + $date->getOffset()).'000';
130
131                }
132            }
133
134            $attend = Controller::read(array('concept' => 'participant'), null, array('filter' => array('AND', array('=','schedulable',$v['id']), array('=','user', Config::me('uidNumber'))  )));
135
136            if(count($attend) > 0 && !empty($attend)){
137
138                if(array_key_exists(0, $attend))
139                    $attend = $attend[0];
140
141                $result[$i]['editable'] = (strstr($attend['acl'],"w") || strstr($attend['acl'],"o") || $attend['isOrganizer'] == '1') ? 1 : 0;
142
143            }else{
144
145               $result[$i]['editable'] = $v['type'] == '2' ? 0 : 2;
146
147            }
148            if( $v['type'] == 2 && $v['tasks'] > 0)
149                $result[$i]['type'] = 3;
150        }
151
152        return $this->toUtf8( $result );
153    }
154
155    function srtToUtf8($data) {
156        return mb_convert_encoding($data, 'UTF-8', 'UTF-8 , ISO-8859-1');
157    }
158
159    function toUtf8($data) {
160        if (is_array($data)) {
161            $return = array();
162            foreach ($data as $i => $v)
163                $return[$this->srtToUtf8($i)] = (is_array($v)) ? $this->toUtf8($v) : $this->srtToUtf8($v);
164
165            return $return;
166        }else
167            return $this->srtToUtf8($data);
168    }
169
170}
171
172$params = $_GET;
173$schedule = new Schedule();
174
175if(isset( $params['rangeStart'] ))
176    $events = $schedule->findEventsRange( $params['rangeStart'], $params['rangeEnd'], $params['calendar'], $params['timezones'] );
177else
178    $events = $schedule->findEventsSearch( $params['summary'], $params['description'], $params['calendar'], $params['timezones'], $params['limit'], $params['offset'] );
179
180echo json_encode( $events );
181?>
Note: See TracBrowser for help on using the repository browser.