source: trunk/prototype/modules/calendar/interceptors/Notifications.php @ 5437

Revision 5437, 22.3 KB checked in by cristiano, 12 years ago (diff)

Ticket #2434 - Atualização modulo agenda e API

RevLine 
[5341]1<?php
2require_once ROOTPATH.'/modules/calendar/constants.php';
3require_once ROOTPATH.'/modules/calendar/interceptors/Helpers.php';
[5399]4require_once ROOTPATH.'/plugins/icalcreator/iCalcreator.class.php';
5require_once ROOTPATH.'/api/parseTPL.php';
[5341]6 
7class Notifications extends Helpers
8{
9   
10    /**
11    * Analisa o commit do conceito participant e encaminha cada participant para seu devido metodo de notrificação
12    *
13    * @license    http://www.gnu.org/copyleft/gpl.html GPL
14    * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
15    * @sponsor    Caixa Econômica Federal
16    * @author     Cristiano Corrêa Schmidt
17    * @return     void
18    * @access     public
19    */
20    public function commitParticipant(&$uri , &$result , &$data , $original)
21    {   
22        if(Config::regGet('noAlarm') !== false) return; //Escapa notificações caso a flag de noAlarm esteja ativa.
[5437]23             
[5341]24        $organizers = array(); //Cache Organizadores
25        $schedulables = array(); //Cache Schedulables
26       
27        foreach ($data as $i => $concept)
28        {
29            if($concept['concept'] === 'participant')
30            {
31               if( $concept['method'] == 'create' )
32                   $created[] = $concept['id'];
33               else if( $concept['method'] == 'update' )
34                   $updated[] = $concept['id'];
35            }
36            else if($concept['concept'] === 'schedulable') //Caso exista schedulable no commit antecipa o carregamento do owner
37            {
38                $schedulables[ $concept['id'] ] = Controller::read( array('concept' => 'schedulable', 'id' => $concept['id']) , false , array('deepness' => '2'));
39                foreach ($schedulables[ $concept['id'] ]['participants'] as $i => $v) //salva em $organizers as informações do organizador     
40                    if(($v['isOrganizer'] === '1') && ($organizers[$concept['id']] = $v) ) break;
41            }
42        }
43       
44        if( isset( $created ) )
45        {
46           $psCreated =  Controller::find(array('concept' => 'participant'), false , array('deepness' => '1' , 'filter' => array('IN', 'id' , $created )));
47           foreach ($psCreated as $i => $pCreated)   
48           {   
49               if($pCreated['isOrganizer'] == '1') continue; //escapa organizador
50               $schedulable = isset( $schedulables[$pCreated['schedulable']] ) ? $schedulables[$pCreated['schedulable']] : Controller::read( array('concept' => 'schedulable', 'id' => $pCreated['schedulable']) , false , array('deepness' => '2'));
51               if(!self::futureEvent( $schedulable['startTime'] , $schedulable['rangeEnd'])) continue; //Escapa eventos do passado
52               
53               $organizer = isset( $organizers[$pCreated['schedulable']] ) ? $organizers[ $pCreated['schedulable'] ] : self::getOrganizer( $pCreated['schedulable'] );
54               
55               switch ($pCreated['status'])
56               {
57                   case STATUS_CONFIRMED:
58                        self::participantStatusChange( $pCreated['id'] , $schedulable , $organizer  , STATUS_ACCEPTED);
59                       break;
60                   case STATUS_UNANSWERED:
61                        self::participantCreated( $pCreated['id'] , $schedulable );
62                       break;
63               }
64           }
65        }
66       
67        if( isset( $updated ) )
68        {
69            $psUpdated  = Controller::find(array('concept' => 'participant'), false , array('deepness' => '1' , 'filter' => array('IN', 'id' , $updated )));
70            foreach ($psUpdated as $i => $pUpdated)   
71            {
72                if($pUpdated['isOrganizer'] == '1') continue; //escapa organizador
73                $schedulable = isset( $schedulables[$pUpdated['schedulable']] ) ? $schedulables[ $pUpdated['schedulable'] ] : Controller::read( array('concept' => 'schedulable', 'id' => $pUpdated['schedulable']) , false , array('deepness' => '2'));
74                if(!self::futureEvent( $schedulable['startTime'] , $schedulable['rangeEnd'])) continue; //Escapa eventos do passado
75                 
76                foreach ($schedulable['participants'] as $i => $v) //salva em $organizer as informações do organizador     
77                    if(($v['isOrganizer'] === '1') && ($organizer = $v) ) break;
78                   
79               
80                if( $pUpdated['status'] != STATUS_UNANSWERED)
81                self::participantStatusChange( $pUpdated['id'] , $schedulable , $organizer , $pUpdated['status'] );   
82            }
83        }
84    }
85   
86
87    /**
88    * Monta o email de convite que sera enviado ao participant
89    *
90    * @license    http://www.gnu.org/copyleft/gpl.html GPL
91    * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
92    * @sponsor    Caixa Econômica Federal
93    * @author     Cristiano Corrêa Schmidt
94    * @return     void
95    * @access     public
96    */
97    public static function participantCreated( &$partID , &$schedulable )
98    {
99       
100           $part = false;
101           if( Config::regGet('ical://'.$schedulable['id'].'/REQUEST') === false ) //Verifica se o ical ja não esta no reg
102           {
103                  $schedulable['URI']['concept'] = 'schedulable';
104                  $ical['source'] = Controller::format( array( 'service' => 'iCal' ) , array($schedulable) , array('method' => 'REQUEST') ); 
105                  $ical['type'] = 'text/calendar';
106                  $ical['name'] = 'calendar.ics';
107                  Config::regSet('ical://'.$schedulable['id'].'/REQUEST', $ical);
108           }
109           else
110                $ical = Config::regGet('ical://'.$schedulable['id'].'/REQUEST');
111       
112           $timezone = new DateTimeZone('UTC');
113           $sTime = new DateTime( '@'.(int)($schedulable['startTime'] / 1000) , $timezone );
114           $eTime =  new DateTime( '@'.(int)($schedulable['endTime'] / 1000) , $timezone );
115           
116           if(isset($schedulable['timezone']))
117           {
118               $sTime->setTimezone(new DateTimeZone($schedulable['timezone']));
119               $eTime->setTimezone(new DateTimeZone($schedulable['timezone']));
120           }
121           
122           $attList = '<UL> ';
123           foreach ($schedulable['participants'] as $i => $v) 
124           {   
125                if($part === false && $v['id'] == $partID)
126                   $part = $v;
127
128                $attList .= ' <LI> '. (isset($v['user']['name']) ? $v['user']['name'] : $v['user']['mail']) ;
129           }
130           $attList .= '</UL>';
131                           
132           $data = array(     'startDate' =>  date_format( $sTime , 'd/m/Y') ,
133                              'startTime' =>  ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i') ,
134                              'endDate' =>  date_format( $eTime , 'd/m/Y') ,
135                              'endTime' =>  ($schedulable['allDay']) ? '' :  date_format( $eTime , 'H:i') ,
136                              'eventTitle' =>  $schedulable['summary'],
137                              'eventLocation' =>  $schedulable['location'],
138                              'timezone' =>  ($schedulable['timezone']) ? $schedulable['timezone'] : 'UTC' ,
139                              'participants' =>  $attList);
140           
141             $subject['notificationType'] = 'Convite de Calendario';
[5399]142             $subject['eventTitle'] = mb_convert_encoding($schedulable['summary'],'ISO-8859-1','ISO-8859-1,UTF-8');
[5341]143             $subject['startDate'] = date_format( $sTime , 'd/m/Y');
144             $subject['startTime'] = ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i');
145             $subject['endDate'] = date_format( $eTime , 'd/m/Y');
146             $subject['endTime'] = ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i');
147             $subject['participant'] = Config::me('uid');
148 
149             self::sendMail($data, $ical , $part['user']['mail'] , $subject , 'notify_create_body');
150       
151    }
152   
153    /**
154    * Monta o email de aceito que sera enviado ao organizador
155    *
156    * @license    http://www.gnu.org/copyleft/gpl.html GPL
157    * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
158    * @sponsor    Caixa Econômica Federal
159    * @author     Cristiano Corrêa Schmidt
160    * @return     void
161    * @access     public
162    */
163    public static function participantStatusChange( &$partID , &$schedulable , &$organizer , $type )
164    {
165        $part = false;
166       
167        foreach ($schedulable['participants'] as $i => $v) //salva em $part as informações do participant a ser notificado     
168            if(($v['id'] == $partID) && ($part = $v) ) break;
169           
170        if( Config::regGet('ical://'.$schedulable['id'].'/REPLY') === false ) //Verifica se o ical ja não esta no reg
171        {
172                  $schedulable['URI']['concept'] = 'schedulable';
173                  $ical['source'] = Controller::format( array( 'service' => 'iCal' ) , array($schedulable) , array('method' => 'REPLY' , 'participant' => $partID) ); 
174                  $ical['type'] = 'text/calendar';
175                  $ical['name'] = 'calendar.ics';
176                  Config::regSet('ical://'.$schedulable['id'].'/REPLY', $ical);
177         }
178         else
179            $ical = Config::regGet('ical://'.$schedulable['id'].'/REPLY');
180       
181       
182        $timezone = new DateTimeZone('UTC');
183        $sTime = new DateTime( '@'.(int)($schedulable['startTime'] / 1000) , $timezone );
184        $eTime =  new DateTime( '@'.(int)($schedulable['endTime'] / 1000) , $timezone );
185       
186        if(isset($schedulable['timezone']))
187        {
188            $sTime->setTimezone(new DateTimeZone($schedulable['timezone']));
189            $eTime->setTimezone(new DateTimeZone($schedulable['timezone']));
190        }
191       
192        $data = array('startDate' =>  date_format( $sTime , 'd/m/Y') ,
193                          'startTime' =>  ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i') ,
194                          'endDate' =>  date_format( $eTime , 'd/m/Y') ,
195                          'endTime' =>  ($schedulable['allDay']) ? '' :  date_format( $eTime , 'H:i') ,
196                          'eventTitle' =>  $schedulable['summary'],
197                          'eventLocation' =>  $schedulable['location'],
198                          'timezone' =>  ($schedulable['timezone']) ? $schedulable['timezone'] : 'UTC' ,
199                          'participant' =>  (isset($part['user']['name']) ? $part['user']['name'] : $part['user']['mail']) );
200         
201        $subject['notificationType'] = 'Convite Aceito';
[5399]202        $subject['eventTitle'] = mb_convert_encoding($schedulable['summary'],'ISO-8859-1','ISO-8859-1,UTF-8');
[5341]203        $subject['startDate'] = date_format( $sTime , 'd/m/Y');
204        $subject['startTime'] = ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i');
205        $subject['endDate'] = date_format( $eTime , 'd/m/Y');
206        $subject['endTime'] = ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i');
207        $subject['participant'] = Config::me('uid');
208         
209       switch($type)
210       {
211            case STATUS_ACCEPTED:
212                $tpl =   'notify_accept_body';
213                $subject['notificationType'] = 'Convite Aceito';
214            break;
215            case STATUS_TENATIVE:
216                $tpl =   'notify_attempt_body';
217                $subject['notificationType'] = 'Convite  aceito provisoriamente';
218            break;
219            case STATUS_CANCELLED:
220                $tpl =   'notify_reject_body';
221                $subject['notificationType'] = 'Convite rejeitado';
222            break;
223
224       }
225       
226        self::sendMail($data, $ical , $organizer['user']['mail'] , $subject , $tpl);
227    }
228   
229    /**
230    * Monta o body e envia o email
231    *
232    * @license    http://www.gnu.org/copyleft/gpl.html GPL
233    * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
234    * @sponsor    Caixa Econômica Federal
235    * @author     Cristiano Corrêa Schmidt
236    * @return     void
237    * @access     public
238    */
239    private static function sendMail(&$data , &$ical , $to ,  &$subject , $template )
240    {
241       $mail['attachments'][] = $ical;
242       $mail['isHtml'] = true;                         
[5399]243       $mail['body'] = parseTPL::load_tpl( $data ,ROOTPATH.'/modules/calendar/templates/'.$template.'.tpl');
244       $mail['subject'] = parseTPL::load_tpl( $subject ,ROOTPATH.'/modules/calendar/templates/notify_subject.tpl');;
[5341]245       $mail['from'] = Config::me('mail');   
246       $mail['to'] = $to;
247       Controller::create( array( 'service' => 'SMTP' ), $mail );
248    }
249   
250 
251    /**
252    * Monta o email de cancelado que sera enviado a todos os participantes
253    *
254    * @license    http://www.gnu.org/copyleft/gpl.html GPL
255    * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
256    * @sponsor    Caixa Econômica Federal
257    * @author     Cristiano Corrêa Schmidt
258    * @return     void
259    * @access     public
260    */
261    public function deleteEvent( &$uri , &$result , &$params , $original  )
262    {           
263       $schedulable =  Controller::read(  array( 'concept' => 'schedulable' , 'id' => $uri['id'] ) , null , array('deepness' => '2') ) ;
264       if((Config::regGet('noAlarm') === false) && (self::futureEvent( $schedulable['startTime'] , $schedulable['rangeEnd'])))
265       {
266           $schedulable['URI']['concept'] = 'schedulable';
267           $timezone = new DateTimeZone('UTC');
268           $sTime = new DateTime( '@'.(int)($schedulable['startTime'] / 1000) , $timezone );
269           $eTime =  new DateTime( '@'.(int)($schedulable['endTime'] / 1000) , $timezone );
270           
271           if(isset($schedulable['timezone']))
272           {
273               $sTime->setTimezone(new DateTimeZone($schedulable['timezone']));
274               $eTime->setTimezone(new DateTimeZone($schedulable['timezone']));
275           }
276           
277           $data =  array('startDate' =>  date_format( $sTime , 'd/m/Y') ,
278                              'startTime' =>  ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i') ,
279                              'endDate' =>  date_format( $eTime , 'd/m/Y') ,
280                              'endTime' =>  ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i') ,
281                              'eventTitle' =>  $schedulable['summary'],
282                              'eventLocation' =>  $schedulable['location'],
283                              'timezone' =>  ($schedulable['timezone']) ? $schedulable['timezone'] : 'UTC');
284                             
285           
286           $ical['source'] = Controller::format( array( 'service' => 'iCal' ) , array($schedulable) , array('method' => 'CANCEL')); 
287           $ical['type'] = 'text/calendar';
288           $ical['name'] = 'calendar.ics';
289           
290           foreach ($schedulable['participants'] as $ii => $participant)
291                if(Config::me('mail') !== $participant['user']['mail'])
292                    $to[] = $participant['user']['mail'];
293                   
294             $subject['notificationType'] = 'Cancelamento de Calendario';
[5399]295             $subject['eventTitle'] = mb_convert_encoding($schedulable['summary'],'ISO-8859-1','ISO-8859-1,UTF-8');
[5341]296             $subject['startDate'] = date_format( $sTime , 'd/m/Y');
297             $subject['startTime'] = ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i');
298             $subject['endDate'] = date_format( $eTime , 'd/m/Y');
299             $subject['endTime'] = ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i');
300             $subject['participant'] = Config::me('uid');     
301               
302           if(count($to) > 0)
303            self::sendMail($data, $ical , implode(',' , $to) ,$subject, 'notify_cancel_body');
304       }
305    }
306   
307    /**
308    * Monta o email de cancelado que sera enviado ao participant deleteado
309    *
310    * @license    http://www.gnu.org/copyleft/gpl.html GPL
311    * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
312    * @sponsor    Caixa Econômica Federal
313    * @author     Cristiano Corrêa Schmidt
314    * @return     void
315    * @access     public
316    */
317    public function deleteParticipant( &$uri , &$result , &$params , $original  )
318    {
319       
320       $participant =  Controller::read(  array( 'concept' => 'participant' , 'id' => $uri['id'] ) , null , array('deepness' => '1') ) ;
321       $schedulable =  Controller::read(  array( 'concept' => 'schedulable' , 'id' => $participant['schedulable'] ) ) ;
322             
323       if((Config::regGet('noAlarm') === false) && (self::futureEvent( $schedulable['startTime'] , $schedulable['rangeEnd'])))
324       { 
325           $schedulable['URI']['concept'] = 'schedulable';
326           $timezone = new DateTimeZone('UTC');
327           $sTime = new DateTime( '@'.(int)($schedulable['startTime'] / 1000) , $timezone );
328           $eTime =  new DateTime( '@'.(int)($schedulable['endTime'] / 1000) , $timezone );
329           
330           if(isset($schedulable['timezone']))
331           {
332               $sTime->setTimezone(new DateTimeZone($schedulable['timezone']));
333               $eTime->setTimezone(new DateTimeZone($schedulable['timezone']));
334           }
335           
336           $data =  array('startDate' =>  date_format( $sTime , 'd/m/Y') ,
337                              'startTime' => ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i') ,
338                              'endDate' =>  date_format( $eTime , 'd/m/Y') ,
339                              'endTime' => ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i') ,
340                              'eventTitle' =>  $schedulable['summary'],
341                              'eventLocation' =>  $schedulable['location'],
342                              'timezone' =>  ($schedulable['timezone']) ? $schedulable['timezone'] : 'UTC');
343
344           $ical['source'] = Controller::format( array( 'service' => 'iCal' ) , array($schedulable) , array('method' => 'CANCEL')); 
345           $ical['type'] = 'text/calendar';
346           $ical['name'] = 'calendar.ics';
347           
348             $subject['notificationType'] = 'Cancelamento de Calendario';
[5399]349             $subject['eventTitle'] = mb_convert_encoding($schedulable['summary'],'ISO-8859-1','ISO-8859-1,UTF-8');
[5341]350             $subject['startDate'] = date_format( $sTime , 'd/m/Y');
351             $subject['startTime'] = ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i');
352             $subject['endDate'] = date_format( $eTime , 'd/m/Y');
353             $subject['endTime'] = ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i');
354             $subject['participant'] = Config::me('uid');
355                         
356           self::sendMail($data, $ical , $participant['user']['mail'] , $subject , 'notify_cancel_body'); 
357        }
358    }
359   
360    /**
361    * Faz um diff do update se ouve realmente uma alteração envia um email a todos os participants
362    *
363    * @license    http://www.gnu.org/copyleft/gpl.html GPL
364    * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
365    * @sponsor    Caixa Econômica Federal
366    * @author     Cristiano Corrêa Schmidt
367    * @return     void
368    * @access     public
369    */
370    public function updateEvent( &$uri , &$params , &$criteria , $original  )
371    {
372       
373       $schedulableOld =  Controller::read( array( 'concept' => 'schedulable' , 'id' => $uri['id'] ) , null , array('deepness' => '2') ) ;     
374       $schedulable = $schedulableOld;
375       $alt = false;
376       
377     
378       foreach ($params as $i => $v) //Verifica se ouve alteração no evento
379           if( isset( $schedulableOld[$i] ) && $schedulableOld[$i] != $v && $i != 'participants')
380           {
381               $schedulable[$i] = $v;
382               $alt = true;   
383           }
384           
385       if ( ($alt === true) && (Config::regGet('noAlarm') === false) && (self::futureEvent($schedulable['startTime'], $schedulable['rangeEnd'])) )
386       {
387            $schedulable['URI']['concept'] = 'schedulable';
388            $ical['source'] = Controller::format(array('service' => 'iCal'), array($schedulable), array('method' => 'REQUEST'));
389            $ical['type'] = 'text/calendar';
390            $ical['name'] = 'calendar.ics';
391
392            $timezone = new DateTimeZone('UTC');
393            $sTime = new DateTime('@' . (int) ($schedulable['startTime'] / 1000), $timezone);
394            $eTime = new DateTime('@' . (int) ($schedulable['endTime'] / 1000), $timezone);
395
396            if (isset($schedulable['timezone'])) {
397                $sTime->setTimezone(new DateTimeZone($schedulable['timezone']));
398                $eTime->setTimezone(new DateTimeZone($schedulable['timezone']));
399            }
400            ///Montando lista de participantes
401            $attList = '<UL> ';
402            foreach ($schedulable['participants'] as $ii => $participant)
403                $attList .= ' <LI> ' . (isset($participant['user']['name']) ? $participant['user']['name'] : $participant['user']['mail']);
404            $attList .= ' </UL>';
405            /////////////////////////
406
407            $data = array('startDate' => date_format($sTime, 'd/m/Y'),
408                'startTime' => ($schedulable['allDay']) ? '' : date_format($sTime, 'H:i'),
409                'endDate' => date_format($eTime, 'd/m/Y'),
410                'endTime' => ($schedulable['allDay']) ? '' : date_format($eTime, 'H:i'),
411                'eventTitle' => $schedulable['summary'],
412                'eventLocation' => $schedulable['location'],
413                'timezone' => ($schedulable['timezone']) ? $schedulable['timezone'] : 'UTC',
414                'participants' => $attList
415            );
416
417
418            foreach ($schedulable['participants'] as $ii => $participant)
419                if (Config::me('mail') !== $participant['user']['mail'])
420                    $to[] = $participant['user']['mail'];
421
422            $subject['notificationType'] = 'Modificação de Calendario';
[5399]423            $subject['eventTitle'] = mb_convert_encoding($schedulable['summary'],'ISO-8859-1','ISO-8859-1,UTF-8');
[5341]424            $subject['startDate'] = date_format($sTime, 'd/m/Y');
425            $subject['startTime'] = ($schedulable['allDay']) ? '' : date_format($sTime, 'H:i');
426            $subject['endDate'] = date_format($eTime, 'd/m/Y');
427            $subject['endTime'] = ($schedulable['allDay']) ? '' : date_format($eTime, 'H:i');
428            $subject['participant'] = Config::me('uid');
429             
430            if ( isset($to) && count($to) > 0)
431                self::sendMail($data, $ical, implode(',', $to), $subject, 'notify_modify_body');
432        }
433    }
434}
435
436?>
Note: See TracBrowser for help on using the repository browser.