public function listJson(Request $request, Application $app)
 {
     $grb = new GroupRepositoryBuilder();
     $grb->setSite($app['currentSite']);
     $ourRequest = new \Request($request);
     $grb->setIncludeDeleted($ourRequest->getGetOrPostBoolean('include_deleted', false));
     $out = array('groups' => array());
     foreach ($grb->fetchAll() as $group) {
         $out['groups'][] = array('slug' => $group->getSlug(), 'slugForURL' => $group->getSlugForUrl(), 'title' => $group->getTitle());
     }
     return json_encode($out);
 }
 function onThisStepSetUpPageView()
 {
     $out = array('groupSearchText' => $this->request->request->get('groupsearch'));
     if ($this->request->request->get('action') == 'groupsearch') {
         $grb = new GroupRepositoryBuilder();
         $grb->setSite($this->site);
         $grb->setIncludeDeleted(false);
         $grb->setFreeTextsearch($this->request->request->get('groupsearch'));
         $grb->setLimit(100);
         $out['groups'] = $grb->fetchAll();
     }
     return $out;
 }
 function json(Request $request, Application $app)
 {
     $groupRepoBuilder = new GroupRepositoryBuilder();
     $groupRepoBuilder->setSite($app['currentSite']);
     if (isset($_GET['search']) && trim($_GET['search'])) {
         $groupRepoBuilder->setFreeTextsearch($_GET['search']);
     }
     if (isset($_GET['includeDeleted'])) {
         if (in_array(strtolower($_GET['includeDeleted']), array('yes', 'on', '1'))) {
             $groupRepoBuilder->setIncludeDeleted(true);
         } else {
             if (in_array(strtolower($_GET['includeDeleted']), array('no', 'off', '0'))) {
                 $groupRepoBuilder->setIncludeDeleted(false);
             }
         }
     }
     $out = array();
     foreach ($groupRepoBuilder->fetchAll() as $group) {
         $out[] = array('slug' => $group->getSlug(), 'title' => $group->getTitle());
     }
     $response = new Response(json_encode(array('data' => $out)));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }
 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');
 }
 /**
  * @return array
  */
 public function getUserNotifyContentForSiteAndUser(\models\SiteModel $siteModel, UserAccountModel $userAccountModel)
 {
     global $CONFIG;
     if (!$siteModel->getIsFeatureGroup()) {
         return array();
     }
     $out = array();
     $grb = new GroupRepositoryBuilder();
     $grb->setSite($siteModel);
     $grb->setLimit(0);
     // all! No limit
     // TODO  don't we still want to do this? How will user A get a notification if user B deletes group? but then so far most group deletetions are by admins.
     $grb->setIncludeDeleted(false);
     foreach ($grb->fetchAll() as $group) {
         $uwg = $this->loadByUserAndGroup($userAccountModel, $group);
         if ($uwg && $uwg->getIsWatching()) {
             $dateSince = $uwg->getSinceDateForNotifyChecking();
             $historyRepositoryBuilder = new HistoryRepositoryBuilder();
             $historyRepositoryBuilder->setGroup($group);
             $historyRepositoryBuilder->setSince($dateSince);
             $historyRepositoryBuilder->setNotUser($userAccountModel);
             // Only admins can change tags at the moment so don't include
             $historyRepositoryBuilder->setIncludeTagHistory(false);
             $histories = $historyRepositoryBuilder->fetchAll();
             if ($histories) {
                 $content = new UserWatchesGroupNotifyContent();
                 $content->setHistories($histories);
                 $userWatchesGroupStopRepository = new UserWatchesGroupStopRepository();
                 $userWatchesGroupStop = $userWatchesGroupStopRepository->getForUserAndGroup($userAccountModel, $group);
                 $content->setUnwatchURL($CONFIG->getWebSiteDomainSecure($siteModel->getSlug()) . '/group/' . $group->getSlugForURL() . '/stopWatchingFromEmail/' . $userAccountModel->getId() . '/' . $userWatchesGroupStop->getAccessKey());
                 $content->setUserAccount($userAccountModel);
                 $content->setSite($siteModel);
                 $content->setGroup($group);
                 $content->setWatchedThingTitle($group->getTitle());
                 $content->setWatchedThingURL($CONFIG->getWebSiteDomainSecure($siteModel->getSlug()) . '/group/' . $group->getSlugForURL() . '/history');
                 $out[] = $content;
             }
         }
     }
     return $out;
 }