$concept)
{
if($concept['concept'] === 'participant')
{
if( $concept['method'] == 'create' )
$created[] = $concept['id'];
else if( $concept['method'] == 'update' )
$updated[] = $concept['id'];
}
else if($concept['concept'] === 'schedulable') //Caso exista schedulable no commit antecipa o carregamento do owner
{
$schedulables[ $concept['id'] ] = Controller::read( array('concept' => 'schedulable', 'id' => $concept['id']) , false , array('deepness' => '2'));
foreach ($schedulables[ $concept['id'] ]['participants'] as $i => $v) //salva em $organizers as informações do organizador
if(($v['isOrganizer'] === '1') && ($organizers[$concept['id']] = $v) ) break;
}
}
if( isset( $created ) )
{
$psCreated = Controller::find(array('concept' => 'participant'), false , array('deepness' => '1' , 'filter' => array('IN', 'id' , $created )));
foreach ($psCreated as $i => $pCreated)
{
if($pCreated['isOrganizer'] == '1') continue; //escapa organizador
$schedulable = isset( $schedulables[$pCreated['schedulable']] ) ? $schedulables[$pCreated['schedulable']] : Controller::read( array('concept' => 'schedulable', 'id' => $pCreated['schedulable']) , false , array('deepness' => '2'));
if(!self::futureEvent( $schedulable['startTime'] , $schedulable['rangeEnd'])) continue; //Escapa eventos do passado
$organizer = isset( $organizers[$pCreated['schedulable']] ) ? $organizers[ $pCreated['schedulable'] ] : self::getOrganizer( $pCreated['schedulable'] );
switch ($pCreated['status'])
{
case STATUS_CONFIRMED:
self::participantStatusChange( $pCreated['id'] , $schedulable , $organizer , STATUS_ACCEPTED);
break;
case STATUS_UNANSWERED:
self::participantCreated( $pCreated['id'] , $schedulable );
break;
}
}
}
if( isset( $updated ) )
{
$psUpdated = Controller::find(array('concept' => 'participant'), false , array('deepness' => '1' , 'filter' => array('IN', 'id' , $updated )));
foreach ($psUpdated as $i => $pUpdated)
{
if($pUpdated['isOrganizer'] == '1') continue; //escapa organizador
$schedulable = isset( $schedulables[$pUpdated['schedulable']] ) ? $schedulables[ $pUpdated['schedulable'] ] : Controller::read( array('concept' => 'schedulable', 'id' => $pUpdated['schedulable']) , false , array('deepness' => '2'));
if(!self::futureEvent( $schedulable['startTime'] , $schedulable['rangeEnd'])) continue; //Escapa eventos do passado
foreach ($schedulable['participants'] as $i => $v) //salva em $organizer as informações do organizador
if(($v['isOrganizer'] === '1') && ($organizer = $v) ) break;
if( $pUpdated['status'] != STATUS_UNANSWERED)
self::participantStatusChange( $pUpdated['id'] , $schedulable , $organizer , $pUpdated['status'] );
}
}
}
/**
* Monta o email de convite que sera enviado ao participant
*
* @license http://www.gnu.org/copyleft/gpl.html GPL
* @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
* @sponsor Caixa Econômica Federal
* @author Cristiano Corrêa Schmidt
* @return void
* @access public
*/
public static function participantCreated( &$partID , &$schedulable )
{
$part = false;
if( Config::regGet('ical://'.$schedulable['id'].'/REQUEST') === false ) //Verifica se o ical ja não esta no reg
{
$schedulable['URI']['concept'] = 'schedulable';
$ical['source'] = Controller::format( array( 'service' => 'iCal' ) , array($schedulable) , array('method' => 'REQUEST') );
$ical['type'] = 'text/calendar';
$ical['name'] = 'calendar.ics';
Config::regSet('ical://'.$schedulable['id'].'/REQUEST', $ical);
}
else
$ical = Config::regGet('ical://'.$schedulable['id'].'/REQUEST');
$timezone = new DateTimeZone('UTC');
$sTime = new DateTime( '@'.(int)($schedulable['startTime'] / 1000) , $timezone );
$eTime = new DateTime( '@'.(int)($schedulable['endTime'] / 1000) , $timezone );
if(isset($schedulable['timezone']))
{
$sTime->setTimezone(new DateTimeZone($schedulable['timezone']));
$eTime->setTimezone(new DateTimeZone($schedulable['timezone']));
}
$attList = '
';
foreach ($schedulable['participants'] as $i => $v)
{
if($part === false && $v['id'] == $partID)
$part = $v;
$attList .= ' - '. (isset($v['user']['name']) ? $v['user']['name'] : $v['user']['mail']) ;
}
$attList .= '
';
$data = array( 'startDate' => date_format( $sTime , 'd/m/Y') ,
'startTime' => ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i') ,
'endDate' => date_format( $eTime , 'd/m/Y') ,
'endTime' => ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i') ,
'eventTitle' => $schedulable['summary'],
'eventLocation' => $schedulable['location'],
'timezone' => ($schedulable['timezone']) ? $schedulable['timezone'] : 'UTC' ,
'participants' => $attList);
$subject['notificationType'] = 'Convite de Calendario';
$subject['eventTitle'] = mb_convert_encoding($schedulable['summary'],'ISO-8859-1','ISO-8859-1,UTF-8');
$subject['startDate'] = date_format( $sTime , 'd/m/Y');
$subject['startTime'] = ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i');
$subject['endDate'] = date_format( $eTime , 'd/m/Y');
$subject['endTime'] = ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i');
$subject['participant'] = Config::me('uid');
self::sendMail($data, $ical , $part['user']['mail'] , $subject , 'notify_create_body');
}
/**
* Monta o email de aceito que sera enviado ao organizador
*
* @license http://www.gnu.org/copyleft/gpl.html GPL
* @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
* @sponsor Caixa Econômica Federal
* @author Cristiano Corrêa Schmidt
* @return void
* @access public
*/
public static function participantStatusChange( &$partID , &$schedulable , &$organizer , $type )
{
$part = false;
foreach ($schedulable['participants'] as $i => $v) //salva em $part as informações do participant a ser notificado
if(($v['id'] == $partID) && ($part = $v) ) break;
if( Config::regGet('ical://'.$schedulable['id'].'/REPLY') === false ) //Verifica se o ical ja não esta no reg
{
$schedulable['URI']['concept'] = 'schedulable';
$ical['source'] = Controller::format( array( 'service' => 'iCal' ) , array($schedulable) , array('method' => 'REPLY' , 'participant' => $partID) );
$ical['type'] = 'text/calendar';
$ical['name'] = 'calendar.ics';
Config::regSet('ical://'.$schedulable['id'].'/REPLY', $ical);
}
else
$ical = Config::regGet('ical://'.$schedulable['id'].'/REPLY');
$timezone = new DateTimeZone('UTC');
$sTime = new DateTime( '@'.(int)($schedulable['startTime'] / 1000) , $timezone );
$eTime = new DateTime( '@'.(int)($schedulable['endTime'] / 1000) , $timezone );
if(isset($schedulable['timezone']))
{
$sTime->setTimezone(new DateTimeZone($schedulable['timezone']));
$eTime->setTimezone(new DateTimeZone($schedulable['timezone']));
}
$data = array('startDate' => date_format( $sTime , 'd/m/Y') ,
'startTime' => ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i') ,
'endDate' => date_format( $eTime , 'd/m/Y') ,
'endTime' => ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i') ,
'eventTitle' => $schedulable['summary'],
'eventLocation' => $schedulable['location'],
'timezone' => ($schedulable['timezone']) ? $schedulable['timezone'] : 'UTC' ,
'participant' => (isset($part['user']['name']) ? $part['user']['name'] : $part['user']['mail']) );
$subject['notificationType'] = 'Convite Aceito';
$subject['eventTitle'] = mb_convert_encoding($schedulable['summary'],'ISO-8859-1','ISO-8859-1,UTF-8');
$subject['startDate'] = date_format( $sTime , 'd/m/Y');
$subject['startTime'] = ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i');
$subject['endDate'] = date_format( $eTime , 'd/m/Y');
$subject['endTime'] = ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i');
$subject['participant'] = Config::me('uid');
switch($type)
{
case STATUS_ACCEPTED:
$tpl = 'notify_accept_body';
$subject['notificationType'] = 'Convite Aceito';
break;
case STATUS_TENATIVE:
$tpl = 'notify_attempt_body';
$subject['notificationType'] = 'Convite aceito provisoriamente';
break;
case STATUS_CANCELLED:
$tpl = 'notify_reject_body';
$subject['notificationType'] = 'Convite rejeitado';
break;
}
self::sendMail($data, $ical , $organizer['user']['mail'] , $subject , $tpl);
}
/**
* Monta o body e envia o email
*
* @license http://www.gnu.org/copyleft/gpl.html GPL
* @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
* @sponsor Caixa Econômica Federal
* @author Cristiano Corrêa Schmidt
* @return void
* @access public
*/
private static function sendMail(&$data , &$ical , $to , &$subject , $template )
{
$mail['attachments'][] = $ical;
$mail['isHtml'] = true;
$mail['body'] = parseTPL::load_tpl( $data ,ROOTPATH.'/modules/calendar/templates/'.$template.'.tpl');
$mail['subject'] = parseTPL::load_tpl( $subject ,ROOTPATH.'/modules/calendar/templates/notify_subject.tpl');;
$mail['from'] = Config::me('mail');
$mail['to'] = $to;
Controller::create( array( 'service' => 'SMTP' ), $mail );
}
/**
* Monta o email de cancelado que sera enviado a todos os participantes
*
* @license http://www.gnu.org/copyleft/gpl.html GPL
* @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
* @sponsor Caixa Econômica Federal
* @author Cristiano Corrêa Schmidt
* @return void
* @access public
*/
public function deleteEvent( &$uri , &$result , &$params , $original )
{
$schedulable = Controller::read( array( 'concept' => 'schedulable' , 'id' => $uri['id'] ) , null , array('deepness' => '2') ) ;
if((Config::regGet('noAlarm') === false) && (self::futureEvent( $schedulable['startTime'] , $schedulable['rangeEnd'])))
{
$schedulable['URI']['concept'] = 'schedulable';
$timezone = new DateTimeZone('UTC');
$sTime = new DateTime( '@'.(int)($schedulable['startTime'] / 1000) , $timezone );
$eTime = new DateTime( '@'.(int)($schedulable['endTime'] / 1000) , $timezone );
if(isset($schedulable['timezone']))
{
$sTime->setTimezone(new DateTimeZone($schedulable['timezone']));
$eTime->setTimezone(new DateTimeZone($schedulable['timezone']));
}
$data = array('startDate' => date_format( $sTime , 'd/m/Y') ,
'startTime' => ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i') ,
'endDate' => date_format( $eTime , 'd/m/Y') ,
'endTime' => ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i') ,
'eventTitle' => $schedulable['summary'],
'eventLocation' => $schedulable['location'],
'timezone' => ($schedulable['timezone']) ? $schedulable['timezone'] : 'UTC');
$ical['source'] = Controller::format( array( 'service' => 'iCal' ) , array($schedulable) , array('method' => 'CANCEL'));
$ical['type'] = 'text/calendar';
$ical['name'] = 'calendar.ics';
foreach ($schedulable['participants'] as $ii => $participant)
if(Config::me('mail') !== $participant['user']['mail'])
$to[] = $participant['user']['mail'];
$subject['notificationType'] = 'Cancelamento de Calendario';
$subject['eventTitle'] = mb_convert_encoding($schedulable['summary'],'ISO-8859-1','ISO-8859-1,UTF-8');
$subject['startDate'] = date_format( $sTime , 'd/m/Y');
$subject['startTime'] = ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i');
$subject['endDate'] = date_format( $eTime , 'd/m/Y');
$subject['endTime'] = ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i');
$subject['participant'] = Config::me('uid');
if(count($to) > 0)
self::sendMail($data, $ical , implode(',' , $to) ,$subject, 'notify_cancel_body');
}
}
/**
* Monta o email de cancelado que sera enviado ao participant deleteado
*
* @license http://www.gnu.org/copyleft/gpl.html GPL
* @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
* @sponsor Caixa Econômica Federal
* @author Cristiano Corrêa Schmidt
* @return void
* @access public
*/
public function deleteParticipant( &$uri , &$result , &$params , $original )
{
$participant = Controller::read( array( 'concept' => 'participant' , 'id' => $uri['id'] ) , null , array('deepness' => '1') ) ;
$schedulable = Controller::read( array( 'concept' => 'schedulable' , 'id' => $participant['schedulable'] ) ) ;
if((Config::regGet('noAlarm') === false) && (self::futureEvent( $schedulable['startTime'] , $schedulable['rangeEnd'])))
{
$schedulable['URI']['concept'] = 'schedulable';
$timezone = new DateTimeZone('UTC');
$sTime = new DateTime( '@'.(int)($schedulable['startTime'] / 1000) , $timezone );
$eTime = new DateTime( '@'.(int)($schedulable['endTime'] / 1000) , $timezone );
if(isset($schedulable['timezone']))
{
$sTime->setTimezone(new DateTimeZone($schedulable['timezone']));
$eTime->setTimezone(new DateTimeZone($schedulable['timezone']));
}
$data = array('startDate' => date_format( $sTime , 'd/m/Y') ,
'startTime' => ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i') ,
'endDate' => date_format( $eTime , 'd/m/Y') ,
'endTime' => ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i') ,
'eventTitle' => $schedulable['summary'],
'eventLocation' => $schedulable['location'],
'timezone' => ($schedulable['timezone']) ? $schedulable['timezone'] : 'UTC');
$ical['source'] = Controller::format( array( 'service' => 'iCal' ) , array($schedulable) , array('method' => 'CANCEL'));
$ical['type'] = 'text/calendar';
$ical['name'] = 'calendar.ics';
$subject['notificationType'] = 'Cancelamento de Calendario';
$subject['eventTitle'] = mb_convert_encoding($schedulable['summary'],'ISO-8859-1','ISO-8859-1,UTF-8');
$subject['startDate'] = date_format( $sTime , 'd/m/Y');
$subject['startTime'] = ($schedulable['allDay']) ? '' : date_format( $sTime , 'H:i');
$subject['endDate'] = date_format( $eTime , 'd/m/Y');
$subject['endTime'] = ($schedulable['allDay']) ? '' : date_format( $eTime , 'H:i');
$subject['participant'] = Config::me('uid');
self::sendMail($data, $ical , $participant['user']['mail'] , $subject , 'notify_cancel_body');
}
}
/**
* Faz um diff do update se ouve realmente uma alteração envia um email a todos os participants
*
* @license http://www.gnu.org/copyleft/gpl.html GPL
* @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
* @sponsor Caixa Econômica Federal
* @author Cristiano Corrêa Schmidt
* @return void
* @access public
*/
public function updateEvent( &$uri , &$params , &$criteria , $original )
{
$schedulableOld = Controller::read( array( 'concept' => 'schedulable' , 'id' => $uri['id'] ) , null , array('deepness' => '2') ) ;
$schedulable = $schedulableOld;
$alt = false;
foreach ($params as $i => $v) //Verifica se ouve alteração no evento
if( isset( $schedulableOld[$i] ) && $schedulableOld[$i] != $v && $i != 'participants')
{
$schedulable[$i] = $v;
$alt = true;
}
if ( ($alt === true) && (Config::regGet('noAlarm') === false) && (self::futureEvent($schedulable['startTime'], $schedulable['rangeEnd'])) )
{
$schedulable['URI']['concept'] = 'schedulable';
$ical['source'] = Controller::format(array('service' => 'iCal'), array($schedulable), array('method' => 'REQUEST'));
$ical['type'] = 'text/calendar';
$ical['name'] = 'calendar.ics';
$timezone = new DateTimeZone('UTC');
$sTime = new DateTime('@' . (int) ($schedulable['startTime'] / 1000), $timezone);
$eTime = new DateTime('@' . (int) ($schedulable['endTime'] / 1000), $timezone);
if (isset($schedulable['timezone'])) {
$sTime->setTimezone(new DateTimeZone($schedulable['timezone']));
$eTime->setTimezone(new DateTimeZone($schedulable['timezone']));
}
///Montando lista de participantes
$attList = ' ';
foreach ($schedulable['participants'] as $ii => $participant)
$attList .= ' - ' . (isset($participant['user']['name']) ? $participant['user']['name'] : $participant['user']['mail']);
$attList .= '
';
/////////////////////////
$data = array('startDate' => date_format($sTime, 'd/m/Y'),
'startTime' => ($schedulable['allDay']) ? '' : date_format($sTime, 'H:i'),
'endDate' => date_format($eTime, 'd/m/Y'),
'endTime' => ($schedulable['allDay']) ? '' : date_format($eTime, 'H:i'),
'eventTitle' => $schedulable['summary'],
'eventLocation' => $schedulable['location'],
'timezone' => ($schedulable['timezone']) ? $schedulable['timezone'] : 'UTC',
'participants' => $attList
);
foreach ($schedulable['participants'] as $ii => $participant)
if (Config::me('mail') !== $participant['user']['mail'])
$to[] = $participant['user']['mail'];
$subject['notificationType'] = 'Modificação de Calendario';
$subject['eventTitle'] = mb_convert_encoding($schedulable['summary'],'ISO-8859-1','ISO-8859-1,UTF-8');
$subject['startDate'] = date_format($sTime, 'd/m/Y');
$subject['startTime'] = ($schedulable['allDay']) ? '' : date_format($sTime, 'H:i');
$subject['endDate'] = date_format($eTime, 'd/m/Y');
$subject['endTime'] = ($schedulable['allDay']) ? '' : date_format($eTime, 'H:i');
$subject['participant'] = Config::me('uid');
if ( isset($to) && count($to) > 0)
self::sendMail($data, $ical, implode(',', $to), $subject, 'notify_modify_body');
}
}
}
?>