Ejemplo n.º 1
0
 /**
  * Returns all participant models for this event and all the related events for a meeting.
  * 
  * @return Participant
  */
 public function getParticipantsForUser()
 {
     //update all participants with this user and event uuid in the system
     $findParams = \GO\Base\Db\FindParams::newInstance();
     $findParams->joinModel(array('model' => 'GO\\Calendar\\Model\\Event', 'localTableAlias' => 't', 'localField' => 'event_id', 'foreignField' => 'id', 'tableAlias' => 'e'));
     $findParams->getCriteria()->addCondition('user_id', $this->user_id)->addCondition('uuid', $this->uuid, '=', 'e')->addCondition('start_time', $this->start_time, '=', 'e')->addCondition("exception_for_event_id", 0, $this->exception_for_event_id == 0 ? '=' : '!=', 'e');
     //the master event or a single occurrence can start at the same time. Therefore we must check if exception event has a value or is 0.
     return Participant::model()->find($findParams);
 }
Ejemplo n.º 2
0
 public function actionInvitation($params)
 {
     $participant = \GO\Calendar\Model\Participant::model()->findSingleByAttributes(array('event_id' => $params['id'], 'email' => $params['email']));
     if (!$participant) {
         throw new \Exception("Could not find the event");
     }
     if ($participant->getSecurityToken() != $params['participantToken']) {
         throw new \Exception("Invalid request");
     }
     if (empty($params['accept'])) {
         $participant->status = \GO\Calendar\Model\Participant::STATUS_DECLINED;
     } else {
         $participant->status = \GO\Calendar\Model\Participant::STATUS_ACCEPTED;
     }
     //save will be handled by organizer when he get's an email
     $participant->save();
     $event = $participant->getParticipantEvent();
     if (!$event && $participant->user_id) {
         //if the participant is a user and there's no event for him yet then create it.
         $participant->event->createCopyForParticipant($participant);
     }
     if ($event) {
         $event->replyToOrganizer();
     } else {
         $participant->event->replyToOrganizer(false, $participant, false);
     }
     $this->render('invitation', array('participant' => $participant, 'event' => $event));
 }
Ejemplo n.º 3
0
 /**
  * Import an event from a VObject 
  * 
  * @param Sabre\VObject\Component $vobject
  * @param array $attributes Extra attributes to apply to the event. Raw values should be past. No input formatting is applied.
  * @param boolean $dontSave. Don't save the event. WARNING. Event can't be fully imported this way because participants and exceptions need an ID. This option is useful if you want to display info about an ICS file.
  * @param boolean $importExternal This should be switched on if importing happens from external ICS calendar.
  * @return Event 
  */
 public function importVObject(Sabre\VObject\Component $vobject, $attributes = array(), $dontSave = false, $makeSureUserParticipantExists = false, $importExternal = false)
 {
     $uid = (string) $vobject->uid;
     if (!empty($uid)) {
         $this->uuid = $uid;
     }
     $this->name = (string) $vobject->summary;
     if (empty($this->name)) {
         $this->name = \GO::t('unnamed');
     }
     $dtstart = $vobject->dtstart ? $vobject->dtstart->getDateTime() : new \DateTime();
     $dtend = $vobject->dtend ? $vobject->dtend->getDateTime() : new \DateTime();
     $substractOnEnd = 0;
     //funambol sends this special parameter
     //		if((string) $vobject->{"X-FUNAMBOL-ALLDAY"}=="1"){
     //			$this->all_day_event=1;
     //		}else
     //		{
     $this->all_day_event = isset($vobject->dtstart['VALUE']) && $vobject->dtstart['VALUE'] == 'DATE' ? 1 : 0;
     //ios sends start and end date at 00:00 hour
     //DTEND;TZID=Europe/Amsterdam:20140121T000000
     //DTSTART;TZID=Europe/Amsterdam:20140120T000000
     if ($dtstart->format('Hi') == "0000" && $dtend->format('Hi') == "0000") {
         $this->all_day_event = true;
         $substractOnEnd = 60;
     }
     //		}
     if ($this->all_day_event) {
         if ($dtstart->getTimezone()->getName() == 'UTC') {
             $this->_utcToLocal($dtstart);
         }
         if ($dtend->getTimezone()->getName() == 'UTC') {
             $this->_utcToLocal($dtend);
         }
     }
     $this->start_time = intval($dtstart->format('U'));
     $this->end_time = intval($dtend->format('U')) - $substractOnEnd;
     if ($vobject->duration) {
         $duration = \GO\Base\VObject\Reader::parseDuration($vobject->duration);
         $this->end_time = $this->start_time + $duration;
     }
     if ($this->end_time <= $this->start_time) {
         $this->end_time = $this->start_time + 3600;
     }
     if ($vobject->description) {
         $this->description = (string) $vobject->description;
     }
     if ((string) $vobject->rrule != "") {
         $rrule = new \GO\Base\Util\Icalendar\Rrule();
         $rrule->readIcalendarRruleString($this->start_time, (string) $vobject->rrule);
         $rrule->shiftDays(true);
         $this->rrule = $rrule->createRrule();
         $this->repeat_end_time = $rrule->until;
     } else {
         $this->rrule = "";
         $this->repeat_end_time = 0;
     }
     if ($vobject->{"last-modified"}) {
         $this->mtime = intval($vobject->{"last-modified"}->getDateTime()->format('U'));
     }
     if ($vobject->location) {
         $this->location = (string) $vobject->location;
     }
     //var_dump($vobject->status);
     if ($vobject->status) {
         $status = (string) $vobject->status;
         if ($this->isValidStatus($status)) {
             $this->status = $status;
         }
     }
     if (isset($vobject->class)) {
         $this->private = strtoupper($vobject->class) != 'PUBLIC';
     }
     $this->reminder = 0;
     //		if($vobject->valarm && $vobject->valarm->trigger){
     //
     //			$type = (string) $vobject->valarm->trigger["value"];
     //
     //
     //			if($type == "DURATION") {
     //				$duration = \GO\Base\VObject\Reader::parseDuration($vobject->valarm->trigger);
     //				if($duration>0){
     //					$this->reminder = $duration*-1;
     //				}
     //			}else
     //			{
     //				\GO::debug("WARNING: Ignoring unsupported reminder value of type: ".$type);
     //			}
     //
     if ($vobject->valarm && $vobject->valarm->trigger) {
         $date = $vobject->valarm->getEffectiveTriggerTime();
         if ($date) {
             if ($this->all_day_event) {
                 $this->_utcToLocal($date);
             }
             $this->reminder = $this->start_time - $date->format('U');
         }
     } elseif ($vobject->aalarm) {
         //funambol sends old vcalendar 1.0 format
         $aalarm = explode(';', (string) $vobject->aalarm);
         if (!empty($aalarm[0])) {
             $p = Sabre\VObject\DateTimeParser::parse($aalarm[0]);
             $this->reminder = $this->start_time - $p->format('U');
         }
     }
     $this->setAttributes($attributes, false);
     $recurrenceIds = $vobject->select('recurrence-id');
     if (count($recurrenceIds)) {
         //this is a single instance of a recurring series.
         //attempt to find the exception of the recurring series event by uuid
         //and recurrence time so we can set the relation cal_exceptions.exception_event_id=cal_events.id
         $firstMatch = array_shift($recurrenceIds);
         $recurrenceTime = $firstMatch->getDateTime()->format('U');
         $whereCriteria = \GO\Base\Db\FindCriteria::newInstance()->addCondition('calendar_id', $this->calendar_id, '=', 'ev')->addCondition('uuid', $this->uuid, '=', 'ev')->addCondition('time', $recurrenceTime, '=', 't');
         $joinCriteria = \GO\Base\Db\FindCriteria::newInstance()->addCondition('event_id', 'ev.id', '=', 't', true, true);
         $findParams = \GO\Base\Db\FindParams::newInstance()->single()->criteria($whereCriteria)->join(Event::model()->tableName(), $joinCriteria, 'ev');
         $exception = Exception::model()->find($findParams);
         if ($exception) {
             $this->exception_for_event_id = $exception->event_id;
             if (empty($this->name) || $this->name == \GO::t('unnamed')) {
                 $this->name = $exception->mainevent->name;
             }
         } else {
             //exception was not found for this recurrence. Find the recurring series and add the exception.
             $recurringEvent = Event::model()->findByUuid($this->uuid, 0, $this->calendar_id);
             if ($recurringEvent) {
                 \GO::debug("Creating MISSING exception for " . date('c', $recurrenceTime));
                 //aftersave will create Exception
                 $this->exception_for_event_id = $recurringEvent->id;
                 //will be saved later
                 $exception = new Exception();
                 $exception->time = $recurrenceTime;
                 $exception->event_id = $recurringEvent->id;
                 if (empty($this->name) || $this->name == \GO::t('unnamed')) {
                     $this->name = $exception->mainevent->name;
                 }
             } else {
                 //ignore this because the invited participant might not be invited to the series
                 //throw new \Exception("Could not find master event!");
                 //hack to make it be seen as an exception
                 $this->exception_for_event_id = -1;
             }
         }
     }
     if ($vobject->valarm && $vobject->valarm->trigger) {
         $reminderTime = $vobject->valarm->getEffectiveTriggerTime();
         //echo $reminderTime->format('c');
         if ($this->all_day_event) {
             $this->_utcToLocal($reminderTime);
         }
         $seconds = $reminderTime->format('U');
         $this->reminder = $this->start_time - $seconds;
         if ($this->reminder < 0) {
             $this->reminder = 0;
         }
     }
     $cats = (string) $vobject->categories;
     if (!empty($cats)) {
         //Group-Office only supports a single category.
         $cats = explode(',', $cats);
         $categoryName = array_shift($cats);
         $category = Category::model()->findByName($this->calendar_id, $categoryName);
         if (!$category && !$dontSave && $this->calendar_id) {
             $category = new Category();
             $category->name = $categoryName;
             $category->calendar_id = $this->calendar_id;
             $category->save(true);
         }
         if ($category) {
             $this->category_id = $category->id;
             $this->background = $category->color;
         }
     }
     //set is_organizer flag
     if ($vobject->organizer && $this->calendar) {
         $organizerEmail = str_replace('mailto:', '', strtolower((string) $vobject->organizer));
         $this->is_organizer = $organizerEmail == $this->calendar->user->email;
     }
     if (!$dontSave) {
         $this->cutAttributeLengths();
         //			try {
         $this->_isImport = true;
         if (!$importExternal) {
             $this->setValidationRule('uuid', 'unique', array('calendar_id', 'start_time', 'exception_for_event_id'));
         }
         //				//make sure no duplicates are imported
         //				if(!is_array($previouslyImportedEventsArray)){
         //					// We do not take events from previous import iterations into account, and we will do a validation check.
         //					$this->setValidationRule('uuid', 'unique', array('calendar_id','start_time'));
         //				}else
         //				{
         //
         //					// We take into account the history if imported items to better handle recurring events, exceptions and rescheduled events.
         //
         //					if (!empty($this->rrule)) {
         //
         //						\GO::debug('=== ['.\GO\Base\Util\Date::get_timestamp($this->start_time).'] '.$this->name.' (with rrule)');
         //
         //						// Handle imported recurring event.
         //
         //						$existingEventModel = Event::model()->find(
         //							\GO\Base\Db\FindParams::newInstance()
         //								->single()
         //								->criteria(\GO\Base\Db\FindCriteria::newInstance()
         //									->addCondition('calendar_id',$this->calendar_id)
         //									->addCondition('uuid',$this->uuid)
         //									->addCondition('rrule','','!=')
         //								)
         //						);
         //
         //						if (!empty($existingEventModel)) {
         //							// Update the existing recurring event in the calendar.
         //							$this->id = $existingEventModel->id;
         //							$this->setIsNew(false);
         //						}
         //
         //					} else {
         //
         //						\GO::debug('=== ['.\GO\Base\Util\Date::get_timestamp($this->start_time).'] '.$this->name);
         //
         //						// Handle imported non-recurring event or exception event.
         //
         //						$existingEventsStmt = Event::model()->find(
         //							\GO\Base\Db\FindParams::newInstance()
         //								->criteria(\GO\Base\Db\FindCriteria::newInstance()
         //									->addCondition('calendar_id',$this->calendar_id)
         //									->addCondition('uuid',$this->uuid)
         //									->addCondition('rrule','','=')
         //								)
         //						);
         //
         //						foreach ($existingEventsStmt as $existingEventModel) {
         //							if ($existingEventModel && !self::eventIsFromCurrentImport($existingEventModel,$previouslyImportedEventsArray)) {
         //								// The existing event model in the database was previously imported during the current import process.
         //
         //								// We rightfully assume here that the latest version of this event will be saved in Group-Office as a new event later in this function.
         //								$existingEventModel->delete();
         //							}
         //						}
         //
         //					}
         //				}
         if (!$this->save()) {
             if ($importExternal) {
                 $installationName = !empty(\GO::config()->title) ? \GO::config()->title : 'Group-Office';
                 $validationErrStr = implode("\n", $this->getValidationErrors()) . "\n";
                 $mailSubject = str_replace(array('%cal', '%event'), array($this->calendar->name, $this->name), \GO::t('eventNotSavedSubject', 'calendar'));
                 $body = \GO::t('eventNotSavedBody', 'calendar');
                 $body = str_replace(array('%goname', '%event', '%starttime', '%cal', '%errormessage'), array($installationName, $this->name, \GO\Base\Util\Date::get_timestamp($this->start_time), $this->calendar->name, $validationErrStr), $body);
                 $message = \GO\Base\Mail\Message::newInstance($mailSubject)->setFrom(\GO::config()->webmaster_email, \GO::config()->title)->addTo($this->calendar->user->email);
                 $message->setHtmlAlternateBody(nl2br($body));
                 if (\GO\Base\Mail\Mailer::newGoInstance()->send($message)) {
                     throw new \GO\Base\Exception\Validation('DUE TO ERROR, CRON SENT MAIL TO: ' . $this->calendar->user->email . '. THIS IS THE EMAIL MESSAGE:' . "\r\n" . $body);
                 } else {
                     throw new \GO\Base\Exception\Validation('CRON COULD NOT SEND EMAIL WITH ERROR MESSAGE TO: ' . $this->calendar->user->email . '. THIS IS THE EMAIL MESSAGE:' . "\r\n" . $body);
                 }
             } else {
                 throw new \GO\Base\Exception\Validation(implode("\n", $this->getValidationErrors()) . "\n");
             }
         }
         $this->_isImport = false;
         //			} catch (\Exception $e) {
         //				throw new \Exception($this->name.' ['.\GO\Base\Util\Date::get_timestamp($this->start_time).' - '.\GO\Base\Util\Date::get_timestamp($this->end_time).'] '.$e->getMessage());
         //			}
         if (!empty($exception)) {
             //save the exception we found by recurrence-id
             $exception->exception_event_id = $this->id;
             $exception->save();
             \GO::debug("saved exception");
         }
         //			$test = (bool) $vobject->organizer;
         //			var_dump($test);
         //			exit();
         //
         if ($vobject->organizer) {
             $p = $this->importVObjectAttendee($this, $vobject->organizer, true);
         } else {
             $p = false;
         }
         $calendarParticipantFound = !empty($p) && $p->user_id == $this->calendar->user_id;
         $attendees = $vobject->select('attendee');
         foreach ($attendees as $attendee) {
             $p = $this->importVObjectAttendee($this, $attendee, false);
             if ($p->user_id == $this->calendar->user_id) {
                 $calendarParticipantFound = true;
             }
         }
         //if the calendar owner is not in the participants then we should chnage the is_organizer flag because otherwise the event can't be opened or accepted.
         if (!$calendarParticipantFound) {
             if ($makeSureUserParticipantExists) {
                 $participant = \GO\Calendar\Model\Participant::model()->findSingleByAttributes(array('event_id' => $this->id, 'email' => $this->calendar->user->email));
                 if (!$participant) {
                     //this is a bad situation. The import thould have detected a user for one of the participants.
                     //It uses the E-mail account aliases to determine a user. See GO_Calendar_Model_Event::importVObject
                     $participant = new \GO\Calendar\Model\Participant();
                     $participant->event_id = $this->id;
                     $participant->user_id = $this->calendar->user_id;
                     $participant->email = $this->calendar->user->email;
                 } else {
                     $participant->user_id = $this->calendar->user_id;
                 }
                 $participant->save();
             } else {
                 $this->is_organizer = true;
                 $this->save();
             }
         }
         //Add exception dates to Event
         foreach ($vobject->select('EXDATE') as $i => $exdate) {
             try {
                 $dts = $exdate->getDateTimes();
                 if ($dts === null) {
                     continue;
                 }
                 foreach ($dts as $dt) {
                     $this->addException($dt->format('U'));
                 }
             } catch (Exception $e) {
                 trigger_error($e->getMessage(), E_USER_NOTICE);
             }
         }
         if ($importExternal && $this->isRecurring()) {
             $exceptionEventsStmt = Event::model()->find(\GO\Base\Db\FindParams::newInstance()->criteria(\GO\Base\Db\FindCriteria::newInstance()->addCondition('calendar_id', $this->calendar_id)->addCondition('uuid', $this->uuid)->addCondition('rrule', '', '=')));
             foreach ($exceptionEventsStmt as $exceptionEventModel) {
                 $exceptionEventModel->exception_for_event_id = $this->id;
                 $exceptionEventModel->save();
                 //TODO: This method only works when an exception takes place on the same day as the original occurence.
                 //We should store the RECURRENCE-ID value so we can find it later.
                 $this->addException($exceptionEventModel->start_time, $exceptionEventModel->id);
                 //					\GO::debug('=== EXCEPTION EVENT === ['.\GO\Base\Util\Date::get_timestamp($exceptionEventModel->start_time).'] '.$exceptionEventModel->name.' (\Exception for event: '.$exceptionEventModel->exception_for_event_id.')');
             }
         }
     }
     return $this;
 }
Ejemplo n.º 4
0
 protected function getStoreParams($params)
 {
     $c = \GO\Base\Db\FindParams::newInstance()->criteria(\GO\Base\Db\FindCriteria::newInstance()->addModel(\GO\Calendar\Model\Participant::model())->addCondition('event_id', $params['event_id']));
     return $c;
 }