html renderer, console renderer uses pear's Console_Table
authorChristian Weiske <cweiske@cweiske.de>
Thu, 23 Jan 2014 16:05:49 +0000 (17:05 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 23 Jan 2014 16:05:49 +0000 (17:05 +0100)
data/bdrem.config.php.dist
src/bdrem/Cli.php
src/bdrem/Config.php
src/bdrem/Event.php
src/bdrem/Renderer/Console.php
src/bdrem/Renderer/Html.php [new file with mode: 0644]
src/bdrem/Renderer/HtmlTable.php [new file with mode: 0644]
src/bdrem/Web.php [new file with mode: 0644]
www/index.php [new file with mode: 0644]

index 522a4772ad63909abc9ae774be9838432875c855..87d01fc3628c2ad9a032df6935d673a899ac0ad4 100644 (file)
@@ -2,4 +2,5 @@
 $source = array('Bdf', '/path/to/birthday.bdf');
 $daysBefore = 3;
 $daysAfter = 14;
 $source = array('Bdf', '/path/to/birthday.bdf');
 $daysBefore = 3;
 $daysAfter = 14;
+$locale = 'de_DE.UTF-8';
 ?>
 ?>
index 5ab5ec8af7974040b400b6d3aa73ae7aa5517bf2..196ca18a3d8052e06054e38c8e53fc8f3311aa82 100644 (file)
@@ -7,6 +7,7 @@ class Cli
     {
         $cfg = new Config();
         $cfg->load();
     {
         $cfg = new Config();
         $cfg->load();
+        setlocale(LC_TIME, $cfg->locale);
         $source = $cfg->loadSource();
 
         $arEvents = $source->getEvents(
         $source = $cfg->loadSource();
 
         $arEvents = $source->getEvents(
index 076cd043fab05b85efba646d437024500786ab79..5d34f7607fe25409c2b4059a7a9ac3319cf5d89c 100644 (file)
@@ -6,6 +6,7 @@ class Config
     public $source;
     public $daysBefore;
     public $daysAfter;
     public $source;
     public $daysBefore;
     public $daysAfter;
+    public $locale;
 
     public function load()
     {
 
     public function load()
     {
@@ -23,6 +24,9 @@ class Config
         $this->source = $source;
         $this->daysBefore = $daysBefore;
         $this->daysAfter = $daysAfter;
         $this->source = $source;
         $this->daysBefore = $daysBefore;
         $this->daysAfter = $daysAfter;
+        if (isset($locale)) {
+            $this->locale = $locale;
+        }
     }
 
     public function loadSource()
     }
 
     public function loadSource()
@@ -35,9 +39,6 @@ class Config
         $class = '\\bdrem\\Source_' . array_shift($settings);
 
         return new $class($settings[0]);
         $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);
     }
 }
 ?>
     }
 }
 ?>
index 65e4b9b498b54a5d53634d14456b8adf10938c08..43b9c92ecbf0f13ca8c53546445b96e3cfadc13d 100644 (file)
@@ -21,6 +21,22 @@ class Event
      */
     public $date;
 
      */
     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
      *
     /**
      * Which repetition this is
      *
@@ -52,10 +68,12 @@ class Event
      */
     public function isWithin($strDate, $nDaysBefore, $nDaysAfter)
     {
      */
     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) {
         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;
             $this->days = 0;
             if ($eYear == '????') {
                 $this->age = null;
@@ -72,8 +90,9 @@ class Event
             $yearOffset = -1;
         }
 
             $yearOffset = -1;
         }
 
+        $this->localDate = ($rYear + $yearOffset) . '-' . $eMonth . '-' . $eDay;
         $rD = new \DateTime($strDate);
         $rD = new \DateTime($strDate);
-        $eD = new \DateTime(($rYear + $yearOffset) . '-' . $eMonth . '-' . $eDay);
+        $eD = new \DateTime($this->localDate);
 
         $nDiff = (int) $rD->diff($eD)->format('%r%a');
 
 
         $nDiff = (int) $rD->diff($eD)->format('%r%a');
 
index 2cd185f4ba43e4308fb0410a3f14d85e18df0a7f..01d6eb2e82bc0475034aaccd433cada6a9969cf7 100644 (file)
@@ -5,35 +5,30 @@ class Renderer_Console
 {
     public function render($arEvents)
     {
 {
     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();
     }
 }
 ?>
     }
 }
 ?>
diff --git a/src/bdrem/Renderer/Html.php b/src/bdrem/Renderer/Html.php
new file mode 100644 (file)
index 0000000..bb027d2
--- /dev/null
@@ -0,0 +1,93 @@
+<?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;
+    }
+}
+?>
diff --git a/src/bdrem/Renderer/HtmlTable.php b/src/bdrem/Renderer/HtmlTable.php
new file mode 100644 (file)
index 0000000..574036f
--- /dev/null
@@ -0,0 +1,58 @@
+<?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;
+    }
+}
+?>
diff --git a/src/bdrem/Web.php b/src/bdrem/Web.php
new file mode 100644 (file)
index 0000000..4222551
--- /dev/null
@@ -0,0 +1,22 @@
+<?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);
+    }
+}
+?>
diff --git a/www/index.php b/www/index.php
new file mode 100644 (file)
index 0000000..14defde
--- /dev/null
@@ -0,0 +1,10 @@
+<?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();
+?>