KeyValueBehavior.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. $newDetail[$this->KeyValue->alias]['id'] = $tmp[$this->KeyValue->alias]['id'];
  121. $newDetail[$this->KeyValue->alias][$foreignKeyField] = $foreignKey;
  122. $newDetail[$this->KeyValue->alias][$keyField] = $key;
  123. $newDetail[$this->KeyValue->alias][$valueField] = $value;
  124. $newDetail[$this->KeyValue->alias]['model'] = $Model->alias;
  125. $this->KeyValue->save($newDetail, false);
  126. }
  127. }
  128. return true;
  129. }
  130. /**
  131. * @return bool $success
  132. */
  133. public function validateSection(Model $Model, $data) {
  134. $validate = $this->settings[$Model->alias]['validate'];
  135. if ($validate === null) {
  136. $validate = 'keyValueValidate';
  137. }
  138. if (empty($Model->{$validate})) {
  139. return true;
  140. }
  141. $rules = $Model->keyValueValidate;
  142. $res = true;
  143. foreach ($data as $model => $array) {
  144. if (empty($rules[$model])) {
  145. continue;
  146. }
  147. $this->KeyValue->{$model} = ClassRegistry::init(array('class'=>'AppModel', 'alias'=>$model));
  148. $this->KeyValue->{$model}->validate = $rules[$model];
  149. $this->KeyValue->{$model}->set($array);
  150. $res = $res && $this->KeyValue->{$model}->validates();
  151. }
  152. return $res;
  153. }
  154. public function defaultValues(Model $Model, $section = null, $key = null) {
  155. $defaults = $this->settings[$Model->alias]['defaults'];
  156. if ($defaults === null) {
  157. $defaults = 'keyValueDefaults';
  158. }
  159. $defaultValues = array();
  160. if (!empty($Model->{$defaults})) {
  161. $defaultValues = $Model->{$defaults};
  162. }
  163. if ($section !== null) {
  164. if ($key !== null) {
  165. return isset($defaultValues[$section][$key]) ? $defaultValues[$section][$key] : null;
  166. }
  167. return isset($defaultValues[$section]) ? $defaultValues[$section] : null;
  168. }
  169. return $defaultValues;
  170. }
  171. /**
  172. * resets the custom data for the specific domains (model, foreign_id)
  173. * careful: passing both null values will result in a complete truncate command
  174. *
  175. * @return bool $success
  176. * 2012-08-08 ms
  177. */
  178. public function resetSection(Model $Model, $foreignKey = null, $section = null, $key = null) {
  179. extract($this->settings[$Model->alias]);
  180. $conditions = array();
  181. if ($foreignKey !== null) {
  182. $conditions[$foreignKeyField] = $foreignKey;
  183. }
  184. if ($section !== null) {
  185. if ($key !== null) {
  186. $conditions[$keyField] = $section . '.'. $key;
  187. } else {
  188. $conditions[$keyField.' LIKE'] = $section.'.%';
  189. }
  190. }
  191. if (empty($conditions)) {
  192. return $this->KeyValue->truncate();
  193. }
  194. return (bool)$this->KeyValue->deleteAll($conditions, false);
  195. }
  196. }