ILIAS  release_7 Revision v7.30-3-g800a261c036
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{
19
23 protected $table_name = "";
27 protected $cache = array();
28
29
33 public function __construct($table_name)
34 {
35 $this->table_name = $table_name;
36 }
37
38
42 public function setTableName($table_name)
43 {
44 $this->table_name = $table_name;
45 }
46
47
51 public function getTableName()
52 {
53 return $this->table_name;
54 }
55
56
63 public function __call($method, $params)
64 {
65 $index = substr($method, 3);
66 if (substr($method, 0, 3) == 'get') {
67 if (!isset($this->cache[$index])) {
68 $this->cache[$index] = $this->getValue(self::_fromCamelCase(substr($method, 3)));
69 }
70 if ($this->cache[$index] == null) {
71 $this->cache[$index] = false;
72 }
73
74 return $this->cache[$index];
75 } else {
76 if (substr($method, 0, 3) == 'set') {
77 $this->cache[$index] = $params[0];
78 $this->setValue(self::_fromCamelCase(substr($method, 3)), $params[0]);
79
80 return true;
81 } else {
83 }
84 }
85 }
86
87
92 public function setValue($key, $value)
93 {
94 global $DIC;
95 $ilDB = $DIC['ilDB'];
96
97 if (!$ilDB->tableExists($this->table_name)) {
99 }
100
101 if (!is_string($this->getValue($key))) {
102 $ilDB->insert($this->table_name, array("config_key" => array("text", $key), "config_value" => array("text", $value)));
103 } else {
104 $ilDB->update($this->table_name, array("config_key" => array("text", $key), "config_value" => array("text", $value)), array("config_key" => array("text", $key)));
105 }
106 }
107
108
114 public function getValue($key)
115 {
116 global $DIC;
117 $ilDB = $DIC['ilDB'];
118
119 if (!$this->tableExists($this->table_name)) {
121 }
122
123 $result = $ilDB->query("SELECT config_value FROM " . $this->table_name . " WHERE config_key = " . $ilDB->quote($key, "text"));
124
125 if ($result->numRows() == 0) {
126 return false;
127 }
128 $record = $ilDB->fetchAssoc($result);
129
130 return (string) $record['config_value'];
131 }
132
133
137 public function initDB()
138 {
139 global $DIC;
140 $ilDB = $DIC['ilDB'];
141
142 if (!$ilDB->tableExists($this->getTableName())) {
143 $fields = array(
144 'config_key' => array(
145 'type' => 'text',
146 'length' => 128,
147 'notnull' => true,
148 ),
149 'config_value' => array(
150 'type' => 'clob',
151 'notnull' => false,
152 ),
153 );
154 $ilDB->createTable($this->getTableName(), $fields);
155 $ilDB->addPrimaryKey($this->getTableName(), array("config_key"));
156 }
157
158 return true;
159 }
160
161
162 //
163 // Helper
164 //
165
171 public static function _fromCamelCase($str)
172 {
173 $str[0] = strtolower($str[0]);
174
175 return preg_replace_callback('/([A-Z])/', function ($c) {
176 return "_" . strtolower($c[1]);
177 }, $str);
178 }
179
180
187 public static function _toCamelCase($str, $capitalise_first_char = false)
188 {
189 if ($capitalise_first_char) {
190 $str[0] = strtoupper($str[0]);
191 }
192
193 return preg_replace_callback('/-([a-z])/', function ($c) {
194 return strtoupper($c[1]);
195 }, $str);
196 }
197
198
199 public function tableExists()
200 {
201 global $DIC;
202 $ilDB = $DIC['ilDB'];
203 $result = $ilDB->query("show tables like '" . $this->getTableName() . "'");
204
205 if ($result->numRows() == 0) {
206 return false;
207 } else {
208 return true;
209 }
210 }
211}
$result
An exception for terminatinating execution or to throw for unit testing.
Class ilCloudPluginConfigException.
Class ilCloudPluginConfig.
static _toCamelCase($str, $capitalise_first_char=false)
$c
Definition: cli.php:37
global $DIC
Definition: goto.php:24
$index
Definition: metadata.php:128
global $ilDB