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"; } $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"; } }
/** * One event, 31 days from now, then 30 days, then 29 days, etc, only 1 email sent * @global type $CONFIG */ function test6() { global $CONFIG; $CONFIG->userWatchesPromptEmailSafeGapDays = 30; \TimeSource::mock(2013, 1, 1, 0, 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("Group"); $groupRepo = new GroupRepository(); $groupRepo->create($group, $site, $user); $event = new EventModel(); $start = \TimeSource::getDateTime(); $start->setDate(2013, 30, 9); $start->setTime(9, 0, 0); $event->setStartAt($start); $end = \TimeSource::getDateTime(); $end->setDate(2013, 30, 9); $end->setTime(12, 0, 0); $event->setEndAt($end); $eventRepo = new EventRepository(); $eventRepo->create($event, $site, $user, $group); // User will watch site automatically in site->create() // We don't want that, we want the group instead $userWatchesSiteRepo = new UserWatchesSiteRepository(); $userWatchesSiteRepo->stopUserWatchingSite($user, $site); $userWatchesGroupRepo = new UserWatchesGroupRepository(); $userWatchesGroupRepo->startUserWatchingGroup($user, $group); #Before email sent! for ($day = 1; $day <= 29; $day++) { \TimeSource::mock(2013, $day, 8, 1, 0, 0); $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group); $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId())); $this->assertFalse($data['moreEventsNeeded']); } #Email sent! \TimeSource::mock(2013, 30, 8, 1, 0, 0); $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group); $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId())); $this->assertTrue($data['moreEventsNeeded']); $userWatchesGroupRepo->markPromptEmailSent($userWatchesGroup, \TimeSource::getDateTime()); #After email sent \TimeSource::mock(2013, 31, 8, 1, 0, 0); $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group); $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId())); $this->assertFalse($data['moreEventsNeeded']); for ($day = 1; $day <= 30; $day++) { \TimeSource::mock(2013, $day, 9, 1, 0, 0); $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group); $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId())); $this->assertFalse($data['moreEventsNeeded']); } for ($day = 1; $day <= 31; $day++) { \TimeSource::mock(2013, $day, 10, 1, 0, 0); $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group); $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId())); $this->assertFalse($data['moreEventsNeeded']); } for ($day = 1; $day <= 30; $day++) { \TimeSource::mock(2013, $day, 11, 1, 0, 0); $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group); $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId())); $this->assertFalse($data['moreEventsNeeded']); } for ($day = 1; $day <= 31; $day++) { \TimeSource::mock(2013, $day, 12, 1, 0, 0); $userWatchesGroup = $userWatchesGroupRepo->loadByUserAndGroup($user, $group); $data = $userWatchesGroup->getPromptEmailData($site, $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId())); $this->assertFalse($data['moreEventsNeeded']); } }