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
17 * Generic user interface class
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
27 abstract class UserInterface
36 * Start the user interface, load config, parse and render events.
43 $this->config = new Config();
44 $parser = $this->loadParameters();
45 $res = $this->parseParameters($parser);
47 $this->config->load();
48 if (!$this->config->cfgFileExists) {
50 "No config file found. Looked at the following places:\n"
51 . '- ' . implode("\n- ", $this->config->cfgFiles)
55 setlocale(LC_TIME, $this->config->locale);
56 $this->handleCommands($res);
58 $source = $this->config->loadSource();
59 $arEvents = $source->getEvents(
61 $this->config->daysPrev, $this->config->daysNext
63 usort($arEvents, '\\bdrem\\Event::compare');
64 $this->render($arEvents);
65 } catch (\Exception $e) {
66 $this->preRenderParameterError();
67 echo 'Error: ' . $e->getMessage() . "\n";
73 * Load parameters for the CLI option parser.
75 * @return \Console_CommandLine CLI option parser
77 protected function loadParameters()
79 $parser = new \Console_CommandLine();
80 $parser->description = 'Birthday reminder';
81 $parser->version = '0.6.0';
87 'long_name' => '--next',
88 'description' => 'Show NUM days after date',
90 'action' => 'StoreInt',
98 'long_name' => '--prev',
99 'description' => 'Show NUM days before date',
100 'help_name' => 'NUM',
101 'action' => 'StoreInt',
108 'short_name' => '-r',
109 'long_name' => '--renderer',
110 'description' => 'Output mode',
111 'action' => 'StoreString',
119 'default' => 'console',
120 'add_list_option' => true,
126 'short_name' => '-e',
127 'long_name' => '--stoponempty',
128 'description' => 'Output nothing when list is empty',
129 'action' => 'StoreTrue',
136 'short_name' => '-d',
137 'long_name' => '--date',
138 'description' => 'Date to show events for',
139 'action' => 'StoreString'
145 'short_name' => '-c',
146 'long_name' => '--config',
147 'help_name' => 'FILE',
148 'description' => 'Path to configuration file',
149 'action' => 'StoreString'
157 * Let the CLI option parser parse the options.
159 * @param object $parser Option parser
161 * @return object Parsed command line parameters
163 protected function parseParameters(\Console_CommandLine $parser)
166 $result = $parser->parse();
168 if ($result->options['configfile'] !== null) {
169 $this->config->cfgFiles = array($result->options['configfile']);
172 $this->config->daysNext = $result->options['daysNext'];
173 $this->config->daysPrev = $result->options['daysPrev'];
174 $this->config->renderer = $result->options['renderer'];
175 $this->config->stopOnEmpty = $result->options['stopOnEmpty'];
176 $this->config->setDate($result->options['date']);
177 if (isset($result->options['ansi'])) {
178 $this->config->ansi = $result->options['ansi'];
181 } catch (\Exception $exc) {
182 $this->preRenderParameterError();
183 $parser->displayError($exc->getMessage());
190 * @param array $arEvents Event objects to render
194 protected function render($arEvents)
196 $r = $this->getRenderer();
197 $r->config = $this->config;
199 if ($this->config->stopOnEmpty && count($arEvents) == 0) {
200 $r->handleStopOnEmpty();
203 $r->renderAndOutput($arEvents);
207 * Load the configured renderer
209 * @return Renderer Renderer instance
211 protected function getRenderer()
213 $renderer = ucfirst($this->config->renderer);
214 if ($renderer == 'Htmltable') {
215 $renderer = 'HtmlTable';
217 $class = '\\bdrem\\Renderer_' . $renderer;
222 * Handle any commands given on the CLI
224 * @param object $res Command line parameters and options
228 protected function handleCommands($res)
233 * Do something before a parameter parsing error is shown
237 protected function preRenderParameterError()