KeyValueBehavior.php 5.6 KB

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