Tree.php 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB;
  7. use Magento\Framework\DB\Tree\Node;
  8. use Magento\Framework\DB\Tree\NodeSet;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Phrase;
  11. /**
  12. * Magento Library
  13. *
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. *
  16. * @deprecated 102.0.0 Not used anymore.
  17. */
  18. class Tree
  19. {
  20. /**
  21. * @var string|int
  22. */
  23. private $_id;
  24. /**
  25. * @var int
  26. */
  27. private $_left;
  28. /**
  29. * @var int
  30. */
  31. private $_right;
  32. /**
  33. * @var int
  34. */
  35. private $_level;
  36. /**
  37. * @var int
  38. */
  39. private $_pid;
  40. /**
  41. * @var array
  42. */
  43. private $_nodesInfo = [];
  44. /**
  45. * Array of additional tables
  46. *
  47. * array(
  48. * [$tableName] => array(
  49. * ['joinCondition']
  50. * ['fields']
  51. * )
  52. * )
  53. *
  54. * @var array
  55. */
  56. private $_extTables = [];
  57. /**
  58. * @var \Magento\Framework\DB\Adapter\AdapterInterface
  59. */
  60. private $_db;
  61. /**
  62. * @var string
  63. */
  64. private $_table;
  65. /**
  66. * @param array $config
  67. * @throws LocalizedException
  68. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  69. * @SuppressWarnings(PHPMD.NPathComplexity)
  70. *
  71. * @deprecated 102.0.0 Not used anymore.
  72. */
  73. public function __construct($config = [])
  74. {
  75. // set a \Zend_Db_Adapter connection
  76. if (!empty($config['db'])) {
  77. // convenience variable
  78. $connection = $config['db'];
  79. // use an object from the registry?
  80. if (is_string($connection)) {
  81. $connection = \Zend::registry($connection);
  82. }
  83. // make sure it's a \Magento\Framework\DB\Adapter\AdapterInterface
  84. if (!$connection instanceof \Magento\Framework\DB\Adapter\AdapterInterface) {
  85. throw new LocalizedException(
  86. new Phrase('db object does not implement \Magento\Framework\DB\Adapter\AdapterInterface')
  87. );
  88. }
  89. // save the connection
  90. $this->_db = $connection;
  91. $conn = $this->_db->getConnection();
  92. if ($conn instanceof \PDO) {
  93. $conn->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
  94. }
  95. } else {
  96. throw new LocalizedException(
  97. new Phrase('The "db object" isn\'t set in config. Set the "db object" and try again.')
  98. );
  99. }
  100. if (!empty($config['table'])) {
  101. $this->setTable($config['table']);
  102. }
  103. if (!empty($config['id'])) {
  104. $this->setIdField($config['id']);
  105. } else {
  106. $this->setIdField('id');
  107. }
  108. if (!empty($config['left'])) {
  109. $this->setLeftField($config['left']);
  110. } else {
  111. $this->setLeftField('left_key');
  112. }
  113. if (!empty($config['right'])) {
  114. $this->setRightField($config['right']);
  115. } else {
  116. $this->setRightField('right_key');
  117. }
  118. if (!empty($config['level'])) {
  119. $this->setLevelField($config['level']);
  120. } else {
  121. $this->setLevelField('level');
  122. }
  123. if (!empty($config['pid'])) {
  124. $this->setPidField($config['pid']);
  125. } else {
  126. $this->setPidField('parent_id');
  127. }
  128. }
  129. /**
  130. * set name of id field
  131. *
  132. * @param string $name
  133. * @return $this
  134. *
  135. * @deprecated 102.0.0 Not used anymore.
  136. */
  137. public function setIdField($name)
  138. {
  139. $this->_id = $name;
  140. return $this;
  141. }
  142. /**
  143. * set name of left field
  144. *
  145. * @param string $name
  146. * @return $this
  147. *
  148. * @deprecated 102.0.0 Not used anymore.
  149. */
  150. public function setLeftField($name)
  151. {
  152. $this->_left = $name;
  153. return $this;
  154. }
  155. /**
  156. * set name of right field
  157. *
  158. * @param string $name
  159. * @return $this
  160. *
  161. * @deprecated 102.0.0 Not used anymore.
  162. */
  163. public function setRightField($name)
  164. {
  165. $this->_right = $name;
  166. return $this;
  167. }
  168. /**
  169. * set name of level field
  170. *
  171. * @param string $name
  172. * @return $this
  173. *
  174. * @deprecated 102.0.0 Not used anymore.
  175. */
  176. public function setLevelField($name)
  177. {
  178. $this->_level = $name;
  179. return $this;
  180. }
  181. /**
  182. * set name of pid Field
  183. *
  184. * @param string $name
  185. * @return $this
  186. *
  187. * @deprecated 102.0.0 Not used anymore.
  188. */
  189. public function setPidField($name)
  190. {
  191. $this->_pid = $name;
  192. return $this;
  193. }
  194. /**
  195. * set table name
  196. *
  197. * @param string $name
  198. * @return $this
  199. *
  200. * @deprecated 102.0.0 Not used anymore.
  201. */
  202. public function setTable($name)
  203. {
  204. $this->_table = $name;
  205. return $this;
  206. }
  207. /**
  208. * @return array
  209. *
  210. * @deprecated 102.0.0 Not used anymore.
  211. */
  212. public function getKeys()
  213. {
  214. $keys = [];
  215. $keys['id'] = $this->_id;
  216. $keys['left'] = $this->_left;
  217. $keys['right'] = $this->_right;
  218. $keys['pid'] = $this->_pid;
  219. $keys['level'] = $this->_level;
  220. return $keys;
  221. }
  222. /**
  223. * Clear table and add root element
  224. *
  225. * @param array $data
  226. * @return string
  227. *
  228. * @deprecated 102.0.0 Not used anymore.
  229. */
  230. public function clear($data = [])
  231. {
  232. // clearing table
  233. $this->_db->query('TRUNCATE ' . $this->_table);
  234. // prepare data for root element
  235. $data[$this->_pid] = 0;
  236. $data[$this->_left] = 1;
  237. $data[$this->_right] = 2;
  238. $data[$this->_level] = 0;
  239. try {
  240. $this->_db->insert($this->_table, $data);
  241. } catch (\PDOException $e) {
  242. echo $e->getMessage();
  243. }
  244. return $this->_db->lastInsertId();
  245. }
  246. /**
  247. * Get node information
  248. *
  249. * @param string|int $nodeId
  250. * @return array
  251. *
  252. * @deprecated 102.0.0 Not used anymore.
  253. */
  254. public function getNodeInfo($nodeId)
  255. {
  256. if (empty($this->_nodesInfo[$nodeId])) {
  257. $sql = 'SELECT * FROM ' . $this->_table . ' WHERE ' . $this->_id . '=:id';
  258. $res = $this->_db->query($sql, ['id' => $nodeId]);
  259. $data = $res->fetch();
  260. $this->_nodesInfo[$nodeId] = $data;
  261. } else {
  262. $data = $this->_nodesInfo[$nodeId];
  263. }
  264. return $data;
  265. }
  266. /**
  267. * @param string|int $nodeId
  268. * @param array $data
  269. * @return false|string
  270. * @SuppressWarnings(PHPMD.ExitExpression)
  271. *
  272. * @deprecated 102.0.0 Not used anymore.
  273. */
  274. public function appendChild($nodeId, $data)
  275. {
  276. $info = $this->getNodeInfo($nodeId);
  277. if (!$info) {
  278. return false;
  279. }
  280. $data[$this->_left] = $info[$this->_right];
  281. $data[$this->_right] = $info[$this->_right] + 1;
  282. $data[$this->_level] = $info[$this->_level] + 1;
  283. $data[$this->_pid] = $nodeId;
  284. // creating a place for the record being inserted
  285. if ($nodeId) {
  286. $this->_db->beginTransaction();
  287. try {
  288. $sql = 'UPDATE ' .
  289. $this->_table .
  290. ' SET' .
  291. ' `' .
  292. $this->_left .
  293. '` = IF( `' .
  294. $this->_left .
  295. '` > :left,' .
  296. ' `' .
  297. $this->_left .
  298. '`+2, `' .
  299. $this->_left .
  300. '`),' .
  301. ' `' .
  302. $this->_right .
  303. '` = IF( `' .
  304. $this->_right .
  305. '`>= :right,' .
  306. ' `' .
  307. $this->_right .
  308. '`+2, `' .
  309. $this->_right .
  310. '`)' .
  311. ' WHERE `' .
  312. $this->_right .
  313. '` >= :right';
  314. $this->_db->query($sql, ['left' => $info[$this->_left], 'right' => $info[$this->_right]]);
  315. $this->_db->insert($this->_table, $data);
  316. $this->_db->commit();
  317. } catch (\PDOException $p) {
  318. $this->_db->rollBack();
  319. echo $p->getMessage();
  320. exit;
  321. } catch (\Exception $e) {
  322. $this->_db->rollBack();
  323. echo $e->getMessage();
  324. echo $sql;
  325. exit;
  326. }
  327. // TODO: change to ZEND LIBRARY
  328. $res = $this->_db->fetchOne('select last_insert_id()');
  329. return $res;
  330. }
  331. return false;
  332. }
  333. /**
  334. * @return array
  335. *
  336. * @deprecated 102.0.0 Not used anymore.
  337. */
  338. public function checkNodes()
  339. {
  340. $sql = $this->_db->select();
  341. $sql->from(
  342. ['t1' => $this->_table],
  343. ['t1.' . $this->_id, new \Zend_Db_Expr('COUNT(t1.' . $this->_id . ') AS rep')]
  344. )->from(
  345. ['t2' => $this->_table]
  346. )->from(
  347. ['t3' => $this->_table],
  348. new \Zend_Db_Expr('MAX(t3.' . $this->_right . ') AS max_right')
  349. );
  350. $sql->where(
  351. 't1.' . $this->_left . ' <> t2.' . $this->_left
  352. )->where(
  353. 't1.' . $this->_left . ' <> t2.' . $this->_right
  354. )->where(
  355. 't1.' . $this->_right . ' <> t2.' . $this->_right
  356. );
  357. $sql->group('t1.' . $this->_id);
  358. $sql->having('max_right <> SQRT(4 * rep + 1) + 1');
  359. return $this->_db->fetchAll($sql);
  360. }
  361. /**
  362. * @param string|int $nodeId
  363. * @return bool|Node|void
  364. *
  365. * @deprecated 102.0.0 Not used anymore.
  366. */
  367. public function removeNode($nodeId)
  368. {
  369. $info = $this->getNodeInfo($nodeId);
  370. if (!$info) {
  371. return false;
  372. }
  373. if ($nodeId) {
  374. $this->_db->beginTransaction();
  375. try {
  376. /**
  377. * DELETE FROM my_tree WHERE left_key >= $left_key AND right_key <= $right_key
  378. */
  379. $this->_db->delete(
  380. $this->_table,
  381. $this->_left .
  382. ' >= ' .
  383. $info[$this->_left] .
  384. ' AND ' .
  385. $this->_right .
  386. ' <= ' .
  387. $info[$this->_right]
  388. );
  389. /**
  390. * UPDATE my_tree SET left_key = IF(left_key > $left_key, left_key – ($right_key - $left_key + 1),
  391. * left_key), right_key = right_key – ($right_key - $left_key + 1) WHERE right_key > $right_key
  392. */
  393. $sql = 'UPDATE ' .
  394. $this->_table .
  395. ' SET ' .
  396. $this->_left .
  397. ' = IF(' .
  398. $this->_left .
  399. ' > ' .
  400. $info[$this->_left] .
  401. ', ' .
  402. $this->_left .
  403. ' - ' .
  404. ($info[$this->_right] -
  405. $info[$this->_left] +
  406. 1) .
  407. ', ' .
  408. $this->_left .
  409. '), ' .
  410. $this->_right .
  411. ' = ' .
  412. $this->_right .
  413. ' - ' .
  414. ($info[$this->_right] -
  415. $info[$this->_left] +
  416. 1) .
  417. ' WHERE ' .
  418. $this->_right .
  419. ' > ' .
  420. $info[$this->_right];
  421. $this->_db->query($sql);
  422. $this->_db->commit();
  423. return new Node($info, $this->getKeys());
  424. } catch (\Exception $e) {
  425. $this->_db->rollBack();
  426. echo $e->getMessage();
  427. }
  428. }
  429. }
  430. /**
  431. * @param string|int $eId
  432. * @param string|int $pId
  433. * @param string|int $aId
  434. * @return bool
  435. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  436. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  437. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  438. * @SuppressWarnings(PHPMD.ExitExpression)
  439. *
  440. * @deprecated 102.0.0 Not used anymore.
  441. */
  442. public function moveNode($eId, $pId, $aId = 0)
  443. {
  444. $eInfo = $this->getNodeInfo($eId);
  445. $pInfo = $this->getNodeInfo($pId);
  446. $leftId = $eInfo[$this->_left];
  447. $rightId = $eInfo[$this->_right];
  448. $level = $eInfo[$this->_level];
  449. $leftIdP = $pInfo[$this->_left];
  450. $rightIdP = $pInfo[$this->_right];
  451. $levelP = $pInfo[$this->_level];
  452. if ($eId == $pId ||
  453. $leftId == $leftIdP ||
  454. $leftIdP >= $leftId && $leftIdP <= $rightId ||
  455. $level == $levelP + 1 && $leftId > $leftIdP && $rightId < $rightIdP
  456. ) {
  457. echo "alert('cant_move_tree');";
  458. return false;
  459. }
  460. if ($leftIdP < $leftId && $rightIdP > $rightId && $levelP < $level - 1) {
  461. $sql = 'UPDATE ' .
  462. $this->_table .
  463. ' SET ' .
  464. $this->_level .
  465. ' = CASE WHEN ' .
  466. $this->_left .
  467. ' BETWEEN ' .
  468. $leftId .
  469. ' AND ' .
  470. $rightId .
  471. ' THEN ' .
  472. $this->_level .
  473. sprintf(
  474. '%+d',
  475. -($level - 1) + $levelP
  476. ) .
  477. ' ELSE ' .
  478. $this->_level .
  479. ' END, ' .
  480. $this->_right .
  481. ' = CASE WHEN ' .
  482. $this->_right .
  483. ' BETWEEN ' .
  484. ($rightId +
  485. 1) .
  486. ' AND ' .
  487. ($rightIdP -
  488. 1) .
  489. ' THEN ' .
  490. $this->_right .
  491. '-' .
  492. ($rightId -
  493. $leftId +
  494. 1) .
  495. ' ' .
  496. 'WHEN ' .
  497. $this->_left .
  498. ' BETWEEN ' .
  499. $leftId .
  500. ' AND ' .
  501. $rightId .
  502. ' THEN ' .
  503. $this->_right .
  504. '+' .
  505. (($rightIdP -
  506. $rightId -
  507. $level +
  508. $levelP) / 2 * 2 +
  509. $level -
  510. $levelP -
  511. 1) .
  512. ' ELSE ' .
  513. $this->_right .
  514. ' END, ' .
  515. $this->_left .
  516. ' = CASE WHEN ' .
  517. $this->_left .
  518. ' BETWEEN ' .
  519. ($rightId +
  520. 1) .
  521. ' AND ' .
  522. ($rightIdP -
  523. 1) .
  524. ' THEN ' .
  525. $this->_left .
  526. '-' .
  527. ($rightId -
  528. $leftId +
  529. 1) .
  530. ' ' .
  531. 'WHEN ' .
  532. $this->_left .
  533. ' BETWEEN ' .
  534. $leftId .
  535. ' AND ' .
  536. $rightId .
  537. ' THEN ' .
  538. $this->_left .
  539. '+' .
  540. (($rightIdP -
  541. $rightId -
  542. $level +
  543. $levelP) / 2 * 2 +
  544. $level -
  545. $levelP -
  546. 1) .
  547. ' ELSE ' .
  548. $this->_left .
  549. ' END ' .
  550. 'WHERE ' .
  551. $this->_left .
  552. ' BETWEEN ' .
  553. ($leftIdP +
  554. 1) .
  555. ' AND ' .
  556. ($rightIdP -
  557. 1);
  558. } elseif ($leftIdP < $leftId) {
  559. $sql = 'UPDATE ' .
  560. $this->_table .
  561. ' SET ' .
  562. $this->_level .
  563. ' = CASE WHEN ' .
  564. $this->_left .
  565. ' BETWEEN ' .
  566. $leftId .
  567. ' AND ' .
  568. $rightId .
  569. ' THEN ' .
  570. $this->_level .
  571. sprintf(
  572. '%+d',
  573. -($level - 1) + $levelP
  574. ) .
  575. ' ELSE ' .
  576. $this->_level .
  577. ' END, ' .
  578. $this->_left .
  579. ' = CASE WHEN ' .
  580. $this->_left .
  581. ' BETWEEN ' .
  582. $rightIdP .
  583. ' AND ' .
  584. ($leftId -
  585. 1) .
  586. ' THEN ' .
  587. $this->_left .
  588. '+' .
  589. ($rightId -
  590. $leftId +
  591. 1) .
  592. ' ' .
  593. 'WHEN ' .
  594. $this->_left .
  595. ' BETWEEN ' .
  596. $leftId .
  597. ' AND ' .
  598. $rightId .
  599. ' THEN ' .
  600. $this->_left .
  601. '-' .
  602. ($leftId -
  603. $rightIdP) .
  604. ' ELSE ' .
  605. $this->_left .
  606. ' END, ' .
  607. $this->_right .
  608. ' = CASE WHEN ' .
  609. $this->_right .
  610. ' BETWEEN ' .
  611. $rightIdP .
  612. ' AND ' .
  613. $leftId .
  614. ' THEN ' .
  615. $this->_right .
  616. '+' .
  617. ($rightId -
  618. $leftId +
  619. 1) .
  620. ' ' .
  621. 'WHEN ' .
  622. $this->_right .
  623. ' BETWEEN ' .
  624. $leftId .
  625. ' AND ' .
  626. $rightId .
  627. ' THEN ' .
  628. $this->_right .
  629. '-' .
  630. ($leftId -
  631. $rightIdP) .
  632. ' ELSE ' .
  633. $this->_right .
  634. ' END ' .
  635. 'WHERE (' .
  636. $this->_left .
  637. ' BETWEEN ' .
  638. $leftIdP .
  639. ' AND ' .
  640. $rightId .
  641. ' ' .
  642. 'OR ' .
  643. $this->_right .
  644. ' BETWEEN ' .
  645. $leftIdP .
  646. ' AND ' .
  647. $rightId .
  648. ')';
  649. } else {
  650. $sql = 'UPDATE ' .
  651. $this->_table .
  652. ' SET ' .
  653. $this->_level .
  654. ' = CASE WHEN ' .
  655. $this->_left .
  656. ' BETWEEN ' .
  657. $leftId .
  658. ' AND ' .
  659. $rightId .
  660. ' THEN ' .
  661. $this->_level .
  662. sprintf(
  663. '%+d',
  664. -($level - 1) + $levelP
  665. ) .
  666. ' ELSE ' .
  667. $this->_level .
  668. ' END, ' .
  669. $this->_left .
  670. ' = CASE WHEN ' .
  671. $this->_left .
  672. ' BETWEEN ' .
  673. $rightId .
  674. ' AND ' .
  675. $rightIdP .
  676. ' THEN ' .
  677. $this->_left .
  678. '-' .
  679. ($rightId -
  680. $leftId +
  681. 1) .
  682. ' ' .
  683. 'WHEN ' .
  684. $this->_left .
  685. ' BETWEEN ' .
  686. $leftId .
  687. ' AND ' .
  688. $rightId .
  689. ' THEN ' .
  690. $this->_left .
  691. '+' .
  692. ($rightIdP -
  693. 1 -
  694. $rightId) .
  695. ' ELSE ' .
  696. $this->_left .
  697. ' END, ' .
  698. $this->_right .
  699. ' = CASE WHEN ' .
  700. $this->_right .
  701. ' BETWEEN ' .
  702. ($rightId +
  703. 1) .
  704. ' AND ' .
  705. ($rightIdP -
  706. 1) .
  707. ' THEN ' .
  708. $this->_right .
  709. '-' .
  710. ($rightId -
  711. $leftId +
  712. 1) .
  713. ' ' .
  714. 'WHEN ' .
  715. $this->_right .
  716. ' BETWEEN ' .
  717. $leftId .
  718. ' AND ' .
  719. $rightId .
  720. ' THEN ' .
  721. $this->_right .
  722. '+' .
  723. ($rightIdP -
  724. 1 -
  725. $rightId) .
  726. ' ELSE ' .
  727. $this->_right .
  728. ' END ' .
  729. 'WHERE (' .
  730. $this->_left .
  731. ' BETWEEN ' .
  732. $leftId .
  733. ' AND ' .
  734. $rightIdP .
  735. ' ' .
  736. 'OR ' .
  737. $this->_right .
  738. ' BETWEEN ' .
  739. $leftId .
  740. ' AND ' .
  741. $rightIdP .
  742. ')';
  743. }
  744. $this->_db->beginTransaction();
  745. try {
  746. $this->_db->query($sql);
  747. $this->_db->commit();
  748. echo "alert('node moved');";
  749. return true;
  750. } catch (\Exception $e) {
  751. $this->_db->rollBack();
  752. echo "alert('node not moved: fatal error');";
  753. echo $e->getMessage();
  754. echo "<br>\r\n";
  755. echo $sql;
  756. echo "<br>\r\n";
  757. exit;
  758. }
  759. }
  760. /**
  761. * @param string|int $eId
  762. * @param string|int $pId
  763. * @param string|int $aId
  764. * @return void
  765. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  766. * @SuppressWarnings(PHPMD.NPathComplexity)
  767. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  768. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  769. * @SuppressWarnings(PHPMD.ExitExpression)
  770. *
  771. * @deprecated 102.0.0 Not used anymore.
  772. */
  773. public function moveNodes($eId, $pId, $aId = 0)
  774. {
  775. $eInfo = $this->getNodeInfo($eId);
  776. if ($pId != 0) {
  777. $pInfo = $this->getNodeInfo($pId);
  778. }
  779. if ($aId != 0) {
  780. $aInfo = $this->getNodeInfo($aId);
  781. }
  782. $level = $eInfo[$this->_level];
  783. $leftKey = $eInfo[$this->_left];
  784. $rightKey = $eInfo[$this->_right];
  785. if ($pId == 0) {
  786. $levelUp = 0;
  787. } else {
  788. $levelUp = $pInfo[$this->_level];
  789. }
  790. $rightKeyNear = 0;
  791. $leftKeyNear = 0;
  792. if ($pId == 0) {
  793. //move to root
  794. $rightKeyNear = $this->_db->fetchOne('SELECT MAX(' . $this->_right . ') FROM ' . $this->_table);
  795. } elseif ($aId != 0 && $pId == $eInfo[$this->_pid]) {
  796. // if we have after ID
  797. $rightKeyNear = $aInfo[$this->_right];
  798. $leftKeyNear = $aInfo[$this->_left];
  799. } elseif ($aId == 0 && $pId == $eInfo[$this->_pid]) {
  800. // if we do not have after ID
  801. $rightKeyNear = $pInfo[$this->_left];
  802. } elseif ($pId != $eInfo[$this->_pid]) {
  803. $rightKeyNear = $pInfo[$this->_right] - 1;
  804. }
  805. $skewLevel = $pInfo[$this->_level] - $eInfo[$this->_level] + 1;
  806. $skewTree = $eInfo[$this->_right] - $eInfo[$this->_left] + 1;
  807. echo "alert('" . $rightKeyNear . "');";
  808. if ($rightKeyNear > $rightKey) {
  809. // up
  810. echo "alert('move up');";
  811. $skewEdit = $rightKeyNear - $leftKey + 1;
  812. $sql = 'UPDATE ' .
  813. $this->_table .
  814. ' SET ' .
  815. $this->_right .
  816. ' = IF(' .
  817. $this->_left .
  818. ' >= ' .
  819. $eInfo[$this->_left] .
  820. ', ' .
  821. $this->_right .
  822. ' + ' .
  823. $skewEdit .
  824. ', IF(' .
  825. $this->_right .
  826. ' < ' .
  827. $eInfo[$this->_left] .
  828. ', ' .
  829. $this->_right .
  830. ' + ' .
  831. $skewTree .
  832. ', ' .
  833. $this->_right .
  834. ')), ' .
  835. $this->_level .
  836. ' = IF(' .
  837. $this->_left .
  838. ' >= ' .
  839. $eInfo[$this->_left] .
  840. ', ' .
  841. $this->_level .
  842. ' + ' .
  843. $skewLevel .
  844. ', ' .
  845. $this->_level .
  846. '), ' .
  847. $this->_left .
  848. ' = IF(' .
  849. $this->_left .
  850. ' >= ' .
  851. $eInfo[$this->_left] .
  852. ', ' .
  853. $this->_left .
  854. ' + ' .
  855. $skewEdit .
  856. ', IF(' .
  857. $this->_left .
  858. ' > ' .
  859. $rightKeyNear .
  860. ', ' .
  861. $this->_left .
  862. ' + ' .
  863. $skewTree .
  864. ', ' .
  865. $this->_left .
  866. '))' .
  867. ' WHERE ' .
  868. $this->_right .
  869. ' > ' .
  870. $rightKeyNear .
  871. ' AND ' .
  872. $this->_left .
  873. ' < ' .
  874. $eInfo[$this->_right];
  875. } elseif ($rightKeyNear < $rightKey) {
  876. // down
  877. echo "alert('move down');";
  878. $skewEdit = $rightKeyNear - $leftKey + 1 - $skewTree;
  879. $sql = 'UPDATE ' .
  880. $this->_table .
  881. ' SET ' .
  882. $this->_left .
  883. ' = IF(' .
  884. $this->_right .
  885. ' <= ' .
  886. $rightKey .
  887. ', ' .
  888. $this->_left .
  889. ' + ' .
  890. $skewEdit .
  891. ', IF(' .
  892. $this->_left .
  893. ' > ' .
  894. $rightKey .
  895. ', ' .
  896. $this->_left .
  897. ' - ' .
  898. $skewTree .
  899. ', ' .
  900. $this->_left .
  901. ')), ' .
  902. $this->_level .
  903. ' = IF(' .
  904. $this->_right .
  905. ' <= ' .
  906. $rightKey .
  907. ', ' .
  908. $this->_level .
  909. ' + ' .
  910. $skewLevel .
  911. ', ' .
  912. $this->_level .
  913. '), ' .
  914. $this->_right .
  915. ' = IF(' .
  916. $this->_right .
  917. ' <= ' .
  918. $rightKey .
  919. ', ' .
  920. $this->_right .
  921. ' + ' .
  922. $skewEdit .
  923. ', IF(' .
  924. $this->_right .
  925. ' <= ' .
  926. $rightKeyNear .
  927. ', ' .
  928. $this->_right .
  929. ' - ' .
  930. $skewTree .
  931. ', ' .
  932. $this->_right .
  933. '))' .
  934. ' WHERE ' .
  935. $this->_right .
  936. ' > ' .
  937. $leftKey .
  938. ' AND ' .
  939. $this->_left .
  940. ' <= ' .
  941. $rightKeyNear;
  942. }
  943. $this->_db->beginTransaction();
  944. try {
  945. $this->_db->query($sql);
  946. $this->_db->commit();
  947. } catch (\Exception $e) {
  948. $this->_db->rollBack();
  949. echo $e->getMessage();
  950. echo "<br>\r\n";
  951. echo $sql;
  952. echo "<br>\r\n";
  953. exit;
  954. }
  955. echo "alert('node added')";
  956. }
  957. /**
  958. * @param string $tableName
  959. * @param string $joinCondition
  960. * @param string $fields
  961. * @return void
  962. *
  963. * @deprecated 102.0.0 Not used anymore.
  964. */
  965. public function addTable($tableName, $joinCondition, $fields = '*')
  966. {
  967. $this->_extTables[$tableName] = ['joinCondition' => $joinCondition, 'fields' => $fields];
  968. }
  969. /**
  970. * @param Select $select
  971. * @return void
  972. *
  973. * @deprecated 102.0.0 Not used anymore.
  974. */
  975. protected function _addExtTablesToSelect(Select &$select)
  976. {
  977. foreach ($this->_extTables as $tableName => $info) {
  978. $select->joinInner($tableName, $info['joinCondition'], $info['fields']);
  979. }
  980. }
  981. /**
  982. * @param string|int $nodeId
  983. * @param int $startLevel
  984. * @param int $endLevel
  985. * @return NodeSet
  986. * @SuppressWarnings(PHPMD.ExitExpression)
  987. *
  988. * @deprecated 102.0.0 Not used anymore.
  989. */
  990. public function getChildren($nodeId, $startLevel = 0, $endLevel = 0)
  991. {
  992. try {
  993. $info = $this->getNodeInfo($nodeId);
  994. } catch (\Exception $e) {
  995. echo $e->getMessage();
  996. exit;
  997. }
  998. $dbSelect = new Select($this->_db);
  999. $dbSelect->from(
  1000. $this->_table
  1001. )->where(
  1002. $this->_left . ' >= :left'
  1003. )->where(
  1004. $this->_right . ' <= :right'
  1005. )->order(
  1006. $this->_left
  1007. );
  1008. $this->_addExtTablesToSelect($dbSelect);
  1009. $data = [];
  1010. $data['left'] = $info[$this->_left];
  1011. $data['right'] = $info[$this->_right];
  1012. if (!empty($startLevel) && empty($endLevel)) {
  1013. $dbSelect->where($this->_level . ' = :minLevel');
  1014. $data['minLevel'] = $info[$this->_level] + $startLevel;
  1015. }
  1016. //echo $dbSelect->__toString();
  1017. $data = $this->_db->fetchAll($dbSelect, $data);
  1018. $nodeSet = new NodeSet();
  1019. foreach ($data as $node) {
  1020. $nodeSet->addNode(new Node($node, $this->getKeys()));
  1021. }
  1022. return $nodeSet;
  1023. }
  1024. /**
  1025. * @param string|int $nodeId
  1026. * @return Node
  1027. *
  1028. * @deprecated 102.0.0 Not used anymore.
  1029. */
  1030. public function getNode($nodeId)
  1031. {
  1032. $dbSelect = new Select($this->_db);
  1033. $dbSelect->from($this->_table)->where($this->_table . '.' . $this->_id . ' >= :id');
  1034. $this->_addExtTablesToSelect($dbSelect);
  1035. $data = [];
  1036. $data['id'] = $nodeId;
  1037. $data = $this->_db->fetchRow($dbSelect, $data);
  1038. return new Node($data, $this->getKeys());
  1039. }
  1040. }