PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/testSource/phpcms/content/component/jstree/_inc/class.tree.php

https://gitlab.com/loda.sun.suryani/qijiatuku
PHP | 417 lines | 381 code | 26 blank | 10 comment | 54 complexity | 232b38b9f4a6acb50995c0e4f6a91bcb MD5 | raw file
  1. <?php
  2. class _tree_struct {
  3. // Structure table and fields
  4. protected $table = "";
  5. protected $fields = array(
  6. "id" => false,
  7. "parent_id" => false,
  8. "position" => false,
  9. "left" => false,
  10. "right" => false,
  11. "level" => false
  12. );
  13. // Constructor
  14. function __construct($table = "cms_tree", $fields = array()) {
  15. $this->table = $table;
  16. if(!count($fields)) {
  17. foreach($this->fields as $k => &$v) { $v = $k; }
  18. }
  19. else {
  20. foreach($fields as $key => $field) {
  21. switch($key) {
  22. case "id":
  23. case "parent_id":
  24. case "position":
  25. case "left":
  26. case "right":
  27. case "level":
  28. $this->fields[$key] = $field;
  29. break;
  30. }
  31. }
  32. }
  33. // Database
  34. $this->db = new _database;
  35. }
  36. function _get_node($id) {
  37. $this->db->query("SELECT `".implode("` , `", $this->fields)."` FROM `".$this->table."` WHERE `".$this->fields["id"]."` = ".(int) $id);
  38. $this->db->nextr();
  39. return $this->db->nf() === 0 ? false : $this->db->get_row("assoc");
  40. }
  41. function _get_children($id, $recursive = false) {
  42. $children = array();
  43. if($recursive) {
  44. $node = $this->_get_node($id);
  45. $this->db->query("SELECT `".implode("` , `", $this->fields)."` FROM `".$this->table."` WHERE `".$this->fields["left"]."` >= ".(int) $node[$this->fields["left"]]." AND `".$this->fields["right"]."` <= ".(int) $node[$this->fields["right"]]." ORDER BY `".$this->fields["left"]."` ASC");
  46. }
  47. else {
  48. $this->db->query("SELECT `".implode("` , `", $this->fields)."` FROM `".$this->table."` WHERE `".$this->fields["parent_id"]."` = ".(int) $id." ORDER BY `".$this->fields["position"]."` ASC");
  49. }
  50. while($this->db->nextr()) $children[$this->db->f($this->fields["id"])] = $this->db->get_row("assoc");
  51. return $children;
  52. }
  53. function _get_path($id) {
  54. $node = $this->_get_node($id);
  55. $path = array();
  56. if(!$node === false) return false;
  57. $this->db->query("SELECT `".implode("` , `", $this->fields)."` FROM `".$this->table."` WHERE `".$this->fields["left"]."` <= ".(int) $node[$this->fields["left"]]." AND `".$this->fields["right"]."` >= ".(int) $node[$this->fields["right"]]);
  58. while($this->db->nextr()) $path[$this->db->f($this->fields["id"])] = $this->db->get_row("assoc");
  59. return $path;
  60. }
  61. function _create($parent, $position) {
  62. return $this->_move(0, $parent, $position);
  63. }
  64. function _remove($id) {
  65. if((int)$id === 1) { return false; }
  66. $data = $this->_get_node($id);
  67. $lft = (int)$data[$this->fields["left"]];
  68. $rgt = (int)$data[$this->fields["right"]];
  69. $dif = $rgt - $lft + 1;
  70. // deleting node and its children
  71. $this->db->query("" .
  72. "DELETE FROM `".$this->table."` " .
  73. "WHERE `".$this->fields["left"]."` >= ".$lft." AND `".$this->fields["right"]."` <= ".$rgt
  74. );
  75. // shift left indexes of nodes right of the node
  76. $this->db->query("".
  77. "UPDATE `".$this->table."` " .
  78. "SET `".$this->fields["left"]."` = `".$this->fields["left"]."` - ".$dif." " .
  79. "WHERE `".$this->fields["left"]."` > ".$rgt
  80. );
  81. // shift right indexes of nodes right of the node and the node's parents
  82. $this->db->query("" .
  83. "UPDATE `".$this->table."` " .
  84. "SET `".$this->fields["right"]."` = `".$this->fields["right"]."` - ".$dif." " .
  85. "WHERE `".$this->fields["right"]."` > ".$lft
  86. );
  87. $pid = (int)$data[$this->fields["parent_id"]];
  88. $pos = (int)$data[$this->fields["position"]];
  89. // Update position of siblings below the deleted node
  90. $this->db->query("" .
  91. "UPDATE `".$this->table."` " .
  92. "SET `".$this->fields["position"]."` = `".$this->fields["position"]."` - 1 " .
  93. "WHERE `".$this->fields["parent_id"]."` = ".$pid." AND `".$this->fields["position"]."` > ".$pos
  94. );
  95. return true;
  96. }
  97. function _move($id, $ref_id, $position = 0, $is_copy = false) {
  98. if((int)$ref_id === 0 || (int)$id === 1) { return false; }
  99. $sql = array(); // Queries executed at the end
  100. $node = $this->_get_node($id); // Node data
  101. $nchildren = $this->_get_children($id); // Node children
  102. $ref_node = $this->_get_node($ref_id); // Ref node data
  103. $rchildren = $this->_get_children($ref_id);// Ref node children
  104. $ndif = 2;
  105. $node_ids = array(-1);
  106. if($node !== false) {
  107. $node_ids = array_keys($this->_get_children($id, true));
  108. // TODO: should be !$is_copy && , but if copied to self - screws some right indexes
  109. if(in_array($ref_id, $node_ids)) return false;
  110. $ndif = $node[$this->fields["right"]] - $node[$this->fields["left"]] + 1;
  111. }
  112. if($position >= count($rchildren)) {
  113. $position = count($rchildren);
  114. }
  115. // Not creating or copying - old parent is cleaned
  116. if($node !== false && $is_copy == false) {
  117. $sql[] = "" .
  118. "UPDATE `".$this->table."` " .
  119. "SET `".$this->fields["position"]."` = `".$this->fields["position"]."` - 1 " .
  120. "WHERE " .
  121. "`".$this->fields["parent_id"]."` = ".$node[$this->fields["parent_id"]]." AND " .
  122. "`".$this->fields["position"]."` > ".$node[$this->fields["position"]];
  123. $sql[] = "" .
  124. "UPDATE `".$this->table."` " .
  125. "SET `".$this->fields["left"]."` = `".$this->fields["left"]."` - ".$ndif." " .
  126. "WHERE `".$this->fields["left"]."` > ".$node[$this->fields["right"]];
  127. $sql[] = "" .
  128. "UPDATE `".$this->table."` " .
  129. "SET `".$this->fields["right"]."` = `".$this->fields["right"]."` - ".$ndif." " .
  130. "WHERE " .
  131. "`".$this->fields["right"]."` > ".$node[$this->fields["left"]]." AND " .
  132. "`".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ";
  133. }
  134. // Preparing new parent
  135. $sql[] = "" .
  136. "UPDATE `".$this->table."` " .
  137. "SET `".$this->fields["position"]."` = `".$this->fields["position"]."` + 1 " .
  138. "WHERE " .
  139. "`".$this->fields["parent_id"]."` = ".$ref_id." AND " .
  140. "`".$this->fields["position"]."` >= ".$position." " .
  141. ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
  142. $ref_ind = $ref_id === 0 ? (int)$rchildren[count($rchildren) - 1][$this->fields["right"]] + 1 : (int)$ref_node[$this->fields["right"]];
  143. $ref_ind = max($ref_ind, 1);
  144. $self = ($node !== false && !$is_copy && (int)$node[$this->fields["parent_id"]] == $ref_id && $position > $node[$this->fields["position"]]) ? 1 : 0;
  145. foreach($rchildren as $k => $v) {
  146. if($v[$this->fields["position"]] - $self == $position) {
  147. $ref_ind = (int)$v[$this->fields["left"]];
  148. break;
  149. }
  150. }
  151. if($node !== false && !$is_copy && $node[$this->fields["left"]] < $ref_ind) {
  152. $ref_ind -= $ndif;
  153. }
  154. $sql[] = "" .
  155. "UPDATE `".$this->table."` " .
  156. "SET `".$this->fields["left"]."` = `".$this->fields["left"]."` + ".$ndif." " .
  157. "WHERE " .
  158. "`".$this->fields["left"]."` >= ".$ref_ind." " .
  159. ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
  160. $sql[] = "" .
  161. "UPDATE `".$this->table."` " .
  162. "SET `".$this->fields["right"]."` = `".$this->fields["right"]."` + ".$ndif." " .
  163. "WHERE " .
  164. "`".$this->fields["right"]."` >= ".$ref_ind." " .
  165. ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
  166. $ldif = $ref_id == 0 ? 0 : $ref_node[$this->fields["level"]] + 1;
  167. $idif = $ref_ind;
  168. if($node !== false) {
  169. $ldif = $node[$this->fields["level"]] - ($ref_node[$this->fields["level"]] + 1);
  170. $idif = $node[$this->fields["left"]] - $ref_ind;
  171. if($is_copy) {
  172. $sql[] = "" .
  173. "INSERT INTO `".$this->table."` (" .
  174. "`".$this->fields["parent_id"]."`, " .
  175. "`".$this->fields["position"]."`, " .
  176. "`".$this->fields["left"]."`, " .
  177. "`".$this->fields["right"]."`, " .
  178. "`".$this->fields["level"]."`" .
  179. ") " .
  180. "SELECT " .
  181. "".$ref_id.", " .
  182. "`".$this->fields["position"]."`, " .
  183. "`".$this->fields["left"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " .
  184. "`".$this->fields["right"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " .
  185. "`".$this->fields["level"]."` - (".$ldif.") " .
  186. "FROM `".$this->table."` " .
  187. "WHERE " .
  188. "`".$this->fields["id"]."` IN (".implode(",", $node_ids).") " .
  189. "ORDER BY `".$this->fields["level"]."` ASC";
  190. }
  191. else {
  192. $sql[] = "" .
  193. "UPDATE `".$this->table."` SET " .
  194. "`".$this->fields["parent_id"]."` = ".$ref_id.", " .
  195. "`".$this->fields["position"]."` = ".$position." " .
  196. "WHERE " .
  197. "`".$this->fields["id"]."` = ".$id;
  198. $sql[] = "" .
  199. "UPDATE `".$this->table."` SET " .
  200. "`".$this->fields["left"]."` = `".$this->fields["left"]."` - (".$idif."), " .
  201. "`".$this->fields["right"]."` = `".$this->fields["right"]."` - (".$idif."), " .
  202. "`".$this->fields["level"]."` = `".$this->fields["level"]."` - (".$ldif.") " .
  203. "WHERE " .
  204. "`".$this->fields["id"]."` IN (".implode(",", $node_ids).") ";
  205. }
  206. }
  207. else {
  208. $sql[] = "" .
  209. "INSERT INTO `".$this->table."` (" .
  210. "`".$this->fields["parent_id"]."`, " .
  211. "`".$this->fields["position"]."`, " .
  212. "`".$this->fields["left"]."`, " .
  213. "`".$this->fields["right"]."`, " .
  214. "`".$this->fields["level"]."` " .
  215. ") " .
  216. "VALUES (" .
  217. $ref_id.", " .
  218. $position.", " .
  219. $idif.", " .
  220. ($idif + 1).", " .
  221. $ldif.
  222. ")";
  223. }
  224. foreach($sql as $q) { $this->db->query($q); }
  225. $ind = $this->db->insert_id();
  226. if($is_copy) $this->_fix_copy($ind, $position);
  227. return $node === false || $is_copy ? $ind : true;
  228. }
  229. function _fix_copy($id, $position) {
  230. $node = $this->_get_node($id);
  231. $children = $this->_get_children($id, true);
  232. $map = array();
  233. for($i = $node[$this->fields["left"]] + 1; $i < $node[$this->fields["right"]]; $i++) {
  234. $map[$i] = $id;
  235. }
  236. foreach($children as $cid => $child) {
  237. if((int)$cid == (int)$id) {
  238. $this->db->query("UPDATE `".$this->table."` SET `".$this->fields["position"]."` = ".$position." WHERE `".$this->fields["id"]."` = ".$cid);
  239. continue;
  240. }
  241. $this->db->query("UPDATE `".$this->table."` SET `".$this->fields["parent_id"]."` = ".$map[(int)$child[$this->fields["left"]]]." WHERE `".$this->fields["id"]."` = ".$cid);
  242. for($i = $child[$this->fields["left"]] + 1; $i < $child[$this->fields["right"]]; $i++) {
  243. $map[$i] = $cid;
  244. }
  245. }
  246. }
  247. function _reconstruct() {
  248. }
  249. function _analyze() {
  250. $report = array();
  251. $this->db->query("" .
  252. "SELECT " .
  253. "`".$this->fields["left"]."` FROM `".$this->table."` s " .
  254. "WHERE " .
  255. "`".$this->fields["parent_id"]."` = 0 "
  256. );
  257. $this->db->nextr();
  258. if($this->db->nf() == 0) {
  259. $report[] = "[FAIL]\tNo root node.";
  260. }
  261. else {
  262. $report[] = ($this->db->nf() > 1) ? "[FAIL]\tMore than one root node." : "[OK]\tJust one root node.";
  263. }
  264. $report[] = ($this->db->f(0) != 1) ? "[FAIL]\tRoot node's left index is not 1." : "[OK]\tRoot node's left index is 1.";
  265. $this->db->query("" .
  266. "SELECT " .
  267. "COUNT(*) FROM `".$this->table."` s " .
  268. "WHERE " .
  269. "`".$this->fields["parent_id"]."` != 0 AND " .
  270. "(SELECT COUNT(*) FROM `".$this->table."` WHERE `".$this->fields["id"]."` = s.`".$this->fields["parent_id"]."`) = 0 ");
  271. $this->db->nextr();
  272. $report[] = ($this->db->f(0) > 0) ? "[FAIL]\tMissing parents." : "[OK]\tNo missing parents.";
  273. $this->db->query("SELECT MAX(`".$this->fields["right"]."`) FROM `".$this->table."`");
  274. $this->db->nextr();
  275. $n = $this->db->f(0);
  276. $this->db->query("SELECT COUNT(*) FROM `".$this->table."`");
  277. $this->db->nextr();
  278. $c = $this->db->f(0);
  279. $report[] = ($n/2 != $c) ? "[FAIL]\tRight index does not match node count." : "[OK]\tRight index matches count.";
  280. $this->db->query("" .
  281. "SELECT COUNT(`".$this->fields["id"]."`) FROM `".$this->table."` s " .
  282. "WHERE " .
  283. "(SELECT COUNT(*) FROM `".$this->table."` WHERE " .
  284. "`".$this->fields["right"]."` < s.`".$this->fields["right"]."` AND " .
  285. "`".$this->fields["left"]."` > s.`".$this->fields["left"]."` AND " .
  286. "`".$this->fields["level"]."` = s.`".$this->fields["level"]."` + 1" .
  287. ") != " .
  288. "(SELECT COUNT(*) FROM `".$this->table."` WHERE " .
  289. "`".$this->fields["parent_id"]."` = s.`".$this->fields["id"]."`" .
  290. ") "
  291. );
  292. $this->db->nextr();
  293. $report[] = ($this->db->f(0) > 0) ? "[FAIL]\tAdjacency and nested set do not match." : "[OK]\tNS and AJ match";
  294. return implode("<br />",$report);
  295. }
  296. function _dump($output = false) {
  297. $nodes = array();
  298. $this->db->query("SELECT * FROM ".$this->table." ORDER BY `".$this->fields["left"]."`");
  299. while($this->db->nextr()) $nodes[] = $this->db->get_row("assoc");
  300. if($output) {
  301. echo "<pre>";
  302. foreach($nodes as $node) {
  303. echo str_repeat("&#160;",(int)$node[$this->fields["level"]] * 2);
  304. echo $node[$this->fields["id"]]." (".$node[$this->fields["left"]].",".$node[$this->fields["right"]].",".$node[$this->fields["level"]].",".$node[$this->fields["parent_id"]].",".$node[$this->fields["position"]].")<br />";
  305. }
  306. echo str_repeat("-",40);
  307. echo "</pre>";
  308. }
  309. return $nodes;
  310. }
  311. }
  312. class json_tree extends _tree_struct {
  313. function __construct($table = "cms_tree", $fields = array(), $add_fields = array("title" => "title", "type" => "type")) {
  314. parent::__construct($table, $fields);
  315. $this->fields = array_merge($this->fields, $add_fields);
  316. $this->add_fields = $add_fields;
  317. }
  318. function create_node($data) {
  319. $id = parent::_create((int)$data[$this->fields["id"]], (int)$data[$this->fields["position"]]);
  320. if($id) {
  321. $data["id"] = $id;
  322. $this->set_data($data);
  323. return "{ \"status\" : 1, \"id\" : ".(int)$id." }";
  324. }
  325. return "{ \"status\" : 0 }";
  326. }
  327. function set_data($data) {
  328. if(count($this->add_fields) == 0) { return "{ \"status\" : 1 }"; }
  329. $s = "UPDATE `".$this->table."` SET `".$this->fields["id"]."` = `".$this->fields["id"]."` ";
  330. foreach($this->add_fields as $k => $v) {
  331. if(isset($data[$k])) $s .= ", `".$this->fields[$v]."` = \"".$this->db->escape($data[$k])."\" ";
  332. else $s .= ", `".$this->fields[$v]."` = `".$this->fields[$v]."` ";
  333. }
  334. $s .= "WHERE `".$this->fields["id"]."` = ".(int)$data["id"];
  335. $this->db->query($s);
  336. return "{ \"status\" : 1 }";
  337. }
  338. function rename_node($data) { return $this->set_data($data); }
  339. function move_node($data) {
  340. $id = parent::_move((int)$data["id"], (int)$data["ref"], (int)$data["position"], (int)$data["copy"]);
  341. if(!$id) return "{ \"status\" : 0 }";
  342. if((int)$data["copy"] && count($this->add_fields)) {
  343. $ids = array_keys($this->_get_children($id, true));
  344. $data = $this->_get_children((int)$data["id"], true);
  345. $i = 0;
  346. foreach($data as $dk => $dv) {
  347. $s = "UPDATE `".$this->table."` SET `".$this->fields["id"]."` = `".$this->fields["id"]."` ";
  348. foreach($this->add_fields as $k => $v) {
  349. if(isset($dv[$k])) $s .= ", `".$this->fields[$v]."` = \"".$this->db->escape($dv[$k])."\" ";
  350. else $s .= ", `".$this->fields[$v]."` = `".$this->fields[$v]."` ";
  351. }
  352. $s .= "WHERE `".$this->fields["id"]."` = ".$ids[$i];
  353. $this->db->query($s);
  354. $i++;
  355. }
  356. }
  357. return "{ \"status\" : 1, \"id\" : ".$id." }";
  358. }
  359. function remove_node($data) {
  360. $id = parent::_remove((int)$data["id"]);
  361. return "{ \"status\" : 1 }";
  362. }
  363. function get_children($data) {
  364. $tmp = $this->_get_children((int)$data["id"]);
  365. if((int)$data["id"] === 1 && count($tmp) === 0) {
  366. $tmp = $this->_get_children((int)$data["id"]);
  367. }
  368. $result = array();
  369. if((int)$data["id"] === 0) return json_encode($result);
  370. foreach($tmp as $k => $v) {
  371. $result[] = array(
  372. "attr" => array("id" => "node_".$k, "rel" => $v[$this->fields["type"]]),
  373. "data" => $v[$this->fields["title"]],
  374. "state" => ((int)$v[$this->fields["right"]] - (int)$v[$this->fields["left"]] > 1) ? "closed" : ""
  375. );
  376. }
  377. return json_encode($result);
  378. }
  379. function search($data) {
  380. $this->db->query("SELECT `".$this->fields["left"]."`, `".$this->fields["right"]."` FROM `".$this->table."` WHERE `".$this->fields["title"]."` LIKE '%".$this->db->escape($data["search_str"])."%'");
  381. if($this->db->nf() === 0) return "[]";
  382. $q = "SELECT DISTINCT `".$this->fields["id"]."` FROM `".$this->table."` WHERE 0 ";
  383. while($this->db->nextr()) {
  384. $q .= " OR (`".$this->fields["left"]."` < ".(int)$this->db->f(0)." AND `".$this->fields["right"]."` > ".(int)$this->db->f(1).") ";
  385. }
  386. $result = array();
  387. $this->db->query($q);
  388. while($this->db->nextr()) { $result[] = "#node_".$this->db->f(0); }
  389. return json_encode($result);
  390. }
  391. }