/**
  * @see Console\Command\Command
  */
 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
 {
     $this->em = $this->getApplication()->getKernel()->getContainer()->get('doctrine')->getManager();
     $etcDirectory = APPLICATION_PATH . '/../conf/';
     $notifierTemplate = '_events_notifier.tpl';
     $message = array();
     if (!FilesystemService::isReadable($etcDirectory . '/install_conf.php')) {
         exit;
     }
     // includes installation configuration file
     require_once $etcDirectory . '/install_conf.php';
     // includes campsite initialisation
     require_once APPLICATION_PATH . '/../include/campsite_init.php';
     if (!is_file($etcDirectory . '/database_conf.php')) {
         $output->writeln('Database configuration file is missing!');
         return;
     }
     $message['reply'] = $this->getReplyAddress();
     $autoId = $this->getAutoId();
     $logTimestamp = $autoId->getLogTimestamp();
     $logs = $this->em->getRepository('Newscoop\\Entity\\Log')->createQueryBuilder('l')->select('l, e, u')->leftJoin('l.eventId', 'e')->leftJoin('l.userId', 'u')->where('l.eventId = e.id')->andWhere('l.userId = u.id')->andWhere('e.notify = :notify')->andWhere('l.created > :logTimestamp')->getQuery()->setParameters(array('logTimestamp' => $logTimestamp, 'notify' => 'Y'))->getResult();
     if (count($logs) == 0) {
         return false;
     }
     $tpl = $this->initSmarty();
     $recipients = $this->getApplication()->getKernel()->getContainer()->getService('notification')->findRecipients();
     $lastTimestamp = null;
     if ($input->getOption('verbose')) {
         $output->writeln('<info>Number of found logs: ' . count($logs) . '.<info>');
     }
     foreach ($logs as $log) {
         $lastTimestamp = $log->getCreated();
         $tpl->assign('user_real_name', $log->getUser()->getFirstName());
         $tpl->assign('user_name', $log->getUser()->getUsername());
         $tpl->assign('user_email', $log->getUser()->getEmail());
         $tpl->assign('event_text', $log->getMessage());
         $tpl->assign('event_timestamp', $log->getCreated()->format('Y-m-d h:i:s'));
         $message['text'] = $tpl->fetch($notifierTemplate);
         if (count($recipients) <= 0) {
             if ($input->getOption('verbose')) {
                 $output->writeln('<error>There is no recipients.<error>');
             }
             continue;
         }
         $message['recipients'] = $recipients;
         $message['subject'] = $log->getEvent()->getName();
         $this->sendEmail($message);
         if ($input->getOption('verbose')) {
             $output->writeln('<info>Send message for event: ' . $log->getEvent()->getName() . '.<info>');
         }
     }
     if ($lastTimestamp != null) {
         $autoId->setLogTimestamp($lastTimestamp);
     }
     $this->em->flush();
 }
 /**
  * @see Console\Command\Command
  */
 protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
 {
     $this->em = $this->getApplication()->getKernel()->getContainer()->getService('em');
     $etcDirectory = APPLICATION_PATH . '/../conf/';
     $notifierTemplate = '_subscription_notifier.tpl';
     $message = array();
     $notifiedIndex = false;
     if (!FilesystemService::isReadable($etcDirectory . '/install_conf.php')) {
         exit;
     }
     require_once $etcDirectory . '/install_conf.php';
     require_once APPLICATION_PATH . '/../include/campsite_init.php';
     if (!is_file($etcDirectory . '/database_conf.php')) {
         $output->writeln('Database configuration file is missed!');
         return;
     }
     $message['reply'] = $this->getReplyAddress();
     $endingSubscriptions = $this->getEndingSubscriptions();
     if (count($endingSubscriptions) == 0) {
         if ($input->getOption('verbose')) {
             $output->writeln('<info>There is no ending subscriptions.<info>');
         }
         return;
     }
     $tpl = $this->initSmarty();
     $notifiedIndex = 0;
     foreach ($endingSubscriptions as $subscription) {
         $issueMaxNumber = $this->getIssueMaxNumber($subscription->getPublication()->getId(), $subscription->getPublication()->getDefaultLanguage()->getId());
         $subscriptionSectionToUpdate = $this->em->getRepository('Newscoop\\Subscription\\Section')->createQueryBuilder('ss')->where('ss.subscription = :subscription')->setParameter('subscription', $subscription->getId());
         if ($issueMaxNumber === false) {
             if ($input->getOption('verbose')) {
                 $output->writeln('<info>There is o issues for publication default language.<info>');
             }
             return;
         }
         $sectionsCount = $this->getSectionCounts($subscription, $issueMaxNumber);
         $subscritpionsSections = $this->getSubscritpionsSections($subscription->getId());
         if ($subscritpionsSections <= 0) {
             continue;
         }
         $text = '';
         $notify = false;
         $subsSections = 0;
         $counter = 0;
         $sections = '';
         foreach ($subscritpionsSections as $subscriptionSection) {
             $startDate = $subscriptionSection[0]->getStartDate();
             $formatedStartDate = $subscriptionSection['formated_start_date'];
             $paidDays = $subscriptionSection[0]->getPaidDays();
             $toDaysStartDate = $subscriptionSection['to_days_start_date'];
             $toDaysNow = $subscriptionSection['to_days_now'];
             $formatedEndDate = $subscriptionSection['formated_end_date'];
             if ($toDaysNow > $paidDays + $toDaysStartDate) {
                 continue;
             }
             $remainedDays = $paidDays + $toDaysStartDate - $toDaysNow;
             if ($remainedDays > 14 || $remainedDays <= 0) {
                 continue;
             }
             $notify = true;
             if (count($subscritpionsSections) == 1) {
                 $subSectionsCount = $this->em->getRepository('Newscoop\\Subscription\\Section')->createQueryBuilder('ss')->select('ss')->where('ss.subscription = :subscription')->andWhere('ss.noticeSent = :noticeSent')->andWhere('ss.startDate = :startDate')->andWhere('ss.paidDays = :paidDays')->getQuery()->setParameters(array('subscription' => $subscription->getId(), 'noticeSent' => 'N', 'startDate' => $startDate, 'paidDays' => $paidDays))->getResult();
             }
             if ($counter == 0) {
                 $subsType = $subscription->getType() == 'P' ? 'paid' : 'trial';
                 $tpl->assign('user_title', $subscription->getUser()->getFirstName());
                 $tpl->assign('user_name', $subscription->getUser()->getFirstName());
                 $tpl->assign('subs_type', $subsType);
                 $tpl->assign('subs_date', $formatedStartDate);
                 $tpl->assign('publication_name', $subscription->getPublication()->getName());
             }
             if ($subsSections == $sectionsCount && count($subscritpionsSections) == 1) {
                 $tpl->assign('subs_expire', 1);
                 $tpl->assign('subs_expire_date', $formatedEndDate);
                 $tpl->assign('subs_remained_days', $remainedDays);
             } else {
                 $sectionData = $this->em->getRepository('Newscoop\\Entity\\Section')->createQueryBuilder('s')->select('s.name, s.number')->from('Newscoop\\Subscription\\Section', 'ss')->andWhere('ss.subscription = :subscription')->andWhere('ss.noticeSent = :noticeSent')->andWhere('ss.startDate = :startDate')->andWhere('ss.paidDays = :paidDays')->andWhere('s.publication = :publication')->andWhere('s.issue = :issue')->andWhere('s.language = :language')->andWhere('s.number = ss.sectionNumber')->getQuery()->setParameters(array('subscription' => $subscription->getId(), 'noticeSent' => 'N', 'startDate' => $startDate, 'paidDays' => $paidDays, 'publication' => $subscription->getPublication()->getId(), 'issue' => $issueMaxNumber, 'language' => $subscription->getPublication()->getDefaultLanguage()))->getResult();
                 if ($counter == 0) {
                     $tpl->assign('subs_expire_plan', 1);
                 }
                 $expirePlan = '\\t- ';
                 $isFirst = true;
                 foreach ($sectionData as $key => $section) {
                     if (!$isFirst) {
                         $expirePlan .= ', ';
                     } else {
                         $isFirst = false;
                     }
                     $subscriptionSectionToUpdate->orWhere('ss.section = :sectionNumber_' . $key);
                     $subscriptionSectionToUpdate->setParameter('sectionNumber_' . $key, $section['number']);
                     $expirePlan .= '"' . $section['name'] . '"';
                 }
                 $tpl->assign('expire_plan', $expirePlan);
                 $tpl->assign('subs_expire_date', $formatedEndDate);
                 $tpl->assign('subs_remained_days', $remainedDays);
                 $tpl->assign('subs_start_date', $formatedStartDate);
                 $counter++;
             }
         }
         $tpl->assign('site', $subscription->getPublication()->getDefaultAlias()->getName());
         if (!$notify) {
             continue;
         }
         $message['recipients'] = array($subscription->getUser()->getEmail());
         $message['text'] = $tpl->fetch($notifierTemplate);
         $message['subject'] = 'Subscription to ' . $subscription->getPublication()->getName();
         if ($this->sendEmail($message) == false) {
             continue;
         }
         $subscriptionSectionToUpdate = $subscriptionSectionToUpdate->getQuery()->getResult();
         foreach ($subscriptionSectionToUpdate as $updateMe) {
             $updateMe->setNoticeSent('Y');
         }
         $this->em->flush();
         $notifiedIndex++;
     }
     if ($notifiedIndex > 0) {
         if ($input->getOption('verbose')) {
             $output->writeln('<info>' . $notifiedIndex . ' user(s) notified.<info>');
         }
     }
 }