Ignore:
Timestamp:
05/04/12 18:38:56 (12 years ago)
Author:
natan
Message:

Ticket #2141 - Eventos com repeticao nao dispara notificacao nos alarmes - implementação final

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/prototype/modules/calendar/alarms.php

    r5724 r6111  
    22 
    33if(!defined('ROOTPATH')) 
    4         define('ROOTPATH', dirname(__FILE__).'/../..'); 
     4    define('ROOTPATH', dirname(__FILE__).'/../..'); 
    55 
    66require_once ROOTPATH.'/api/controller.php'; 
    77require_once ROOTPATH.'/modules/calendar/constants.php'; 
     8require_once ROOTPATH.'/api/parseTPL.php'; 
    89 
    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           );  
     10$target = (gmdate('U') - 300 ).'000'; 
    1511 
    16 //Busca os Alarmes no Range 
    17 $al = Controller::find( array('concept' => 'alarm'), false ,array( 'filter' => $filter)); 
     12$parts = Controller::service('PostgreSQL')->execSql("SELECT calendar_participant.user_info_id as user, co.id as schedulable, co.dtend as endTime, co.dtstart as startTime, co.summary as summary, co.tzid as timezone, co.location as location, al.id as id ". 
     13                                                      "FROM calendar_participant, calendar_alarm as al, calendar_object as co, calendar_repeat as rep WHERE ". 
     14                                                      "al.participant_id = calendar_participant.id AND ". 
     15                                                      "calendar_participant.object_id = co.id AND ". 
     16                                                      "al.action_id = '".ALARM_MAIL."' AND ". 
     17                                                      "al.sent = '0' AND ". 
     18                                                      "CASE WHEN rep.object_id = co.id ". 
     19                                                      "THEN  ( select count(occurrence) FROM calendar_repeat_occurrence WHERE rep.object_id = co.id AND rep.id = calendar_repeat_occurrence.repeat_id AND occurrence - al.alarm_offset >= '$target' ) > 0 ". 
     20                                                      "ELSE co.range_start - al.alarm_offset >= '$target' END"); 
    1821 
    19 if(is_array($al)) 
    20 foreach ($al as $i => $v) 
     22if(!is_array($parts)) 
     23  return; 
     24 
     25$ids = array(); 
     26 
     27foreach ($parts as $i => $part) 
    2128{ 
    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          
     29        ///Montando lista de participantes 
    3030 
    31         if( strtotime($v['rangeEnd']) >= time() ) // Evento Recursivo ? 
     31        $users = Controller::find( array( 'concept' => 'participant' ) , array( 'user', 'id', 'isExternal' ) ,array('filter' => array ('=', 'schedulable' , $part['schedulable'] ), 'deepness' => 1 ) ); 
     32 
     33        $attList = array(); 
     34 
     35        foreach( $users as $user ) 
    3236        { 
    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'] )); 
     37            if( $part['user'] === $user['user']['id'] ) 
     38                $part['mail'] = $user['user']['mail']; 
    6139 
    62                                 $attList .= '<LI> '.$att[$ii]['name'] .'</LI> '; 
     40            $attList[] = $user['user']['name']; 
     41        } 
    6342 
    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; 
     43      ///////////////////////// 
    10044 
    101                                 case 'sms': 
    102                                         //Sem suporte atualmente 
    103                                         break; 
     45        $timezone = ($part['timezone']) ? $part['timezone'] : 'UTC'; 
     46        $sTime = new DateTime( '@'.(int)($part['startTime'] / 1000) , new DateTimeZone($timezone) ); 
     47        $eTime =  new DateTime( '@'.(int)($part['endTime'] / 1000) , new DateTimeZone($timezone) ); 
    10448 
    105                         } 
    106                 } 
    107         } 
    108          
     49        $data = array('startDate' =>  date_format( $sTime , 'd/m/Y') , 
     50                      'startTime' =>  date_format( $sTime , 'H:i') , 
     51                      'endDate' =>  date_format( $eTime , 'd/m/Y') , 
     52                      'endTime' =>  date_format( $eTime , 'H:i') , 
     53                      'eventTitle' =>  $part['summary'], 
     54                      'eventLocation' =>  $part['location'], 
     55                      'timezone' => $timezone, 
     56                      'participants' =>  '<UL> <LI> '.implode( '<LI></LI> ', $attList ).'</LI> </UL>'); 
     57 
     58        Controller::create( array( 'service' => 'SMTP' ), array( 'body' => parseTPL::load_tpl( $data, ROOTPATH.'/modules/calendar/templates/notify_alarm_body.tpl' ), 
     59                                                                  'isHtml' => true, 
     60                                                                  'subject' => 'Alarme de Calendario', 
     61                                                                  'from' => $part['mail'], 
     62                                                                  'to' => $part['mail'] ) ); 
     63 
     64        Config::regSet('noAlarm', TRUE); //Evita o envio de notificação ????? 
     65        $ids[] = $part['id']; 
    10966} 
    11067 
     68if( !empty( $ids ) ) 
     69    Controller::update( array( 'concept' => 'alarm' ) , array('sent' => '1'), array( 'IN', 'id', $ids ) ); 
    11170 
    11271?> 
Note: See TracChangeset for help on using the changeset viewer.