private function loadImportURLIfNeeded()
 {
     if (!$this->importURL && property_exists($this->data, 'importurl') && $this->data->importurl) {
         $repo = new ImportURLRepository();
         $this->importURL = $repo->loadById($this->data->importurl);
     }
 }
 function testBasic()
 {
     global $CONFIG;
     \TimeSource::mock(2013, 10, 1, 1, 1, 1);
     $CONFIG->importURLAllowEventsSecondsIntoFuture = 7776000;
     // 90 days
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $importURL = new ImportURLModel();
     $importURL->setIsEnabled(true);
     $importURL->setSiteId($site->getId());
     $importURL->setGroupId($group->getId());
     $importURL->setTitle("Test");
     $importURL->setUrl("http://test.com");
     $importURLRepository->create($importURL, $site, $user);
     // Import
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/Lanyrd1.ical');
     $importURLRun->setFlag(ImportURLRun::$FLAG_ADD_UIDS);
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Load!
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $event = $events[0];
     $this->assertEquals("State of the Map Scotland 2013", $event->getSummary());
     $this->assertEquals('2013-10-11 00:00:00', $event->getStartAt()->format('Y-m-d H:i:s'));
     $this->assertEquals('2013-10-14 23:59:59', $event->getEndAt()->format('Y-m-d H:i:s'));
     $this->assertEquals("http://sotms.eventbrite.com/\n\nhttp://lanyrd.com/crkmt", $event->getDescription());
     $this->assertEquals('http://lanyrd.com/2013/sotmscot2013/', $event->getURL());
     $this->assertFalse($event->getIsDeleted());
     // Look for event
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $erb->setImportURL($importURL);
     $this->assertEquals(1, count($erb->fetchAll()));
 }
 function testBasic()
 {
     global $CONFIG;
     \TimeSource::mock(2013, 10, 1, 1, 1, 1);
     $CONFIG->importURLAllowEventsSecondsIntoFuture = 7776000;
     // 90 days
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $importURL = new ImportURLModel();
     $importURL->setIsEnabled(true);
     $importURL->setSiteId($site->getId());
     $importURL->setGroupId($group->getId());
     $importURL->setTitle("Test");
     $importURL->setUrl("http://test.com");
     $importURLRepository->create($importURL, $site, $user);
     // Import
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/Eventbrite1.ical');
     $importURLRun->setFlag(ImportURLRun::$FLAG_ADD_UIDS);
     $importURLRun->setFlag(ImportURLRun::$FLAG_SET_TICKET_URL_AS_URL);
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Load!
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $event = $events[0];
     $this->assertEquals("Computing At School Scotland Conference 2013", $event->getSummary());
     $this->assertEquals('2013-10-26 07:30:00', $event->getStartAt()->format('Y-m-d H:i:s'));
     $this->assertEquals('2013-10-26 16:00:00', $event->getEndAt()->format('Y-m-d H:i:s'));
     $this->assertEquals('For details, click here: https://casscot13.eventbrite.co.uk', $event->getDescription());
     $this->assertEquals('https://casscot13.eventbrite.co.uk', $event->getURL());
     $this->assertEquals('https://casscot13.eventbrite.co.uk', $event->getTicketURL());
     $this->assertFalse($event->getIsDeleted());
 }
 function testBasic()
 {
     global $CONFIG;
     \TimeSource::mock(2013, 10, 1, 1, 1, 1);
     $CONFIG->importURLAllowEventsSecondsIntoFuture = 7776000;
     // 90 days
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $importURL = new ImportURLModel();
     $importURL->setIsEnabled(true);
     $importURL->setSiteId($site->getId());
     $importURL->setGroupId($group->getId());
     $importURL->setTitle("Test");
     $importURL->setUrl("http://test.com");
     $importURLRepository->create($importURL, $site, $user);
     // Import
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/Meetup1.ics');
     $importURLRun->setFlag(ImportURLRun::$FLAG_ADD_UIDS);
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Load!
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $event = $events[0];
     $this->assertEquals("Talk & Build AngularJS", $event->getSummary());
     $this->assertEquals('2013-10-17 18:00:00', $event->getStartAt()->format('Y-m-d H:i:s'));
     $this->assertEquals('2013-10-17 21:00:00', $event->getEndAt()->format('Y-m-d H:i:s'));
     $this->assertEquals("AngularJS - Edinburgh\nThursday, October 17 at 7:00 PM\n\nDetails: http://www.meetup.com/AngularJS-Edinburgh/events/141654792/", $event->getDescription());
     $this->assertEquals('http://www.meetup.com/AngularJS-Edinburgh/events/141654792/', $event->getURL());
     $this->assertFalse($event->getIsDeleted());
 }
 function test1()
 {
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $newImportURL = new ImportURLModel();
     $newImportURL->setIsEnabled(true);
     $newImportURL->setSiteId($site->getId());
     $newImportURL->setGroupId($group->getId());
     $newImportURL->setTitle("Test");
     $newImportURL->setUrl("http://test.com");
     # no clash
     $clash = $importURLRepository->loadClashForImportUrl($newImportURL);
     $this->assertNull($clash);
     # save import, now clash!
     $importURLRepository->create($newImportURL, $site, $user);
     $newImportURL2 = new ImportURLModel();
     $newImportURL2->setIsEnabled(true);
     $newImportURL2->setSiteId($site->getId());
     $newImportURL2->setGroupId($group->getId());
     $newImportURL2->setTitle("Test.com site");
     $newImportURL2->setUrl("http://TEST.com");
     # no clash
     $clash = $importURLRepository->loadClashForImportUrl($newImportURL2);
     $this->assertTrue($clash != null);
 }
 function testRRuleDeleteByExDate1()
 {
     global $CONFIG;
     \TimeSource::mock(2015, 1, 1, 1, 1, 1);
     $CONFIG->importURLAllowEventsSecondsIntoFuture = 77760000;
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $importURL = new ImportURLModel();
     $importURL->setIsEnabled(true);
     $importURL->setSiteId($site->getId());
     $importURL->setGroupId($group->getId());
     $importURL->setTitle("Test");
     $importURL->setUrl("http://test.com");
     $importURLRepository->create($importURL, $site, $user);
     // ============================================= Import CREATE
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/ImportRRuleDeleteByExDate1Part1.ics');
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $i->setLimitToSaveOnEachRun(7);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Is it loaded on Imported Events?
     $ierb = new \repositories\builders\ImportedEventRepositoryBuilder();
     $importedEvents = $ierb->fetchAll();
     $this->assertEquals(1, count($importedEvents));
     $importedEvent = $importedEvents[0];
     $reoccur = $importedEvent->getReoccur();
     $this->assertEquals(true, is_array($reoccur));
     $this->assertEquals(true, isset($reoccur['ical_rrule']));
     $this->assertEquals(true, is_array($reoccur['ical_rrule']));
     $this->assertEquals("WEEKLY", $reoccur['ical_rrule']["FREQ"]);
     $this->assertEquals("TH", $reoccur['ical_rrule']["BYDAY"]);
     // now test real events
     $erb = new EventRepositoryBuilder();
     $erb->setImportedEvent($importedEvent);
     $erb->setAfterNow();
     $events = $erb->fetchAll();
     $this->assertTrue(count($events) > 5);
     $event = $events[0];
     $this->assertEquals("2015-02-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     $event = $events[1];
     $this->assertEquals("2015-02-26T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-26T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     $event = $events[2];
     $this->assertEquals("2015-03-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-03-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     \TimeSource::mock(2015, 1, 2, 1, 1, 1);
     // ============================================= Import With no changes
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/ImportRRuleDeleteByExDate1Part1.ics');
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $i->setLimitToSaveOnEachRun(7);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Is it loaded on Imported Events?
     $ierb = new \repositories\builders\ImportedEventRepositoryBuilder();
     $importedEvents = $ierb->fetchAll();
     $this->assertEquals(1, count($importedEvents));
     $importedEvent = $importedEvents[0];
     $reoccur = $importedEvent->getReoccur();
     $this->assertEquals(true, is_array($reoccur));
     $this->assertEquals(true, isset($reoccur['ical_rrule']));
     $this->assertEquals(true, is_array($reoccur['ical_rrule']));
     $this->assertEquals("WEEKLY", $reoccur['ical_rrule']["FREQ"]);
     $this->assertEquals("TH", $reoccur['ical_rrule']["BYDAY"]);
     // now test real events
     $erb = new EventRepositoryBuilder();
     $erb->setImportedEvent($importedEvent);
     $erb->setAfterNow();
     $events = $erb->fetchAll();
     $this->assertTrue(count($events) > 5);
     $event = $events[0];
     $this->assertEquals("2015-02-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     $event = $events[1];
     $this->assertEquals("2015-02-26T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-26T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     $event = $events[2];
     $this->assertEquals("2015-03-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-03-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     \TimeSource::mock(2015, 1, 3, 1, 1, 1);
     // ============================================= Import WITH ONE DELETED!
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/ImportRRuleDeleteByExDate1Part2.ics');
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $i->setLimitToSaveOnEachRun(7);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Is it loaded on Imported Events?
     $ierb = new \repositories\builders\ImportedEventRepositoryBuilder();
     $importedEvents = $ierb->fetchAll();
     $this->assertEquals(1, count($importedEvents));
     $importedEvent = $importedEvents[0];
     $reoccur = $importedEvent->getReoccur();
     $this->assertEquals(true, is_array($reoccur));
     $this->assertEquals(true, isset($reoccur['ical_rrule']));
     $this->assertEquals(true, is_array($reoccur['ical_rrule']));
     $this->assertEquals("WEEKLY", $reoccur['ical_rrule']["FREQ"]);
     $this->assertEquals("TH", $reoccur['ical_rrule']["BYDAY"]);
     // now test real events
     $erb = new EventRepositoryBuilder();
     $erb->setImportedEvent($importedEvent);
     $erb->setAfterNow();
     $erb->setIncludeDeleted(true);
     $events = $erb->fetchAll();
     $this->assertTrue(count($events) > 5);
     $event = $events[0];
     $this->assertEquals("2015-02-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
     $event = $events[1];
     $this->assertEquals("2015-02-26T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-02-26T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertTrue($event->getIsDeleted());
     $event = $events[2];
     $this->assertEquals("2015-03-12T09:00:00+00:00", $event->getStartAtInUTC()->format("c"));
     $this->assertEquals("2015-03-12T10:00:00+00:00", $event->getEndAtInUTC()->format("c"));
     $this->assertFalse($event->getIsDeleted());
 }
 function testLimits()
 {
     global $CONFIG;
     \TimeSource::mock(2012, 9, 1, 1, 1, 1);
     $CONFIG->importURLAllowEventsSecondsIntoFuture = 77760000;
     $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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $importURLRepository = new ImportURLRepository();
     $importURL = new ImportURLModel();
     $importURL->setIsEnabled(true);
     $importURL->setSiteId($site->getId());
     $importURL->setGroupId($group->getId());
     $importURL->setTitle("Test");
     $importURL->setUrl("http://test.com");
     $importURLRepository->create($importURL, $site, $user);
     // Import
     $importURLRun = new ImportURLRun($importURL, $site);
     $importURLRun->setTemporaryFileStorageForTesting(dirname(__FILE__) . '/data/ICALManyEvents.ical');
     $i = new ImportURLICalHandler();
     $i->setImportURLRun($importURLRun);
     $i->setLimitToSaveOnEachRun(2);
     $this->assertTrue($i->canHandle());
     $r = $i->handle();
     // Load!
     $erb = new EventRepositoryBuilder();
     $erb->setSite($site);
     $events = $erb->fetchAll();
     $this->assertEquals(2, count($events));
 }
 function disable($slug, Request $request, Application $app)
 {
     if (!$this->build($slug, $request, $app)) {
         $app->abort(404, "Import does not exist.");
     }
     if (!$this->parameters['importurl']->getIsEnabled()) {
         die('NO');
         // TODO
     }
     if ($request->request->get('disable') == 'yes' && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         $iRepository = new ImportURLRepository();
         $iRepository->disable($this->parameters['importurl'], $app['currentUser']);
         $erb = new EventRepositoryBuilder();
         $erb->setAfterNow();
         $erb->setIncludeDeleted(false);
         $erb->setImportURL($this->parameters['importurl']);
         $eventRepository = new EventRepository();
         foreach ($erb->fetchAll() as $event) {
             $eventRepository->delete($event, $app['currentUser']);
         }
         return $app->redirect("/importurl/" . $this->parameters['importurl']->getSlug());
     }
     return $app['twig']->render('site/importurl/disable.html.twig', $this->parameters);
 }
 protected function build($slug, Request $request, Application $app)
 {
     global $CONFIG;
     $this->parameters = array('group' => null, 'venue' => null, 'country' => null, 'area' => null, 'parentAreas' => array(), 'childAreas' => array(), 'importurl' => null, 'eventIsDuplicateOf' => null);
     if (strpos($slug, "-")) {
         $slug = array_shift(explode("-", $slug, 2));
     }
     $eventRepository = new EventRepository();
     $this->parameters['event'] = $eventRepository->loadBySlug($app['currentSite'], $slug);
     if (!$this->parameters['event']) {
         return false;
     }
     if ($this->parameters['event']->getCountryID()) {
         $cr = new CountryRepository();
         $this->parameters['country'] = $cr->loadById($this->parameters['event']->getCountryID());
     }
     $areaID = null;
     if ($this->parameters['event']->getVenueID()) {
         $cr = new VenueRepository();
         $this->parameters['venue'] = $cr->loadById($this->parameters['event']->getVenueID());
         $areaID = $this->parameters['venue']->getAreaId();
     } else {
         if ($this->parameters['event']->getAreaId()) {
             $areaID = $this->parameters['event']->getAreaId();
         }
     }
     if ($areaID) {
         $ar = new AreaRepository();
         $this->parameters['area'] = $ar->loadById($areaID);
         if (!$this->parameters['area']) {
             return false;
         }
         $checkArea = $this->parameters['area']->getParentAreaId() ? $ar->loadById($this->parameters['area']->getParentAreaId()) : null;
         while ($checkArea) {
             array_unshift($this->parameters['parentAreas'], $checkArea);
             $checkArea = $checkArea->getParentAreaId() ? $ar->loadById($checkArea->getParentAreaId()) : null;
         }
     }
     if ($this->parameters['event']->getImportUrlId()) {
         $iur = new ImportURLRepository();
         $this->parameters['importurl'] = $iur->loadById($this->parameters['event']->getImportUrlId());
     }
     $groupRB = new GroupRepositoryBuilder();
     $groupRB->setEvent($this->parameters['event']);
     $this->parameters['groups'] = $groupRB->fetchAll();
     if ($this->parameters['event']->getGroupId()) {
         foreach ($this->parameters['groups'] as $group) {
             if ($group->getId() == $this->parameters['event']->getGroupId()) {
                 $this->parameters['group'] = $group;
             }
         }
     }
     if ($this->parameters['event']->getIsDuplicateOfId()) {
         $this->parameters['eventIsDuplicateOf'] = $eventRepository->loadByID($this->parameters['event']->getIsDuplicateOfId());
     }
     $app['currentUserActions']->set("org.openacalendar", "eventHistory", true);
     $app['currentUserActions']->set("org.openacalendar", "eventEditDetails", $app['currentUserPermissions']->hasPermission("org.openacalendar", "EVENTS_CHANGE") && !$this->parameters['event']->getIsDeleted() && !$this->parameters['event']->getIsCancelled());
     $app['currentUserActions']->set("org.openacalendar", "eventEditDetailsRollback", $app['currentUserActions']->has("org.openacalendar", "eventEditDetails"));
     $app['currentUserActions']->set("org.openacalendar", "eventEditVenue", $app['currentUserPermissions']->hasPermission("org.openacalendar", "EVENTS_CHANGE") && !$this->parameters['event']->getIsDeleted() && !$this->parameters['event']->getIsCancelled() && $app['currentSite']->getIsFeaturePhysicalEvents());
     $app['currentUserActions']->set("org.openacalendar", "eventEditTags", $app['currentUserPermissions']->hasPermission("org.openacalendar", "EVENTS_CHANGE") && !$this->parameters['event']->getIsDeleted() && !$this->parameters['event']->getIsCancelled() && $app['currentSite']->getIsFeatureTag());
     $app['currentUserActions']->set("org.openacalendar", "eventEditGroups", $app['currentUserPermissions']->hasPermission("org.openacalendar", "EVENTS_CHANGE") && !$this->parameters['event']->getIsDeleted() && !$this->parameters['event']->getIsCancelled() && $app['currentSite']->getIsFeatureGroup());
     $app['currentUserActions']->set("org.openacalendar", "eventEditMedia", $app['currentUserPermissions']->hasPermission("org.openacalendar", "EVENTS_CHANGE") && !$this->parameters['event']->getIsDeleted() && !$this->parameters['event']->getIsCancelled() && $CONFIG->isFileStore());
     $app['currentUserActions']->set("org.openacalendar", "eventRecur", $app['currentUserPermissions']->hasPermission("org.openacalendar", "EVENTS_CHANGE") && !$this->parameters['event']->getIsImported() && !$this->parameters['event']->getIsDeleted() && !$this->parameters['event']->getIsCancelled());
     $app['currentUserActions']->set("org.openacalendar", "eventDelete", $app['currentUserPermissions']->hasPermission("org.openacalendar", "EVENTS_CHANGE") && !$this->parameters['event']->getIsImported() && !$this->parameters['event']->getIsDeleted());
     $app['currentUserActions']->set("org.openacalendar", "eventCancel", $app['currentUserPermissions']->hasPermission("org.openacalendar", "EVENTS_CHANGE") && !$this->parameters['event']->getIsImported() && !$this->parameters['event']->getIsCancelled() && !$this->parameters['event']->getIsDeleted());
     $app['currentUserActions']->set("org.openacalendar", "eventUndelete", $app['currentUserPermissions']->hasPermission("org.openacalendar", "EVENTS_CHANGE") && !$this->parameters['event']->getIsImported() && $this->parameters['event']->getIsDeleted());
     $app['currentUserActions']->set("org.openacalendar", "eventUncancel", $app['currentUserPermissions']->hasPermission("org.openacalendar", "EVENTS_CHANGE") && !$this->parameters['event']->getIsImported() && $this->parameters['event']->getIsCancelled());
     return true;
 }
 public static function run(Application $app, $verbose = false)
 {
     global $CONFIG;
     if ($verbose) {
         print "Starting " . date("c") . "\n";
     }
     $siteRepo = new SiteRepository();
     $groupRepo = new GroupRepository();
     $importURLRepo = new ImportURLRepository();
     $userRepo = new UserAccountRepository();
     $userWatchesSiteStopRepository = new UserWatchesSiteStopRepository();
     $userWatchesGroupStopRepository = new UserWatchesGroupStopRepository();
     $userAccountGeneralSecurityKeyRepository = new UserAccountGeneralSecurityKeyRepository();
     $userNotificationRepo = new UserNotificationRepository();
     /** @var usernotifications/UpcomingEventsUserNotificationType **/
     $userNotificationType = $app['extensions']->getCoreExtension()->getUserNotificationType('ImportURLExpired');
     $iurlBuilder = new ImportURLRepositoryBuilder();
     foreach ($iurlBuilder->fetchAll() as $importURL) {
         $site = $siteRepo->loadById($importURL->getSiteID());
         $group = $groupRepo->loadById($importURL->getGroupId());
         if ($verbose) {
             print date("c") . " ImportURL " . $importURL->getId() . " " . $importURL->getTitle() . " Site " . $site->getTitle() . "\n";
         }
         if ($site->getIsClosedBySysAdmin()) {
             if ($verbose) {
                 print " - site closed by sys admin\n";
             }
         } else {
             if (!$site->getIsFeatureImporter()) {
                 if ($verbose) {
                     print " - site feature disabled\n";
                 }
             } else {
                 if (!$group) {
                     if ($verbose) {
                         print " - no group - this should be impossible\n";
                     }
                 } else {
                     if ($group->getIsDeleted()) {
                         if ($verbose) {
                             print " - group deleted\n";
                         }
                     } else {
                         if ($importURL->getExpiredAt()) {
                             if ($verbose) {
                                 print " - expired\n";
                             }
                         } else {
                             if (!$importURL->getIsEnabled()) {
                                 if ($verbose) {
                                     print " - not enabled\n";
                                 }
                             } else {
                                 if ($importURL->isShouldExpireNow()) {
                                     if ($verbose) {
                                         print " - expiring\n";
                                     }
                                     $importURLRepo->expire($importURL);
                                     configureAppForSite($site);
                                     $uwsb = new UserWatchesSiteRepositoryBuilder();
                                     $uwsb->setSite($site);
                                     foreach ($uwsb->fetchAll() as $userWatchesSite) {
                                         $user = $userRepo->loadByID($userWatchesSite->getUserAccountId());
                                         if ($userWatchesSite->getIsWatching()) {
                                             /// Notification Class
                                             $userNotification = $userNotificationType->getNewNotification($user, $site);
                                             $userNotification->setImportURL($importURL);
                                             $userNotification->setGroup($group);
                                             ////// Save Notification Class
                                             $userNotificationRepo->create($userNotification);
                                             ////// Send Email
                                             if ($userNotification->getIsEmail()) {
                                                 configureAppForUser($user);
                                                 $userAccountGeneralSecurityKey = $userAccountGeneralSecurityKeyRepository->getForUser($user);
                                                 $userWatchesSiteStop = $userWatchesSiteStopRepository->getForUserAndSite($user, $site);
                                                 $message = \Swift_Message::newInstance();
                                                 $message->setSubject("Please confirm this is still valid: " . $importURL->getTitle());
                                                 $message->setFrom(array($CONFIG->emailFrom => $CONFIG->emailFromName));
                                                 $message->setTo($user->getEmail());
                                                 $messageText = $app['twig']->render('email/importURLExpired.watchesSite.txt.twig', array('user' => $user, 'importurl' => $importURL, 'stopCode' => $userWatchesSiteStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey()));
                                                 if ($CONFIG->isDebug) {
                                                     file_put_contents('/tmp/importURLExpired.watchesSite.txt', $messageText);
                                                 }
                                                 $message->setBody($messageText);
                                                 $messageHTML = $app['twig']->render('email/importURLExpired.watchesSite.html.twig', array('user' => $user, 'importurl' => $importURL, 'stopCode' => $userWatchesSiteStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey()));
                                                 if ($CONFIG->isDebug) {
                                                     file_put_contents('/tmp/importURLExpired.watchesSite.html', $messageHTML);
                                                 }
                                                 $message->addPart($messageHTML, 'text/html');
                                                 if (!$CONFIG->isDebug) {
                                                     $app['mailer']->send($message);
                                                 }
                                                 $userNotificationRepo->markEmailed($userNotification);
                                             }
                                         }
                                     }
                                     $uwgb = new UserWatchesGroupRepositoryBuilder();
                                     $uwgb->setGroup($group);
                                     foreach ($uwgb->fetchAll() as $userWatchesGroup) {
                                         $user = $userRepo->loadByID($userWatchesGroup->getUserAccountId());
                                         if ($userWatchesGroup->getIsWatching()) {
                                             /// Notification Class
                                             $userNotification = $userNotificationType->getNewNotification($user, $site);
                                             $userNotification->setImportURL($importURL);
                                             $userNotification->setGroup($group);
                                             ////// Save Notification Class
                                             $userNotificationRepo->create($userNotification);
                                             ////// Send Email
                                             if ($userNotification->getIsEmail()) {
                                                 $userAccountGeneralSecurityKey = $userAccountGeneralSecurityKeyRepository->getForUser($user);
                                                 $userWatchesGroupStop = $userWatchesGroupStopRepository->getForUserAndGroup($user, $group);
                                                 $message = \Swift_Message::newInstance();
                                                 $message->setSubject("Please confirm this is still valid: " . $importURL->getTitle());
                                                 $message->setFrom(array($CONFIG->emailFrom => $CONFIG->emailFromName));
                                                 $message->setTo($user->getEmail());
                                                 $messageText = $app['twig']->render('email/importURLExpired.watchesGroup.txt.twig', array('user' => $user, 'importurl' => $importURL, 'stopCode' => $userWatchesGroupStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'group' => $group));
                                                 if ($CONFIG->isDebug) {
                                                     file_put_contents('/tmp/importURLExpired.watchesGroup.txt', $messageText);
                                                 }
                                                 $message->setBody($messageText);
                                                 $messageHTML = $app['twig']->render('email/importURLExpired.watchesGroup.html.twig', array('user' => $user, 'importurl' => $importURL, 'stopCode' => $userWatchesGroupStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'group' => $group));
                                                 if ($CONFIG->isDebug) {
                                                     file_put_contents('/tmp/importURLExpired.watchesGroup.html', $messageHTML);
                                                 }
                                                 $message->addPart($messageHTML, 'text/html');
                                                 if (!$CONFIG->isDebug) {
                                                     $app['mailer']->send($message);
                                                 }
                                                 $userNotificationRepo->markEmailed($userNotification);
                                             }
                                         }
                                     }
                                 } else {
                                     $lastRunDate = $importURLRepo->getLastRunDateForImportURL($importURL);
                                     $nowDate = \TimeSource::getDateTime();
                                     if (!$lastRunDate || $lastRunDate->getTimestamp() < $nowDate->getTimestamp() - $CONFIG->importURLSecondsBetweenImports) {
                                         if ($verbose) {
                                             print " - importing\n";
                                         }
                                         $runner = new ImportURLRunner();
                                         $runner->go($importURL);
                                     } else {
                                         if ($verbose) {
                                             print " - already done on " . $lastRunDate->format("c") . "\n";
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($verbose) {
         print "Finished " . date("c") . "\n";
     }
 }
 function newImportURL($slug, Request $request, Application $app)
 {
     if (!$this->build($slug, $request, $app)) {
         $app->abort(404, "Group does not exist.");
     }
     $importurl = new ImportURLModel();
     // we must setSiteId() here so loadClashForImportUrl() works
     $importurl->setSiteId($app['currentSite']->getId());
     $importurl->setGroupId($this->parameters['group']->getId());
     $form = $app['form.factory']->create(new ImportURLNewForm($app['currentSite'], $app['currentTimeZone']), $importurl);
     if ('POST' == $request->getMethod()) {
         $form->bind($request);
         if ($form->isValid()) {
             $importURLRepository = new ImportURLRepository();
             $clash = $importURLRepository->loadClashForImportUrl($importurl);
             if ($clash) {
                 $importurl->setIsEnabled(false);
                 $app['flashmessages']->addMessage("There was a problem enabling this importer. Please try to enable it for details.");
             } else {
                 $importurl->setIsEnabled(true);
             }
             $area = null;
             $areaRepository = new AreaRepository();
             $areasPost = $request->request->get('areas');
             if (is_array($areasPost)) {
                 foreach ($areasPost as $areaCode) {
                     if (substr($areaCode, 0, 9) == 'EXISTING:') {
                         $area = $areaRepository->loadBySlug($app['currentSite'], substr($areaCode, 9));
                     }
                 }
             }
             $importurl->setAreaId($area ? $area->getId() : null);
             $importURLRepository->create($importurl, $app['currentSite'], $app['currentUser']);
             return $app->redirect("/importurl/" . $importurl->getSlug());
         }
     }
     $this->parameters['form'] = $form->createView();
     return $app['twig']->render('site/group/newimporturl.html.twig', $this->parameters);
 }
 public function isShouldExpireNow()
 {
     global $CONFIG;
     $r = new ImportURLRepository();
     $lastEdit = $r->getLastEditDateForImportURL($this);
     return $lastEdit->getTimeStamp() < \TimeSource::time() - $CONFIG->importURLExpireSecondsAfterLastEdit;
 }