ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilCloudPluginConfig.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once('./Modules/Cloud/exceptions/class.ilCloudPluginConfigException.php');
5
18{
22 protected $table_name = "";
23
27 protected $cache = array();
28
33 {
34 $this->table_name = $table_name;
35 }
36
40 public function setTableName($table_name)
41 {
42 $this->table_name = $table_name;
43 }
44
48 public function getTableName()
49 {
50 return $this->table_name;
51 }
52
58 function __call($method, $params)
59 {
60 $index = substr($method,3);
61 if (substr($method, 0, 3) == 'get')
62 {
63 if (!isset($this->cache[$index]))
64 {
65 $this->cache[$index] = $this->getValue(self::_fromCamelCase(substr($method, 3)));
66 }
67 if ($this->cache[$index] == null)
68 {
69 $this->cache[$index] = false;
70 }
71
72 return $this->cache[$index];
73 } else if (substr($method, 0, 3) == 'set')
74 {
75 $this->cache[$index] = $params[0];
76 $this->setValue(self::_fromCamelCase(substr($method, 3)), $params[0]);
77 return true;
78 } else
79 {
81 }
82
83 }
84
89 public function setValue($key, $value)
90 {
91 global $DIC;
92 $ilDB = $DIC['ilDB'];
93
94 if(!$ilDB->tableExists($this->table_name))
95 {
97 }
98
99 if (!is_string($this->getValue($key)))
100 {
101 $ilDB->insert($this->table_name, array("config_key" => array("text", $key), "config_value" => array("text", $value)));
102 }
103
104 else
105 {
106 $ilDB->update($this->table_name, array("config_key" => array("text", $key), "config_value" => array("text", $value)), array("config_key" => array("text", $key)));
107 }
108 }
109
114 public function getValue($key)
115 {
116 global $DIC;
117 $ilDB = $DIC['ilDB'];
118
119
120 if (!$this->tableExists($this->table_name))
121 {
123 }
124
125 $result = $ilDB->query("SELECT config_value FROM ". $this->table_name ." WHERE config_key = " . $ilDB->quote($key, "text"));
126
127 if ($result->numRows() == 0)
128 {
129 return false;
130 }
131 $record = $ilDB->fetchAssoc($result);
132 return (string)$record['config_value'];
133 }
134
135
136
140 public function initDB()
141 {
142 global $DIC;
143 $ilDB = $DIC['ilDB'];
144
145 if (!$ilDB->tableExists($this->getTableName()))
146 {
147 $fields = array(
148 'config_key' => array(
149 'type' => 'text',
150 'length' => 128,
151 'notnull' => true),
152 'config_value' => array(
153 'type' => 'clob',
154 'notnull' => false),);
155 $ilDB->createTable($this->getTableName(), $fields);
156 $ilDB->addPrimaryKey($this->getTableName(), array("config_key"));
157 }
158
159 return true;
160 }
161
162
163 //
164 // Helper
165 //
166
167
172 public static function _fromCamelCase($str)
173 {
174 $str[0] = strtolower($str[0]);
175 $func = create_function('$c', 'return "_" . strtolower($c[1]);');
176 return preg_replace_callback('/([A-Z])/', $func, $str);
177 }
178
184 public static function _toCamelCase($str, $capitalise_first_char = false)
185 {
186 if ($capitalise_first_char)
187 {
188 $str[0] = strtoupper($str[0]);
189 }
190 $func = create_function('$c', 'return strtoupper($c[1]);');
191 return preg_replace_callback('/-([a-z])/', $func, $str);
192 }
193
194 public function tableExists()
195 {
196 global $DIC;
197 $ilDB = $DIC['ilDB'];
198 $result = $ilDB->query("show tables like '".$this->getTableName()."'");
199
200 if ($result->numRows() == 0)
201 {
202 return false;
203 }
204 else return true;
205 }
206}
207
208?>
$result
An exception for terminatinating execution or to throw for unit testing.
Class ilCloudPluginConfigException.
Class ilCloudPluginConfig.
static _toCamelCase($str, $capitalise_first_char=false)
$params
Definition: example_049.php:96
global $ilDB
global $DIC