SortableBehavior.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. App::uses('ModelBehavior', 'Model');
  3. /**
  4. * Allow Sort up/down of records.
  5. *
  6. * Expects a sort field to be present. This field will be sorted DESC.
  7. * The higher the sort value, the higher the record in the list.
  8. * You can also reverse the direction.
  9. *
  10. * Natural (default) order:
  11. * The sort value of new records is 0. This should be used in combination with
  12. * a secondary and possibly unique sort value for collisions around 0.
  13. *
  14. * Reversed order:
  15. * The sort value of a new record will be calculated (currently highest + 1).
  16. *
  17. * @cakephp 2.x
  18. * @author Mark Scherer
  19. * @license MIT
  20. */
  21. class SortableBehavior extends ModelBehavior {
  22. protected $_defaultConfig = array(
  23. 'field' => 'sort',
  24. 'reverse' => false // To make 0 the "highest" value
  25. );
  26. /**
  27. * SortableBehavior::setup()
  28. *
  29. * @param Model $Model
  30. * @param mixed $config
  31. * @return void
  32. */
  33. public function setup(Model $Model, $config = array()) {
  34. if (!isset($this->settings[$Model->alias])) {
  35. $this->settings[$Model->alias] = $this->_defaultConfig;
  36. }
  37. $this->settings[$Model->alias] = $config + $this->settings[$Model->alias];
  38. }
  39. /**
  40. * SortableBehavior::beforeSave()
  41. *
  42. * @param Model $Model
  43. * @param mixed $options
  44. * @return void
  45. */
  46. public function beforeSave(Model $Model, $options = array()) {
  47. if ($Model->id === false && isset($Model->data[$Model->alias]) &&
  48. !isset($Model->data[$Model->alias][$this->settings[$Model->alias]['field']])) {
  49. $sort = $this->_determineNextSortValue($Model);
  50. $Model->data[$Model->alias][$this->settings[$Model->alias]['field']] = $sort;
  51. }
  52. return true;
  53. }
  54. /**
  55. * SortableBehavior::_determineNextSortValue()
  56. *
  57. * @param Model $Model
  58. * @return int Sort value.
  59. */
  60. protected function _determineNextSortValue(Model $Model) {
  61. if (empty($this->settings[$Model->alias]['reverse'])) {
  62. return 0;
  63. }
  64. $sort = $Model->find('first', array(
  65. 'fields' => array(
  66. $this->settings[$Model->alias]['field']
  67. ),
  68. 'order' => array(
  69. $this->settings[$Model->alias]['field'] => 'DESC'
  70. )
  71. ));
  72. if (!empty($sort)) {
  73. $sort = $sort[$Model->alias][$this->settings[$Model->alias]['field']];
  74. $sort++;
  75. } else {
  76. $sort = 1;
  77. }
  78. return $sort;
  79. }
  80. /**
  81. * @return bool Success
  82. */
  83. public function moveUp(Model $Model, $id, $steps = 1) {
  84. return $this->_moveUpDown($Model, 'up', $id, $steps);
  85. }
  86. /**
  87. * @return bool Success
  88. */
  89. public function moveDown(Model $Model, $id, $steps = 1) {
  90. return $this->_moveUpDown($Model, 'down', $id, $steps);
  91. }
  92. /**
  93. * @param Model $Model
  94. * @param string $direction
  95. * @param int $steps Steps to jump. Defaults to 1.
  96. * @return bool Success
  97. */
  98. protected function _moveUpDown(Model $Model, $direction, $id, $steps = 1) {
  99. // FIXME: Sort over more than one placement.
  100. if ($direction === 'down' && empty($this->settings[$Model->alias]['reverse'])) {
  101. $order = '<=';
  102. $findOrder = 'DESC';
  103. } else {
  104. $order = '>=';
  105. $findOrder = 'ASC';
  106. }
  107. $sort = $Model->find('list', array(
  108. 'fields' => array(
  109. $this->settings[$Model->alias]['field']
  110. ),
  111. 'conditions' => array(
  112. 'id' => $id
  113. )
  114. ));
  115. if (empty($sort)) {
  116. return false;
  117. }
  118. list($sort) = array_values($sort);
  119. $data = $Model->find('list', array(
  120. 'fields' => array(
  121. 'id',
  122. $this->settings[$Model->alias]['field']
  123. ),
  124. 'conditions' => array(
  125. $this->settings[$Model->alias]['field'] . ' ' . $order => $sort
  126. ),
  127. 'order' => array(
  128. $this->settings[$Model->alias]['field'] => $findOrder
  129. ),
  130. 'limit' => $steps + 1
  131. ));
  132. $value = end($data);
  133. $key = key($data);
  134. if ($key == $id) {
  135. return;
  136. }
  137. $lastId = $Model->id;
  138. if ($sort == $value) {
  139. if ($direction === 'down' && empty($this->settings[$Model->alias]['reverse'])) {
  140. $value++;
  141. } else {
  142. $value--;
  143. }
  144. }
  145. $Model->id = $key;
  146. $Model->saveField($this->settings[$Model->alias]['field'], $sort);
  147. $Model->id = $id;
  148. $Model->saveField($this->settings[$Model->alias]['field'], $value);
  149. $Model->id = $lastId;
  150. return true;
  151. }
  152. }