fix dates in ical and add calendar title
[bdrem.git] / src / bdrem / Config.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  * Configuration options for bdrem
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 Config
28 {
29     /**
30      * Current date, YYYY-MM-DD
31      * @var string
32      */
33     public $date;
34
35     /**
36      * Days to show before $date
37      * @var integer
38      */
39     public $daysPrev;
40
41     /**
42      * Days to show after $date
43      * @var integer
44      */
45     public $daysNext;
46
47     /**
48      * Locale to render the dates in, e.g. "de_DE.UTF-8"
49      * @var string
50      */
51     public $locale;
52
53     /**
54      * Renderer name to use (e.g. "console")
55      * @var string
56      */
57     public $renderer;
58
59     /**
60      * Event source configuration.
61      * - First value is source name ("Ldap", "Sql")
62      * - Second value is the source configuration
63      * @var array
64      */
65     public $source;
66
67     /**
68      * Do not output anything if there are no events to show
69      * @var boolean
70      */
71     public $stopOnEmpty;
72
73     /**
74      * List of config file paths that were tried to load
75      * @var array
76      */
77     public $cfgFiles = array();
78
79     /**
80      * If a configuration file could be found
81      * @var boolean
82      */
83     public $cfgFileExists;
84
85
86
87     /**
88      * Init configuration file path loading
89      */
90     public function __construct()
91     {
92         $this->loadConfigFilePaths();
93     }
94
95     /**
96      * Load the configuration from the first configuration file found.
97      *
98      * @return void
99      */
100     public function load()
101     {
102         foreach ($this->cfgFiles as $file) {
103             if (file_exists($file)) {
104                 $this->cfgFileExists = true;
105                 return $this->loadFile($file);
106             }
107         }
108         $this->cfgFileExists = false;
109     }
110
111     /**
112      * Load possible configuration file paths into $this->cfgFiles.
113      *
114      * @return void
115      */
116     protected function loadConfigFilePaths()
117     {
118         $pharFile = \Phar::running();
119         if ($pharFile == '') {
120             $this->cfgFiles[] = __DIR__ . '/../../data/bdrem.config.php';
121         } else {
122             //remove phar:// from the path
123             $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
124         }
125
126         //TODO: add ~/.config/bdrem.php
127
128         $this->cfgFiles[] = '/etc/bdrem.php';
129     }
130
131     /**
132      * Load a single configuration file and set the config class variables
133      *
134      * @param string $filename Configuration file path
135      *
136      * @return void
137      */
138     protected function loadFile($filename)
139     {
140         include $filename;
141         $vars = get_defined_vars();
142         foreach ($vars as $k => $value) {
143             if (!isset($this->$k) || $this->$k === null) {
144                 $this->$k = $value;
145             }
146         }
147     }
148
149     /**
150      * Load a event source from $this->source.
151      * Class name has to be \bdrem\Source_$source
152      *
153      * @return object Source object
154      */
155     public function loadSource()
156     {
157         if ($this->source === null) {
158             throw new \Exception('No source defined');
159         }
160
161         $settings = $this->source;
162         $class = '\\bdrem\\Source_' . array_shift($settings);
163
164         return new $class($settings[0]);
165     }
166
167     /**
168      * Set the current date
169      *
170      * @param string $date Date in any format
171      *
172      * @return void
173      */
174     public function setDate($date)
175     {
176         if ($date === null) {
177             $this->date = date('Y-m-d');
178         } else {
179             $dt = new \DateTime($date);
180             $this->date = $dt->format('Y-m-d');
181         }
182     }
183
184     /**
185      * Get a configuration variable
186      *
187      * @param string $varname Configuration variable name
188      * @param string $default Default value in case the variable is not set
189      *                        or is empty
190      *
191      * @return mixed Configuration value or default
192      */
193     public function get($varname, $default = '')
194     {
195         if (!isset($this->$varname) || $this->$varname == '') {
196             return $default;
197         }
198         return $this->$varname;
199     }
200 }
201 ?>