Make CSS inline in HTML mails
[bdrem.git] / src / bdrem / Renderer / Console.php
1 <?php
2 /**
3  * Part of bdrem
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Bdrem
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/bdrem.htm
13  */
14 namespace bdrem;
15
16 /**
17  * Render events on the terminal as ASCII table
18  *
19  * @category  Tools
20  * @package   Bdrem
21  * @author    Christian Weiske <cweiske@cweiske.de>
22  * @copyright 2014 Christian Weiske
23  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
24  * @version   Release: @package_version@
25  * @link      http://cweiske.de/bdrem.htm
26  */
27 class Renderer_Console extends Renderer
28 {
29     /**
30      * HTTP content type
31      * @var string
32      */
33     protected $httpContentType = 'text/plain; charset=utf-8';
34
35     /**
36      * Use ANSI color codes for output coloring
37      *
38      * @var boolean
39      */
40     public $ansi = false;
41
42     /**
43      * @var \Console_Color2
44      */
45     protected $cc;
46
47     /**
48      * Render events as console table
49      *
50      * @param array $arEvents Array of events to render
51      *
52      * @return string ASCII table
53      */
54     public function render($arEvents)
55     {
56         $this->loadConfig();
57         if ($this->ansi) {
58             $this->cc = new \Console_Color2();
59         }
60
61         $tbl = new \Console_Table(
62             CONSOLE_TABLE_ALIGN_LEFT,
63             array('intersection' => '', 'horizontal' => '-', 'vertical' => ''),
64             1, null, $this->ansi
65         );
66         $tbl->setAlign(0, CONSOLE_TABLE_ALIGN_RIGHT);
67         $tbl->setAlign(1, CONSOLE_TABLE_ALIGN_RIGHT);
68
69         $tbl->setHeaders(
70             $this->ansiWrap(
71                 array('Days', 'Age', 'Name', 'Event', 'Date', 'Day'),
72                 '%_%9'
73             )
74         );
75         $tbl->setBorderVisibility(
76             array(
77                 'left'   => false,
78                 'right'  => false,
79                 'top'    => true,
80                 'bottom' => false,
81                 'inner'  => true,
82             )
83         );
84
85         foreach ($arEvents as $event) {
86             $colorCode = null;
87             if ($event->days == 0) {
88                 $colorCode = '%R';
89             }
90             $tbl->addRow(
91                 $this->ansiWrap(
92                     array(
93                         $event->days,
94                         $event->age,
95                         wordwrap($event->title, 30, "\n", true),
96                         wordwrap($event->type, 20, "\n", true),
97                         $this->getLocalDate($event->date),
98                         strftime('%a', strtotime($event->localDate))
99                     ),
100                     $colorCode
101                 )
102             );
103         }
104         return $tbl->getTable();
105     }
106
107     /**
108      * Wrap each string in an array in an ANSI color code
109      *
110      * @param array  $data      Array of strings
111      * @param string $colorCode ANSI color code or name
112      *
113      * @return array Wrapped data
114      */
115     protected function ansiWrap($data, $colorCode = null)
116     {
117         if (!$this->ansi || $colorCode === null) {
118             return $data;
119         }
120
121         foreach ($data as $k => &$value) {
122             $value = $this->cc->convert(
123                 $colorCode . $value . '%n'
124             );
125         }
126         return $data;
127     }
128
129     /**
130      * Load configuration values into the class
131      *
132      * @return void
133      */
134     protected function loadConfig()
135     {
136         if (isset($this->config->ansi)) {
137             $this->ansi = $this->config->ansi;
138         }
139     }
140 }
141 ?>