function test1()
 {
     $userOwner = new UserAccountModel();
     $userOwner->setEmail("*****@*****.**");
     $userOwner->setUsername("test2");
     $userOwner->setPassword("password");
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $userRepo->create($userOwner);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $userOwner, array(), $this->getSiteQuotaUsedForTesting());
     $userWatchesSiteRepo = new UserWatchesSiteRepository();
     # Part 1: User does not watch site
     $t = $userWatchesSiteRepo->loadByUserAndSite($user, $site);
     $this->assertNull($t);
     $b = new UserWatchesSiteRepositoryBuilder();
     $t = $b->fetchAll();
     $this->assertEquals(1, count($t));
     # Part 2: Watches!
     $userWatchesSiteRepo->startUserWatchingSite($user, $site);
     $t = $userWatchesSiteRepo->loadByUserAndSite($user, $site);
     $this->assertEquals($user->getId(), $t->getUserAccountId());
     $this->assertEquals(true, $t->getIsWatching());
     $this->assertEquals(true, $t->getIsWasOnceWatching());
     $b = new UserWatchesSiteRepositoryBuilder();
     $t = $b->fetchAll();
     $this->assertEquals(2, count($t));
     # Part 3: Stops Watching!
     $userWatchesSiteRepo->stopUserWatchingSite($user, $site);
     $t = $userWatchesSiteRepo->loadByUserAndSite($user, $site);
     $this->assertEquals(false, $t->getIsWatching());
     $this->assertEquals(true, $t->getIsWasOnceWatching());
     $b = new UserWatchesSiteRepositoryBuilder();
     $t = $b->fetchAll();
     $this->assertEquals(1, count($t));
 }
 protected function run()
 {
     global $CONFIG;
     $userRepo = new UserAccountRepository();
     $siteRepo = new SiteRepository();
     $eventRepo = new EventRepository();
     $userWatchesSiteRepository = new UserWatchesSiteRepository();
     $userWatchesSiteStopRepository = new UserWatchesSiteStopRepository();
     $userAccountGeneralSecurityKeyRepository = new UserAccountGeneralSecurityKeyRepository();
     $userNotificationRepo = new UserNotificationRepository();
     $userHasNoEditorPermissionsInSiteRepo = new UserHasNoEditorPermissionsInSiteRepository();
     $userPermissionsRepo = new UserPermissionsRepository($this->app['extensions']);
     /** @var usernotifications/UserWatchesSiteGroupPromptNotificationType **/
     $userNotificationType = $this->app['extensions']->getCoreExtension()->getUserNotificationType('UserWatchesSitePrompt');
     $b = new UserWatchesSiteRepositoryBuilder();
     foreach ($b->fetchAll() as $userWatchesSite) {
         $user = $userRepo->loadByID($userWatchesSite->getUserAccountId());
         $site = $siteRepo->loadById($userWatchesSite->getSiteId());
         $siteRepo->loadLegacyFeaturesOnSite($site);
         // 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);
         $this->logVerbose(" User " . $user->getEmail() . " Site " . $site->getTitle());
         if ($site->getIsClosedBySysAdmin()) {
             $this->logVerbose(" ... site is closed");
         } else {
             if ($userHasNoEditorPermissionsInSiteRepo->isUserInSite($user, $site)) {
                 $this->logVerbose(" ... user does not have edit permissions allowed in site");
             } else {
                 if (!$userPermissions->hasPermission("org.openacalendar", "CALENDAR_CHANGE")) {
                     $this->logVerbose(" ... user does not have org.openacalendar/CALENDAR_CHANGE permission in site");
                     // Technically UserWatchesSiteRepositoryBuilder() should only return getIsWatching() == true but lets double check
                 } else {
                     if ($userWatchesSite->getIsWatching()) {
                         $this->logVerbose(" ... searching for data");
                         $lastEvent = $eventRepo->loadLastNonDeletedNonImportedByStartTimeInSiteId($site->getId());
                         $data = $userWatchesSite->getPromptEmailData($site, $lastEvent);
                         if ($data['moreEventsNeeded']) {
                             $this->logVerbose(" ... found data");
                             ///// Notification Class
                             $userNotification = $userNotificationType->getNewNotification($user, $site);
                             ////// Save Notification Class
                             $userNotificationRepo->create($userNotification);
                             ////// Send Email
                             if ($userNotification->getIsEmail()) {
                                 $userWatchesSiteStop = $userWatchesSiteStopRepository->getForUserAndSite($user, $site);
                                 configureAppForSite($site);
                                 configureAppForUser($user);
                                 $userAccountGeneralSecurityKey = $userAccountGeneralSecurityKeyRepository->getForUser($user);
                                 $unsubscribeURL = $CONFIG->getWebIndexDomainSecure() . '/you/emails/' . $user->getId() . '/' . $userAccountGeneralSecurityKey->getAccessKey();
                                 $lastEventsBuilder = new EventRepositoryBuilder();
                                 $lastEventsBuilder->setSite($site);
                                 $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 " . $site->getTitle() . "?");
                                 $message->setFrom(array($CONFIG->emailFrom => $CONFIG->emailFromName));
                                 $message->setTo($user->getEmail());
                                 $messageText = $this->app['twig']->render('email/userWatchesSitePromptEmail.txt.twig', array('user' => $user, 'lastEvents' => $lastEvents, 'stopCode' => $userWatchesSiteStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'unsubscribeURL' => $unsubscribeURL));
                                 if ($CONFIG->isDebug) {
                                     file_put_contents('/tmp/userWatchesSitePromptEmail.txt', $messageText);
                                 }
                                 $message->setBody($messageText);
                                 $messageHTML = $this->app['twig']->render('email/userWatchesSitePromptEmail.html.twig', array('user' => $user, 'lastEvents' => $lastEvents, 'stopCode' => $userWatchesSiteStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'unsubscribeURL' => $unsubscribeURL));
                                 if ($CONFIG->isDebug) {
                                     file_put_contents('/tmp/userWatchesSitePromptEmail.html', $messageHTML);
                                 }
                                 $message->addPart($messageHTML, 'text/html');
                                 $headers = $message->getHeaders();
                                 $headers->addTextHeader('List-Unsubscribe', $unsubscribeURL);
                                 $this->logVerbose(" ... sending");
                                 if (!$CONFIG->isDebug) {
                                     $this->app['mailer']->send($message);
                                 }
                                 $userNotificationRepo->markEmailed($userNotification);
                             }
                             $userWatchesSiteRepository->markPromptEmailSent($userWatchesSite, $data['checkTime']);
                         }
                     }
                 }
             }
         }
     }
     return array('result' => 'ok');
 }
 protected function run()
 {
     global $CONFIG;
     $userRepo = new UserAccountRepository();
     $siteRepo = new SiteRepository();
     $eventRepo = new EventRepository();
     $userWatchesSiteRepository = new UserWatchesSiteRepository();
     $userWatchesSiteStopRepository = new UserWatchesSiteStopRepository();
     $userAccountGeneralSecurityKeyRepository = new UserAccountGeneralSecurityKeyRepository();
     $userNotificationRepo = new UserNotificationRepository();
     /** @var usernotifications/UserWatchesSiteGroupPromptNotificationType **/
     $userNotificationType = $this->app['extensions']->getCoreExtension()->getUserNotificationType('UserWatchesSiteGroupPrompt');
     $b = new UserWatchesSiteRepositoryBuilder();
     foreach ($b->fetchAll() as $userWatchesSite) {
         $user = $userRepo->loadByID($userWatchesSite->getUserAccountId());
         $site = $siteRepo->loadById($userWatchesSite->getSiteId());
         $siteRepo->loadLegacyFeaturesOnSite($site);
         // to avoid flooding user we only send one group email per run
         $anyGroupNotificationsSent = false;
         $this->logVerbose(" User " . $user->getEmail() . " Site " . $site->getTitle());
         if ($site->getIsClosedBySysAdmin()) {
             $this->logVerbose(" ... site is closed");
             // Technically UserWatchesSiteRepositoryBuilder() should only return getIsWatching() == true but lets double check
         } else {
             if ($userWatchesSite->getIsWatching()) {
                 $groupRepoBuilder = new GroupRepositoryBuilder();
                 $groupRepoBuilder->setSite($site);
                 $groupRepoBuilder->setIncludeDeleted(false);
                 foreach ($groupRepoBuilder->fetchAll() as $group) {
                     if (!$anyGroupNotificationsSent) {
                         $this->logVerbose(" ... searching group " . $group->getSlug() . " for data");
                         $lastEvent = $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId());
                         $data = $userWatchesSite->getGroupPromptEmailData($site, $group, $lastEvent);
                         if ($data['moreEventsNeeded']) {
                             $this->logVerbose(" ... found data ");
                             ///// Notification Class
                             $userNotification = $userNotificationType->getNewNotification($user, $site);
                             $userNotification->setGroup($group);
                             ////// Save Notification Class
                             $userNotificationRepo->create($userNotification);
                             ////// Send Email
                             if ($userNotification->getIsEmail()) {
                                 $userWatchesSiteStop = $userWatchesSiteStopRepository->getForUserAndSite($user, $site);
                                 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->userWatchesSiteGroupPromptEmailShowEvents);
                                 $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 = $this->app['twig']->render('email/userWatchesSiteGroupPromptEmail.txt.twig', array('user' => $user, 'group' => $group, 'lastEvents' => $lastEvents, 'stopCode' => $userWatchesSiteStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'unsubscribeURL' => $unsubscribeURL));
                                 if ($CONFIG->isDebug) {
                                     file_put_contents('/tmp/userWatchesSiteGroupPromptEmail.txt', $messageText);
                                 }
                                 $message->setBody($messageText);
                                 $messageHTML = $this->app['twig']->render('email/userWatchesSiteGroupPromptEmail.html.twig', array('user' => $user, 'group' => $group, 'lastEvents' => $lastEvents, 'stopCode' => $userWatchesSiteStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'unsubscribeURL' => $unsubscribeURL));
                                 if ($CONFIG->isDebug) {
                                     file_put_contents('/tmp/userWatchesSiteGroupPromptEmail.html', $messageHTML);
                                 }
                                 $message->addPart($messageHTML, 'text/html');
                                 $headers = $message->getHeaders();
                                 $headers->addTextHeader('List-Unsubscribe', $unsubscribeURL);
                                 $this->logVerbose(" ... sending");
                                 if (!$CONFIG->isDebug) {
                                     $this->app['mailer']->send($message);
                                 }
                                 $userNotificationRepo->markEmailed($userNotification);
                             }
                             $userWatchesSiteRepository->markGroupPromptEmailSent($userWatchesSite, $group, $data['checkTime']);
                             $anyGroupNotificationsSent = true;
                         }
                     }
                 }
             }
         }
     }
     return array('result' => 'ok');
 }
 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";
     }
 }