use local date format
[bdrem.git] / src / bdrem / Renderer / Console.php
1 <?php
2 namespace bdrem;
3
4 class Renderer_Console extends Renderer
5 {
6     protected $httpContentType = 'text/plain; charset=utf-8';
7
8     /**
9      * Use ANSI color codes for output coloring
10      *
11      * @var boolean
12      */
13     public $ansi = false;
14
15     /**
16      * @var \Console_Color2
17      */
18     protected $cc;
19
20     public function render($arEvents)
21     {
22         $this->loadConfig();
23         if ($this->ansi) {
24             $this->cc = new \Console_Color2();
25         }
26
27         $tbl = new \Console_Table(
28             CONSOLE_TABLE_ALIGN_LEFT,
29             array('intersection' => '', 'horizontal' => '-', 'vertical' => ''),
30             1, null, $this->ansi
31         );
32         $tbl->setAlign(0, CONSOLE_TABLE_ALIGN_RIGHT);
33         $tbl->setAlign(1, CONSOLE_TABLE_ALIGN_RIGHT);
34
35         $tbl->setHeaders(
36             $this->ansiWrap(
37                 array('Days', 'Age', 'Name', 'Event', 'Date', 'Day'),
38                 '%_%9'
39             )
40         );
41         $tbl->setBorderVisibility(
42             array(
43                 'left'   => false,
44                 'right'  => false,
45                 'top'    => true,
46                 'bottom' => false,
47                 'inner'  => true,
48             )
49         );
50
51         foreach ($arEvents as $event) {
52             $colorCode = null;
53             if ($event->days == 0) {
54                 $colorCode = '%R';
55             }
56             $tbl->addRow(
57                 $this->ansiWrap(
58                     array(
59                         $event->days,
60                         $event->age,
61                         wordwrap($event->title, 30, "\n", true),
62                         wordwrap($event->type, 20, "\n", true),
63                         $this->getLocalDate($event->date),
64                         strftime('%a', strtotime($event->localDate))
65                     ),
66                     $colorCode
67                 )
68             );
69         }
70         return $tbl->getTable();
71     }
72
73     protected function ansiWrap($data, $colorCode = null)
74     {
75         if (!$this->ansi || $colorCode === null) {
76             return $data;
77         }
78
79         foreach ($data as $k => &$value) {
80             $value = $this->cc->convert(
81                 $colorCode . $value . '%n'
82             );
83         }
84         return $data;
85     }
86
87     protected function loadConfig()
88     {
89         if (isset($this->config->ansi)) {
90             $this->ansi = $this->config->ansi;
91         }
92     }
93 }
94 ?>