KeyValueBehavior.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. App::uses('ModelBehavior', 'Model');
  3. /**
  4. * KeyValue Behavior
  5. *
  6. * TODO: long text? separate table or not at all?
  7. * TODO: caching
  8. *
  9. * @license http://opensource.org/licenses/mit-license.php MIT
  10. * @modified Mark Scherer
  11. */
  12. class KeyValueBehavior extends ModelBehavior {
  13. /**
  14. * Storage model for all key value pairs
  15. */
  16. public $KeyValue = null;
  17. /**
  18. * Default settings
  19. *
  20. * @var array
  21. */
  22. protected $_defaultConfig = [
  23. 'foreignKeyField' => 'foreign_id',
  24. 'keyField' => 'key',
  25. 'valueField' => 'value',
  26. 'defaults' => null, // looks for `public $keyValueDefaults` property in the model,
  27. 'validate' => null, // looks for `public $keyValueValidate` property in the model
  28. 'defaultOnEmpty' => false, // if nothing is posted, delete (0 is not nothing)
  29. 'deleteIfDefault' => false, // if default value is posted, delete
  30. ];
  31. /**
  32. * Setup
  33. *
  34. * @param object AppModel
  35. * @param array $config
  36. */
  37. public function setup(Model $Model, $config = []) {
  38. $config += $this->_defaultConfig;
  39. $this->settings[$Model->alias] = $config;
  40. if (!$this->KeyValue) {
  41. $this->KeyValue = ClassRegistry::init('Tools.KeyValue');
  42. }
  43. /*
  44. if ($this->settings[$Model->alias]['validate']) {
  45. foreach ($this->settings[$Model->alias]['validate'] as $key => $validate) {
  46. $this->KeyValue->validate[$key] = $validate;
  47. }
  48. }
  49. */
  50. }
  51. /**
  52. * Returns details for named section
  53. *
  54. * @var string
  55. * @var string
  56. * @return mixed Flat array or direct value
  57. */
  58. public function getSection(Model $Model, $foreignKey, $section = null, $key = null) {
  59. extract($this->settings[$Model->alias]);
  60. $results = $this->KeyValue->find('all', [
  61. 'recursive' => -1,
  62. 'conditions' => [$foreignKeyField => $foreignKey],
  63. 'fields' => ['key', 'value']
  64. ]);
  65. $defaultValues = $this->defaultValues($Model);
  66. $detailArray = [];
  67. foreach ($results as $value) {
  68. $keyArray = preg_split('/\./', $value[$this->KeyValue->alias]['key'], 2);
  69. $detailArray[$keyArray[0]][$keyArray[1]] = $value[$this->KeyValue->alias]['value'];
  70. }
  71. foreach ($defaultValues as $model => $values) {
  72. foreach ($values as $valueKey => $val) {
  73. if (isset($detailArray[$model][$valueKey])) {
  74. continue;
  75. }
  76. $detailArray[$model][$valueKey] = $val;
  77. }
  78. }
  79. if ($section === null) {
  80. return $detailArray;
  81. }
  82. if (empty($detailArray[$section])) {
  83. return [];
  84. }
  85. if ($key === null) {
  86. return $detailArray[$section];
  87. }
  88. if (!isset($detailArray[$section][$key])) {
  89. return null;
  90. }
  91. return $detailArray[$section][$key];
  92. }
  93. /**
  94. * Save details for named section
  95. *
  96. * TODO: validate
  97. *
  98. * @var string
  99. * @var array
  100. * @var string
  101. * @return bool Success
  102. */
  103. public function saveSection(Model $Model, $foreignKey, $data, $section = null, $validate = true) {
  104. if ($validate && !$this->validateSection($Model, $data)) {
  105. return false;
  106. }
  107. extract($this->settings[$Model->alias]);
  108. foreach ($data as $model => $details) {
  109. if ($section && $section !== $model) {
  110. continue;
  111. }
  112. foreach ($details as $field => $value) {
  113. $newDetail = [];
  114. $section = $section ? $section : $model;
  115. $key = $section . '.' . $field;
  116. if ($defaultOnEmpty && (string)$value === '' || $deleteIfDefault && (string)$value === (string)$this->defaultValues($Model, $section, $field)) {
  117. return $this->resetSection($Model, $foreignKey, $section, $field);
  118. }
  119. $tmp = $this->KeyValue->find('first', [
  120. 'recursive' => -1,
  121. 'conditions' => [$foreignKeyField => $foreignKey, $keyField => $key],
  122. 'fields' => ['id']]);
  123. if ($tmp) {
  124. $newDetail[$this->KeyValue->alias]['id'] = $tmp[$this->KeyValue->alias]['id'];
  125. } else {
  126. $this->KeyValue->create();
  127. }
  128. $newDetail[$this->KeyValue->alias][$foreignKeyField] = $foreignKey;
  129. $newDetail[$this->KeyValue->alias][$keyField] = $key;
  130. $newDetail[$this->KeyValue->alias][$valueField] = $value;
  131. $newDetail[$this->KeyValue->alias]['model'] = $Model->alias;
  132. $this->KeyValue->save($newDetail, ['validate' => false]);
  133. }
  134. }
  135. return true;
  136. }
  137. /**
  138. * @return bool Success
  139. */
  140. public function validateSection(Model $Model, $data, $section = null) {
  141. $validate = $this->settings[$Model->alias]['validate'];
  142. if ($validate === null) {
  143. $validate = 'keyValueValidate';
  144. }
  145. if (empty($Model->{$validate})) {
  146. return true;
  147. }
  148. $rules = $Model->{$validate};
  149. $res = true;
  150. foreach ($data as $model => $array) {
  151. if ($section && $section !== $model) {
  152. continue;
  153. }
  154. if (empty($rules[$model])) {
  155. continue;
  156. }
  157. $this->KeyValue->{$model} = ClassRegistry::init(['class' => 'AppModel', 'alias' => $model, 'table' => false]);
  158. $this->KeyValue->{$model}->validate = $rules[$model];
  159. $this->KeyValue->{$model}->set($array);
  160. $res = $res && $this->KeyValue->{$model}->validates();
  161. }
  162. return $res;
  163. }
  164. /**
  165. * KeyValueBehavior::defaultValues()
  166. *
  167. * @param Model $Model
  168. * @param mixed $section
  169. * @param mixed $key
  170. * @return array
  171. */
  172. public function defaultValues(Model $Model, $section = null, $key = null) {
  173. $defaults = $this->settings[$Model->alias]['defaults'];
  174. if ($defaults === null) {
  175. $defaults = 'keyValueDefaults';
  176. }
  177. $defaultValues = [];
  178. if (!empty($Model->{$defaults})) {
  179. $defaultValues = $Model->{$defaults};
  180. }
  181. if ($section !== null) {
  182. if ($key !== null) {
  183. return isset($defaultValues[$section][$key]) ? $defaultValues[$section][$key] : null;
  184. }
  185. return isset($defaultValues[$section]) ? $defaultValues[$section] : null;
  186. }
  187. return $defaultValues;
  188. }
  189. /**
  190. * Resets the custom data for the specific domains (model, foreign_id)
  191. * careful: passing both null values will result in a complete truncate command
  192. *
  193. * @return bool Success
  194. */
  195. public function resetSection(Model $Model, $foreignKey = null, $section = null, $key = null) {
  196. extract($this->settings[$Model->alias]);
  197. $conditions = [];
  198. if ($foreignKey !== null) {
  199. $conditions[$foreignKeyField] = $foreignKey;
  200. }
  201. if ($section !== null) {
  202. if ($key !== null) {
  203. $conditions[$keyField] = $section . '.' . $key;
  204. } else {
  205. $conditions[$keyField . ' LIKE'] = $section . '.%';
  206. }
  207. }
  208. if (empty($conditions)) {
  209. return $this->KeyValue->truncate();
  210. }
  211. return (bool)$this->KeyValue->deleteAll($conditions, false);
  212. }
  213. }