TreeBehavior.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. <?php
  2. /**
  3. * Tree behavior class.
  4. *
  5. * Enables a model object to act as a node-based tree.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2012, Cake Software Foundation, Inc.
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP Project
  17. * @package Cake.Model.Behavior
  18. * @since CakePHP v 1.2.0.4487
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. /**
  22. * Tree Behavior.
  23. *
  24. * Enables a model object to act as a node-based tree. Using Modified Preorder Tree Traversal
  25. *
  26. * @see http://en.wikipedia.org/wiki/Tree_traversal
  27. * @package Cake.Model.Behavior
  28. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html
  29. */
  30. class TreeBehavior extends ModelBehavior {
  31. /**
  32. * Errors
  33. *
  34. * @var array
  35. */
  36. public $errors = array();
  37. /**
  38. * Defaults
  39. *
  40. * @var array
  41. */
  42. protected $_defaults = array(
  43. 'parent' => 'parent_id', 'left' => 'lft', 'right' => 'rght',
  44. 'scope' => '1 = 1', 'type' => 'nested', '__parentChange' => false, 'recursive' => -1
  45. );
  46. /**
  47. * Used to preserve state between delete callbacks.
  48. *
  49. * @var array
  50. */
  51. protected $_deletedRow = null;
  52. /**
  53. * Initiate Tree behavior
  54. *
  55. * @param Model $Model instance of model
  56. * @param array $config array of configuration settings.
  57. * @return void
  58. */
  59. public function setup(Model $Model, $config = array()) {
  60. if (isset($config[0])) {
  61. $config['type'] = $config[0];
  62. unset($config[0]);
  63. }
  64. $settings = array_merge($this->_defaults, $config);
  65. if (in_array($settings['scope'], $Model->getAssociated('belongsTo'))) {
  66. $data = $Model->getAssociated($settings['scope']);
  67. $parent = $Model->{$settings['scope']};
  68. $settings['scope'] = $Model->alias . '.' . $data['foreignKey'] . ' = ' . $parent->alias . '.' . $parent->primaryKey;
  69. $settings['recursive'] = 0;
  70. }
  71. $this->settings[$Model->alias] = $settings;
  72. }
  73. /**
  74. * After save method. Called after all saves
  75. *
  76. * Overridden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
  77. * parameters to be saved.
  78. *
  79. * @param Model $Model Model instance.
  80. * @param boolean $created indicates whether the node just saved was created or updated
  81. * @return boolean true on success, false on failure
  82. */
  83. public function afterSave(Model $Model, $created) {
  84. extract($this->settings[$Model->alias]);
  85. if ($created) {
  86. if ((isset($Model->data[$Model->alias][$parent])) && $Model->data[$Model->alias][$parent]) {
  87. return $this->_setParent($Model, $Model->data[$Model->alias][$parent], $created);
  88. }
  89. } elseif ($this->settings[$Model->alias]['__parentChange']) {
  90. $this->settings[$Model->alias]['__parentChange'] = false;
  91. return $this->_setParent($Model, $Model->data[$Model->alias][$parent]);
  92. }
  93. }
  94. /**
  95. * Runs before a find() operation
  96. *
  97. * @param Model $Model Model using the behavior
  98. * @param array $query Query parameters as set by cake
  99. * @return array
  100. */
  101. public function beforeFind(Model $Model, $query) {
  102. if ($Model->findQueryType == 'threaded' && !isset($query['parent'])) {
  103. $query['parent'] = $this->settings[$Model->alias]['parent'];
  104. }
  105. return $query;
  106. }
  107. /**
  108. * Stores the record about to be deleted.
  109. *
  110. * This is used to delete child nodes in the afterDelete.
  111. *
  112. * @param Model $Model Model instance
  113. * @param boolean $cascade
  114. * @return boolean
  115. */
  116. public function beforeDelete(Model $Model, $cascade = true) {
  117. extract($this->settings[$Model->alias]);
  118. $data = current($Model->find('first', array(
  119. 'conditions' => array($Model->alias . '.' . $Model->primaryKey => $Model->id),
  120. 'fields' => array($Model->alias . '.' . $left, $Model->alias . '.' . $right),
  121. 'recursive' => -1)));
  122. $this->_deletedRow = $data;
  123. return true;
  124. }
  125. /**
  126. * After delete method.
  127. *
  128. * Will delete the current node and all children using the deleteAll method and sync the table
  129. *
  130. * @param Model $Model Model instance
  131. * @return boolean true to continue, false to abort the delete
  132. */
  133. public function afterDelete(Model $Model) {
  134. extract($this->settings[$Model->alias]);
  135. $data = $this->_deletedRow;
  136. $this->_deletedRow = null;
  137. if (!$data[$right] || !$data[$left]) {
  138. return true;
  139. }
  140. $diff = $data[$right] - $data[$left] + 1;
  141. if ($diff > 2) {
  142. if (is_string($scope)) {
  143. $scope = array($scope);
  144. }
  145. $scope[]["{$Model->alias}.{$left} BETWEEN ? AND ?"] = array($data[$left] + 1, $data[$right] - 1);
  146. $Model->deleteAll($scope);
  147. }
  148. $this->_sync($Model, $diff, '-', '> ' . $data[$right]);
  149. return true;
  150. }
  151. /**
  152. * Before save method. Called before all saves
  153. *
  154. * Overridden to transparently manage setting the lft and rght fields if and only if the parent field is included in the
  155. * parameters to be saved. For newly created nodes with NO parent the left and right field values are set directly by
  156. * this method bypassing the setParent logic.
  157. *
  158. * @since 1.2
  159. * @param Model $Model Model instance
  160. * @return boolean true to continue, false to abort the save
  161. */
  162. public function beforeSave(Model $Model) {
  163. extract($this->settings[$Model->alias]);
  164. $this->_addToWhitelist($Model, array($left, $right));
  165. if (!$Model->id) {
  166. if (array_key_exists($parent, $Model->data[$Model->alias]) && $Model->data[$Model->alias][$parent]) {
  167. $parentNode = $Model->find('first', array(
  168. 'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
  169. 'fields' => array($Model->primaryKey, $right), 'recursive' => $recursive
  170. ));
  171. if (!$parentNode) {
  172. return false;
  173. }
  174. list($parentNode) = array_values($parentNode);
  175. $Model->data[$Model->alias][$left] = 0;
  176. $Model->data[$Model->alias][$right] = 0;
  177. } else {
  178. $edge = $this->_getMax($Model, $scope, $right, $recursive);
  179. $Model->data[$Model->alias][$left] = $edge + 1;
  180. $Model->data[$Model->alias][$right] = $edge + 2;
  181. }
  182. } elseif (array_key_exists($parent, $Model->data[$Model->alias])) {
  183. if ($Model->data[$Model->alias][$parent] != $Model->field($parent)) {
  184. $this->settings[$Model->alias]['__parentChange'] = true;
  185. }
  186. if (!$Model->data[$Model->alias][$parent]) {
  187. $Model->data[$Model->alias][$parent] = null;
  188. $this->_addToWhitelist($Model, $parent);
  189. } else {
  190. $values = $Model->find('first', array(
  191. 'conditions' => array($scope, $Model->escapeField() => $Model->id),
  192. 'fields' => array($Model->primaryKey, $parent, $left, $right), 'recursive' => $recursive)
  193. );
  194. if ($values === false) {
  195. return false;
  196. }
  197. list($node) = array_values($values);
  198. $parentNode = $Model->find('first', array(
  199. 'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
  200. 'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive
  201. ));
  202. if (!$parentNode) {
  203. return false;
  204. }
  205. list($parentNode) = array_values($parentNode);
  206. if (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
  207. return false;
  208. } elseif ($node[$Model->primaryKey] == $parentNode[$Model->primaryKey]) {
  209. return false;
  210. }
  211. }
  212. }
  213. return true;
  214. }
  215. /**
  216. * Get the number of child nodes
  217. *
  218. * If the direct parameter is set to true, only the direct children are counted (based upon the parent_id field)
  219. * If false is passed for the id parameter, all top level nodes are counted, or all nodes are counted.
  220. *
  221. * @param Model $Model Model instance
  222. * @param mixed $id The ID of the record to read or false to read all top level nodes
  223. * @param boolean $direct whether to count direct, or all, children
  224. * @return integer number of child nodes
  225. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::childCount
  226. */
  227. public function childCount(Model $Model, $id = null, $direct = false) {
  228. if (is_array($id)) {
  229. extract (array_merge(array('id' => null), $id));
  230. }
  231. if ($id === null && $Model->id) {
  232. $id = $Model->id;
  233. } elseif (!$id) {
  234. $id = null;
  235. }
  236. extract($this->settings[$Model->alias]);
  237. if ($direct) {
  238. return $Model->find('count', array('conditions' => array($scope, $Model->escapeField($parent) => $id)));
  239. }
  240. if ($id === null) {
  241. return $Model->find('count', array('conditions' => $scope));
  242. } elseif ($Model->id === $id && isset($Model->data[$Model->alias][$left]) && isset($Model->data[$Model->alias][$right])) {
  243. $data = $Model->data[$Model->alias];
  244. } else {
  245. $data = $Model->find('first', array('conditions' => array($scope, $Model->escapeField() => $id), 'recursive' => $recursive));
  246. if (!$data) {
  247. return 0;
  248. }
  249. $data = $data[$Model->alias];
  250. }
  251. return ($data[$right] - $data[$left] - 1) / 2;
  252. }
  253. /**
  254. * Get the child nodes of the current model
  255. *
  256. * If the direct parameter is set to true, only the direct children are returned (based upon the parent_id field)
  257. * If false is passed for the id parameter, top level, or all (depending on direct parameter appropriate) are counted.
  258. *
  259. * @param Model $Model Model instance
  260. * @param mixed $id The ID of the record to read
  261. * @param boolean $direct whether to return only the direct, or all, children
  262. * @param mixed $fields Either a single string of a field name, or an array of field names
  263. * @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") defaults to the tree order
  264. * @param integer $limit SQL LIMIT clause, for calculating items per page.
  265. * @param integer $page Page number, for accessing paged data
  266. * @param integer $recursive The number of levels deep to fetch associated records
  267. * @return array Array of child nodes
  268. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::children
  269. */
  270. public function children(Model $Model, $id = null, $direct = false, $fields = null, $order = null, $limit = null, $page = 1, $recursive = null) {
  271. if (is_array($id)) {
  272. extract (array_merge(array('id' => null), $id));
  273. }
  274. $overrideRecursive = $recursive;
  275. if ($id === null && $Model->id) {
  276. $id = $Model->id;
  277. } elseif (!$id) {
  278. $id = null;
  279. }
  280. extract($this->settings[$Model->alias]);
  281. if (!is_null($overrideRecursive)) {
  282. $recursive = $overrideRecursive;
  283. }
  284. if (!$order) {
  285. $order = $Model->alias . '.' . $left . ' asc';
  286. }
  287. if ($direct) {
  288. $conditions = array($scope, $Model->escapeField($parent) => $id);
  289. return $Model->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
  290. }
  291. if (!$id) {
  292. $conditions = $scope;
  293. } else {
  294. $result = array_values((array)$Model->find('first', array(
  295. 'conditions' => array($scope, $Model->escapeField() => $id),
  296. 'fields' => array($left, $right),
  297. 'recursive' => $recursive
  298. )));
  299. if (empty($result) || !isset($result[0])) {
  300. return array();
  301. }
  302. $conditions = array($scope,
  303. $Model->escapeField($right) . ' <' => $result[0][$right],
  304. $Model->escapeField($left) . ' >' => $result[0][$left]
  305. );
  306. }
  307. return $Model->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
  308. }
  309. /**
  310. * A convenience method for returning a hierarchical array used for HTML select boxes
  311. *
  312. * @param Model $Model Model instance
  313. * @param mixed $conditions SQL conditions as a string or as an array('field' =>'value',...)
  314. * @param string $keyPath A string path to the key, i.e. "{n}.Post.id"
  315. * @param string $valuePath A string path to the value, i.e. "{n}.Post.title"
  316. * @param string $spacer The character or characters which will be repeated
  317. * @param integer $recursive The number of levels deep to fetch associated records
  318. * @return array An associative array of records, where the id is the key, and the display field is the value
  319. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::generateTreeList
  320. */
  321. public function generateTreeList(Model $Model, $conditions = null, $keyPath = null, $valuePath = null, $spacer = '_', $recursive = null) {
  322. $overrideRecursive = $recursive;
  323. extract($this->settings[$Model->alias]);
  324. if (!is_null($overrideRecursive)) {
  325. $recursive = $overrideRecursive;
  326. }
  327. if ($keyPath == null && $valuePath == null && $Model->hasField($Model->displayField)) {
  328. $fields = array($Model->primaryKey, $Model->displayField, $left, $right);
  329. } else {
  330. $fields = null;
  331. }
  332. if ($keyPath == null) {
  333. $keyPath = '{n}.' . $Model->alias . '.' . $Model->primaryKey;
  334. }
  335. if ($valuePath == null) {
  336. $valuePath = array('{0}{1}', '{n}.tree_prefix', '{n}.' . $Model->alias . '.' . $Model->displayField);
  337. } elseif (is_string($valuePath)) {
  338. $valuePath = array('{0}{1}', '{n}.tree_prefix', $valuePath);
  339. } else {
  340. $valuePath[0] = '{' . (count($valuePath) - 1) . '}' . $valuePath[0];
  341. $valuePath[] = '{n}.tree_prefix';
  342. }
  343. $order = $Model->alias . '.' . $left . ' asc';
  344. $results = $Model->find('all', compact('conditions', 'fields', 'order', 'recursive'));
  345. $stack = array();
  346. foreach ($results as $i => $result) {
  347. while ($stack && ($stack[count($stack) - 1] < $result[$Model->alias][$right])) {
  348. array_pop($stack);
  349. }
  350. $results[$i]['tree_prefix'] = str_repeat($spacer, count($stack));
  351. $stack[] = $result[$Model->alias][$right];
  352. }
  353. if (empty($results)) {
  354. return array();
  355. }
  356. return Set::combine($results, $keyPath, $valuePath);
  357. }
  358. /**
  359. * Get the parent node
  360. *
  361. * reads the parent id and returns this node
  362. *
  363. * @param Model $Model Model instance
  364. * @param mixed $id The ID of the record to read
  365. * @param string|array $fields
  366. * @param integer $recursive The number of levels deep to fetch associated records
  367. * @return array|boolean Array of data for the parent node
  368. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::getParentNode
  369. */
  370. public function getParentNode(Model $Model, $id = null, $fields = null, $recursive = null) {
  371. if (is_array($id)) {
  372. extract (array_merge(array('id' => null), $id));
  373. }
  374. $overrideRecursive = $recursive;
  375. if (empty ($id)) {
  376. $id = $Model->id;
  377. }
  378. extract($this->settings[$Model->alias]);
  379. if (!is_null($overrideRecursive)) {
  380. $recursive = $overrideRecursive;
  381. }
  382. $parentId = $Model->find('first', array('conditions' => array($Model->primaryKey => $id), 'fields' => array($parent), 'recursive' => -1));
  383. if ($parentId) {
  384. $parentId = $parentId[$Model->alias][$parent];
  385. $parent = $Model->find('first', array('conditions' => array($Model->escapeField() => $parentId), 'fields' => $fields, 'recursive' => $recursive));
  386. return $parent;
  387. }
  388. return false;
  389. }
  390. /**
  391. * Get the path to the given node
  392. *
  393. * @param Model $Model Model instance
  394. * @param mixed $id The ID of the record to read
  395. * @param mixed $fields Either a single string of a field name, or an array of field names
  396. * @param integer $recursive The number of levels deep to fetch associated records
  397. * @return array Array of nodes from top most parent to current node
  398. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::getPath
  399. */
  400. public function getPath(Model $Model, $id = null, $fields = null, $recursive = null) {
  401. if (is_array($id)) {
  402. extract (array_merge(array('id' => null), $id));
  403. }
  404. $overrideRecursive = $recursive;
  405. if (empty ($id)) {
  406. $id = $Model->id;
  407. }
  408. extract($this->settings[$Model->alias]);
  409. if (!is_null($overrideRecursive)) {
  410. $recursive = $overrideRecursive;
  411. }
  412. $result = $Model->find('first', array('conditions' => array($Model->escapeField() => $id), 'fields' => array($left, $right), 'recursive' => $recursive));
  413. if ($result) {
  414. $result = array_values($result);
  415. } else {
  416. return null;
  417. }
  418. $item = $result[0];
  419. $results = $Model->find('all', array(
  420. 'conditions' => array($scope, $Model->escapeField($left) . ' <=' => $item[$left], $Model->escapeField($right) . ' >=' => $item[$right]),
  421. 'fields' => $fields, 'order' => array($Model->escapeField($left) => 'asc'), 'recursive' => $recursive
  422. ));
  423. return $results;
  424. }
  425. /**
  426. * Reorder the node without changing the parent.
  427. *
  428. * If the node is the last child, or is a top level node with no subsequent node this method will return false
  429. *
  430. * @param Model $Model Model instance
  431. * @param mixed $id The ID of the record to move
  432. * @param integer|boolean $number how many places to move the node or true to move to last position
  433. * @return boolean true on success, false on failure
  434. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::moveDown
  435. */
  436. public function moveDown(Model $Model, $id = null, $number = 1) {
  437. if (is_array($id)) {
  438. extract (array_merge(array('id' => null), $id));
  439. }
  440. if (!$number) {
  441. return false;
  442. }
  443. if (empty ($id)) {
  444. $id = $Model->id;
  445. }
  446. extract($this->settings[$Model->alias]);
  447. list($node) = array_values($Model->find('first', array(
  448. 'conditions' => array($scope, $Model->escapeField() => $id),
  449. 'fields' => array($Model->primaryKey, $left, $right, $parent), 'recursive' => $recursive
  450. )));
  451. if ($node[$parent]) {
  452. list($parentNode) = array_values($Model->find('first', array(
  453. 'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
  454. 'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive
  455. )));
  456. if (($node[$right] + 1) == $parentNode[$right]) {
  457. return false;
  458. }
  459. }
  460. $nextNode = $Model->find('first', array(
  461. 'conditions' => array($scope, $Model->escapeField($left) => ($node[$right] + 1)),
  462. 'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive)
  463. );
  464. if ($nextNode) {
  465. list($nextNode) = array_values($nextNode);
  466. } else {
  467. return false;
  468. }
  469. $edge = $this->_getMax($Model, $scope, $right, $recursive);
  470. $this->_sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]);
  471. $this->_sync($Model, $nextNode[$left] - $node[$left], '-', 'BETWEEN ' . $nextNode[$left] . ' AND ' . $nextNode[$right]);
  472. $this->_sync($Model, $edge - $node[$left] - ($nextNode[$right] - $nextNode[$left]), '-', '> ' . $edge);
  473. if (is_int($number)) {
  474. $number--;
  475. }
  476. if ($number) {
  477. $this->moveDown($Model, $id, $number);
  478. }
  479. return true;
  480. }
  481. /**
  482. * Reorder the node without changing the parent.
  483. *
  484. * If the node is the first child, or is a top level node with no previous node this method will return false
  485. *
  486. * @param Model $Model Model instance
  487. * @param mixed $id The ID of the record to move
  488. * @param integer|boolean $number how many places to move the node, or true to move to first position
  489. * @return boolean true on success, false on failure
  490. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::moveUp
  491. */
  492. public function moveUp(Model $Model, $id = null, $number = 1) {
  493. if (is_array($id)) {
  494. extract (array_merge(array('id' => null), $id));
  495. }
  496. if (!$number) {
  497. return false;
  498. }
  499. if (empty ($id)) {
  500. $id = $Model->id;
  501. }
  502. extract($this->settings[$Model->alias]);
  503. list($node) = array_values($Model->find('first', array(
  504. 'conditions' => array($scope, $Model->escapeField() => $id),
  505. 'fields' => array($Model->primaryKey, $left, $right, $parent), 'recursive' => $recursive
  506. )));
  507. if ($node[$parent]) {
  508. list($parentNode) = array_values($Model->find('first', array(
  509. 'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
  510. 'fields' => array($Model->primaryKey, $left, $right), 'recursive' => $recursive
  511. )));
  512. if (($node[$left] - 1) == $parentNode[$left]) {
  513. return false;
  514. }
  515. }
  516. $previousNode = $Model->find('first', array(
  517. 'conditions' => array($scope, $Model->escapeField($right) => ($node[$left] - 1)),
  518. 'fields' => array($Model->primaryKey, $left, $right),
  519. 'recursive' => $recursive
  520. ));
  521. if ($previousNode) {
  522. list($previousNode) = array_values($previousNode);
  523. } else {
  524. return false;
  525. }
  526. $edge = $this->_getMax($Model, $scope, $right, $recursive);
  527. $this->_sync($Model, $edge - $previousNode[$left] + 1, '+', 'BETWEEN ' . $previousNode[$left] . ' AND ' . $previousNode[$right]);
  528. $this->_sync($Model, $node[$left] - $previousNode[$left], '-', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right]);
  529. $this->_sync($Model, $edge - $previousNode[$left] - ($node[$right] - $node[$left]), '-', '> ' . $edge);
  530. if (is_int($number)) {
  531. $number--;
  532. }
  533. if ($number) {
  534. $this->moveUp($Model, $id, $number);
  535. }
  536. return true;
  537. }
  538. /**
  539. * Recover a corrupted tree
  540. *
  541. * The mode parameter is used to specify the source of info that is valid/correct. The opposite source of data
  542. * will be populated based upon that source of info. E.g. if the MPTT fields are corrupt or empty, with the $mode
  543. * 'parent' the values of the parent_id field will be used to populate the left and right fields. The missingParentAction
  544. * parameter only applies to "parent" mode and determines what to do if the parent field contains an id that is not present.
  545. *
  546. * @todo Could be written to be faster, *maybe*. Ideally using a subquery and putting all the logic burden on the DB.
  547. * @param Model $Model Model instance
  548. * @param string $mode parent or tree
  549. * @param mixed $missingParentAction 'return' to do nothing and return, 'delete' to
  550. * delete, or the id of the parent to set as the parent_id
  551. * @return boolean true on success, false on failure
  552. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::recover
  553. */
  554. public function recover(Model $Model, $mode = 'parent', $missingParentAction = null) {
  555. if (is_array($mode)) {
  556. extract (array_merge(array('mode' => 'parent'), $mode));
  557. }
  558. extract($this->settings[$Model->alias]);
  559. $Model->recursive = $recursive;
  560. if ($mode == 'parent') {
  561. $Model->bindModel(array('belongsTo' => array('VerifyParent' => array(
  562. 'className' => $Model->name,
  563. 'foreignKey' => $parent,
  564. 'fields' => array($Model->primaryKey, $left, $right, $parent),
  565. ))));
  566. $missingParents = $Model->find('list', array(
  567. 'recursive' => 0,
  568. 'conditions' => array($scope, array(
  569. 'NOT' => array($Model->escapeField($parent) => null), $Model->VerifyParent->escapeField() => null
  570. ))
  571. ));
  572. $Model->unbindModel(array('belongsTo' => array('VerifyParent')));
  573. if ($missingParents) {
  574. if ($missingParentAction == 'return') {
  575. foreach ($missingParents as $id => $display) {
  576. $this->errors[] = 'cannot find the parent for ' . $Model->alias . ' with id ' . $id . '(' . $display . ')';
  577. }
  578. return false;
  579. } elseif ($missingParentAction == 'delete') {
  580. $Model->deleteAll(array($Model->primaryKey => array_flip($missingParents)));
  581. } else {
  582. $Model->updateAll(array($parent => $missingParentAction), array($Model->escapeField($Model->primaryKey) => array_flip($missingParents)));
  583. }
  584. }
  585. $count = 1;
  586. foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey), 'order' => $left)) as $array) {
  587. $lft = $count++;
  588. $rght = $count++;
  589. $Model->create(false);
  590. $Model->id = $array[$Model->alias][$Model->primaryKey];
  591. $Model->save(array($left => $lft, $right => $rght), array('callbacks' => false, 'validate' => false));
  592. }
  593. foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) {
  594. $Model->create(false);
  595. $Model->id = $array[$Model->alias][$Model->primaryKey];
  596. $this->_setParent($Model, $array[$Model->alias][$parent]);
  597. }
  598. } else {
  599. $db = ConnectionManager::getDataSource($Model->useDbConfig);
  600. foreach ($Model->find('all', array('conditions' => $scope, 'fields' => array($Model->primaryKey, $parent), 'order' => $left)) as $array) {
  601. $path = $this->getPath($Model, $array[$Model->alias][$Model->primaryKey]);
  602. if ($path == null || count($path) < 2) {
  603. $parentId = null;
  604. } else {
  605. $parentId = $path[count($path) - 2][$Model->alias][$Model->primaryKey];
  606. }
  607. $Model->updateAll(array($parent => $db->value($parentId, $parent)), array($Model->escapeField() => $array[$Model->alias][$Model->primaryKey]));
  608. }
  609. }
  610. return true;
  611. }
  612. /**
  613. * Reorder method.
  614. *
  615. * Reorders the nodes (and child nodes) of the tree according to the field and direction specified in the parameters.
  616. * This method does not change the parent of any node.
  617. *
  618. * Requires a valid tree, by default it verifies the tree before beginning.
  619. *
  620. * Options:
  621. *
  622. * - 'id' id of record to use as top node for reordering
  623. * - 'field' Which field to use in reordering defaults to displayField
  624. * - 'order' Direction to order either DESC or ASC (defaults to ASC)
  625. * - 'verify' Whether or not to verify the tree before reorder. defaults to true.
  626. *
  627. * @param Model $Model Model instance
  628. * @param array $options array of options to use in reordering.
  629. * @return boolean true on success, false on failure
  630. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::reorder
  631. */
  632. public function reorder(Model $Model, $options = array()) {
  633. $options = array_merge(array('id' => null, 'field' => $Model->displayField, 'order' => 'ASC', 'verify' => true), $options);
  634. extract($options);
  635. if ($verify && !$this->verify($Model)) {
  636. return false;
  637. }
  638. $verify = false;
  639. extract($this->settings[$Model->alias]);
  640. $fields = array($Model->primaryKey, $field, $left, $right);
  641. $sort = $field . ' ' . $order;
  642. $nodes = $this->children($Model, $id, true, $fields, $sort, null, null, $recursive);
  643. $cacheQueries = $Model->cacheQueries;
  644. $Model->cacheQueries = false;
  645. if ($nodes) {
  646. foreach ($nodes as $node) {
  647. $id = $node[$Model->alias][$Model->primaryKey];
  648. $this->moveDown($Model, $id, true);
  649. if ($node[$Model->alias][$left] != $node[$Model->alias][$right] - 1) {
  650. $this->reorder($Model, compact('id', 'field', 'order', 'verify'));
  651. }
  652. }
  653. }
  654. $Model->cacheQueries = $cacheQueries;
  655. return true;
  656. }
  657. /**
  658. * Remove the current node from the tree, and reparent all children up one level.
  659. *
  660. * If the parameter delete is false, the node will become a new top level node. Otherwise the node will be deleted
  661. * after the children are reparented.
  662. *
  663. * @param Model $Model Model instance
  664. * @param mixed $id The ID of the record to remove
  665. * @param boolean $delete whether to delete the node after reparenting children (if any)
  666. * @return boolean true on success, false on failure
  667. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::removeFromTree
  668. */
  669. public function removeFromTree(Model $Model, $id = null, $delete = false) {
  670. if (is_array($id)) {
  671. extract (array_merge(array('id' => null), $id));
  672. }
  673. extract($this->settings[$Model->alias]);
  674. list($node) = array_values($Model->find('first', array(
  675. 'conditions' => array($scope, $Model->escapeField() => $id),
  676. 'fields' => array($Model->primaryKey, $left, $right, $parent),
  677. 'recursive' => $recursive
  678. )));
  679. if ($node[$right] == $node[$left] + 1) {
  680. if ($delete) {
  681. return $Model->delete($id);
  682. } else {
  683. $Model->id = $id;
  684. return $Model->saveField($parent, null);
  685. }
  686. } elseif ($node[$parent]) {
  687. list($parentNode) = array_values($Model->find('first', array(
  688. 'conditions' => array($scope, $Model->escapeField() => $node[$parent]),
  689. 'fields' => array($Model->primaryKey, $left, $right),
  690. 'recursive' => $recursive
  691. )));
  692. } else {
  693. $parentNode[$right] = $node[$right] + 1;
  694. }
  695. $db = ConnectionManager::getDataSource($Model->useDbConfig);
  696. $Model->updateAll(
  697. array($parent => $db->value($node[$parent], $parent)),
  698. array($Model->escapeField($parent) => $node[$Model->primaryKey])
  699. );
  700. $this->_sync($Model, 1, '-', 'BETWEEN ' . ($node[$left] + 1) . ' AND ' . ($node[$right] - 1));
  701. $this->_sync($Model, 2, '-', '> ' . ($node[$right]));
  702. $Model->id = $id;
  703. if ($delete) {
  704. $Model->updateAll(
  705. array(
  706. $Model->escapeField($left) => 0,
  707. $Model->escapeField($right) => 0,
  708. $Model->escapeField($parent) => null
  709. ),
  710. array($Model->escapeField() => $id)
  711. );
  712. return $Model->delete($id);
  713. } else {
  714. $edge = $this->_getMax($Model, $scope, $right, $recursive);
  715. if ($node[$right] == $edge) {
  716. $edge = $edge - 2;
  717. }
  718. $Model->id = $id;
  719. return $Model->save(
  720. array($left => $edge + 1, $right => $edge + 2, $parent => null),
  721. array('callbacks' => false, 'validate' => false)
  722. );
  723. }
  724. }
  725. /**
  726. * Check if the current tree is valid.
  727. *
  728. * Returns true if the tree is valid otherwise an array of (type, incorrect left/right index, message)
  729. *
  730. * @param Model $Model Model instance
  731. * @return mixed true if the tree is valid or empty, otherwise an array of (error type [index, node],
  732. * [incorrect left/right index,node id], message)
  733. * @link http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html#TreeBehavior::verify
  734. */
  735. public function verify(Model $Model) {
  736. extract($this->settings[$Model->alias]);
  737. if (!$Model->find('count', array('conditions' => $scope))) {
  738. return true;
  739. }
  740. $min = $this->_getMin($Model, $scope, $left, $recursive);
  741. $edge = $this->_getMax($Model, $scope, $right, $recursive);
  742. $errors = array();
  743. for ($i = $min; $i <= $edge; $i++) {
  744. $count = $Model->find('count', array('conditions' => array(
  745. $scope, 'OR' => array($Model->escapeField($left) => $i, $Model->escapeField($right) => $i)
  746. )));
  747. if ($count != 1) {
  748. if ($count == 0) {
  749. $errors[] = array('index', $i, 'missing');
  750. } else {
  751. $errors[] = array('index', $i, 'duplicate');
  752. }
  753. }
  754. }
  755. $node = $Model->find('first', array('conditions' => array($scope, $Model->escapeField($right) . '< ' . $Model->escapeField($left)), 'recursive' => 0));
  756. if ($node) {
  757. $errors[] = array('node', $node[$Model->alias][$Model->primaryKey], 'left greater than right.');
  758. }
  759. $Model->bindModel(array('belongsTo' => array('VerifyParent' => array(
  760. 'className' => $Model->name,
  761. 'foreignKey' => $parent,
  762. 'fields' => array($Model->primaryKey, $left, $right, $parent)
  763. ))));
  764. foreach ($Model->find('all', array('conditions' => $scope, 'recursive' => 0)) as $instance) {
  765. if (is_null($instance[$Model->alias][$left]) || is_null($instance[$Model->alias][$right])) {
  766. $errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
  767. 'has invalid left or right values');
  768. } elseif ($instance[$Model->alias][$left] == $instance[$Model->alias][$right]) {
  769. $errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
  770. 'left and right values identical');
  771. } elseif ($instance[$Model->alias][$parent]) {
  772. if (!$instance['VerifyParent'][$Model->primaryKey]) {
  773. $errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
  774. 'The parent node ' . $instance[$Model->alias][$parent] . ' doesn\'t exist');
  775. } elseif ($instance[$Model->alias][$left] < $instance['VerifyParent'][$left]) {
  776. $errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
  777. 'left less than parent (node ' . $instance['VerifyParent'][$Model->primaryKey] . ').');
  778. } elseif ($instance[$Model->alias][$right] > $instance['VerifyParent'][$right]) {
  779. $errors[] = array('node', $instance[$Model->alias][$Model->primaryKey],
  780. 'right greater than parent (node ' . $instance['VerifyParent'][$Model->primaryKey] . ').');
  781. }
  782. } elseif ($Model->find('count', array('conditions' => array($scope, $Model->escapeField($left) . ' <' => $instance[$Model->alias][$left], $Model->escapeField($right) . ' >' => $instance[$Model->alias][$right]), 'recursive' => 0))) {
  783. $errors[] = array('node', $instance[$Model->alias][$Model->primaryKey], 'The parent field is blank, but has a parent');
  784. }
  785. }
  786. if ($errors) {
  787. return $errors;
  788. }
  789. return true;
  790. }
  791. /**
  792. * Sets the parent of the given node
  793. *
  794. * The force parameter is used to override the "don't change the parent to the current parent" logic in the event
  795. * of recovering a corrupted table, or creating new nodes. Otherwise it should always be false. In reality this
  796. * method could be private, since calling save with parent_id set also calls setParent
  797. *
  798. * @param Model $Model Model instance
  799. * @param mixed $parentId
  800. * @param boolean $created
  801. * @return boolean true on success, false on failure
  802. */
  803. protected function _setParent(Model $Model, $parentId = null, $created = false) {
  804. extract($this->settings[$Model->alias]);
  805. list($node) = array_values($Model->find('first', array(
  806. 'conditions' => array($scope, $Model->escapeField() => $Model->id),
  807. 'fields' => array($Model->primaryKey, $parent, $left, $right),
  808. 'recursive' => $recursive
  809. )));
  810. $edge = $this->_getMax($Model, $scope, $right, $recursive, $created);
  811. if (empty ($parentId)) {
  812. $this->_sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
  813. $this->_sync($Model, $node[$right] - $node[$left] + 1, '-', '> ' . $node[$left], $created);
  814. } else {
  815. $values = $Model->find('first', array(
  816. 'conditions' => array($scope, $Model->escapeField() => $parentId),
  817. 'fields' => array($Model->primaryKey, $left, $right),
  818. 'recursive' => $recursive
  819. ));
  820. if ($values === false) {
  821. return false;
  822. }
  823. $parentNode = array_values($values);
  824. if (empty($parentNode) || empty($parentNode[0])) {
  825. return false;
  826. }
  827. $parentNode = $parentNode[0];
  828. if (($Model->id == $parentId)) {
  829. return false;
  830. } elseif (($node[$left] < $parentNode[$left]) && ($parentNode[$right] < $node[$right])) {
  831. return false;
  832. }
  833. if (empty($node[$left]) && empty($node[$right])) {
  834. $this->_sync($Model, 2, '+', '>= ' . $parentNode[$right], $created);
  835. $result = $Model->save(
  836. array($left => $parentNode[$right], $right => $parentNode[$right] + 1, $parent => $parentId),
  837. array('validate' => false, 'callbacks' => false)
  838. );
  839. $Model->data = $result;
  840. } else {
  841. $this->_sync($Model, $edge - $node[$left] + 1, '+', 'BETWEEN ' . $node[$left] . ' AND ' . $node[$right], $created);
  842. $diff = $node[$right] - $node[$left] + 1;
  843. if ($node[$left] > $parentNode[$left]) {
  844. if ($node[$right] < $parentNode[$right]) {
  845. $this->_sync($Model, $diff, '-', 'BETWEEN ' . $node[$right] . ' AND ' . ($parentNode[$right] - 1), $created);
  846. $this->_sync($Model, $edge - $parentNode[$right] + $diff + 1, '-', '> ' . $edge, $created);
  847. } else {
  848. $this->_sync($Model, $diff, '+', 'BETWEEN ' . $parentNode[$right] . ' AND ' . $node[$right], $created);
  849. $this->_sync($Model, $edge - $parentNode[$right] + 1, '-', '> ' . $edge, $created);
  850. }
  851. } else {
  852. $this->_sync($Model, $diff, '-', 'BETWEEN ' . $node[$right] . ' AND ' . ($parentNode[$right] - 1), $created);
  853. $this->_sync($Model, $edge - $parentNode[$right] + $diff + 1, '-', '> ' . $edge, $created);
  854. }
  855. }
  856. }
  857. return true;
  858. }
  859. /**
  860. * get the maximum index value in the table.
  861. *
  862. * @param Model $Model
  863. * @param string $scope
  864. * @param string $right
  865. * @param integer $recursive
  866. * @param boolean $created
  867. * @return integer
  868. */
  869. protected function _getMax(Model $Model, $scope, $right, $recursive = -1, $created = false) {
  870. $db = ConnectionManager::getDataSource($Model->useDbConfig);
  871. if ($created) {
  872. if (is_string($scope)) {
  873. $scope .= " AND {$Model->alias}.{$Model->primaryKey} <> ";
  874. $scope .= $db->value($Model->id, $Model->getColumnType($Model->primaryKey));
  875. } else {
  876. $scope['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
  877. }
  878. }
  879. $name = $Model->alias . '.' . $right;
  880. list($edge) = array_values($Model->find('first', array(
  881. 'conditions' => $scope,
  882. 'fields' => $db->calculate($Model, 'max', array($name, $right)),
  883. 'recursive' => $recursive
  884. )));
  885. return (empty($edge[$right])) ? 0 : $edge[$right];
  886. }
  887. /**
  888. * get the minimum index value in the table.
  889. *
  890. * @param Model $Model
  891. * @param string $scope
  892. * @param string $left
  893. * @param integer $recursive
  894. * @return integer
  895. */
  896. protected function _getMin(Model $Model, $scope, $left, $recursive = -1) {
  897. $db = ConnectionManager::getDataSource($Model->useDbConfig);
  898. $name = $Model->alias . '.' . $left;
  899. list($edge) = array_values($Model->find('first', array(
  900. 'conditions' => $scope,
  901. 'fields' => $db->calculate($Model, 'min', array($name, $left)),
  902. 'recursive' => $recursive
  903. )));
  904. return (empty($edge[$left])) ? 0 : $edge[$left];
  905. }
  906. /**
  907. * Table sync method.
  908. *
  909. * Handles table sync operations, Taking account of the behavior scope.
  910. *
  911. * @param Model $Model
  912. * @param integer $shift
  913. * @param string $dir
  914. * @param array $conditions
  915. * @param boolean $created
  916. * @param string $field
  917. * @return void
  918. */
  919. protected function _sync(Model $Model, $shift, $dir = '+', $conditions = array(), $created = false, $field = 'both') {
  920. $ModelRecursive = $Model->recursive;
  921. extract($this->settings[$Model->alias]);
  922. $Model->recursive = $recursive;
  923. if ($field == 'both') {
  924. $this->_sync($Model, $shift, $dir, $conditions, $created, $left);
  925. $field = $right;
  926. }
  927. if (is_string($conditions)) {
  928. $conditions = array("{$Model->alias}.{$field} {$conditions}");
  929. }
  930. if (($scope != '1 = 1' && $scope !== true) && $scope) {
  931. $conditions[] = $scope;
  932. }
  933. if ($created) {
  934. $conditions['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
  935. }
  936. $Model->updateAll(array($Model->alias . '.' . $field => $Model->escapeField($field) . ' ' . $dir . ' ' . $shift), $conditions);
  937. $Model->recursive = $ModelRecursive;
  938. }
  939. }