* @license PHP License * @package WB * @subpackage MailMime */ WBClass::load('WBCli' , 'WBClock' , 'WBObserver' ); /** * Command Line Interface class: Newsletter * * Send Newsletter Issue * * @version 1.2.1 * @package WB */ class WBCli_Newsletter extends WBCli { /** * Command line arguments * @var array */ protected $arguments = array( 'issue' => array( 'short' => 'i', 'max' => 1, 'min' => 0, 'default' => '', 'desc' => 'load issue', ), 'subscriber' => array( 'short' => 's', 'max' => 1, 'min' => 0, 'default' => '', 'desc' => 'send to given subscriber, only', ), 'ignoreapproved' => array( // 'short' => 'il', 'max' => 0, 'desc' => 'ignore double opt-in approved flag', ), 'ignorechecked' => array( // 'short' => 'il', 'max' => 0, 'desc' => 'ignore checked flag', ), 'ignorelang' => array( // 'short' => 'il', 'max' => 0, 'desc' => 'ignore language of newsletter issue', ), 'ignoresubscribedtimestamp' => array( // 'short' => 'il', 'max' => 0, 'desc' => 'Do not verify subscription timestamp', ), 'dontmarkissueassent' => array( // 'short' => 'il', 'max' => 0, 'desc' => "Don't mark newsletter issue as sent", ), 'dontmarksubscriberassent' => array( // 'short' => 'il', 'max' => 0, 'desc' => "Don't mark newsletter subriber as sent", ), CONSOLE_GETARGS_PARAMS => array( 'min' => 0, 'max' => 1, 'desc' => 'Command', 'default' => '' ) ); /** * Receiver List * @var WBDatasource_Newsletter_Receiver */ private $rcpt; /** * Newsletter Issue * @var WBDatasource_Newsletter_Issue */ private $issue; /** * Newsletter Mailer * @var WBMail_Newsletter */ private $nlMailer; /** * Mailer * @var WBMailer */ private $mail; /** * Lock * @var WBLock */ private $lock; /** * 2nd constructor * */ protected function init() { $this->rcpt = WBClass::create('WBDatasource_Newsletter_Receiver'); $this->issue = WBClass::create('WBDatasource_Newsletter_Issue'); $this->nlMailer = WBClass::create('WBMail_Newsletter'); $this->mail = WBClass::create('WBMailer'); } /** * execute programme * * Use observer and execute event queue */ protected function execute() { if (empty($this->commands)) { $this->commands = array('countrcpt'); } foreach ($this->commands as $c) { if ('ls' == strtolower($c)) { return $this->doLs(); } $method = 'do' . ucfirst($c); if (method_exists($this, $method)) { continue; } $this->pl($c . ': invalid command', STDERR); return; } $sub = $this->args->getValue('subscriber'); if (!empty($sub)) { if (!is_array($sub)) { $sub = array($sub); } $this->rcpt->setSubscribers($sub); } if ($this->args->isDefined('dontmarkissueassent')) { $this->pvl("Don't mark newsletter issue as sent"); } if ($this->args->isDefined('dontmarksubscriberassent')) { $this->pvl("Don't mark subscriber as sent"); } $this->rcpt->checkSubscriptionTimestamp(true); if ($this->args->isDefined('ignoresubscribedtimestamp')) { $this->pvl('Timestamp verification ignored'); $this->rcpt->checkSubscriptionTimestamp(false); } $this->rcpt->useApproved(true); if ($this->args->isDefined('ignoreapproved')) { $this->pvl('Double Opt-In approve ignored'); $this->rcpt->useApproved(false); } $this->rcpt->useChecked(true); if ($this->args->isDefined('ignorechecked')) { $this->pvl('Check flag ignored'); $this->rcpt->useChecked(false); } // load by given issue or get list $issues = $this->args->getValue('issue'); if (empty($issues)) { $issues = $this->issue->getList(-1, 10, 0); } if (!is_array($issues)) { $issues = array( array('id' => $issues) ); } // process issues foreach ($issues as $i) { $this->issue->load($i['id']); if (!$this->issue->isOk()) { continue; } $iss = $this->issue->get(); $this->pl('Issue ' . $iss['id']); $this->pvl(' ' . $iss['title']); $this->nlMailer->setIssue($this->issue); $this->rcpt->setIssue($this->issue); $this->rcpt->useLang(false); if (!$this->args->isDefined('ignorelang')) { $this->rcpt->setLang($this->issue->getLang()); $this->rcpt->useLang(true); $this->pvl(' Language ' . $this->issue->getLang()); } $top = $this->issue->getTopic(); foreach ($top as $t) { $this->pvl(' Topic ' . trim(strip_tags($t['title']))); } $this->rcpt->useTopic($top); foreach ($this->commands as $c) { $method = 'do' . ucfirst($c); if (method_exists($this, $method)) { $start = WBClock::now(); $this->$method($i['id']); $this->pvl(' Elapsed ' . WBClock::stop($start, 1, 1) . 's'); } } } } /** * Execute Command: List Issues * * @param string issue id */ private function doLs() { $this->pvl(' Newsletter Issues '); $issue = $this->issue->getList(-1, 10, 0); $this->pvl(sprintf('%15s | %20s | %35s', 'Id', 'Date', 'Title')); foreach ($issue as $i) { $this->pvl(sprintf('%15s | %20s | %35s', $i['id'], $i['created'], $i['title'])); } // $this->pl($this->rcpt->count()); // $this->pvl('list'); } /** * Execute Command: CountRCPT * * @param string issue id */ private function doCountrcpt($issue) { $this->pv(' RCPTs '); $this->pl($this->rcpt->count()); } /** * Execute Command: Resend * * @param string issue id */ private function doResend($issue) { $this->p(' Resend'); if (empty($this->issue->getSentDate())) { $this->pvl(' not sent, yet!'); return; } $this->pvl(''); $cnt = $this->sendNewsletter(); } /** * Execute Command: Send * * @param string issue id */ private function doSend($issue) { $this->p(' Send'); if (!empty($this->issue->getSentDate())) { $this->pvl(' already sent!'); return; } $this->pvl(''); $cnt = $this->sendNewsletter(); if (!$this->args->isDefined('dontmarkissueassent')) { $this->issue->setSent(); } } /** * Actually Send Newsletter * * @return int number of rcpts that newsletter was sent to */ private function sendNewsletter() { if (!$this->aquireLock()) { $this->pl(' locked!'); return 0; } $cnt = 0; foreach ($this->rcpt as $sub) { if (empty($sub['email'])) { continue; } $sent = $this->rcpt->getSent($this->issue->getId()); if (!empty($sent)) { continue; } $this->nlMailer->setSubscriberData($sub); $this->nlMailer->prepare(); $this->mail->setMimeMail($this->nlMailer->getMimeMail()); $this->mail->addRcpt($sub['email']); $this->mail->send(); if (!$this->args->isDefined('dontmarksubscriberassent')) { $this->rcpt->markSent($this->issue->getId()); } $this->pvl(' ' .$sub['id'] . ' ' . $sub['email']); ++$cnt; } $this->releaseLock(); $this->pv(' RCPTs'); $this->pl(' ' . $cnt); return $cnt; } /** * Aquire Lock for Sending * * Non-blocking lock * @return bool true of lock was available */ private function aquireLock() { if (empty($this->lock)) { $this->lock = WBClass::create('WBLock'); $this->lock->setName('newsletter-send'); } return $this->lock->acquire(); } /** * Release Sending Lock * * Give back lock and allow othere to send */ private function releaseLock() { return $this->lock->release(); } /** * get help text - head * * Override this method to set suitable help text * * @return string */ protected function getHelpHead() { $head = array(); $head[] = 'Newsletter Help'; $head[] = ''; $head[] = 'Usage: ' . basename($_SERVER['_']) . ' [OPTION]... COMMAND... '; $head[] = ''; $head[] = 'Commands: '; $head[] = ' - ls: list newsletter issues that are ready to send'; $head[] = ' - countrcpt: count potiential recipients for newsletter'; $head[] = ' - send: actually send newsletter!'; $head[] = ' - resend: send newsletter that was already marked as sent.'; $head[] = ' Skip subscribers who were marked as receivers of this newsletter'; $head[] = 'Options:'; $head[] = ''; return implode("\n", $head); } }