1: <?php
2:
3: App::import('Model', 'Base.CustomDataModel');
4: App::import('Lib', 'ConfigurationKeys.ConfigurationKeys');
5:
6: class ConfigurationKey extends CustomDataModel {
7:
8: public $alwaysInitialize = true;
9: public $displayField = 'name';
10: public $primaryKey = 'name';
11: public $validate = array(
12: 'name' => array(
13: 'notempty' => array(
14: 'rule' => array('notempty'),
15: 'required' => true
16: ),
17: 'keyExists' => array(
18: 'rule' => array('keyExists'),
19: ),
20: ),
21: 'setted_value' => array(
22: 'required' => array(
23: 'rule' => '/.*/i',
24: 'required' => true,
25: ),
26: 'listOptions' => array(
27: 'rule' => 'listOptionsValidation',
28: 'message' => 'Valor informado não está contido na lista de valores permitidos',
29: ),
30: ),
31: );
32:
33: private function _findKeyValue($name) {
34: $SettedConfigurationKey = ClassRegistry::init('SettedConfigurationKey');
35: $settedConfigurationKey = $SettedConfigurationKey->findByName($name);
36: if ($settedConfigurationKey) {
37: return $settedConfigurationKey[$SettedConfigurationKey->alias]['value'];
38: } else {
39: return null;
40: }
41: }
42:
43: private function _keyIsSetted($name) {
44: return ClassRegistry::init('SettedConfigurationKey')->findByName($name) ?
45: true :
46: false;
47: }
48:
49: protected function customData() {
50: $data = array();
51:
52: foreach (ConfigurationKeys::getKeys() as $name) {
53: $row = array(
54: 'name' => $name,
55: 'description' => ConfigurationKeys::getKeyOptions($name, 'description'),
56: 'default_value' => ConfigurationKeys::getKeyOptions($name, 'defaultValue'),
57: 'setted_value' => $this->_findKeyValue($name),
58: 'setted' => $this->_keyIsSetted($name),
59: );
60:
61: $row['current_value'] = $row['setted_value'] ? $row['setted_value'] : $row['default_value'];
62: $data[] = $row;
63: }
64:
65: return $data;
66: }
67:
68: protected function customDelete($row) {
69: $SettedConfigurationKey = ClassRegistry::init('SettedConfigurationKey');
70: $settedConfigurationKey = $SettedConfigurationKey->findByName($this->id);
71:
72: if ($settedConfigurationKey) {
73: return $SettedConfigurationKey->delete($settedConfigurationKey[$SettedConfigurationKey->alias][$SettedConfigurationKey->primaryKey]);
74: } else {
75: return false;
76: }
77: }
78:
79: protected function customSave($oldData, $newData) {
80: $SettedConfigurationKey = ClassRegistry::init('SettedConfigurationKey');
81: $settedConfigurationKey = $SettedConfigurationKey->findByName($newData['name']);
82:
83: if (!$settedConfigurationKey) {
84: $settedConfigurationKey[$SettedConfigurationKey->alias]['name'] = $newData['name'];
85: }
86:
87: $settedConfigurationKey[$SettedConfigurationKey->alias]['value'] = $newData['setted_value'];
88:
89: return $SettedConfigurationKey->save($settedConfigurationKey);
90: }
91:
92: public function keyExists($check, $params) {
93: foreach ($check as $key) {
94: if (!ConfigurationKeys::hasKey($key)) {
95: return false;
96: }
97: }
98:
99: return true;
100: }
101:
102: public function nullValidation($check, $params) {
103: return true;
104: }
105:
106: public function listOptionsValidation($check) {
107: if (empty($this->data[$this->alias]['name'])) {
108: return false;
109: }
110: try {
111: $listOptions = ConfigurationKeys::getKeyOptions($this->data[$this->alias]['name'], 'listOptions');
112: } catch (Exception $ex) {
113: return false;
114: }
115: if (is_array($listOptions)) {
116: foreach($check as $value) {
117: if (!in_array($value, $listOptions)) {
118: return false;
119: }
120: }
121: return true;
122: }
123: else {
124: return true;
125: }
126: }
127:
128: }
129:
130: ?>