function eventMyAttendanceJson($siteSlug, $eventSlug, Request $request, Application $app)
 {
     if (!$this->build($siteSlug, $request, $app)) {
         $app->abort(404, "Site does not exist.");
     }
     $repo = new \repositories\EventRepository();
     $this->parameters['event'] = $repo->loadBySlug($this->parameters['site'], $eventSlug);
     if (!$this->parameters['event']) {
         $app->abort(404, "Event does not exist.");
     }
     $uaer = new UserAtEventRepository();
     $userAtEvent = $uaer->loadByUserAndEventOrInstanciate($app['currentUser'], $this->parameters['event']);
     $data = array();
     if ($request->request->get('CSFRToken') == $app['websession']->getCSFRToken() && !$this->parameters['event']->isInPast()) {
         if ($request->request->get('privacy') == 'public') {
             $userAtEvent->setIsPlanPublic(true);
         } else {
             if ($request->request->get('privacy') == 'private') {
                 $userAtEvent->setIsPlanPublic(false);
             }
         }
         if ($request->request->get('attending') == 'no') {
             $userAtEvent->setIsPlanAttending(false);
             $userAtEvent->setIsPlanMaybeAttending(false);
         } else {
             if ($request->request->get('attending') == 'maybe') {
                 $userAtEvent->setIsPlanAttending(false);
                 $userAtEvent->setIsPlanMaybeAttending(true);
             } else {
                 if ($request->request->get('attending') == 'yes') {
                     $userAtEvent->setIsPlanAttending(true);
                     $userAtEvent->setIsPlanMaybeAttending(false);
                 }
             }
         }
         $uaer->save($userAtEvent);
     }
     $data['attending'] = $userAtEvent->getIsPlanAttending() ? 'yes' : ($userAtEvent->getIsPlanMaybeAttending() ? 'maybe' : 'no');
     $data['privacy'] = $userAtEvent->getIsPlanPublic() ? 'public' : 'private';
     $data['inPast'] = $this->parameters['event']->isInPast() ? 1 : 0;
     $data['CSFRToken'] = $app['websession']->getCSFRToken();
     $response = new Response(json_encode($data));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }
 /**
  * 
  * @return type array(array(), array(), array(), boolean) - upcoming events, other events, user at event data, flag if any to send
  */
 public function getDataForUpcomingEventsEmail()
 {
     $flag = false;
     $start = \TimeSource::getDateTime();
     $end = \TimeSource::getDateTime();
     if ($this->email_upcoming_events_days_notice > 0) {
         $interval = new \DateInterval("P" . $this->email_upcoming_events_days_notice . "D");
         $start->add($interval);
         $end->add($interval);
     }
     $start->setTime(0, 0, 0);
     $end->setTime(23, 59, 59);
     $upcomingEvents = array();
     $allEvents = array();
     $userAtEvent = array();
     $userAtEventRepo = new UserAtEventRepository();
     $erb = new EventRepositoryBuilder();
     $erb->setAfterNow();
     $erb->setIncludeDeleted(false);
     $erb->setIncludeCancelled(true);
     $erb->setUserAccount($this, false, true);
     foreach ($erb->fetchAll() as $event) {
         $userAtEvent[$event->getId()] = $userAtEventRepo->loadByUserAndEvent($this, $event);
         if ($start->getTimestamp() <= $event->getStartAt()->getTimestamp() && $event->getStartAt()->getTimestamp() <= $end->getTimestamp()) {
             $upcomingEvents[] = $event;
             if ($this->email_upcoming_events == 'w') {
                 $flag = true;
             } else {
                 if ($this->email_upcoming_events == 'a') {
                     if ($userAtEvent[$event->getId()] && $userAtEvent[$event->getId()]->getIsPlanAttending()) {
                         $flag = true;
                     }
                 } else {
                     if ($this->email_upcoming_events == 'm') {
                         if ($userAtEvent[$event->getId()] && ($userAtEvent[$event->getId()]->getIsPlanAttending() || $userAtEvent[$event->getId()]->getIsPlanMaybeAttending())) {
                             $flag = true;
                         }
                     }
                 }
             }
         }
         $allEvents[] = $event;
     }
     return array($upcomingEvents, $allEvents, $userAtEvent, $flag);
 }
 public function markDuplicateWithMetaData(EventModel $duplicateEvent, EventModel $originalEvent, EventEditMetaDataModel $eventEditMetaDataModel)
 {
     global $DB;
     if ($duplicateEvent->getId() == $originalEvent->getId()) {
         return;
     }
     try {
         $DB->beginTransaction();
         $duplicateEvent->setIsDeleted(true);
         $duplicateEvent->setIsDuplicateOfId($originalEvent->getId());
         $this->eventDBAccess->update($duplicateEvent, array('is_deleted', 'is_duplicate_of_id'), $eventEditMetaDataModel);
         $uaeRepo = new UserAtEventRepository();
         $uaerb = new UserAtEventRepositoryBuilder();
         $uaerb->setEventOnly($duplicateEvent);
         foreach ($uaerb->fetchAll() as $uaeDuplicate) {
             if ($uaeDuplicate->getIsPlanAttending() || $uaeDuplicate->getIsPlanMaybeAttending()) {
                 $uaeOriginal = $uaeRepo->loadByUserIDAndEventOrInstanciate($uaeDuplicate->getUserAccountId(), $originalEvent);
                 // does user already have plans for this event? If so leave them.
                 if (!$uaeOriginal->getIsPlanAttending() && !$uaeOriginal->getIsPlanMaybeAttending()) {
                     $uaeOriginal->setIsPlanAttending($uaeDuplicate->getIsPlanAttending());
                     $uaeOriginal->setIsPlanMaybeAttending($uaeDuplicate->getIsPlanMaybeAttending());
                     $uaeOriginal->setIsPlanPublic($uaeDuplicate->getIsPlanPublic());
                     $uaeRepo->save($uaeOriginal);
                 }
             }
         }
         $DB->commit();
     } catch (Exception $e) {
         $DB->rollBack();
     }
 }
 /**
  * @dataProvider dataForTest1
  */
 function test1($emailOption, $goingOption, $result)
 {
     TimeSource::mock(2013, 8, 1, 7, 0, 0);
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user, array(), $this->getSiteQuotaUsedForTesting());
     $event = new EventModel();
     $event->setSummary("test");
     $event->setDescription("test test");
     $event->setStartAt($this->mktime(2013, 8, 2, 19, 0, 0));
     $event->setEndAt($this->mktime(2013, 8, 2, 21, 0, 0));
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user);
     $user->setEmailUpcomingEventsDaysNotice(1);
     $user->setEmailUpcomingEvents($emailOption);
     $userRepo->editEmailsOptions($user);
     if ($goingOption == 'a') {
         $userAtEvent = new UserAtEventModel();
         $userAtEvent->setUserAccountId($user->getId());
         $userAtEvent->setEventId($event->getId());
         $userAtEvent->setIsPlanAttending(true);
         $uaeRepo = new UserAtEventRepository();
         $uaeRepo->save($userAtEvent);
     } else {
         if ($goingOption == 'm') {
             $userAtEvent = new UserAtEventModel();
             $userAtEvent->setUserAccountId($user->getId());
             $userAtEvent->setEventId($event->getId());
             $userAtEvent->setIsPlanMaybeAttending(true);
             $uaeRepo = new UserAtEventRepository();
             $uaeRepo->save($userAtEvent);
         }
     }
     list($upcomingEvents, $allEvents, $userAtEvent, $flag) = $user->getDataForUpcomingEventsEmail();
     $this->assertEquals($result, $flag);
 }
 protected function actionThingsToDoAfterGetUser(Application $app, UserAccountModel $user)
 {
     // events
     $uaerepo = new UserAtEventRepository();
     $eventsAdded = false;
     foreach ($this->parameters['afterGetUserAddEvents'] as $event) {
         if ($event->getIsAllowedForAfterGetUser()) {
             $uae = $uaerepo->loadByUserAndEventOrInstanciate($user, $event);
             if (!$uae->getIsPlanAttending() && !$uae->getIsPlanMaybeAttending()) {
                 $uae->setIsPlanMaybeAttending(true);
                 $uaerepo->save($uae);
                 $eventsAdded = true;
             }
         }
     }
     if ($eventsAdded) {
         $app['flashmessages']->addMessage("Check out your personal calendar for events you are interested in!");
         // TODO add link
     }
     // areas
     $uwarepo = new UserWatchesAreaRepository();
     foreach ($this->parameters['afterGetUserAddAreas'] as $area) {
         if ($area->getIsAllowedForAfterGetUser()) {
             $uwarepo->startUserWatchingArea($user, $area);
         }
     }
     // reset
     $app['websession']->setArray("afterGetUserAddEvents", array());
     $app['websession']->setArray("afterGetUserAddAreas", array());
 }