1: <?php
2:
3: App::uses('ClassRegistry', 'Utility');
4: App::uses('Sanitize', 'Utility');
5:
6: class ConfigurationKeys {
7:
8: const LIST_SEPARATOR = '|';
9:
10: 11: 12:
13: private static $keys = null;
14:
15: public static function reset() {
16: self::$keys = null;
17: }
18:
19: private static function _getKeys() {
20: if (self::$keys === null) {
21: if ((self::$keys = Configure::read('configurationKeys'))) {
22: foreach (self::$keys as $key => $options) {
23: if (is_int($key)) {
24: $key = $options;
25: $options = array();
26: }
27: self::$keys[$key] = $options + array(
28: 'description' => null,
29: 'defaultValue' => null,
30: 'listOptions' => null,
31: );
32: }
33: }
34: }
35: return self::$keys;
36: }
37:
38: public static function getKeys() {
39: return array_keys(self::_getKeys());
40: }
41:
42: public static function getRequiredKeyValue($key) {
43: if (($value = self::getKeyValue($key))) {
44: return $value;
45: } else {
46: throw new Exception(sprintf(__d('configuration_keys','Configuration value not found to key=%s'), $key));
47: }
48: }
49:
50: public static function getKeyValue($key) {
51: self::_throwExceptionIfKeyNotExists($key);
52:
53: $SettedConfigurationKey = ClassRegistry::init('ConfigurationKeys.SettedConfigurationKey');
54: $settedConfigurationKey = $SettedConfigurationKey->findByName($key);
55: if ($settedConfigurationKey) {
56: return $settedConfigurationKey[$SettedConfigurationKey->alias]['value'];
57: } else {
58: return self::$keys[$key]['defaultValue'];
59: }
60: }
61:
62: public static function getKeyValueAsList($key) {
63: return array_map(function($value) {
64: return trim($value);
65: }, explode(self::LIST_SEPARATOR, self::getKeyValue($key)));
66: }
67:
68: public static function getKeyDefaultValue($key) {
69: self::_throwExceptionIfKeyNotExists($key);
70: return self::$keys[$key]['defaultValue'];
71: }
72:
73: public static function getKeyValueSql($key) {
74: self::_throwExceptionIfKeyNotExists($key);
75: $SettedConfigurationKey = ClassRegistry::init('ConfigurationKeys.SettedConfigurationKey');
76: $defaultValue = Sanitize::escape(self::$keys[$key]['defaultValue'], $SettedConfigurationKey->useDbConfig);
77: $key = Sanitize::escape($key, $SettedConfigurationKey->useDbConfig);
78: return <<<EOT
79: ifnull(
80: (
81: select scc.value
82: from {$SettedConfigurationKey->tablePrefix}{$SettedConfigurationKey->table} scc
83: where scc.name = '$key'
84: limit 1)
85: , '$defaultValue'
86: )
87: EOT;
88: }
89:
90: public static function setKeyValue($key, $value) {
91: self::_throwExceptionIfKeyNotExists($key);
92:
93: $SettedConfigurationKey = ClassRegistry::init('ConfigurationKeys.SettedConfigurationKey');
94: $settedConfigurationKey = $SettedConfigurationKey->findByName($key);
95:
96: if (empty($settedConfigurationKey)) {
97: $SettedConfigurationKey->create();
98: $settedConfigurationKey[$SettedConfigurationKey->alias]['name'] = $key;
99: }
100:
101: $settedConfigurationKey[$SettedConfigurationKey->alias]['value'] = $value;
102: return $SettedConfigurationKey->save($settedConfigurationKey);
103: }
104:
105: public static function clearKeyValue($key) {
106: self::_throwExceptionIfKeyNotExists($key);
107:
108: $SettedConfigurationKey = ClassRegistry::init('ConfigurationKeys.SettedConfigurationKey');
109: $settedConfigurationKey = $SettedConfigurationKey->findByName($key);
110:
111: if (!empty($settedConfigurationKey)) {
112: $SettedConfigurationKey->delete($settedConfigurationKey[$SettedConfigurationKey->alias][$SettedConfigurationKey->primaryKey]);
113: }
114: }
115:
116: public static function hasKey($key) {
117: return array_key_exists($key, self::_getKeys());
118: }
119:
120: public static function getKeyOptions($key, $option = null) {
121: self::_throwExceptionIfKeyNotExists($key);
122: $options = self::$keys[$key];
123: return $option ? $options[$option] : $option;
124: }
125:
126: private static function _throwExceptionIfKeyNotExists($key) {
127: if (!self::hasKey($key)) {
128: throw new Exception(sprintf(__d('configuration_keys','Key not setted: %s (Keys setted: %s)'), $key, implode(',', self::getKeys())));
129: }
130: }
131:
132: }
133:
134: ?>
135: