Introduced a debug option and a localization string for holidays.
[timetracker.git] / WEB-INF / lib / ttConfigHelper.class.php
index b33734c..33e94e5 100644 (file)
@@ -38,30 +38,56 @@ class ttConfigHelper {
 
   // Constructor.
   function __construct($config) {
-    $this->config = $config;
-    $this->config_array = explode(',', $this->config);
+    $this->config = trim($config, ' ,');
+    if ($this->config)
+      $this->config_array = explode(',', $this->config);
   }
 
   // getDefinedValue determines if a value identified by name is defined.
   function getDefinedValue($name) {
-    return in_array($name, $this->config_array);
+    $defined = $this->config_array ? in_array($name, $this->config_array) : false;
+    return $defined;
   }
 
-  // The getIntValue parses an integer value from the source config string.
-  function getIntValue($name) {
+  // setDefinedValue either sets or deletes a defined value identified by name.
+  function setDefinedValue($name, $set = true) {
+    if ($set) {
+      // Setting part.
+      if (!$this->getDefinedValue($name)) {
+        $this->config_array[] = $name;
+        $this->config = implode(',', $this->config_array);
+        return;
+      }
+    } else {
+      // Deleting part.
+      if ($this->config_array) {
+        foreach ($this->config_array as $key => $value) {
+          if ($value === $name) {
+            unset($this->config_array[$key]);
+            $this->config = implode(',', $this->config_array);
+            return;
+          }
+        }
+      }
+    }
+  }
+
+  // The getIntValue parses an integer value from the source config array.
+  function getIntValue($name, $defaultVal = 0) {
     $name_with_colon = $name.':';
     $len = strlen($name_with_colon);
 
-    foreach ($this->config_array as $unparsed_value) {
-      if (substr($unparsed_value, 0, $len) === $name_with_colon) {
-        // Found value.
-        $unparsed_len = strlen($unparsed_value);
-        $int_value = (int) substr($unparsed_value, -($unparsed_len - $len));
-        return $int_value;
+    if ($this->config_array) {
+      foreach ($this->config_array as $unparsed_value) {
+        if (substr($unparsed_value, 0, $len) === $name_with_colon) {
+          // Found value.
+          $unparsed_len = strlen($unparsed_value);
+          $int_value = (int) substr($unparsed_value, -($unparsed_len - $len));
+          return $int_value;
+        }
       }
-        
     }
-    return false;
+    return $defaultVal;
   }
 
   // The setIntValue sets an integer value into config array.
@@ -70,19 +96,20 @@ class ttConfigHelper {
     $name_with_colon = $name.':';
     $len = strlen($name_with_colon);
 
-    foreach ($this->config_array as $key => $unparsed_value) {
-      if (substr($unparsed_value, 0, $len) === $name_with_colon) {
-        // Found an already existing value.
-        // Remove value if our new value is NULL.
-        if ($value !== null) {
-          // Replace value.
-          $this->config_array[$key] = $name.':'.$value;
-        } else {
-          // Remove value.
-          unset($this->config_array[$key]);
+    if ($this->config_array) {
+      foreach ($this->config_array as $key => $unparsed_value) {
+        if (substr($unparsed_value, 0, $len) === $name_with_colon) {
+          // Found an already existing value.
+          if ($value !== null) {
+            // Replace value.
+            $this->config_array[$key] = $name.':'.$value;
+          } else {
+            // Remove value if our new value is NULL.
+            unset($this->config_array[$key]);
+          }
+          $this->config = implode(',', $this->config_array);
+          return;
         }
-        $this->config = implode(',', $this->config_array);
-        return;
       }
     }
     // If we get here, the value was not found, so add it.
@@ -93,6 +120,6 @@ class ttConfigHelper {
 
   // The getConfig returns the config string.
   function getConfig() {
-    return $this->config;
+    return trim($this->config, ' ,');
   }
 }