PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Meta/Model/Behavior/MetaBehavior.php

https://github.com/kareypowell/croogo
PHP | 137 lines | 70 code | 14 blank | 53 comment | 8 complexity | 44a50da26f932c57843152855cdd40b2 MD5 | raw file
  1. <?php
  2. App::uses('ModelBehavior', 'Model');
  3. /**
  4. * Meta Behavior
  5. *
  6. * @category Behavior
  7. * @package Croogo.Meta.Model.Behavior
  8. * @version 1.0
  9. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class MetaBehavior extends ModelBehavior {
  14. /**
  15. * Setup
  16. *
  17. * @param Model $model
  18. * @param array $config
  19. * @return void
  20. */
  21. public function setup(Model $model, $config = array()) {
  22. if (is_string($config)) {
  23. $config = array($config);
  24. }
  25. $this->settings[$model->alias] = $config;
  26. $model->bindModel(array(
  27. 'hasMany' => array(
  28. 'Meta' => array(
  29. 'className' => 'Meta.Meta',
  30. 'foreignKey' => 'foreign_key',
  31. 'dependent' => true,
  32. 'conditions' => array(
  33. 'Meta.model' => $model->alias,
  34. ),
  35. 'order' => 'Meta.key ASC',
  36. ),
  37. ),
  38. ), false);
  39. $callback = array($this, 'onBeforeSaveNode');
  40. $eventManager = $model->getEventManager();
  41. $eventManager->attach($callback, 'Model.Node.beforeSaveNode');
  42. }
  43. /**
  44. * afterFind callback
  45. *
  46. * @param Model $model
  47. * @param array $created
  48. * @param boolean $primary
  49. * @return array
  50. */
  51. public function afterFind(Model $model, $results, $primary = false) {
  52. if ($primary && isset($results[0][$model->alias])) {
  53. foreach ($results as $i => $result) {
  54. $customFields = array();
  55. if (isset($result['Meta']) && count($result['Meta']) > 0) {
  56. $customFields = Hash::combine($result, 'Meta.{n}.key', 'Meta.{n}.value');
  57. }
  58. $results[$i]['CustomFields'] = $customFields;
  59. }
  60. } elseif (isset($results[$model->alias])) {
  61. $customFields = array();
  62. if (isset($results['Meta']) && count($results['Meta']) > 0) {
  63. $customFields = Hash::combine($results, 'Meta.{n}.key', 'Meta.{n}.value');
  64. }
  65. $results['CustomFields'] = $customFields;
  66. }
  67. return $results;
  68. }
  69. /**
  70. * Prepare data
  71. *
  72. * @param Model $model
  73. * @param array $data
  74. * @return array
  75. */
  76. public function prepareData(Model $model, $data) {
  77. return $this->_prepareMeta($data);
  78. }
  79. /**
  80. * Protected method for MetaBehavior::prepareData()
  81. *
  82. * @param Model $model
  83. * @param array $data
  84. * @return array
  85. */
  86. protected function _prepareMeta($data) {
  87. if (isset($data['Meta']) &&
  88. is_array($data['Meta']) &&
  89. count($data['Meta']) > 0 &&
  90. !Hash::numeric(array_keys($data['Meta']))) {
  91. $meta = $data['Meta'];
  92. $data['Meta'] = array();
  93. $i = 0;
  94. foreach ($meta as $metaArray) {
  95. $data['Meta'][$i] = $metaArray;
  96. $i++;
  97. }
  98. }
  99. return $data;
  100. }
  101. /**
  102. * Handle Model.Node.beforeSaveNode event
  103. *
  104. * @param CakeEvent $event
  105. */
  106. public function onBeforeSaveNode($event) {
  107. $event->data['data'] = $this->_prepareMeta($event->data['data']);
  108. return true;
  109. }
  110. /**
  111. * Save with meta
  112. *
  113. * @param Model $model
  114. * @param array $data
  115. * @param array $options
  116. * @return void
  117. * @deprecated Use standard Model::saveAll()
  118. */
  119. public function saveWithMeta(Model $model, $data, $options = array()) {
  120. $data = $this->_prepareMeta($data);
  121. return $model->saveAll($data, $options);
  122. }
  123. }