1 | <?php |
---|
2 | |
---|
3 | if(!defined('ROOTPATH')) |
---|
4 | define('ROOTPATH', dirname(__FILE__).'/../..'); |
---|
5 | |
---|
6 | require_once ROOTPATH.'/api/controller.php'; |
---|
7 | require_once ROOTPATH.'/modules/calendar/constants.php'; |
---|
8 | |
---|
9 | $filter = array( 'AND', |
---|
10 | array( '=' , 'sent' , '0') , |
---|
11 | array( '=' , 'type' , ALARM_MAIL) , |
---|
12 | array( '>=' , 'rangeStart' ,(gmdate('U') - 300 ).'000') , |
---|
13 | array( '<=' , 'rangeEnd' , (gmdate('U') + 86400).'000') |
---|
14 | ); |
---|
15 | |
---|
16 | //Busca os Alarmes no Range |
---|
17 | $al = Controller::find( array('concept' => 'alarm'), false ,array( 'filter' => $filter)); |
---|
18 | |
---|
19 | if(is_array($al)) |
---|
20 | foreach ($al as $i => $v) |
---|
21 | { |
---|
22 | $ev = Controller::read( array( 'concept' => 'schedulable' , 'id' => $v['schedulable']) ) ; |
---|
23 | |
---|
24 | $unit = 0; |
---|
25 | switch ($v['unit']) { |
---|
26 | case 'm': $unit = 60; break; |
---|
27 | case 'H': $unit = 3600; break; |
---|
28 | case 'd': $unit = 86400; break; } |
---|
29 | |
---|
30 | |
---|
31 | if( strtotime($v['rangeEnd']) >= time() ) // Evento Recursivo ? |
---|
32 | { |
---|
33 | //TODO: Fazer Acontecer |
---|
34 | } |
---|
35 | else |
---|
36 | { |
---|
37 | if((((int)( $ev['startTime'] / 1000 )) - ( $v['time'] * $unit )) < gmmktime() ) //ja esta na hora de mandar o alarme ? |
---|
38 | { |
---|
39 | require_once (ROOTPATH.'/api/parseTPL.php' ); |
---|
40 | |
---|
41 | switch ($v['type']) { |
---|
42 | case 'mail': |
---|
43 | |
---|
44 | $part = Controller::read( array( 'concept' => 'participant' , 'id' => $v['participant']) ); |
---|
45 | $participants = Controller::find( array( 'concept' => 'participant' ) , false ,array('filter' => array ('=', 'schedulable' , $part['schedulable'] )) ); |
---|
46 | |
---|
47 | if($part['isExternal'] == 1) |
---|
48 | $user = Controller::read( array( 'concept' => 'user' , 'id' => $part['user'] , 'service' => 'PostgreSQL' ) ); |
---|
49 | else |
---|
50 | $user = Controller::read( array( 'concept' => 'user' , 'id' => $part['user'] ) ); |
---|
51 | |
---|
52 | ///Montando lista de participantes |
---|
53 | $attList = '<UL> '; |
---|
54 | $organizer = array(); |
---|
55 | foreach ($participants as $ii => $participant) |
---|
56 | { |
---|
57 | if($participant['isExternal'] == 1) |
---|
58 | $att[$ii] = Controller::read( array( 'concept' => 'user' , 'id' => $participant['user'] , 'service' => 'PostgreSQL' ) ); |
---|
59 | else |
---|
60 | $att[$ii] = Controller::read( array( 'concept' => 'user' , 'id' => $participant['user'] )); |
---|
61 | |
---|
62 | $attList .= '<LI> '.$att[$ii]['name'] .'</LI> '; |
---|
63 | |
---|
64 | } |
---|
65 | $attList .= '</UL>'; |
---|
66 | ///////////////////////// |
---|
67 | |
---|
68 | $timezone = new DateTimeZone('UTC'); |
---|
69 | $sTime = new DateTime( '@'.(int)($ev['startTime'] / 1000) , $timezone ); |
---|
70 | $eTime = new DateTime( '@'.(int)($ev['endTime'] / 1000) , $timezone ); |
---|
71 | if($ev['timezone']) |
---|
72 | { |
---|
73 | $sTime->setTimezone(new DateTimeZone($ev['timezone'])); |
---|
74 | $eTime->setTimezone(new DateTimeZone($ev['timezone'])); |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | $data = array('startDate' => date_format( $sTime , 'd/m/Y') , |
---|
79 | 'startTime' => date_format( $sTime , 'H:i') , |
---|
80 | 'endDate' => date_format( $eTime , 'd/m/Y') , |
---|
81 | 'endTime' => date_format( $eTime , 'H:i') , |
---|
82 | 'eventTitle' => $ev['summary'], |
---|
83 | 'eventLocation' => $ev['location'], |
---|
84 | 'timezone' => ($ev['timezone']) ? $ev['timezone'] : 'UTC' , |
---|
85 | 'participants' => $attList |
---|
86 | ); |
---|
87 | |
---|
88 | $mail['isHtml'] = true; |
---|
89 | $mail['body'] = parseTPL::load_tpl($data,ROOTPATH.'/modules/calendar/templates/notify_alarm_body.tpl'); |
---|
90 | |
---|
91 | $mail['subject'] = 'Alarme de Calendario'; |
---|
92 | $mail['from'] = $user['mail']; |
---|
93 | $mail['to'] = $user['mail']; |
---|
94 | |
---|
95 | |
---|
96 | Controller::create( array( 'service' => 'SMTP' ), $mail ); |
---|
97 | Config::regSet('noAlarm', TRUE); //Evita o envio de notificação |
---|
98 | Controller::update( array( 'concept' => 'alarm' , 'id' => $v['id']) , array('sent' => '1')); |
---|
99 | break; |
---|
100 | |
---|
101 | case 'sms': |
---|
102 | //Sem suporte atualmente |
---|
103 | break; |
---|
104 | |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | ?> |
---|