class Cli extends UserInterface
{
- protected function loadParameters($cfg)
+ protected function loadParameters()
{
$params = $GLOBALS['argv'];
array_shift($params);
$storeInto = null;
foreach ($params as $param) {
if ($storeInto !== null) {
- $cfg->$storeInto = (int)$param;
+ $this->config->$storeInto = (int)$param;
$storeInto = null;
continue;
}
protected function render($arEvents)
{
- $r = new Renderer_Console();
+ $r = new Renderer_Mail();
+ $r->config = $this->config;
$r->ansi = true;
echo $r->render($arEvents);
}
class Config
{
public $source;
+ public $date;
public $daysBefore;
public $daysAfter;
public $locale;
protected function loadFile($filename)
{
include $filename;
- $this->source = $source;
- $this->daysBefore = $daysBefore;
- $this->daysAfter = $daysAfter;
- if (isset($locale)) {
- $this->locale = $locale;
+ $vars = get_defined_vars();
+ foreach ($vars as $k => $value) {
+ $this->$k = $value;
}
}
return new $class($settings[0]);
}
+
+ public function get($varname, $default = '')
+ {
+ if (!isset($this->$varname) || $this->$varname == '') {
+ return $default;
+ }
+ return $this->$varname;
+ }
}
?>
--- /dev/null
+<?php
+namespace bdrem;
+
+require_once 'Mail/mime.php';
+
+class Renderer_Mail
+{
+ public function render($arEvents)
+ {
+ $todays = array();
+ foreach ($arEvents as $event) {
+ if ($event->days == 0) {
+ $todays[] = $this->shorten($event->title, 15);
+ }
+ }
+ $subject = 'Birthday reminder';
+ if (count($todays)) {
+ $subject .= ': ' . implode(', ', $todays);
+ }
+
+ $rc = new Renderer_Console();
+ $rh = new Renderer_Html();
+
+ $hdrs = array(
+ 'Subject' => $subject
+ );
+ $mime = new \Mail_mime(
+ array(
+ 'eol' => "\n",
+ 'head_charset' => 'utf-8',
+ 'text_charset' => 'utf-8',
+ 'html_charset' => 'utf-8',
+ )
+ );
+
+ $mime->setTXTBody($rc->render($arEvents));
+ $mime->setHTMLBody($rh->render($arEvents));
+
+ $body = $mime->get();
+ $hdrs = $mime->headers($hdrs);
+ $textHeaders = '';
+ foreach ($hdrs as $k => $v) {
+ $textHeaders .= $k . ':' . $v . "\n";
+ }
+
+ foreach ((array) $this->config->get('mail_to') as $recipient) {
+ mail($recipient, $subject, $body, $textHeaders);
+ }
+ }
+
+ protected function shorten($str, $len)
+ {
+ if (mb_strlen($str) <= $len) {
+ return $str;
+ }
+
+ return mb_substr($str, 0, $len - 1) . '…';
+ }
+}
+?>
\ No newline at end of file
abstract class UserInterface
{
+ protected $config;
+
public function run()
{
- $cfg = new Config();
- $cfg->load();
- setlocale(LC_TIME, $cfg->locale);
- $source = $cfg->loadSource();
+ $this->config = new Config();
+ $this->config->load();
+ $this->config->date = date('Y-m-d');
+ setlocale(LC_TIME, $this->config->locale);
+ $source = $this->config->loadSource();
- $this->loadParameters($cfg);
+ $this->loadParameters($this->config);
$arEvents = $source->getEvents(
- date('Y-m-d'), $cfg->daysBefore, $cfg->daysAfter
+ $this->config->date,
+ $this->config->daysBefore, $this->config->daysAfter
);
usort($arEvents, '\\bdrem\\Event::compare');
$this->render($arEvents);
}
- protected function loadParameters($cfg)
+ protected function loadParameters()
{
}
echo $r->render($arEvents);
}
- protected function loadParameters($cfg)
+ protected function loadParameters()
{
if (isset($_GET['daysBefore'])) {
- $cfg->daysBefore = (int) $_GET['daysBefore'];
+ $this->config->daysBefore = (int) $_GET['daysBefore'];
}
if (isset($_GET['daysAfter'])) {
- $cfg->daysAfter = (int) $_GET['daysAfter'];
+ $this->config->daysAfter = (int) $_GET['daysAfter'];
}
}
}