source: trunk/prototype/modules/calendar/schedules.php @ 7789

Revision 7789, 9.2 KB checked in by acoutinho, 9 years ago (diff)

Ticket #3359 - Inconsistencia com icones e telas de edicao no Expresso Calendar

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