use local date format
[bdrem.git] / src / bdrem / UserInterface.php
1 <?php
2 namespace bdrem;
3
4 abstract class UserInterface
5 {
6     protected $config;
7
8     public function run()
9     {
10         try {
11             $this->config = new Config();
12             $this->config->load();
13             setlocale(LC_TIME, $this->config->locale);
14             $source = $this->config->loadSource();
15
16             $parser = $this->loadParameters();
17             $this->parseParameters($parser);
18
19             $arEvents = $source->getEvents(
20                 $this->config->date,
21                 $this->config->daysPrev, $this->config->daysNext
22             );
23             usort($arEvents, '\\bdrem\\Event::compare');
24             $this->render($arEvents);
25         } catch (\Exception $e) {
26             $this->preRenderParameterError();
27             echo 'Exception: ' . $e->getCode() . ' ' . $e->getMessage() . "\n";
28             exit(1);
29         }
30     }
31
32     protected function loadParameters()
33     {
34         $parser = new \Console_CommandLine();
35         $parser->description = 'Birthday reminder';
36         $parser->version = '0.1.0';
37
38         $parser->addOption(
39             'daysNext',
40             array(
41                 'short_name'  => '-n',
42                 'long_name'   => '--days-next',
43                 'description' => 'Show NUM days after date',
44                 'help_name'   => 'NUM',
45                 'action'      => 'StoreInt',
46                 'default'     => $this->config->daysNext,
47             )
48         );
49         $parser->addOption(
50             'daysPrev',
51             array(
52                 'short_name'  => '-p',
53                 'long_name'   => '--previous',
54                 'description' => 'Show NUM days before date',
55                 'help_name'   => 'NUM',
56                 'action'      => 'StoreInt',
57                 'default'     => $this->config->daysPrev,
58             )
59         );
60         $parser->addOption(
61             'renderer',
62             array(
63                 'short_name'  => '-r',
64                 'long_name'   => '--renderer',
65                 'description' => 'Output mode',
66                 'action'      => 'StoreString',
67                 'choices'     => array(
68                     'console',
69                     'html',
70                     'htmltable',
71                     'mail',
72                 ),
73                 'default'     => 'console',
74                 'add_list_option' => true,
75             )
76         );
77         $parser->addOption(
78             'stopOnEmpty',
79             array(
80                 'short_name'  => '-e',
81                 'long_name'   => '--stoponempty',
82                 'description' => 'Output nothing when list is empty',
83                 'action'      => 'StoreTrue',
84                 'default'     => false
85             )
86         );
87         $parser->addOption(
88             'date',
89             array(
90                 'short_name'  => '-d',
91                 'long_name'   => '--date',
92                 'description' => 'Date to show events for',
93                 'action'      => 'StoreString'
94             )
95         );
96         $parser->addOption(
97             'quiet',
98             array(
99                 'short_name'  => '-q',
100                 'long_name'   => '--quiet',
101                 'description' => "Don't print status messages to stdout",
102                 'action'      => 'StoreTrue'
103             )
104         );
105         return $parser;
106     }
107
108     protected function parseParameters($parser)
109     {
110         try {
111             $result = $parser->parse();
112             // do something with the result object
113             $this->config->daysNext    = $result->options['daysNext'];
114             $this->config->daysPrev    = $result->options['daysPrev'];
115             $this->config->renderer    = $result->options['renderer'];
116             $this->config->quiet       = $result->options['quiet'];
117             $this->config->stopOnEmpty = $result->options['stopOnEmpty'];
118             $this->config->setDate($result->options['date']);
119         } catch (\Exception $exc) {
120             $this->preRenderParameterError();
121             $parser->displayError($exc->getMessage());
122         }
123     }
124
125     protected function render($arEvents)
126     {
127         $r = $this->getRenderer();
128         $r->config = $this->config;
129
130         if ($this->config->stopOnEmpty && count($arEvents) == 0) {
131             $r->handleStopOnEmpty();
132             return;
133         }
134         $r->renderAndOutput($arEvents);
135     }
136
137     protected function getRenderer()
138     {
139         $renderer = ucfirst($this->config->renderer);
140         if ($renderer == 'Htmltable') {
141             $renderer = 'HtmlTable';
142         }
143         $class = '\\bdrem\\Renderer_' . $renderer;
144         return new $class();
145     }
146
147     protected function preRenderParameterError()
148     {
149     }
150 }
151 ?>