$source = array('Bdf', '/path/to/birthday.bdf');
$daysBefore = 3;
$daysAfter = 14;
+$locale = 'de_DE.UTF-8';
?>
{
$cfg = new Config();
$cfg->load();
+ setlocale(LC_TIME, $cfg->locale);
$source = $cfg->loadSource();
$arEvents = $source->getEvents(
public $source;
public $daysBefore;
public $daysAfter;
+ public $locale;
public function load()
{
$this->source = $source;
$this->daysBefore = $daysBefore;
$this->daysAfter = $daysAfter;
+ if (isset($locale)) {
+ $this->locale = $locale;
+ }
}
public function loadSource()
$class = '\\bdrem\\Source_' . array_shift($settings);
return new $class($settings[0]);
- //$rm = new \ReflectionMethod($class, '__construct');
- //return $rm->invokeArgs(null, $settings);
- //return call_user_func_array($class . '::__construct', $settings);
}
}
?>
*/
public $date;
+ /**
+ * Reference date against which $age and $days are calculated
+ * (often today)
+ *
+ * @var string YYYY-MM-DD
+ */
+ public $refDate;
+
+ /**
+ * "Localized" $date used for calculations against $refDate.
+ * Month and day are the same as in $date, year is near $refDate's year.
+ *
+ * @var string YYYY-MM-DD
+ */
+ public $localDate;
+
/**
* Which repetition this is
*
*/
public function isWithin($strDate, $nDaysBefore, $nDaysAfter)
{
+ $this->refDate = $strDate;
list($rYear, $rMonth, $rDay) = explode('-', $strDate);
list($eYear, $eMonth, $eDay) = explode('-', $this->date);
if ($rMonth == $eMonth && $rDay == $eDay) {
+ $this->localDate = $strDate;
$this->days = 0;
if ($eYear == '????') {
$this->age = null;
$yearOffset = -1;
}
+ $this->localDate = ($rYear + $yearOffset) . '-' . $eMonth . '-' . $eDay;
$rD = new \DateTime($strDate);
- $eD = new \DateTime(($rYear + $yearOffset) . '-' . $eMonth . '-' . $eDay);
+ $eD = new \DateTime($this->localDate);
$nDiff = (int) $rD->diff($eD)->format('%r%a');
{
public function render($arEvents)
{
- $s = "Days Age Name Event Date\n";
- $s .= "---- --- ---------------------------------------- -------------------- ----------\n";
- foreach ($arEvents as $event) {
- $s .= sprintf(
- "%3d %4s %s %s %s\n",
- $event->days,
- $event->age,
- $this->str_pad($event->title, 40),
- $this->str_pad($event->type, 20),
- $event->date
- );
- }
- return $s;
- }
+ $tbl = new \Console_Table(
+ CONSOLE_TABLE_ALIGN_LEFT,
+ array('sect' => '', 'rule' => '-', 'vert' => '')
+ );
+ $tbl->setAlign(0, CONSOLE_TABLE_ALIGN_RIGHT);
+ $tbl->setAlign(1, CONSOLE_TABLE_ALIGN_RIGHT);
- public function str_pad(
- $input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT
- ) {
- $l = mb_strlen($input, 'utf-8');
- if ($l >= $pad_length) {
- return $input;
- }
+ $tbl->setHeaders(
+ array('Days', 'Age', 'Name', 'Event', 'Date', 'Day')
+ );
- $p = str_repeat($pad_string, $pad_length - $l);
- if ($pad_type == STR_PAD_RIGHT) {
- return $input . $p;
- } else {
- return $p . $input;
+ foreach ($arEvents as $event) {
+ $tbl->addRow(
+ array(
+ $event->days,
+ $event->age,
+ wordwrap($event->title, 30, "\n", true),
+ wordwrap($event->type, 20, "\n", true),
+ $event->date,
+ strftime('%a', strtotime($event->localDate))
+ )
+ );
}
+ return $tbl->getTable();
}
}
?>
--- /dev/null
+<?php
+namespace bdrem;
+
+class Renderer_Html
+{
+ public function render($arEvents)
+ {
+ $tr = new Renderer_HtmlTable();
+ $table = $tr->render($arEvents);
+ $s = <<<HTM
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+ <head>
+ <title>bdrem</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <style type="text/css">
+table {
+ border: 1px solid black;
+ border-collapse: collapse;
+ margin-left: auto;
+ margin-right: auto;
+}
+td, th {
+ border: 1px solid grey;
+ border-left: 0px;
+ border-right: 0px;
+ padding: 0.1ex 1ex;
+}
+
+tr.prev td {
+ background-color: #C4DDF4;
+}
+tr.today td {
+ background-color: #FEDCBA;
+}
+tr.next td {
+ background-color: #DEFABC;
+}
+tr:hover td {
+ background-color: white;
+}
+
+.r {
+ text-align: right;
+}
+
+tr td.icon {
+ background-color: white;
+}
+tr.prev td.icon {
+ color: #00A;
+}
+tr.today td.icon {
+ color: black;
+ background-color: #FEDCBA;
+}
+tr.next td.icon {
+ color: #080;
+}
+
+tr.d-3 td.icon:before {
+ content: "\342\227\224"
+}
+tr.d-2 td.icon:before {
+ content: "\342\227\221"
+}
+tr.d-1 td.icon:before {
+ content: "\342\227\225"
+}
+tr.d0 td.icon:before {
+ content: "\342\230\205"
+}
+tr.d1 td.icon:before {
+ content: "\342\227\225"
+}
+tr.d2 td.icon:before {
+ content: "\342\227\221"
+}
+tr.d3 td.icon:before {
+ content: "\342\227\224"
+}
+ </style>
+ </head>
+ <body>
+$table
+ </body>
+</html>
+HTM;
+ return $s;
+ }
+}
+?>
--- /dev/null
+<?php
+namespace bdrem;
+
+class Renderer_HtmlTable
+{
+ public function render($arEvents)
+ {
+ $s = <<<HTM
+<table>
+ <thead>
+ <tr>
+ <th colspan="2">Days</th>
+ <th>Age</th>
+ <th>Event</th>
+ <th>Name</th>
+ <th>Date</th>
+ <th>Day</th>
+ </tr>
+ </thead>
+ <tbody>
+
+HTM;
+ foreach ($arEvents as $event) {
+ $class = 'd' . $event->days;
+ if ($event->days < 0) {
+ $class .= ' prev';
+ } else if ($event->days == 0) {
+ $class .= ' today';
+ } else {
+ $class .= ' next';
+ }
+ $s .= sprintf(
+ '<tr class="' . trim($class) . '">'
+ . '<td class="icon"></td>'
+ . '<td class="r">%d</td>'
+ . '<td class="r">%s</td>'
+ . '<td>%s</td>'
+ . '<td>%s</td>'
+ . '<td class="r">%s</td>'
+ . '<td>%s</td>'
+ . "</tr>\n",
+ $event->days,
+ $event->age,
+ $event->title,
+ $event->type,
+ $event->date,
+ strftime('%a', strtotime($event->localDate))
+ );
+ }
+ $s .= <<<HTM
+ </tbody>
+</table>
+
+HTM;
+ return $s;
+ }
+}
+?>
--- /dev/null
+<?php
+namespace bdrem;
+
+class Web
+{
+ public function run()
+ {
+ $cfg = new Config();
+ $cfg->load();
+ setlocale(LC_TIME, $cfg->locale);
+ $source = $cfg->loadSource();
+
+ $arEvents = $source->getEvents(
+ date('Y-m-d'), $cfg->daysBefore, $cfg->daysAfter
+ );
+ usort($arEvents, '\\bdrem\\Event::compare');
+
+ $r = new Renderer_Html();
+ echo $r->render($arEvents);
+ }
+}
+?>
--- /dev/null
+<?php
+namespace bdrem;
+
+if (file_exists(__DIR__ . '/../src/bdrem/Autoloader.php')) {
+ require_once __DIR__ . '/../src/bdrem/Autoloader.php';
+ Autoloader::register();
+}
+$web = new Web();
+$web->run();
+?>