NeighborBehavior.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Tools\Model\Behavior;
  3. use Cake\ORM\Behavior;
  4. use Cake\ORM\Query;
  5. use Cake\Utility\Hash;
  6. /**
  7. * WhoDidIt Behavior
  8. *
  9. * Handles created_by, modified_by fields for a given Model, if they exist in the Model DB table.
  10. * It's similar to the created, modified automagic, but it stores the id of the logged in user
  11. * in the models that have $actsAs = array('WhoDidIt').
  12. *
  13. * This is useful to track who created records, and the last user that has changed them.
  14. *
  15. * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#using-created-and-modified
  16. */
  17. class NeighborBehavior extends Behavior {
  18. /**
  19. * Default config for a model that has this behavior attached.
  20. *
  21. * Setting force_modified to true will have the same effect as overriding the save method as
  22. * described in the code example for "Using created and modified" in the Cookbook.
  23. *
  24. * @var array
  25. * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#using-created-and-modified
  26. */
  27. protected $_defaultConfig = array(
  28. );
  29. public function neighbors($id, array $options = []) {
  30. if (empty($id)) {
  31. throw new \InvalidArgumentException("The 'id' key is required for find('neighbors')");
  32. }
  33. $sortField = $this->_table->hasField('created') ? 'created' : $this->_table->primaryKey();
  34. $defaults = [
  35. 'sortField' => $this->_table->alias() . '.' . $sortField,
  36. //'displayField' => $this->_table->alias() . '.' . $this->_table->displayField()
  37. ];
  38. $options += $defaults;
  39. $normalDirection = (!empty($options['reverse']) ? false : true);
  40. $sortDirWord = $normalDirection ? ['ASC', 'DESC'] : ['DESC', 'ASC'];
  41. $sortDirSymb = $normalDirection ? ['>=', '<='] : ['<=', '>='];
  42. if (empty($options['value'])) {
  43. $data = $this->_table->find('all', ['conditions' => [$this->_table->primaryKey() => $id]])->first();
  44. list($model, $sortField) = pluginSplit($options['sortField']);
  45. $options['value'] = $data[$sortField];
  46. }
  47. $return = [];
  48. $findOptions = [];
  49. if (isset($options['contain'])) {
  50. $findOptions['contain'] = $options['contain'];
  51. }
  52. if (!empty($options['fields'])) {
  53. $findOptions['fields'] = $options['fields'];
  54. }
  55. $findOptions['conditions'][$this->_table->alias() . '.' . $this->_table->primaryKey() . ' !='] = $id;
  56. $prevOptions = $findOptions;
  57. $prevOptions['conditions'] = Hash::merge($prevOptions['conditions'], [$options['sortField'] . ' ' . $sortDirSymb[1] => $options['value']]);
  58. $prevOptions['order'] = [$options['sortField'] => $sortDirWord[1]];
  59. //debug($prevOptions);
  60. $return['prev'] = $this->_table->find('all', $prevOptions)->first();
  61. $nextOptions = $findOptions;
  62. $nextOptions['conditions'] = Hash::merge($nextOptions['conditions'], [$options['sortField'] . ' ' . $sortDirSymb[0] => $options['value']]);
  63. $nextOptions['order'] = [$options['sortField'] => $sortDirWord[0]];
  64. //debug($nextOptions);
  65. $return['next'] = $this->_table->find('all', $nextOptions)->first();
  66. return $return;
  67. }
  68. }