function test1()
 {
     \TimeSource::mock(2014, 1, 1, 0, 0, 0);
     $user1 = new UserAccountModel();
     $user1->setEmail("*****@*****.**");
     $user1->setUsername("test");
     $user1->setPassword("password");
     $user2 = new UserAccountModel();
     $user2->setEmail("*****@*****.**");
     $user2->setUsername("test2");
     $user2->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user1);
     $userRepo->create($user2);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user1, array(), $this->getSiteQuotaUsedForTesting());
     $group1 = new GroupModel();
     $group1->setTitle("test1");
     $group1->setDescription("test test");
     $group1->setUrl("http://www.group.com");
     $group2 = new GroupModel();
     $group2->setTitle("test this looks similar");
     $group2->setDescription("test test");
     $group2->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     \TimeSource::mock(2014, 1, 1, 1, 0, 0);
     $groupRepo->create($group1, $site, $user1);
     $groupRepo->create($group2, $site, $user2);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setStartAt(getUTCDateTime(2014, 5, 10, 19, 0, 0));
     $event->setEndAt(getUTCDateTime(2014, 5, 10, 21, 0, 0));
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user1, $group2);
     $uwgr = new UserWatchesGroupRepository();
     // Test before
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setGroup($group1);
     $this->assertEquals(0, count($erb->fetchAll()));
     $this->assertNull($uwgr->loadByUserAndGroup($user2, $group1));
     $group2 = $groupRepo->loadById($group2->getId());
     $this->assertFalse($group2->getIsDeleted());
     $this->assertNull($group2->getIsDuplicateOfId());
     // Mark
     \TimeSource::mock(2014, 1, 1, 2, 0, 0);
     $groupRepo->markDuplicate($group2, $group1, $user1);
     // Test Duplicate
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setGroup($group1);
     $this->assertEquals(1, count($erb->fetchAll()));
     $uwg = $uwgr->loadByUserAndGroup($user2, $group1);
     $this->assertNotNull($uwg);
     $group2 = $groupRepo->loadById($group2->getId());
     $this->assertTrue($group2->getIsDeleted());
     $this->assertEquals($group1->getId(), $group2->getIsDuplicateOfId());
 }
 function testMultiple()
 {
     TimeSource::mock(2013, 7, 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());
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupDupe = new GroupModel();
     $groupDupe->setTitle("test DUPE");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     $groupRepo->create($groupDupe, $site, $user);
     TimeSource::mock(2013, 7, 1, 7, 1, 0);
     $groupRepo->markDuplicate($groupDupe, $group);
     $ufgr = new UserWatchesGroupRepository();
     $ufgr->startUserWatchingGroupIdIfNotWatchedBefore($user, $group->getId());
     $event = new EventModel();
     $event->setSummary("test");
     $event->setDescription("test test");
     $event->setStartAt(getUTCDateTime(2013, 8, 1, 19, 0, 0));
     $event->setEndAt(getUTCDateTime(2013, 8, 1, 21, 0, 0));
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user, $group);
     ## TEST
     $this->assertNotNull($groupRepo->loadById($group->getId()));
     $groupRB = new GroupRepositoryBuilder();
     $groupRB->setEvent($event);
     $groups = $groupRB->fetchAll();
     $this->assertEquals(1, count($groups));
     ## PURGE!
     $groupRepo->purge($group);
     ## TEST
     $this->assertNull($groupRepo->loadById($group->getId()));
     $groupRB = new GroupRepositoryBuilder();
     $groupRB->setEvent($event);
     $groups = $groupRB->fetchAll();
     $this->assertEquals(0, count($groups));
 }
 private function loadGroupIfNeeded()
 {
     if (!$this->group && property_exists($this->data, 'group') && $this->data->group) {
         $repo = new GroupRepository();
         $this->group = $repo->loadById($this->data->group);
     }
 }
 protected function build($slug, Request $request, Application $app)
 {
     $this->parameters = array('country' => null, 'area' => null, 'parentAreas' => array());
     $iurlRepository = new ImportURLRepository();
     $this->parameters['importurl'] = $iurlRepository->loadBySlug($app['currentSite'], $slug);
     if (!$this->parameters['importurl']) {
         return false;
     }
     if ($this->parameters['importurl']->getCountryID()) {
         $cr = new CountryRepository();
         $this->parameters['country'] = $cr->loadById($this->parameters['importurl']->getCountryID());
     }
     if ($this->parameters['importurl']->getGroupId()) {
         $gr = new GroupRepository();
         $this->parameters['group'] = $gr->loadById($this->parameters['importurl']->getGroupId());
     }
     if ($this->parameters['importurl']->getAreaId()) {
         $ar = new AreaRepository();
         $this->parameters['area'] = $ar->loadById($this->parameters['importurl']->getAreaId());
         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;
         }
     }
     $app['currentUserActions']->set("org.openacalendar", "importURLLog", true);
     $app['currentUserActions']->set("org.openacalendar", "importURLEditDetails", $app['currentUserPermissions']->hasPermission("org.openacalendar", "IMPORTURL_CHANGE") && $app['currentSite']->getIsFeatureImporter());
     $app['currentUserActions']->set("org.openacalendar", "importURLDisable", $app['currentUserPermissions']->hasPermission("org.openacalendar", "IMPORTURL_CHANGE") && $app['currentSite']->getIsFeatureImporter() && $this->parameters['importurl']->getIsEnabled());
     $app['currentUserActions']->set("org.openacalendar", "importURLEnable", $app['currentUserPermissions']->hasPermission("org.openacalendar", "IMPORTURL_CHANGE") && $app['currentSite']->getIsFeatureImporter() && (!$this->parameters['importurl']->getIsEnabled() || $this->parameters['importurl']->getIsExpired()));
     return true;
 }
 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);
     $this->checkGroupInTest1($groupRepo->loadById($group->getId()));
     $this->checkGroupInTest1($groupRepo->loadBySlug($site, $group->getSlug()));
     $grb = new GroupRepositoryBuilder();
     $grb->setFreeTextsearch('test');
     $this->assertEquals(1, count($grb->fetchAll()));
     $grb = new GroupRepositoryBuilder();
     $grb->setFreeTextsearch('cats');
     $this->assertEquals(0, count($grb->fetchAll()));
 }
 protected function build($siteid, $slug, Request $request, Application $app)
 {
     $this->parameters = array('group' => null, 'venue' => null, 'country' => null);
     $sr = new SiteRepository();
     $this->parameters['site'] = $sr->loadById($siteid);
     if (!$this->parameters['site']) {
         $app->abort(404);
     }
     $er = new EventRepository();
     $this->parameters['event'] = $er->loadBySlug($this->parameters['site'], $slug);
     $this->parameters['eventisduplicateof'] = $this->parameters['event']->getIsDuplicateOfId() ? $er->loadById($this->parameters['event']->getIsDuplicateOfId()) : null;
     if (!$this->parameters['event']) {
         $app->abort(404);
     }
     if ($this->parameters['event']->getGroupId()) {
         $gr = new GroupRepository();
         $this->parameters['group'] = $gr->loadById($this->parameters['event']->getGroupId());
     }
     if ($this->parameters['event']->getCountryID()) {
         $cr = new CountryRepository();
         $this->parameters['country'] = $cr->loadById($this->parameters['event']->getCountryID());
     }
     if ($this->parameters['event']->getVenueID()) {
         $cr = new VenueRepository();
         $this->parameters['venue'] = $cr->loadById($this->parameters['event']->getVenueID());
     }
 }
 function testIntegration1()
 {
     \TimeSource::mock(2014, 1, 1, 12, 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());
     ## Create group
     \TimeSource::mock(2014, 1, 1, 13, 0, 0);
     $group = new GroupModel();
     $group->setTitle("test");
     $group->setDescription("test test");
     $group->setUrl("http://www.group.com");
     $groupRepo = new GroupRepository();
     $groupRepo->create($group, $site, $user);
     ## Edit group
     \TimeSource::mock(2014, 1, 1, 14, 0, 0);
     $group = $groupRepo->loadById($group->getId());
     $group->setTwitterUsername("testy");
     $groupRepo->edit($group, $user);
     ## Now save changed flags on these .....
     $groupHistoryRepo = new GroupHistoryRepository();
     $stat = $this->app['db']->prepare("SELECT * FROM group_history");
     $stat->execute();
     while ($data = $stat->fetch()) {
         $groupHistory = new GroupHistoryModel();
         $groupHistory->setFromDataBaseRow($data);
         $groupHistoryRepo->ensureChangedFlagsAreSet($groupHistory);
     }
     ## Now load and check
     $historyRepo = new HistoryRepositoryBuilder();
     $historyRepo->setGroup($group);
     $historyRepo->setIncludeEventHistory(false);
     $historyRepo->setIncludeVenueHistory(false);
     $historyRepo->setIncludeGroupHistory(true);
     $histories = $historyRepo->fetchAll();
     $this->assertEquals(2, count($histories));
     #the edit
     $this->assertEquals(FALSE, $histories[0]->getTitleChanged());
     $this->assertEquals(false, $histories[0]->getDescriptionChanged());
     $this->assertEquals(false, $histories[0]->getUrlChanged());
     $this->assertEquals(true, $histories[0]->getTwitterUsernameChanged());
     $this->assertEquals(false, $histories[0]->getIsDeletedChanged());
     #the create
     $this->assertEquals(true, $histories[1]->getTitleChanged());
     $this->assertEquals(true, $histories[1]->getDescriptionChanged());
     $this->assertEquals(true, $histories[1]->getUrlChanged());
     $this->assertEquals(false, $histories[1]->getTwitterUsernameChanged());
     $this->assertEquals(false, $histories[1]->getIsDeletedChanged());
 }
 protected function build($siteid, $slug, Request $request, Application $app)
 {
     $this->parameters = array('group' => null);
     $sr = new SiteRepository();
     $this->parameters['site'] = $sr->loadById($siteid);
     if (!$this->parameters['site']) {
         $app->abort(404);
     }
     $gr = new GroupRepository();
     $this->parameters['group'] = $gr->loadBySlug($this->parameters['site'], $slug);
     if (!$this->parameters['group']) {
         $app->abort(404);
     }
     $this->parameters['groupisduplicateof'] = $this->parameters['group']->getIsDuplicateOfId() ? $gr->loadById($this->parameters['group']->getIsDuplicateOfId()) : null;
 }
 function __construct(ImportURLModel $importURL, SiteModel $site = null)
 {
     $this->importURL = $importURL;
     $this->realurl = $importURL->getUrl();
     if ($site) {
         $this->site = $site;
     } else {
         $siteRepo = new SiteRepository();
         $this->site = $siteRepo->loadById($importURL->getSiteId());
     }
     if ($importURL->getCountryId()) {
         $countryRepo = new CountryRepository();
         $this->country = $countryRepo->loadById($importURL->getCountryId());
     }
     if ($importURL->getAreaId()) {
         $areaRepo = new AreaRepository();
         $this->area = $areaRepo->loadById($importURL->getAreaId());
     }
     $groupRepository = new GroupRepository();
     $this->group = $groupRepository->loadById($importURL->getGroupId());
 }
 public static function run(Application $app, $verbose = false)
 {
     global $CONFIG;
     if ($verbose) {
         print "Starting " . date("c") . "\n";
     }
     $userRepo = new UserAccountRepository();
     $siteRepo = new SiteRepository();
     $groupRepo = new GroupRepository();
     $eventRepo = new EventRepository();
     $userWatchesGroupRepository = new UserWatchesGroupRepository();
     $userWatchesGroupStopRepository = new UserWatchesGroupStopRepository();
     $userAccountGeneralSecurityKeyRepository = new UserAccountGeneralSecurityKeyRepository();
     $userNotificationRepo = new UserNotificationRepository();
     $userHasNoEditorPermissionsInSiteRepo = new UserHasNoEditorPermissionsInSiteRepository();
     $userPermissionsRepo = new UserPermissionsRepository($app['extensions']);
     /** @var usernotifications/UserWatchesGroupPromptNotificationType **/
     $userNotificationType = $app['extensions']->getCoreExtension()->getUserNotificationType('UserWatchesGroupPrompt');
     $b = new UserWatchesGroupRepositoryBuilder();
     foreach ($b->fetchAll() as $userWatchesGroup) {
         $user = $userRepo->loadByID($userWatchesGroup->getUserAccountId());
         $group = $groupRepo->loadById($userWatchesGroup->getGroupId());
         $site = $siteRepo->loadById($group->getSiteID());
         // This is not the most efficient as it involves DB access and the results might not be used. But it'll do for now.
         $userPermissions = $userPermissionsRepo->getPermissionsForUserInSite($user, $site, false, true);
         if ($verbose) {
             print date("c") . " User " . $user->getEmail() . " Site " . $site->getTitle() . " Group " . $group->getTitle() . "\n";
         }
         // UserWatchesGroupRepositoryBuilder() should only return instances where site is not also watched
         if ($site->getIsClosedBySysAdmin()) {
             if ($verbose) {
                 print " ... site is closed\n";
             }
         } else {
             if ($group->getIsDeleted()) {
                 if ($verbose) {
                     print " ... group is deleted\n";
                 }
             } else {
                 if ($userHasNoEditorPermissionsInSiteRepo->isUserInSite($user, $site)) {
                     if ($verbose) {
                         print " ... user does not have edit permissions allowed in site\n";
                     }
                 } else {
                     if (!$userPermissions->hasPermission("org.openacalendar", "CALENDAR_CHANGE")) {
                         if ($verbose) {
                             print " ... user does not have org.openacalendar/CALENDAR_CHANGE permission in site\n";
                         }
                         // Technically UserWatchesSiteRepositoryBuilder() should only return getIsWatching() == true but lets double check
                     } else {
                         if ($userWatchesGroup->getIsWatching()) {
                             if ($verbose) {
                                 print " ... searching for data\n";
                             }
                             $lastEvent = $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId());
                             $data = $userWatchesGroup->getPromptEmailData($site, $lastEvent);
                             if ($data['moreEventsNeeded']) {
                                 if ($verbose) {
                                     print " ... found data\n";
                                 }
                                 ///// Notification Class
                                 $userNotification = $userNotificationType->getNewNotification($user, $site);
                                 $userNotification->setGroup($group);
                                 ////// Save Notification Class
                                 $userNotificationRepo->create($userNotification);
                                 ////// Send Email
                                 if ($userNotification->getIsEmail()) {
                                     $userWatchesGroupStop = $userWatchesGroupStopRepository->getForUserAndGroup($user, $group);
                                     configureAppForSite($site);
                                     configureAppForUser($user);
                                     $userAccountGeneralSecurityKey = $userAccountGeneralSecurityKeyRepository->getForUser($user);
                                     $unsubscribeURL = $CONFIG->getWebIndexDomainSecure() . '/you/emails/' . $user->getId() . '/' . $userAccountGeneralSecurityKey->getAccessKey();
                                     $lastEventsBuilder = new EventRepositoryBuilder();
                                     $lastEventsBuilder->setSite($site);
                                     $lastEventsBuilder->setGroup($group);
                                     $lastEventsBuilder->setOrderByStartAt(true);
                                     $lastEventsBuilder->setIncludeDeleted(false);
                                     $lastEventsBuilder->setIncludeImported(false);
                                     $lastEventsBuilder->setLimit($CONFIG->userWatchesGroupPromptEmailShowEvents);
                                     $lastEvents = $lastEventsBuilder->fetchAll();
                                     $message = \Swift_Message::newInstance();
                                     $message->setSubject("Any news about " . $group->getTitle() . "?");
                                     $message->setFrom(array($CONFIG->emailFrom => $CONFIG->emailFromName));
                                     $message->setTo($user->getEmail());
                                     $messageText = $app['twig']->render('email/userWatchesGroupPromptEmail.txt.twig', array('group' => $group, 'user' => $user, 'lastEvents' => $lastEvents, 'stopCode' => $userWatchesGroupStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'unsubscribeURL' => $unsubscribeURL));
                                     if ($CONFIG->isDebug) {
                                         file_put_contents('/tmp/userWatchesGroupPromptEmail.txt', $messageText);
                                     }
                                     $message->setBody($messageText);
                                     $messageHTML = $app['twig']->render('email/userWatchesGroupPromptEmail.html.twig', array('group' => $group, 'user' => $user, 'lastEvents' => $lastEvents, 'stopCode' => $userWatchesGroupStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'unsubscribeURL' => $unsubscribeURL));
                                     if ($CONFIG->isDebug) {
                                         file_put_contents('/tmp/userWatchesGroupPromptEmail.html', $messageHTML);
                                     }
                                     $message->addPart($messageHTML, 'text/html');
                                     $headers = $message->getHeaders();
                                     $headers->addTextHeader('List-Unsubscribe', $unsubscribeURL);
                                     if ($verbose) {
                                         print " ... sending\n";
                                     }
                                     if (!$CONFIG->isDebug) {
                                         $app['mailer']->send($message);
                                     }
                                     $userNotificationRepo->markEmailed($userNotification);
                                 }
                                 $userWatchesGroupRepository->markPromptEmailSent($userWatchesGroup, $data['checkTime']);
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($verbose) {
         print "Finished " . date("c") . "\n";
     }
 }
 public function setFromJSON($json)
 {
     if (isset($json->event)) {
         if (isset($json->event->summary)) {
             $this->summary = $json->event->summary;
         }
         if (isset($json->event->description)) {
             $this->description = $json->event->description;
         }
         if (isset($json->event->url)) {
             $this->url = $json->event->url;
         }
         $timezone = new \DateTimeZone($this->timezone);
         if (isset($json->event->start->str)) {
             $this->start_at = new \DateTime($json->event->start->str, $timezone);
         }
         if (isset($json->event->end->str)) {
             $this->end_at = new \DateTime($json->event->end->str, $timezone);
         }
         if (isset($json->event->country) && isset($json->event->country->code) && $json->event->country->code) {
             $countryRepo = new CountryRepository();
             // Delibrately setting NULL on failure so user gets an error message.
             $this->country = $countryRepo->loadByTwoCharCode($json->event->country->code);
             // TODO check allowed in this site
         }
         if (isset($json->event->timezone)) {
             // Delibrately setting NULL on failure so user gets an error message.
             $this->timezone = $this->country && in_array($json->event->timezone, $this->country->getTimezonesAsList()) ? $json->event->timezone : null;
         }
     }
     if (isset($json->site)) {
         $siteRepo = new SiteRepository();
         if (isset($json->site->id)) {
             $this->site = $siteRepo->loadById($json->site->id);
         }
         if (isset($json->site->slug)) {
             $this->site = $siteRepo->loadBySlug($json->site->slug);
         }
     }
     if (isset($json->user)) {
         $userRepo = new UserAccountRepository();
         if (isset($json->user->email)) {
             $this->user = $userRepo->loadByEmail($json->user->email);
         } else {
             if (isset($json->user->username)) {
                 $this->user = $userRepo->loadByUserName($json->user->username);
             }
         }
     }
     if (isset($json->group)) {
         $groupRepo = new GroupRepository();
         if (isset($json->group->slug) && $this->site) {
             $this->group = $groupRepo->loadBySlug($this->site, $json->group->slug);
         } else {
             if (isset($json->group->id)) {
                 $this->group = $groupRepo->loadById($json->group->id);
             }
         }
     }
 }
 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";
     }
 }