KeyValueBehavior.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 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 $_defaults = array(
  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 = array()) {
  38. $settings = array_merge($this->_defaults, $config);
  39. $this->settings[$Model->alias] = $settings;
  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
  57. */
  58. public function getSection(Model $Model, $foreignKey, $section = null, $key = null) {
  59. extract($this->settings[$Model->alias]);
  60. $results = $this->KeyValue->find('all', array(
  61. 'recursive' => -1,
  62. 'conditions' => array($foreignKeyField => $foreignKey),
  63. 'fields' => array('key', 'value')
  64. ));
  65. $defaultValues = $this->defaultValues($Model);
  66. $detailArray = array();
  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 array();
  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) {
  104. if (!$this->validateSection($Model, $data)) {
  105. return false;
  106. }
  107. extract($this->settings[$Model->alias]);
  108. foreach ($data as $model => $details) {
  109. foreach ($details as $field => $value) {
  110. $newDetail = array();
  111. $section = $section ? $section : $model;
  112. $key = $section . '.' . $field;
  113. if ($defaultOnEmpty && (String)$value === '' || $deleteIfDefault && (String)$value === (String)$this->defaultValues($Model, $section, $field)) {
  114. return $this->resetSection($Model, $foreignKey, $section, $field);
  115. }
  116. $tmp = $this->KeyValue->find('first', array(
  117. 'recursive' => -1,
  118. 'conditions' => array($foreignKeyField => $foreignKey, $keyField => $key),
  119. 'fields' => array('id')));
  120. if ($tmp) {
  121. $newDetail[$this->KeyValue->alias]['id'] = $tmp[$this->KeyValue->alias]['id'];
  122. } else {
  123. $this->KeyValue->create();
  124. }
  125. $newDetail[$this->KeyValue->alias][$foreignKeyField] = $foreignKey;
  126. $newDetail[$this->KeyValue->alias][$keyField] = $key;
  127. $newDetail[$this->KeyValue->alias][$valueField] = $value;
  128. $newDetail[$this->KeyValue->alias]['model'] = $Model->alias;
  129. $this->KeyValue->save($newDetail, false);
  130. }
  131. }
  132. return true;
  133. }
  134. /**
  135. * @return bool $success
  136. */
  137. public function validateSection(Model $Model, $data) {
  138. $validate = $this->settings[$Model->alias]['validate'];
  139. if ($validate === null) {
  140. $validate = 'keyValueValidate';
  141. }
  142. if (empty($Model->{$validate})) {
  143. return true;
  144. }
  145. $rules = $Model->keyValueValidate;
  146. $res = true;
  147. foreach ($data as $model => $array) {
  148. if (empty($rules[$model])) {
  149. continue;
  150. }
  151. $this->KeyValue->{$model} = ClassRegistry::init(array('class'=>'AppModel', 'alias'=>$model));
  152. $this->KeyValue->{$model}->validate = $rules[$model];
  153. $this->KeyValue->{$model}->set($array);
  154. $res = $res && $this->KeyValue->{$model}->validates();
  155. }
  156. return $res;
  157. }
  158. public function defaultValues(Model $Model, $section = null, $key = null) {
  159. $defaults = $this->settings[$Model->alias]['defaults'];
  160. if ($defaults === null) {
  161. $defaults = 'keyValueDefaults';
  162. }
  163. $defaultValues = array();
  164. if (!empty($Model->{$defaults})) {
  165. $defaultValues = $Model->{$defaults};
  166. }
  167. if ($section !== null) {
  168. if ($key !== null) {
  169. return isset($defaultValues[$section][$key]) ? $defaultValues[$section][$key] : null;
  170. }
  171. return isset($defaultValues[$section]) ? $defaultValues[$section] : null;
  172. }
  173. return $defaultValues;
  174. }
  175. /**
  176. * resets the custom data for the specific domains (model, foreign_id)
  177. * careful: passing both null values will result in a complete truncate command
  178. *
  179. * @return bool $success
  180. * 2012-08-08 ms
  181. */
  182. public function resetSection(Model $Model, $foreignKey = null, $section = null, $key = null) {
  183. extract($this->settings[$Model->alias]);
  184. $conditions = array();
  185. if ($foreignKey !== null) {
  186. $conditions[$foreignKeyField] = $foreignKey;
  187. }
  188. if ($section !== null) {
  189. if ($key !== null) {
  190. $conditions[$keyField] = $section . '.'. $key;
  191. } else {
  192. $conditions[$keyField.' LIKE'] = $section.'.%';
  193. }
  194. }
  195. if (empty($conditions)) {
  196. return $this->KeyValue->truncate();
  197. }
  198. return (bool)$this->KeyValue->deleteAll($conditions, false);
  199. }
  200. }