Introduced a debug option and a localization string for holidays.
[timetracker.git] / WEB-INF / lib / ttConfigHelper.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // Class ttConfigHelper is a helper class to handle comma-separated lists of config data.
30 // For example, in tt_groups table we have a field called "config", that may store something like:
31 //
32 // allow_overlap,uncompleted_indicators,minutes_in_unit:15,future_entries
33 //   Above, some values are simply defined by their presence such as allow_overlap.
34 //   Other values have associated values after colon such as minutes_in_work_unit (15).
35 class ttConfigHelper {
36   var $config = null; // Source config string.
37   var $config_array = null;  // Array of config values.
38
39   // Constructor.
40   function __construct($config) {
41     $this->config = trim($config, ' ,');
42     if ($this->config)
43       $this->config_array = explode(',', $this->config);
44   }
45
46   // getDefinedValue determines if a value identified by name is defined.
47   function getDefinedValue($name) {
48     $defined = $this->config_array ? in_array($name, $this->config_array) : false;
49     return $defined;
50   }
51
52   // setDefinedValue either sets or deletes a defined value identified by name.
53   function setDefinedValue($name, $set = true) {
54     if ($set) {
55       // Setting part.
56       if (!$this->getDefinedValue($name)) {
57         $this->config_array[] = $name;
58         $this->config = implode(',', $this->config_array);
59         return;
60       }
61     } else {
62       // Deleting part.
63       if ($this->config_array) {
64         foreach ($this->config_array as $key => $value) {
65           if ($value === $name) {
66             unset($this->config_array[$key]);
67             $this->config = implode(',', $this->config_array);
68             return;
69           }
70         }
71       }
72     }
73   }
74
75   // The getIntValue parses an integer value from the source config array.
76   function getIntValue($name, $defaultVal = 0) {
77     $name_with_colon = $name.':';
78     $len = strlen($name_with_colon);
79
80     if ($this->config_array) {
81       foreach ($this->config_array as $unparsed_value) {
82         if (substr($unparsed_value, 0, $len) === $name_with_colon) {
83           // Found value.
84           $unparsed_len = strlen($unparsed_value);
85           $int_value = (int) substr($unparsed_value, -($unparsed_len - $len));
86           return $int_value;
87         }
88       }
89     }
90     return $defaultVal;
91   }
92
93   // The setIntValue sets an integer value into config array.
94   function setIntValue($name, $value) {
95     // Try to find and replace an already existing value.
96     $name_with_colon = $name.':';
97     $len = strlen($name_with_colon);
98
99     if ($this->config_array) {
100       foreach ($this->config_array as $key => $unparsed_value) {
101         if (substr($unparsed_value, 0, $len) === $name_with_colon) {
102           // Found an already existing value.
103           if ($value !== null) {
104             // Replace value.
105             $this->config_array[$key] = $name.':'.$value;
106           } else {
107             // Remove value if our new value is NULL.
108             unset($this->config_array[$key]);
109           }
110           $this->config = implode(',', $this->config_array);
111           return;
112         }
113       }
114     }
115     // If we get here, the value was not found, so add it.
116     $this->config_array[] = $name.':'.$value;
117     $this->config = implode(',', $this->config_array);
118     return;
119   }
120
121   // The getConfig returns the config string.
122   function getConfig() {
123     return trim($this->config, ' ,');
124   }
125 }