Index: trunk/prototype/services/iCal.php
===================================================================
--- trunk/prototype/services/iCal.php	(revision 5341)
+++ trunk/prototype/services/iCal.php	(revision 5344)
@@ -142,5 +142,7 @@
                                break;
 
-                         default:
+                         default:                             
+                               $veventID = mt_rand().'1(Formatter)';
+                               
                                if($exists !== false)
                                {
@@ -155,4 +157,5 @@
                                       $calendarToSchedulable['schedulable'] = $exists['id'];
                                       $interation['calendarToSchedulable://'.mt_rand().'(Formatter)'] = $calendarToSchedulable;
+                                      continue;
                                   }
 // Codigo para atualizar status do importador. não nescessario pois sera enviado um email de configmação para o organizador.
@@ -160,208 +163,206 @@
 //                           if($participant['user']['id'] == Config::me('uidNumber'))
 //                               $interation['participant://'.$participant['id']] = array( 'id' => $participant['id'], 'status' => STATUS_CONFIRMED);
-                               
+                                    
+                                 $veventID = $exists['id'];
+                                 $vevent = $exists;
                                }
-                               else
-                               {   
-                                    /* Gera um randon id para o contexto formater */
-                                        $veventID = mt_rand().'1(Formatter)';
-
-                                    /* Data de Inicio*/
-                                        $startTime = $component->getProperty( 'dtstart', false , true );
-
-                                    /* Tiem zone do evento*/    
-                                       if(isset($startTime['params']['TZID']))
-                                          $vevent['timezone'] = self::nomalizeTZID ($startTime['params']['TZID']); 
-                                       else
-                                          $vevent['timezone'] = $params['calendar_timezone'] ? $params['calendar_timezone'] : 'America/Sao_Paulo'; 
-
-                                        if($startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone']) )
-                                        {
-                                            $vevent['allDay'] = 1;
-                                            $vevent['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC',$params['calendar_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 */
-                                            $vevent['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC',$startTime['params']['TZID']) . '000';      
-                                        else
-                                            $vevent['startTime'] = self::date2timestamp($startTime['value']) . '000';                         
-
-                                     /* Data de Termino*/
-                                        $endTime = $component->getProperty( 'dtend', false , true ); 
-
-                                        if($endTime['params']['VALUE'] === 'DATE')
-                                            $vevent['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC',$params['calendar_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 */
-                                            $vevent['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC',$endTime['params']['TZID']) . '000';
-                                        else
-                                            $vevent['endTime'] = self::date2timestamp($endTime['value']) . '000';
-
-                                    $vevent['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 ))
-                                            $vevent['description'] = mb_convert_encoding (str_ireplace ('\n', "\n", $desc) ,'UTF-8' , 'UTF-8,ISO-8859-1');
-
-                                     /* Definindo location */
-                                        if($location = $component->getProperty( 'location' , false , false ))
-                                            $vevent['location'] = mb_convert_encoding ( $location , 'UTF-8' , 'UTF-8,ISO-8859-1') ;
-
-
-                                    /* Definindo Class */
-                                        $class = $component->getProperty( 'class' , false , false );
-                                        if( $class && defined(constant(strtoupper('CLASS_'.$class))) )
-                                            $vevent['class'] = constant(strtoupper('CLASS_'.$class));
-                                        else if( !isset($exists['class']) )
-                                            $vevent['class'] = CLASS_PRIVATE ; // padrão classe private
-
-                                    /* Definindo RRULE */
-                                        if(  $rrule = $component->getProperty( 'rrule' , false , false ))
-                                        {   
-                                            /* Gera um randon id para o contexto formater */
-                                                $repeatID = mt_rand().'3(Formatter)';
-
-                                            $repeat = array();
-                                            $repeat['schedulable'] = $veventID;
-                                            foreach($rrule as $i => $v)
-                                               $repeat[strtolower($i)] = $v;
-
-                                            $interation['repeat://'.$repeatID] = $repeat;
-                                        }
-
-
-                                    $vevent['calendar'] = $params['calendar'];           
-
-                                    $participantsInEvent = array();
-
-                                    //TODO: Participants com delegated não estao sendo levados em conta
-                                    while($property = $component->getProperty('attendee',FALSE , TRUE))
+                          
+                                /* Data de Inicio*/
+                                    $startTime = $component->getProperty( 'dtstart', false , true );
+
+                                /* Tiem zone do evento*/    
+                                   if(isset($startTime['params']['TZID']))
+                                      $vevent['timezone'] = self::nomalizeTZID ($startTime['params']['TZID']); 
+                                   else
+                                      $vevent['timezone'] = $params['calendar_timezone'] ? $params['calendar_timezone'] : 'America/Sao_Paulo'; 
+
+                                    if($startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone']) )
                                     {
-                                        $participant = array();
-
-                                        $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
-
+                                        $vevent['allDay'] = 1;
+                                        $vevent['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC',$params['calendar_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 */
+                                        $vevent['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC',$startTime['params']['TZID']) . '000';      
+                                    else
+                                        $vevent['startTime'] = self::date2timestamp($startTime['value']) . '000';                         
+
+                                 /* Data de Termino*/
+                                    $endTime = $component->getProperty( 'dtend', false , true ); 
+
+                                    if($endTime['params']['VALUE'] === 'DATE')
+                                        $vevent['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC',$params['calendar_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 */
+                                        $vevent['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC',$endTime['params']['TZID']) . '000';
+                                    else
+                                        $vevent['endTime'] = self::date2timestamp($endTime['value']) . '000';
+
+                                $vevent['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 ))
+                                        $vevent['description'] = mb_convert_encoding (str_ireplace ('\n', "\n", $desc) ,'UTF-8' , 'UTF-8,ISO-8859-1');
+
+                                 /* Definindo location */
+                                    if($location = $component->getProperty( 'location' , false , false ))
+                                        $vevent['location'] = mb_convert_encoding ( $location , 'UTF-8' , 'UTF-8,ISO-8859-1') ;
+
+
+                                /* Definindo Class */
+                                    $class = $component->getProperty( 'class' , false , false );
+                                    if( $class && defined(constant(strtoupper('CLASS_'.$class))) )
+                                        $vevent['class'] = constant(strtoupper('CLASS_'.$class));
+                                    else if( !isset($exists['class']) )
+                                        $vevent['class'] = CLASS_PRIVATE ; // padrão classe private
+
+                                /* Definindo RRULE */
+                                    if(  $rrule = $component->getProperty( 'rrule' , false , false ))
+                                    {   
+                                        /* Gera um randon id para o contexto formater */
+                                            $repeatID = mt_rand().'3(Formatter)';
+
+                                        $repeat = array();
+                                        $repeat['schedulable'] = $veventID;
+                                        foreach($rrule as $i => $v)
+                                           $repeat[strtolower($i)] = $v;
+
+                                        $interation['repeat://'.$repeatID] = $repeat;
+                                    }
+
+
+                                $vevent['calendar'] = $params['calendar'];           
+
+                                $participantsInEvent = array();
+
+                                //TODO: Participants com delegated não estao sendo levados em conta
+                                while($property = $component->getProperty('attendee',FALSE , TRUE))
+                                {
+                                    $participant = array();
+
+                                    $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
+
+                                    $participantID = mt_rand().'2(Formatter)';
+                                    $participant['schedulable'] = $veventID;                                      
+                                    $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_'.$property['params']['PARTSTAT']) !== null ) ? constant('STATUS_'.$property['params']['PARTSTAT']) : STATUS_UNANSWERED;
+                                    $participant['isOrganizer'] = '0';
+
+                                    /* Verifica se este usuario é um usuario interno do ldap */     
+                                    $intUser = Controller::find( array( 'concept' => 'user', 'service' => 'OpenLDAP' ) , array('uidNumber') ,array( 'filter' => array('OR' , array('=' , 'mail' ,  $mailUser ), array('=' , 'mailAlternateAddress' ,  $mailUser ) )) ); 
+
+                                    if( !is_array($intUser) || count($intUser) < 1 )
+                                         $intUser = Controller::find( array( 'concept' => 'user' ,'service' => 'PostgreSQL') , array('uidNumber') ,array( 'filter' => array('=' , 'mail' ,  $mailUser )) ); 
+
+
+                                    $user = null;    
+                                    if( $intUser && count($intUser) > 0 )
+                                    {
+                                        $participant['isExternal']  = 0;
+                                        $participant['user'] = $intUser[0]['id'];
+                                    }
+                                    else
+                                    {
+                                        $participant['isExternal']  = 1;
+                                         /* Gera um randon id para o contexto formater */
+                                            $userID = mt_rand().'4(Formatter)';
+
+                                        $user['mail'] = $mailUser;
+                                        $user['isExternal'] = '1';
+                                        $user['name'] =  ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
+                                        $user['participants'] = array($participantID);
+                                        $participant['user'] = $userID;
+                                        $interation['user://'.$userID] = $user;
+                                    }
+
+                                    $interation['participant://'.$participantID] = $participant;
+                                    $vevent['participants'][] = $participantID;
+
+                                };
+
+                                if($property = $component->getProperty('organizer',FALSE , TRUE))
+                                {
+                                    $participant = array();
+                                    $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
+
+                                    $participantID = mt_rand().'2(Formatter)';
+
+                                    $participant['schedulable'] = $veventID;
+                                    $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_'.$property['params']['PARTSTAT']) !== null ) ? constant('STATUS_'.$property['params']['PARTSTAT']) : STATUS_UNANSWERED;
+                                    $participant['isOrganizer'] = '1';
+
+                                    /* Verifica se este usuario é um usuario interno do ldap */     
+                                    $intUser = Controller::find( array( 'concept' => 'user' ,'service' => 'OpenLDAP') , array('uidNumber') ,array( 'filter' => array('OR' , array('=' , 'mail' ,  $mailUser ), array('=' , 'mailAlternateAddress' ,  $mailUser ) )) ); 
+
+                                    if( !is_array($intUser) || count($intUser) < 1 )
+                                         $intUser = Controller::find( array( 'concept' => 'user' ,'service' => 'PostgreSQL') , array('uidNumber') ,array( 'filter' => array('=' , 'mail' ,  $mailUser )) ); 
+
+
+
+                                    $user = null;    
+                                    if( $intUser && count($intUser) > 0 )
+                                    {
+                                        $participant['isExternal']  = 0;
+                                        $participant['user'] = $intUser[0]['id'];
+                                    }
+                                    else
+                                    {
+                                        $participant['isExternal']  = 1;
+                                         /* Gera um randon id para o contexto formater */
+                                            $userID = mt_rand().'4(Formatter)';
+
+                                        $user['mail'] = $mailUser;
+                                        $user['name'] =  ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
+                                        $user['participants'] = array($participantID);
+                                        $user['isExternal'] = '1';
+                                        $participant['user'] = $userID;
+                                        $interation['user://'.$userID] = $user;
+                                    }
+
+                                    $interation['participant://'.$participantID] = $participant;
+                                    $vevent['participants'][] = $participantID;
+
+                                }
+                                else //caso não tenha organizador o usuario se torna organizador
+                                {
+                                   $user = Controller::read( array( 'concept' => 'user', 'id' => $params['owner'] ) , array('mail')); 
+
+                                   if(!self::_getParticipantByMail($user['mail'] , $exists['participants']))
+                                   {
                                         $participantID = mt_rand().'2(Formatter)';
-                                        $participant['schedulable'] = $veventID;                                      
-                                        $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_'.$property['params']['PARTSTAT']) !== null ) ? constant('STATUS_'.$property['params']['PARTSTAT']) : STATUS_UNANSWERED;
-                                        $participant['isOrganizer'] = '0';
-
-                                        /* Verifica se este usuario é um usuario interno do ldap */     
-                                        $intUser = Controller::find( array( 'concept' => 'user', 'service' => 'OpenLDAP' ) , array('uidNumber') ,array( 'filter' => array('OR' , array('=' , 'mail' ,  $mailUser ), array('=' , 'mailAlternateAddress' ,  $mailUser ) )) ); 
-                                            
-                                        if( !is_array($intUser) || count($intUser) < 1 )
-                                             $intUser = Controller::find( array( 'concept' => 'user' ,'service' => 'PostgreSQL') , array('uidNumber') ,array( 'filter' => array('=' , 'mail' ,  $mailUser )) ); 
-                                       
-                                        
-                                        $user = null;    
-                                        if( $intUser && count($intUser) > 0 )
-                                        {
-                                            $participant['isExternal']  = 0;
-                                            $participant['user'] = $intUser[0]['id'];
-                                        }
-                                        else
-                                        {
-                                            $participant['isExternal']  = 1;
-                                             /* Gera um randon id para o contexto formater */
-                                                $userID = mt_rand().'4(Formatter)';
-
-                                            $user['mail'] = $mailUser;
-                                            $user['isExternal'] = '1';
-                                            $user['name'] =  ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
-                                            $user['participants'] = array($participantID);
-                                            $participant['user'] = $userID;
-                                            $interation['user://'.$userID] = $user;
-                                        }
-
+
+                                        $participant['schedulable'] = $veventID;
+                                        $participant['status'] = STATUS_CONFIRMED;
+                                        $participant['isOrganizer'] = '1';
+                                        $participant['isExternal']  = 0;
+                                        $participant['user'] =  $params['owner'];
                                         $interation['participant://'.$participantID] = $participant;
                                         $vevent['participants'][] = $participantID;
-                                         
-                                    };
-
-                                    if($property = $component->getProperty('organizer',FALSE , TRUE))
-                                    {
-                                        $participant = array();
-                                        $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
-
-                                        $participantID = mt_rand().'2(Formatter)';
-
-                                        $participant['schedulable'] = $veventID;
-                                        $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_'.$property['params']['PARTSTAT']) !== null ) ? constant('STATUS_'.$property['params']['PARTSTAT']) : STATUS_UNANSWERED;
-                                        $participant['isOrganizer'] = '1';
-
-                                        /* Verifica se este usuario é um usuario interno do ldap */     
-                                        $intUser = Controller::find( array( 'concept' => 'user' ,'service' => 'OpenLDAP') , array('uidNumber') ,array( 'filter' => array('OR' , array('=' , 'mail' ,  $mailUser ), array('=' , 'mailAlternateAddress' ,  $mailUser ) )) ); 
-                                       
-                                        if( !is_array($intUser) || count($intUser) < 1 )
-                                             $intUser = Controller::find( array( 'concept' => 'user' ,'service' => 'PostgreSQL') , array('uidNumber') ,array( 'filter' => array('=' , 'mail' ,  $mailUser )) ); 
-                                       
-                                        
-                                        
-                                        $user = null;    
-                                        if( $intUser && count($intUser) > 0 )
-                                        {
-                                            $participant['isExternal']  = 0;
-                                            $participant['user'] = $intUser[0]['id'];
-                                        }
-                                        else
-                                        {
-                                            $participant['isExternal']  = 1;
-                                             /* Gera um randon id para o contexto formater */
-                                                $userID = mt_rand().'4(Formatter)';
-
-                                            $user['mail'] = $mailUser;
-                                            $user['name'] =  ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
-                                            $user['participants'] = array($participantID);
-                                            $user['isExternal'] = '1';
-                                            $participant['user'] = $userID;
-                                            $interation['user://'.$userID] = $user;
-                                        }
-                                        
-                                        $interation['participant://'.$participantID] = $participant;
-                                        $vevent['participants'][] = $participantID;
-                                         
-                                    }
-                                    else //caso não tenha organizador o usuario se torna organizador
-                                    {
-                                       $user = Controller::read( array( 'concept' => 'user', 'id' => $params['owner'] ) , array('mail')); 
-
-                                       if(!self::_getParticipantByMail($user['mail'] , $exists['participants']))
-                                       {
-                                            $participantID = mt_rand().'2(Formatter)';
-
-                                            $participant['schedulable'] = $veventID;
-                                            $participant['status'] = STATUS_CONFIRMED;
-                                            $participant['isOrganizer'] = '1';
-                                            $participant['isExternal']  = 0;
-                                            $participant['user'] =  $params['owner'];
-                                            $interation['participant://'.$participantID] = $participant;
-                                            $vevent['participants'][] = $participantID;
-                                       }
-                                    }
-
-                                     /* Definindo DTSTAMP */
-                                    if($dtstamp = $component->getProperty( 'dtstamp', false , true ))
-                                    {
-                                        if(isset($dtstamp['params']['TZID']) && !isset($dtstamp['value']['tz']))
-                                            $vevent['dtstamp'] = self::date2timestamp($dtstamp['value']) - self::_getTzOffset('UTC',$dtstamp['params']['TZID']) . '000';
-                                        else
-                                            $vevent['dtstamp'] = self::date2timestamp($dtstamp['value']) . '000';
-                                    }
-
-                                    /* Definindo TRANSP */
-                                    if(($tranp = $component->getProperty( 'transp', false , true )) && strtoupper($tranp) == 'OPAQUE')
-                                        $vevent['transparent'] = 1;
-
-                                     /* Definindo last_update */
-                                    if($lastUpdate = $component->getProperty( 'LAST-MODIFIED', false , true ))
-                                    {
-                                        if(isset($lastUpdate['params']['TZID']) && !isset($lastUpdate['value']['tz']))
-                                            $vevent['lasUpdate'] = self::date2timestamp($lastUpdate['value']) - self::_getTzOffset('UTC',$lastUpdate['params']['TZID']) . '000';
-                                        else
-                                            $vevent['lastUpdate'] = self::date2timestamp($lastUpdate['value']) . '000';
-                                    }
-
-                                    $interation['schedulable://'.$veventID] = $vevent;
-
-                               }
+                                   }
+                                }
+
+                                 /* Definindo DTSTAMP */
+                                if($dtstamp = $component->getProperty( 'dtstamp', false , true ))
+                                {
+                                    if(isset($dtstamp['params']['TZID']) && !isset($dtstamp['value']['tz']))
+                                        $vevent['dtstamp'] = self::date2timestamp($dtstamp['value']) - self::_getTzOffset('UTC',$dtstamp['params']['TZID']) . '000';
+                                    else
+                                        $vevent['dtstamp'] = self::date2timestamp($dtstamp['value']) . '000';
+                                }
+
+                                /* Definindo TRANSP */
+                                if(($tranp = $component->getProperty( 'transp', false , true )) && strtoupper($tranp) == 'OPAQUE')
+                                    $vevent['transparent'] = 1;
+
+                                 /* Definindo last_update */
+                                if($lastUpdate = $component->getProperty( 'LAST-MODIFIED', false , true ))
+                                {
+                                    if(isset($lastUpdate['params']['TZID']) && !isset($lastUpdate['value']['tz']))
+                                        $vevent['lasUpdate'] = self::date2timestamp($lastUpdate['value']) - self::_getTzOffset('UTC',$lastUpdate['params']['TZID']) . '000';
+                                    else
+                                        $vevent['lastUpdate'] = self::date2timestamp($lastUpdate['value']) . '000';
+                                }
+
+                                $interation['schedulable://'.$veventID] = $vevent;
+
                                break;
+                             
                     }
                     
