Index: /trunk/prototype/services/iCal.php
===================================================================
--- /trunk/prototype/services/iCal.php	(revision 7680)
+++ /trunk/prototype/services/iCal.php	(revision 7695)
@@ -563,222 +563,225 @@
 			case 'REQUEST':
 			    $schedulable = self::_getSchedulable($uid);
+                $calendar = false;
 
 			    if ($schedulable) { //Caso o evento exista
-				if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
-				    $calendarToSchedulable = array();
-				    $calendarToSchedulable['calendar'] = $params['calendar'];
-				    $calendarToSchedulable['schedulable'] = $schedulable['id'];
-				    $interation['calendarToSchedulable://' . mt_rand() . '(Formatter)'] = $calendarToSchedulable;
-    
-				    if (isset($params['status'])) {
-					if($params['owner'] != Config::me("uidNumber")){					    
-					    $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );					    
-					    $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
-					}else{
-    					    $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
-                            //caso nõa seja participante adiciona a lista de participantes
-                            if(!$pID){
-                                $pID =  mt_rand() . '2(Formatter)';
+                    if (!($calendar = self::_existInMyCalendars($schedulable['id'], $params['owner']) )) {
+                        $calendarToSchedulable = array();
+                        $calendarToSchedulable['calendar'] = $params['calendar'];
+                        $calendarToSchedulable['schedulable'] = $schedulable['id'];
+                        $interation['calendarToSchedulable://' . mt_rand() . '(Formatter)'] = $calendarToSchedulable;
+
+                        if (isset($params['status'])) {
+                            if($params['owner'] != Config::me("uidNumber")){
+                                $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );
+                                $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
+                            }else{
+                                $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
+                                //caso nõa seja participante adiciona a lista de participantes
+                                if(!$pID){
+                                    $pID =  mt_rand() . '2(Formatter)';
+                                    $interation['participant://' . $pID]['status'] = $params['status'];
+                                    $interation['participant://' . $pID]['user'] = $params['owner'];
+                                    $interation['participant://' . $pID]['isOrganizer'] = '0';
+                                    $interation['participant://' . $pID]['schedulable'] = $schedulable['id'];
+                                }else
+                                    $interation['participant://' . $pID]['status'] = $params['status'];
+                            }
+                        }
+
+                        Config::regSet('noAlarm', FALSE); //reativa notificação
+                    } else {
+
+                        if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']){ //Organizador esta requisitando que você atualize o evento
+
+                            $params['calendar'] = $params['calendar'] == 'true' ? $calendar : $params['calendar'];
+                            $interation = self::_makeVEVENT($schedulable, $component, $params);
+                        }else if ($component->getProperty('sequence', false, false) === $schedulable['sequence']) {
+                            //Ler melhor rfc sobre isto 3.2.2.2
+                            //Aparentemente é para retornar um ical com o evento atualizado para o attende
+                        }
+
+                        if (isset($params['status'])) {
+                            if($params['owner'] != Config::me("uidNumber")){
+                               $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );
+                                $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
+                            }else
+                                $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
+                            //Verifica a importação de eventos em que não participo
+                            if ($pID) {
                                 $interation['participant://' . $pID]['status'] = $params['status'];
-                                $interation['participant://' . $pID]['user'] = $params['owner'];
-                                $interation['participant://' . $pID]['isOrganizer'] = '0';
-                                $interation['participant://' . $pID]['schedulable'] = $schedulable['id'];
-                            }else
-    				            $interation['participant://' . $pID]['status'] = $params['status'];
+                            }
                         }
-				    }
-
-				    Config::regSet('noAlarm', FALSE); //reativa notificação
-				} else {
-
-				    if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']) //Organizador esta requisitando que você atualize o evento
-					$interation = self::_makeVEVENT($schedulable, $component, $params);
-				    else if ($component->getProperty('sequence', false, false) === $schedulable['sequence']) {
-					//Ler melhor rfc sobre isto 3.2.2.2
-					//Aparentemente é para retornar um ical com o evento atualizado para o attende
-				    }
-
-				    if (isset($params['status'])) {
-					if($params['owner'] != Config::me("uidNumber")){					    
-					   $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );					    
-					    $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
-					}else
-					    $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
-					//Verifica a importação de eventos em que não participo
-					if ($pID) {
-					    $interation['participant://' . $pID]['status'] = $params['status'];
-					}
-				    }
-				}
-			    } else { // Importar evento
-				$interation = self::_makeVEVENT(array(), $component, $params);
-
-				if (strpos($params['prodid'], 'kigkonsult.se') !== false) { //envia notificação para fora
-
-				    /* Data de Inicio */
-				    $startTime = $component->getProperty('dtstart', false, true);
-
-				    $tzid = isset($startTime['params']['TZID']) ? $startTime['params']['TZID'] : $params['X-WR-TIMEZONE'];
-
-				    /* Tiem zone do evento */	
-				    if ($tzid)
-					$sc['timezone'] = self::nomalizeTZID($tzid);
-				    else
-					$sc['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
-
-				    $objTimezone = new DateTimeZone($sc['timezone']);
-
-				    if (isset($startTime['params']['VALUE']) && $startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone'])) {
-					$sc['allDay'] = 1;
-					$sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
-				    } elseif (isset($startTime['params']['TZID']) && !isset($startTime['value']['tz']))/* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
-					$sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $startTime['params']['TZID']) . '000';
-				    else {
-					$sc['startTime'] = self::date2timestamp($startTime['value']) . '000';
-					if (strpos($params['prodid'], 'Outlook') !== false) {
-					    //Se o ics veio em utc não aplicar horario de verão
-					    $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), new DateTimeZone('UTC'));
-					    $sTime->setTimezone($objTimezone);
-					    if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
-						$sc['startTime'] = $sc['startTime'] - 3600000;
-					}
-				    }
-
-
-				    /* Data de Termino */
-				    $endTime = $component->getProperty('dtend', false, true);
-
-				    if (isset($endTime['params']['VALUE']) && $endTime['params']['VALUE'] === 'DATE')
-					$sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
-				    else if (isset($endTime['params']['TZID']) && !isset($endTime['value']['tz'])) /* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
-					$sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $endTime['params']['TZID']) . '000';
-				    else {
-					$sc['endTime'] = self::date2timestamp($endTime['value']) . '000';
-					if (strpos($params['prodid'], 'Outlook') !== false) {
-					    //Se o ics veio em utc não aplicar horario de verão
-					    $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), new DateTimeZone('UTC'));
-					    $eTime->setTimezone($objTimezone);
-					    if ($eTime->format('I'))
-						$sc['endTime'] = $sc['endTime'] - 3600000;
-					}
-				    }
-
-
-				    if ($uid = $component->getProperty('uid', false, false))
-					;
-				    $sc['uid'] = $uid;
-
-
-				    $sc['summary'] = mb_convert_encoding($component->getProperty('summary', false, false), 'UTF-8', 'UTF-8,ISO-8859-1');
-
-				    /* Definindo Description */
-				    if ($desc = $component->getProperty('description', false, false))
-					$sc['description'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $desc), 'UTF-8', 'UTF-8,ISO-8859-1');
-
-				    /* Definindo location */
-				    if ($location = $component->getProperty('location', false, false))
-					$sc['location'] = mb_convert_encoding($location, 'UTF-8', 'UTF-8,ISO-8859-1');
-
-
-
-				    if ($property = $component->getProperty('organizer', FALSE, TRUE)) {
-					$participant = array();
-					$mailUser = trim(str_replace('MAILTO:', '', $property['value']));
-
-					$participantID = mt_rand() . '2(Formatter)';
-
-					$participant['isOrganizer'] = '1';
-
-					$user = null;
-
-					$participant['isExternal'] = 1;
-					/* Gera um randon id para o contexto formater */
-					$userID = mt_rand() . '4(Formatter)';
-
-					$user['mail'] = $mailUser;
-					$organizerMail = $mailUser;
-
-					$user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
-					$user['isExternal'] = '1';
-					$participant['user'] = $user;
-
-					$sc['participants'][] = $participant;
-				    }
-
-
-				    $participant['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
-				    $participant['isOrganizer'] = '0';
-				    $participant['isExternal'] = 0;
-				    $participant['user'] = array('mail' => Config::me('mail'), 'name' => Config::me('cn'));
-				    $sc['participants'][] = $participant;
-				    $sc['type'] = EVENT_ID;
-
-
-				    $ical['source'] = Controller::format(array('service' => 'iCal'), array($sc), array('method' => 'REPLY'));
-				    $ical['type'] = 'application/ics';
-				    $ical['name'] = 'outlook.ics';
-
-				    $ical2['source'] = $ical['source'];
-				    $ical2['type'] = 'text/calendar; method=REPLY';
-				    $ical2['name'] = 'thunderbird.ics';
-
-				    $timezone = new DateTimeZone('UTC');
-				    $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), $timezone);
-				    $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), $timezone);
-
-				    if (isset($sc['timezone'])) {
-					$sTime->setTimezone(new DateTimeZone($sc['timezone']));
-					$eTime->setTimezone(new DateTimeZone($sc['timezone']));
-				    }
-
-				    $data = array('startDate' => date_format($sTime, 'd/m/Y'),
-					'startTime' => (isset($sc['allDay']) && $sc['allDay'] ) ? '' : date_format($sTime, 'H:i'),
-					'endDate' => date_format($eTime, 'd/m/Y'),
-					'endTime' => isset($sc['allDay']) ? '' : date_format($eTime, 'H:i'),
-					'eventTitle' => $sc['summary'],
-					'eventLocation' => isset($sc['location']) ? $sc['location'] : '',
-					'timezone' => ($sc['timezone']) ? $sc['timezone'] : 'UTC',
-					'participant' => (isset($part['user']['name']) ? $part['user']['name'] : $part['user']['mail']));
-
-				    $subject['notificationType'] = 'Convite Aceito';
-				    $subject['eventTitle'] = mb_convert_encoding($sc['summary'], 'ISO-8859-1', 'ISO-8859-1,UTF-8');
-				    $subject['startDate'] = date_format($sTime, 'd/m/Y');
-				    $subject['startTime'] = ($sc['allDay']) ? '' : date_format($sTime, 'H:i');
-				    $subject['endDate'] = date_format($eTime, 'd/m/Y');
-				    $subject['endTime'] = ($sc['allDay']) ? '' : date_format($eTime, 'H:i');
-				    $subject['participant'] = Config::me('uid');
-
-				    $params['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
-
-				    switch ($params['status']) {
-					case STATUS_ACCEPTED:
-					    $tpl = 'notify_accept_body';
-					    $subject['notificationType'] = 'Convite Aceito';
-					    break;
-					case STATUS_TENTATIVE:
-					    $tpl = 'notify_attempt_body';
-					    $subject['notificationType'] = 'Convite  aceito provisoriamente';
-					    break;
-					case STATUS_CANCELLED:
-					    $tpl = 'notify_reject_body';
-					    $subject['notificationType'] = 'Convite rejeitado';
-					    break;
-				    }
-				    require_once ROOTPATH . '/api/parseTPL.php';
-
-				    $mail = array();
-				    $mail['attachments'][] = $ical;
-				    $mail['attachments'][] = $ical2;
-
-				    $mail['isHtml'] = true;
-				    $mail['body'] = parseTPL::load_tpl($data, ROOTPATH . '/modules/calendar/templates/' . $tpl . '.tpl');
-				    $mail['subject'] = parseTPL::load_tpl($subject, ROOTPATH . '/modules/calendar/templates/notify_subject.tpl');
-				    ;
-				    $mail['from'] = '"' . Config::me('cn') . '" <' . Config::me('mail') . '>';
-				    $mail['to'] = $organizerMail;
-
-
-				    Controller::create(array('service' => 'SMTP'), $mail);
-				}
+                    }
+                } else { // Importar evento
+                    $interation = self::_makeVEVENT(array(), $component, $params);
+
+                    if (strpos($params['prodid'], 'kigkonsult.se') !== false) { //envia notificação para fora
+
+                        /* Data de Inicio */
+                        $startTime = $component->getProperty('dtstart', false, true);
+
+                        $tzid = isset($startTime['params']['TZID']) ? $startTime['params']['TZID'] : $params['X-WR-TIMEZONE'];
+
+                        /* Tiem zone do evento */
+                        if ($tzid)
+                        $sc['timezone'] = self::nomalizeTZID($tzid);
+                        else
+                        $sc['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
+
+                        $objTimezone = new DateTimeZone($sc['timezone']);
+
+                        if (isset($startTime['params']['VALUE']) && $startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone'])) {
+                        $sc['allDay'] = 1;
+                        $sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
+                        } elseif (isset($startTime['params']['TZID']) && !isset($startTime['value']['tz']))/* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
+                        $sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $startTime['params']['TZID']) . '000';
+                        else {
+                        $sc['startTime'] = self::date2timestamp($startTime['value']) . '000';
+                        if (strpos($params['prodid'], 'Outlook') !== false) {
+                            //Se o ics veio em utc não aplicar horario de verão
+                            $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), new DateTimeZone('UTC'));
+                            $sTime->setTimezone($objTimezone);
+                            if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
+                            $sc['startTime'] = $sc['startTime'] - 3600000;
+                        }
+                        }
+
+
+                        /* Data de Termino */
+                        $endTime = $component->getProperty('dtend', false, true);
+
+                        if (isset($endTime['params']['VALUE']) && $endTime['params']['VALUE'] === 'DATE')
+                        $sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
+                        else if (isset($endTime['params']['TZID']) && !isset($endTime['value']['tz'])) /* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
+                        $sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $endTime['params']['TZID']) . '000';
+                        else {
+                        $sc['endTime'] = self::date2timestamp($endTime['value']) . '000';
+                        if (strpos($params['prodid'], 'Outlook') !== false) {
+                            //Se o ics veio em utc não aplicar horario de verão
+                            $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), new DateTimeZone('UTC'));
+                            $eTime->setTimezone($objTimezone);
+                            if ($eTime->format('I'))
+                            $sc['endTime'] = $sc['endTime'] - 3600000;
+                        }
+                        }
+
+
+                        if ($uid = $component->getProperty('uid', false, false))
+                        ;
+                        $sc['uid'] = $uid;
+
+
+                        $sc['summary'] = mb_convert_encoding($component->getProperty('summary', false, false), 'UTF-8', 'UTF-8,ISO-8859-1');
+
+                        /* Definindo Description */
+                        if ($desc = $component->getProperty('description', false, false))
+                        $sc['description'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $desc), 'UTF-8', 'UTF-8,ISO-8859-1');
+
+                        /* Definindo location */
+                        if ($location = $component->getProperty('location', false, false))
+                        $sc['location'] = mb_convert_encoding($location, 'UTF-8', 'UTF-8,ISO-8859-1');
+
+
+
+                        if ($property = $component->getProperty('organizer', FALSE, TRUE)) {
+                        $participant = array();
+                        $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
+
+                        $participantID = mt_rand() . '2(Formatter)';
+
+                        $participant['isOrganizer'] = '1';
+
+                        $user = null;
+
+                        $participant['isExternal'] = 1;
+                        /* Gera um randon id para o contexto formater */
+                        $userID = mt_rand() . '4(Formatter)';
+
+                        $user['mail'] = $mailUser;
+                        $organizerMail = $mailUser;
+
+                        $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
+                        $user['isExternal'] = '1';
+                        $participant['user'] = $user;
+
+                        $sc['participants'][] = $participant;
+                        }
+
+
+                        $participant['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
+                        $participant['isOrganizer'] = '0';
+                        $participant['isExternal'] = 0;
+                        $participant['user'] = array('mail' => Config::me('mail'), 'name' => Config::me('cn'));
+                        $sc['participants'][] = $participant;
+                        $sc['type'] = EVENT_ID;
+
+
+                        $ical['source'] = Controller::format(array('service' => 'iCal'), array($sc), array('method' => 'REPLY'));
+                        $ical['type'] = 'application/ics';
+                        $ical['name'] = 'outlook.ics';
+
+                        $ical2['source'] = $ical['source'];
+                        $ical2['type'] = 'text/calendar; method=REPLY';
+                        $ical2['name'] = 'thunderbird.ics';
+
+                        $timezone = new DateTimeZone('UTC');
+                        $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), $timezone);
+                        $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), $timezone);
+
+                        if (isset($sc['timezone'])) {
+                        $sTime->setTimezone(new DateTimeZone($sc['timezone']));
+                        $eTime->setTimezone(new DateTimeZone($sc['timezone']));
+                        }
+
+                        $data = array('startDate' => date_format($sTime, 'd/m/Y'),
+                        'startTime' => (isset($sc['allDay']) && $sc['allDay'] ) ? '' : date_format($sTime, 'H:i'),
+                        'endDate' => date_format($eTime, 'd/m/Y'),
+                        'endTime' => isset($sc['allDay']) ? '' : date_format($eTime, 'H:i'),
+                        'eventTitle' => $sc['summary'],
+                        'eventLocation' => isset($sc['location']) ? $sc['location'] : '',
+                        'timezone' => ($sc['timezone']) ? $sc['timezone'] : 'UTC',
+                        'participant' => (isset($part['user']['name']) ? $part['user']['name'] : $part['user']['mail']));
+
+                        $subject['notificationType'] = 'Convite Aceito';
+                        $subject['eventTitle'] = mb_convert_encoding($sc['summary'], 'ISO-8859-1', 'ISO-8859-1,UTF-8');
+                        $subject['startDate'] = date_format($sTime, 'd/m/Y');
+                        $subject['startTime'] = ($sc['allDay']) ? '' : date_format($sTime, 'H:i');
+                        $subject['endDate'] = date_format($eTime, 'd/m/Y');
+                        $subject['endTime'] = ($sc['allDay']) ? '' : date_format($eTime, 'H:i');
+                        $subject['participant'] = Config::me('uid');
+
+                        $params['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
+
+                        switch ($params['status']) {
+                        case STATUS_ACCEPTED:
+                            $tpl = 'notify_accept_body';
+                            $subject['notificationType'] = 'Convite Aceito';
+                            break;
+                        case STATUS_TENTATIVE:
+                            $tpl = 'notify_attempt_body';
+                            $subject['notificationType'] = 'Convite  aceito provisoriamente';
+                            break;
+                        case STATUS_CANCELLED:
+                            $tpl = 'notify_reject_body';
+                            $subject['notificationType'] = 'Convite rejeitado';
+                            break;
+                        }
+                        require_once ROOTPATH . '/api/parseTPL.php';
+
+                        $mail = array();
+                        $mail['attachments'][] = $ical;
+                        $mail['attachments'][] = $ical2;
+
+                        $mail['isHtml'] = true;
+                        $mail['body'] = parseTPL::load_tpl($data, ROOTPATH . '/modules/calendar/templates/' . $tpl . '.tpl');
+                        $mail['subject'] = parseTPL::load_tpl($subject, ROOTPATH . '/modules/calendar/templates/notify_subject.tpl');
+                        ;
+                        $mail['from'] = '"' . Config::me('cn') . '" <' . Config::me('mail') . '>';
+                        $mail['to'] = $organizerMail;
+
+
+                        Controller::create(array('service' => 'SMTP'), $mail);
+                    }
 			    }
 			    break;
@@ -1189,45 +1192,59 @@
 			    $schedulable = self::_getSchedulable($uid);
 			    if ($schedulable) { //Caso o evento exista
-				$isOrganizer = false;
-				$isParticipant = false;
-
-				foreach ($schedulable['participants'] as $value)
-				    if ($value['user']['id'] == $params['owner']) {
-    					$isParticipant = true;
-    					if ($value['isOrganizer'])
-    					    $isOrganizer = true;
-
-    					if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
-    					    $interation = ICAL_ACTION_UPDATE;
-    					    $interation = ( strrpos($value['acl'], ATTENDEE_ACL_PARTICIPATION_REQUIRED) ) ? ICAL_ACTION_IMPORT_REQUIRED : array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
-    					    break;
-    					}
-				    } else {
-    					if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']) //Organizador esta requisitando que você atualize o evento
-    					    $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_UPDATE : ICAL_ACTION_UPDATE;
-    					else
-    					    $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_NONE : ICAL_ACTION_NONE;
-				    }
-				if (!$isParticipant){
-				      $interation = self::_checkParticipantByPermissions($schedulable);
-				    }
-			    }else
-				    $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
-			    
-			    if(($interation != ICAL_ACTION_NONE) && ($interation != ICAL_ACTION_ORGANIZER_NONE) && ($interation != ICAL_ACTION_ORGANIZER_UPDATE) && ($interation != ICAL_ACTION_NONE)) {
-				if($params['owner'] != Config::me("uidNumber")){
-				    $sig = Controller::find(array('concept' => 'calendarSignature'), array('calendar'), array('filter' => array('AND', array('=', 'user', $params['owner']), array('=', 'isOwner', '1'))));
-				    $calendars = array();
-				    foreach ($sig as $val)
-					$calendars[] = $val['calendar'];
-				
-				    $calendarsPermission = Controller::find(array('concept' => 'calendarToPermission'), array('calendar'), array('filter' => array('AND', array('=', 'user', Config::me("uidNumber")), array('IN', 'calendar', $calendars))));   
-				    
-				    foreach ($calendarsPermission as $val)
-					$ids[] = $val['calendar'];
-
-				    $interation = array('action' => ICAL_ACTION_IMPORT_FROM_PERMISSION ,'calendar' => $ids);
-				}
-			    }
+                    $isOrganizer = false;
+                    $isParticipant = false;
+
+                    foreach ($schedulable['participants'] as $value){
+
+                        if ($value['user']['id'] == $params['owner']) {
+                            $isParticipant = true;
+                            if ($value['isOrganizer'])
+                                $isOrganizer = true;
+
+                            if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
+                                $interation = ICAL_ACTION_UPDATE;
+                                $interation = ( strrpos($value['acl'], ATTENDEE_ACL_PARTICIPATION_REQUIRED) ) ? ICAL_ACTION_IMPORT_REQUIRED : array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
+                                break;
+                            }
+                        } else {
+                            if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']) //Organizador esta requisitando que você atualize o evento
+                                $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_UPDATE : ICAL_ACTION_UPDATE;
+                            else
+                                $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_NONE : ICAL_ACTION_NONE;
+                        }
+
+                    }
+
+                    if (!$isParticipant){
+                        if( self::_existInMyCalendars($schedulable['id'], $params['owner']) ){
+
+                            $interation = (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] ? ICAL_ACTION_UPDATE  :  ICAL_ACTION_NONE);
+
+                        }else{
+
+                            $interation =  self::_checkParticipantByPermissions($schedulable);
+
+                        }
+
+
+                    }
+                }else
+                    $interation = array('action' => ICAL_ACTION_IMPORT_REQUIRED, 'type' => 'calendarIds');
+
+                    if(($interation != ICAL_ACTION_NONE) && ($interation != ICAL_ACTION_ORGANIZER_NONE) && ($interation != ICAL_ACTION_ORGANIZER_UPDATE) && ($interation != ICAL_ACTION_NONE) && ($interation != ICAL_ACTION_UPDATE) && (!is_array($interation ) )) {
+                        if($params['owner'] != Config::me("uidNumber")){
+                            $sig = Controller::find(array('concept' => 'calendarSignature'), array('calendar'), array('filter' => array('AND', array('=', 'user', $params['owner']), array('=', 'isOwner', '1'))));
+                            $calendars = array();
+                            foreach ($sig as $val)
+                            $calendars[] = $val['calendar'];
+
+                            $calendarsPermission = Controller::find(array('concept' => 'calendarToPermission'), array('calendar'), array('filter' => array('AND', array('=', 'user', Config::me("uidNumber")), array('IN', 'calendar', $calendars))));
+
+                            foreach ($calendarsPermission as $val)
+                            $ids[] = $val['calendar'];
+
+                            $interation = array('action' => ICAL_ACTION_IMPORT_FROM_PERMISSION ,'calendar' => $ids);
+                        }
+                    }
 
 			    break;
Index: /trunk/expressoMail1_2/js/main.js
===================================================================
--- /trunk/expressoMail1_2/js/main.js	(revision 7690)
+++ /trunk/expressoMail1_2/js/main.js	(revision 7695)
@@ -5077,5 +5077,5 @@
     function decodeOwner(){
 	owner = User.me.id;
-	var imapBox = openTab.imapBox[currentTab].split(cyrus_delimiter);
+	var imapBox = (openTab.imapBox[currentTab].indexOf( 'user' ) >= 0 ? openTab.imapBox[currentTab].split(cyrus_delimiter) : [] );
 	if(imapBox.length > 1){
 	    var user = DataLayer.get('user', {filter: ['=','uid',imapBox[1]]});		    
@@ -5206,4 +5206,5 @@
 					return;
 					break;
+					case 4:
 					case 9:
 					    alert('Seu evento/tarefa não possui alterações!');
