source: trunk/prototype/modules/calendar/alarms.php @ 7696

Revision 7696, 3.0 KB checked in by cristiano, 7 years ago (diff)

Ticket #3260 - Requisição de alarmes de eventos custosa quando existem muitos eventos

Line 
1<?php
2
3if(!defined('ROOTPATH'))
4    define('ROOTPATH', dirname(__FILE__).'/../..');
5
6require_once ROOTPATH.'/api/controller.php';
7require_once ROOTPATH.'/modules/calendar/constants.php';
8require_once ROOTPATH.'/api/parseTPL.php';
9
10use prototype\api\Config as Config;
11
12$target = (gmdate('U') - 300 ).'000';
13
14$parts = Controller::service('PostgreSQL')->execSql('SELECT part.user_info_id as "user", co.id as "schedulable", co.type_id as "type", co.allDay as "allDay" ,co.dtend as "endTime", co.dtstart as "startTime", co.summary as "summary", co.tzid as "timezone", co.location as "location", al.id as "id" FROM calendar_object as co INNER JOIN calendar_alarm al ON co.id = al.object_id JOIN calendar_participant part  ON part.id = al.participant_id LEFT JOIN calendar_repeat rep ON  rep.object_id = co.id  LEFT JOIN calendar_repeat_occurrence occ ON occ.repeat_id = rep.id WHERE ( al.action_id = \''.ALARM_MAIL.'\' AND al.sent = \'0\' AND CASE WHEN occ.occurrence > 0 THEN occ.occurrence - al.alarm_offset ELSE co.dtstart - al.alarm_offset END BETWEEN \''.$target.'\' AND \''.($target + 360000).'\') ');
15
16if(!is_array($parts))
17  return;
18
19$ids = array();
20
21foreach ($parts as $i => $part)
22{
23        ///Montando lista de participantes
24
25        $users = Controller::find( array( 'concept' => 'participant' ) , array( 'user', 'id', 'isExternal' ) ,array('filter' => array ('=', 'schedulable' , $part['schedulable'] ), 'deepness' => 1 ) );
26
27        $attList = array();
28
29        foreach( $users as $user )
30        {
31            if( $part['user'] === $user['user']['id'] )
32                $part['mail'] = $user['user']['mail'];
33
34            $attList[] = $user['user']['name'];
35        }
36
37        $timezone = new DateTimeZone('UTC');
38        $sTime = new DateTime('@' . (int) ($part['startTime'] / 1000), $timezone);
39        $eTime = new DateTime('@' . (int) ($part['endTime'] / 1000), $timezone);
40
41        $timezone = $part['timezone'];
42        $sTime->setTimezone(new DateTimeZone($part['timezone']));
43        $eTime->setTimezone(new DateTimeZone($part['timezone']));
44       
45        $data = array('startDate' =>  date_format( $sTime , 'd/m/Y') ,
46                      'startTime' =>  $part['allDay'] ? '' : date_format( $sTime , 'H:i'),
47                      'endDate' =>  date_format( $eTime , 'd/m/Y') ,
48                      'endTime' =>  $part['allDay'] ? '' : date_format( $eTime , 'H:i'),
49                      'eventTitle' =>  $part['summary'],
50                      'eventLocation' =>  $part['location'],
51                      'timezone' => $timezone,
52                      'participants' =>  '<UL> <LI> '.implode( '<LI></LI> ', $attList ).'</LI> </UL>');
53
54        Controller::create( array( 'service' => 'SMTP' ), array( 'body' => parseTPL::load_tpl( $data, ROOTPATH.'/modules/calendar/templates/'. ($parts['type'] == '1' ? 'notify_alarm_body.tpl' : 'notify_alarm_body_task.tpl')),
55                                                                  'isHtml' => true,
56                                                                  'subject' => 'Alarme de Calendario',
57                                                                  'from' => $part['mail'],
58                                                                  'to' => $part['mail'] ) );
59
60        Config::regSet('noAlarm', TRUE); //Evita o envio de notificação ?????
61        $ids[] = $part['id'];
62}
63
64if( !empty( $ids ) )
65    Controller::update( array( 'concept' => 'alarm' ) , array('sent' => '1'), array('filter' => array( 'IN', 'id', $ids ) ));
66
67?>
Note: See TracBrowser for help on using the repository browser.