Newscoop\Tools\Console\Command\SubscriptionsNotifierCommand::execute PHP Method

execute() protected method

See also: Console\Command\Command
protected execute ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output )
$input Symfony\Component\Console\Input\InputInterface
$output Symfony\Component\Console\Output\OutputInterface
    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>');
            }
        }
    }