[7702] | 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 | */
|
---|
| 9 | if(!defined('ROOTPATH'))
|
---|
| 10 | define('ROOTPATH', dirname(__FILE__).'/../..');
|
---|
| 11 |
|
---|
| 12 | require_once ROOTPATH.'/api/controller.php';
|
---|
| 13 | require_once ROOTPATH.'/modules/calendar/constants.php';
|
---|
| 14 | require_once ROOTPATH.'/modules/calendar/interceptors/DBMapping.php';
|
---|
| 15 |
|
---|
| 16 | use prototype\api\Config as Config;
|
---|
| 17 |
|
---|
| 18 | class 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", '
|
---|
[7797] | 33 | .'count(calendar_task_to_activity_object.id) as "tasks", '
|
---|
[7702] | 34 | .'calendar_object.tzid as "timezone" ,calendar_to_calendar_object.calendar_id as '
|
---|
[7797] | 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 '
|
---|
[7702] | 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) '
|
---|
[7955] | 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';
|
---|
[7702] | 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", '
|
---|
[7797] | 86 | .'count(calendar_task_to_activity_object.id) as "tasks", '
|
---|
[7702] | 87 | .'calendar_object.tzid as "timezone" ,calendar_to_calendar_object.calendar_id as '
|
---|
[7797] | 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 '
|
---|
[7702] | 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 |
|
---|
[7955] | 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 |
|
---|
[7702] | 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 |
|
---|
[8006] | 115 | $mySig = Controller::find(array('concept' => 'calendarSignature') , array('calendar') , array('filter' => array( 'AND' , array('=' , 'type' , '0' ) , array( '=' , 'user' ,Config::me('uidNumber') ) , array('=' , 'isOwner' , '0' ))));
|
---|
[7990] | 116 |
|
---|
[8006] | 117 | $signedCalendars = array();
|
---|
[8010] | 118 | if(is_array($mySig))
|
---|
| 119 | foreach($mySig as $v)
|
---|
| 120 | {
|
---|
| 121 | $tmp = Controller::find(array('concept' => 'calendarToPermission') , array('acl' ,'owner') , array('filter' => array( 'AND' ,array( '=' , 'calendar' , $v['calendar'] ) , array( '=' , 'user' , Config::me('uidNumber') ) )));
|
---|
| 122 | $signedCalendars[$v['calendar']] = $tmp[0];
|
---|
| 123 | }
|
---|
[7990] | 124 |
|
---|
[7702] | 125 | $date = new DateTime('now', new DateTimeZone('UTC'));
|
---|
| 126 | $DayLigth = array();
|
---|
| 127 |
|
---|
| 128 | foreach ($result as $i => $v) {
|
---|
| 129 |
|
---|
| 130 | $currentTimezone = (isset($v['calendar']) && isset($timezones[$v['calendar']])) ? $timezones[$v['calendar']] : $v['timezone'];
|
---|
| 131 |
|
---|
| 132 | $date->setTimestamp((int) ($v['startTime'] / 1000));
|
---|
| 133 | $date->setTimezone( new DateTimeZone( $v['timezone'] ));
|
---|
| 134 | $DayLigth['event']['startTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
|
---|
| 135 |
|
---|
| 136 | $date->setTimezone( new DateTimeZone($currentTimezone));
|
---|
| 137 | $DayLigth['calendar']['startTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
|
---|
| 138 |
|
---|
| 139 | $date->setTimestamp((int) ($v['endTime'] / 1000));
|
---|
| 140 | $date->setTimezone( new DateTimeZone($currentTimezone));
|
---|
| 141 | $DayLigth['event']['endTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
|
---|
| 142 |
|
---|
| 143 | if(isset($v['due']) && $v['due'] != '0'){
|
---|
| 144 | $date->setTimestamp((int) ($v['due'] / 1000));
|
---|
| 145 | $DayLigth['event']['due'] = ($date->getTimestamp() + $date->getOffset()).'000';
|
---|
| 146 | }else{
|
---|
| 147 | $DayLigth['event']['due'] = $v['due'];
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | $date->setTimezone( new DateTimeZone($currentTimezone));
|
---|
| 151 | $DayLigth['calendar']['endTime'] = ($date->getTimestamp() + $date->getOffset()).'000';
|
---|
| 152 |
|
---|
| 153 | $result[$i]['DayLigth'] = $DayLigth;
|
---|
| 154 |
|
---|
[7967] | 155 |
|
---|
[7702] | 156 | if(isset( $v['occurrences'] ) && count( $v['occurrences'] ) > 0){
|
---|
| 157 |
|
---|
[7967] | 158 | $date->setTimestamp((int) ($v['startTime'] / 1000));
|
---|
[8049] | 159 | $date->setTimezone( new DateTimeZone( $currentTimezone ));
|
---|
[7967] | 160 |
|
---|
[7702] | 161 | foreach( $result[$i]['occurrences'] as &$o){
|
---|
| 162 |
|
---|
[7967] | 163 | $o = ((int) ($o / 1000) + $date->getOffset()).'000';
|
---|
[7702] | 164 |
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[8006] | 168 | $attend = (isset($signedCalendars[$result[$i]['calendar']])) ?
|
---|
| 169 | Controller::read(array('concept' => 'participant'), null, array('filter' => array('AND', array('=','schedulable',$v['id']), array('=','user', $signedCalendars[$result[$i]['calendar']]['owner'] ) ))):
|
---|
| 170 | Controller::read(array('concept' => 'participant'), null, array('filter' => array('AND', array('=','schedulable',$v['id']), array('=','user', Config::me('uidNumber')) )));
|
---|
[7982] | 171 |
|
---|
[8006] | 172 | $result[$i]['unanswered'] = 0;
|
---|
| 173 |
|
---|
[7702] | 174 | if(count($attend) > 0 && !empty($attend)){
|
---|
[8006] | 175 | if(array_key_exists(0, $attend))
|
---|
| 176 | $attend = $attend[0];
|
---|
[7982] | 177 |
|
---|
[8006] | 178 | if(isset($signedCalendars[$result[$i]['calendar']])) //Caso agenda compartilhada verificar tmb se tem compartilhamento de escrita
|
---|
| 179 | $result[$i]['editable'] = (strpos($signedCalendars[$result[$i]['calendar']]['acl'],"w") >=0 && (strstr($attend['acl'],"w") || strstr($attend['acl'],"o") || $attend['isOrganizer'] == '1') ) ? 1 : 0;
|
---|
| 180 | else
|
---|
| 181 | $result[$i]['editable'] = (strstr($attend['acl'],"w") || strstr($attend['acl'],"o") || $attend['isOrganizer'] == '1') ? 1 : 0;
|
---|
| 182 |
|
---|
| 183 | if($attend['status'] == STATUS_UNANSWERED && !isset($signedCalendars[$result[$i]['calendar']]) )
|
---|
| 184 | $result[$i]['unanswered'] = 1;
|
---|
[7702] | 185 |
|
---|
| 186 | }else{
|
---|
| 187 |
|
---|
| 188 | $result[$i]['editable'] = $v['type'] == '2' ? 0 : 2;
|
---|
| 189 |
|
---|
| 190 | }
|
---|
[7797] | 191 | if( $v['type'] == 2 && $v['tasks'] > 0)
|
---|
| 192 | $result[$i]['type'] = 3;
|
---|
[7702] | 193 | }
|
---|
| 194 |
|
---|
| 195 | return $this->toUtf8( $result );
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | function srtToUtf8($data) {
|
---|
| 199 | return mb_convert_encoding($data, 'UTF-8', 'UTF-8 , ISO-8859-1');
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | function toUtf8($data) {
|
---|
| 203 | if (is_array($data)) {
|
---|
| 204 | $return = array();
|
---|
| 205 | foreach ($data as $i => $v)
|
---|
| 206 | $return[$this->srtToUtf8($i)] = (is_array($v)) ? $this->toUtf8($v) : $this->srtToUtf8($v);
|
---|
| 207 |
|
---|
| 208 | return $return;
|
---|
| 209 | }else
|
---|
| 210 | return $this->srtToUtf8($data);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | $params = $_GET;
|
---|
| 216 | $schedule = new Schedule();
|
---|
| 217 |
|
---|
| 218 | if(isset( $params['rangeStart'] ))
|
---|
| 219 | $events = $schedule->findEventsRange( $params['rangeStart'], $params['rangeEnd'], $params['calendar'], $params['timezones'] );
|
---|
| 220 | else
|
---|
| 221 | $events = $schedule->findEventsSearch( $params['summary'], $params['description'], $params['calendar'], $params['timezones'], $params['limit'], $params['offset'] );
|
---|
| 222 |
|
---|
| 223 | echo json_encode( $events );
|
---|
[7805] | 224 | ?>
|
---|