|
Membre expérimenté
Développeur de bug Inscription : janvier 2003 Messages : 516 Détails du profil  Informations personnelles : Sexe :  Âge : 31 Localisation : France Informations professionnelles :
Activité : Développeur de bug Informations forums :
Inscription : janvier 2003 Messages : 516 Points : 535 Points : 535
|
Treeview avec jsTree
bonjour,
j'ai suivi le tutoriel de Ivan Bozhanov sur http://www.jstree.com/demo pour créer des arbres. Toutefois, j'ai besoin de pouvoir créer plusieurs arbres, j'ai donc modifié la structure de la table donnée en exemple (qui ne contenait que les items d'un seul arbre, pas conçu pour plusieurs): j'ai ajouté une colonne IDE_plans (clé étrangère).
Mais je ne parviens pas à modifier le code en conséquence, pouvez vous m'aider? 
La page php:
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
| <?php
/**
* @file affichage_plan.php
* Affichage d'un plan
*/
$session = new Session();
require_once ('../params/config.inc.php');
require_once ('../classes/loader.class.inc.php');
require_once ('HTML/QuickForm.php');
$html = new html();
$titre_page=gettext($plan_en_cours->getTitre());
// Affichage
$html->page_title = $titre_page;
$html->body_open('head.php');
$html->addJavascriptLibrary('jquery.js');
$html->addJavascriptLibrary('jquery.cookie.js');
$html->addJavascriptLibrary('jquery.hotkeys.js');
$html->addJavascriptLibrary('jquery.jstree.js');
// titres
echo '<h2>'.gettext('Plan').'</h2>';
echo '<h4>'.$plan_en_cours->getTitre().'</h4>';
// Affichage du plan - Arbre JsTree
echo '<fieldset>
<legend>'.gettext('Plan').'</legend>
<div id="plan" class="plan" style="height:200px;"></div>';
?>
<div style="height:30px; text-align:center;">
<input type="button" style='width:170px; height:24px; margin:5px auto;' value="reconstruct" onclick="$.get('./server_jsTree.php?reconstruct', function () { $('#plan').jstree('refresh',-1); });" />
<input type="button" style='width:170px; height:24px; margin:5px auto;' value="refresh" onclick="$('#plan').jstree('refresh',-1);" />
</div>
<?php
echo '</fieldset>';
?>
<!-- JavaScript neccessary for the tree -->
<script type="text/javascript" class="source below">
jQuery(document).ready(function () {
jQuery("#plan")
.jstree({
"core" : {
/* core options */
"html_titles" : false,
"strings" : { loading : "Chargement en cours ...", new_node : "Nouvel intitulé" }
},
"plugins" : [
"themes","json_data","ui","crrm","cookies","dnd","search","types","hotkeys","contextmenu"
],
// I usually configure the plugin that handles the data first
// This example uses JSON as it is most common
"json_data" : {
// This tree is ajax enabled - as this is most common, and maybe a bit more complex
// All the options are almost the same as jQuery's AJAX (read the docs)
"ajax" : {
// the URL to fetch the data
"url" : "./server_jsTree.php?idplan=<?php echo $id_plan ?>",
// the `data` function is executed in the instance's scope
// the parameter is the node being loaded
// (may be -1, 0, or undefined when loading the root nodes)
"data" : function (n) {
// the result is fed to the AJAX request `data` option
return {
"operation" : "get_children",
"id" : n.attr ? n.attr("id").replace("node_","") : 1
};
}
}
},
// Configuring the search plugin
"search" : {
// As this has been a common question - async search
// Same as above - the `ajax` config option is actually jQuery's AJAX object
"ajax" : {
"url" : "./server_jsTree.php?idplan=<?php echo $id_plan ?>",
// You get the search string as a parameter
"data" : function (str) {
return {
"operation" : "search",
"search_str" : str
};
}
}
},
// Using types - most of the time this is an overkill
// read the docs carefully to decide whether you need types
"types" : {
// I set both options to -2, as I do not need depth and children count checking
// Those two checks may slow jstree a lot, so use only when needed
"max_depth" : -2,
"max_children" : -2,
// I want only `drive` nodes to be root nodes
// This will prevent moving or creating any other type as a root node
"valid_children" : [ "drive" ],
"types" : {
// The default type
"default" : {
// I want this type to have no children (so only leaf nodes)
// In my case - those are files
"valid_children" : "none",
// If we specify an icon for the default type it WILL OVERRIDE the theme icons
"icon" : {
"image" : "./images/file.png"
}
},
// The `folder` type
"folder" : {
// can have files and other folders inside of it, but NOT `drive` nodes
"valid_children" : [ "default", "folder" ],
"icon" : {
"image" : "./images/folder2.png"
}
},
// The `drive` nodes
"drive" : {
// can have files and folders inside, but NOT other `drive` nodes
"valid_children" : [ "default", "folder" ],
"icon" : {
"image" : "./images/root.png"
},
// those prevent the functions with the same name to be used on `drive` nodes
// internally the `before` event is used
"start_drag" : false,
"move_node" : false,
"delete_node" : false,
"remove" : false
}
}
},
// the UI plugin - it handles selecting/deselecting/hovering nodes
"ui" : {
// this makes the node with ID node_4 selected onload
"initially_select" : [ "node_1" ]
},
// the core plugin - not many options here
"core" : {
// just open those two nodes up
// as this is an AJAX enabled tree, both will be downloaded from the server
"initially_open" : [ "node_2" , "node_3" ]
}
}
)
.bind("create.jstree", function (e, data) {
jQuery.post(
"./server_jsTree.php?idplan=<?php echo $id_plan ?>",
{
"operation" : "create_node",
"id" : data.rslt.parent.attr("id").replace("node_",""),
"position" : data.rslt.position,
"title" : data.rslt.name,
"type" : data.rslt.obj.attr("rel")
},
function (r) {
if(r.status) {
jQuery(data.rslt.obj).attr("id", "node_" + r.id);
}
else {
jQuery.jstree.rollback(data.rlbk);
}
}
);
})
.bind("remove.jstree", function (e, data) {
data.rslt.obj.each(function () {
jQuery.ajax({
async : false,
type: 'POST',
url: "./server_jsTree.php?idplan=<?php echo $id_plan ?>",
data : {
"operation" : "remove_node",
"id" : this.id.replace("node_","")
},
success : function (r) {
if(!r.status) {
data.inst.refresh();
}
}
});
});
})
.bind("rename.jstree", function (e, data) {
jQuery.post(
"./server_jsTree.php?idplan=<?php echo $id_plan ?>",
{
"operation" : "rename_node",
"id" : data.rslt.obj.attr("id").replace("node_",""),
"title" : data.rslt.new_name
},
function (r) {
if(!r.status) {
jQuery.jstree.rollback(data.rlbk);
}
}
);
})
.bind("move_node.jstree", function (e, data) {
data.rslt.o.each(function (i) {
jQuery.ajax({
async : false,
type: 'POST',
url: "./server_jsTree.php?idplan=<?php echo $id_plan ?>",
data : {
"operation" : "move_node",
"id" : jQuery(this).attr("id").replace("node_",""),
"ref" : data.rslt.cr === -1 ? 1 : data.rslt.np.attr("id").replace("node_",""),
"position" : data.rslt.cp + i,
"title" : data.rslt.name,
"copy" : data.rslt.cy ? 1 : 0
},
success : function (r) {
if(!r.status) {
jQuery.jstree.rollback(data.rlbk);
}
else {
jQuery(data.rslt.oc).attr("id", "node_" + r.id);
if(data.rslt.cy && jQuery(data.rslt.oc).children("UL").length) {
data.inst.refresh(data.inst._get_parent(data.rslt.oc));
}
}
jQuery("#analyze").click();
}
});
});
});
});
</script>
<?php
$html->body_close(); |
server_jsTree.php:
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?php
$id_plan=1;
if(isset($_REQUEST["idplan"])) {
$id_plan=$_REQUEST["idplan"];
}
require_once("../params/config_jsTree.php");
$jstree = new json_tree();
$jstree->setIdePlans($id_plan);
if(isset($_GET["reconstruct"])) {
$jstree->_reconstruct();
die();
}
if(isset($_GET["analyze"])) {
echo $jstree->_analyze();
die();
}
if($_REQUEST["operation"] && strpos($_REQUEST["operation"], "_") !== 0 && method_exists($jstree, $_REQUEST["operation"])) {
header("HTTP/1.0 200 OK");
header('Content-type: application/json; charset=utf-8');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Pragma: no-cache");
echo $jstree->{$_REQUEST["operation"]}($_REQUEST);
die();
}
header("HTTP/1.0 404 Not Found");
?> |
La classe:
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
| <?php
class _tree_struct {
// Structure table and fields
protected $table = "";
protected $ide_plans = 0; // val par defaut, plan par defaut
protected $fields = array(
"id" => false,
"IDE_plans" => false,
"parent_id" => false,
"position" => false,
"left" => false,
"right" => false,
"level" => false,
"title" => false,
"type" => false,
"type_item" => false,
"muid" => false,
"mtime" => false,
"cuid" => false,
"ctime" => false
);
// Constructor
function __construct($table = "plans_item", $fields = array()) {
$this->table = $table;
if(!count($fields)) {
foreach($this->fields as $k => &$v) { $v = $k; }
}
else {
foreach($fields as $key => $field) {
switch($key) {
case "id":
case "IDE_plans":
case "parent_id":
case "position":
case "left":
case "right":
case "level":
case "title":
case "type":
case "type_item":
case "muid":
case "mtime":
case "cuid":
case "ctime":
$this->fields[$key] = $field;
break;
}
}
}
// Database
$this->db = new _database;
}
function setIdePlans($idplan) {
$this->ide_plans=$idplan;
}
function getIdePlans() {
return $this->ide_plans;
}
function _get_node($id) {
$this->db->query("SELECT `".implode("` , `", $this->fields)."`
FROM `".$this->table."`
WHERE `".$this->fields["id"]."` = ".(int) $id)."
AND `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans();
$this->db->nextr();
return $this->db->nf() === 0 ? false : $this->db->get_row("assoc");
}
function _get_children($id, $recursive = false) {
$children = array();
$idplan=$this->getIdePlans();
if($recursive) {
$node = $this->_get_node($id);
// Mes essais :
$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"]]."
AND `".$this->fields["IDE_plans"]."` = ".$idplan."
ORDER BY `".$this->fields["left"]."` ASC");
// La bonne version sigh :
/*$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");*/
}
else {
// Mes essais :
$this->db->query("SELECT `".implode("` , `", $this->fields)."` FROM `".$this->table."`
WHERE `".$this->fields["parent_id"]."` = ".(int) $id."
AND `".$this->fields["IDE_plans"]."` = ".$idplan."
ORDER BY `".$this->fields["position"]."` ASC");
// La bonne version sigh :
/*$this->db->query("SELECT `".implode("` , `", $this->fields)."` FROM `".$this->table."`
WHERE `".$this->fields["parent_id"]."` = ".(int) $id."
ORDER BY `".$this->fields["position"]."` ASC");*/
}
while($this->db->nextr()) $children[$this->db->f($this->fields["id"])] = $this->db->get_row("assoc");
return $children;
}
function _get_path($id) {
$node = $this->_get_node($id);
$path = array();
if(!$node === false) return false;
$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"]]."
AND `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans());
while($this->db->nextr()) $path[$this->db->f($this->fields["id"])] = $this->db->get_row("assoc");
return $path;
}
function _create($parent, $position) {
return $this->_move(0, $parent, $position);
}
function _remove($id) {
if((int)$id === 1) { return false; }
$data = $this->_get_node($id);
$lft = (int)$data[$this->fields["left"]];
$rgt = (int)$data[$this->fields["right"]];
$dif = $rgt - $lft + 1;
// deleting node and its children
$this->db->query("" .
"DELETE FROM `".$this->table."` " .
"WHERE `".$this->fields["left"]."` >= ".$lft." AND `".$this->fields["right"]."` <= ".$rgt
);
// shift left indexes of nodes right of the node
$this->db->query("".
"UPDATE `".$this->table."` " .
"SET `".$this->fields["left"]."` = `".$this->fields["left"]."` - ".$dif." " .
"WHERE `".$this->fields["left"]."` > ".$rgt
);
// shift right indexes of nodes right of the node and the node's parents
$this->db->query("" .
"UPDATE `".$this->table."` " .
"SET `".$this->fields["right"]."` = `".$this->fields["right"]."` - ".$dif." " .
"WHERE `".$this->fields["right"]."` > ".$lft
);
$pid = (int)$data[$this->fields["parent_id"]];
$pos = (int)$data[$this->fields["position"]];
// Update position of siblings below the deleted node
$this->db->query("" .
"UPDATE `".$this->table."` " .
"SET `".$this->fields["position"]."` = `".$this->fields["position"]."` - 1 " .
"WHERE `".$this->fields["parent_id"]."` = ".$pid." AND `".$this->fields["position"]."` > ".$pos
);
return true;
}
function _move($id, $ref_id, $position = 0, $is_copy = false) {
if((int)$ref_id === 0 || (int)$id === 1) { return false; }
$sql = array(); // Queries executed at the end
$node = $this->_get_node($id); // Node data
$nchildren = $this->_get_children($id); // Node children
$ref_node = $this->_get_node($ref_id); // Ref node data
$rchildren = $this->_get_children($ref_id);// Ref node children
$ndif = 2;
$node_ids = array(-1);
if($node !== false) {
$node_ids = array_keys($this->_get_children($id, true));
// TODO: should be !$is_copy && , but if copied to self - screws some right indexes
if(in_array($ref_id, $node_ids)) return false;
$ndif = $node[$this->fields["right"]] - $node[$this->fields["left"]] + 1;
}
if($position >= count($rchildren)) {
$position = count($rchildren);
}
// Not creating or copying - old parent is cleaned
if($node !== false && $is_copy == false) {
$sql[] = "" .
"UPDATE `".$this->table."` " .
"SET `".$this->fields["position"]."` = `".$this->fields["position"]."` - 1 " .
"WHERE " .
"`".$this->fields["parent_id"]."` = ".$node[$this->fields["parent_id"]]." AND " .
"`".$this->fields["position"]."` > ".$node[$this->fields["position"]];
$sql[] = "" .
"UPDATE `".$this->table."` " .
"SET `".$this->fields["left"]."` = `".$this->fields["left"]."` - ".$ndif." " .
"WHERE `".$this->fields["left"]."` > ".$node[$this->fields["right"]];
$sql[] = "" .
"UPDATE `".$this->table."` " .
"SET `".$this->fields["right"]."` = `".$this->fields["right"]."` - ".$ndif." " .
"WHERE " .
"`".$this->fields["right"]."` > ".$node[$this->fields["left"]]." AND " .
"`".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ";
}
// Preparing new parent
$sql[] = "" .
"UPDATE `".$this->table."` " .
"SET `".$this->fields["position"]."` = `".$this->fields["position"]."` + 1 " .
"WHERE " .
"`".$this->fields["parent_id"]."` = ".$ref_id." AND " .
"`".$this->fields["position"]."` >= ".$position." " .
( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
$ref_ind = $ref_id === 0 ? (int)$rchildren[count($rchildren) - 1][$this->fields["right"]] + 1 : (int)$ref_node[$this->fields["right"]];
$ref_ind = max($ref_ind, 1);
$self = ($node !== false && !$is_copy && (int)$node[$this->fields["parent_id"]] == $ref_id && $position > $node[$this->fields["position"]]) ? 1 : 0;
foreach($rchildren as $k => $v) {
if($v[$this->fields["position"]] - $self == $position) {
$ref_ind = (int)$v[$this->fields["left"]];
break;
}
}
if($node !== false && !$is_copy && $node[$this->fields["left"]] < $ref_ind) {
$ref_ind -= $ndif;
}
$sql[] = "" .
"UPDATE `".$this->table."` " .
"SET `".$this->fields["left"]."` = `".$this->fields["left"]."` + ".$ndif." " .
"WHERE " .
"`".$this->fields["left"]."` >= ".$ref_ind." " .
( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
$sql[] = "" .
"UPDATE `".$this->table."` " .
"SET `".$this->fields["right"]."` = `".$this->fields["right"]."` + ".$ndif." " .
"WHERE " .
"`".$this->fields["right"]."` >= ".$ref_ind." " .
( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
$ldif = $ref_id == 0 ? 0 : $ref_node[$this->fields["level"]] + 1;
$idif = $ref_ind;
if($node !== false) {
$ldif = $node[$this->fields["level"]] - ($ref_node[$this->fields["level"]] + 1);
$idif = $node[$this->fields["left"]] - $ref_ind;
if($is_copy) {
$sql[] = "" .
"INSERT INTO `".$this->table."` (" .
"`".$this->fields["parent_id"]."`, " .
"`".$this->fields["position"]."`, " .
"`".$this->fields["left"]."`, " .
"`".$this->fields["right"]."`, " .
"`".$this->fields["level"]."`" .
") " .
"SELECT " .
"".$ref_id.", " .
"`".$this->fields["position"]."`, " .
"`".$this->fields["left"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " .
"`".$this->fields["right"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " .
"`".$this->fields["level"]."` - (".$ldif.") " .
"FROM `".$this->table."` " .
"WHERE " .
"`".$this->fields["id"]."` IN (".implode(",", $node_ids).") " .
" AND `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans().
" ORDER BY `".$this->fields["level"]."` ASC";
}
else {
$sql[] = "" .
"UPDATE `".$this->table."` SET " .
"`".$this->fields["parent_id"]."` = ".$ref_id.", " .
"`".$this->fields["position"]."` = ".$position." " .
"WHERE " .
"`".$this->fields["id"]."` = ".$id;
$sql[] = "" .
"UPDATE `".$this->table."` SET " .
"`".$this->fields["left"]."` = `".$this->fields["left"]."` - (".$idif."), " .
"`".$this->fields["right"]."` = `".$this->fields["right"]."` - (".$idif."), " .
"`".$this->fields["level"]."` = `".$this->fields["level"]."` - (".$ldif.") " .
"WHERE " .
"`".$this->fields["id"]."` IN (".implode(",", $node_ids).") ";
}
}
else {
$sql[] = "" .
"INSERT INTO `".$this->table."` (" .
"`".$this->fields["parent_id"]."`, " .
"`".$this->fields["position"]."`, " .
"`".$this->fields["left"]."`, " .
"`".$this->fields["right"]."`, " .
"`".$this->fields["level"]."` " .
") " .
"VALUES (" .
$ref_id.", " .
$position.", " .
$idif.", " .
($idif + 1).", " .
$ldif.
")";
}
foreach($sql as $q) { $this->db->query($q); }
$ind = $this->db->insert_id();
if($is_copy) $this->_fix_copy($ind, $position);
return $node === false || $is_copy ? $ind : true;
}
function _fix_copy($id, $position) {
$node = $this->_get_node($id);
$children = $this->_get_children($id, true);
$map = array();
for($i = $node[$this->fields["left"]] + 1; $i < $node[$this->fields["right"]]; $i++) {
$map[$i] = $id;
}
foreach($children as $cid => $child) {
if((int)$cid == (int)$id) {
$this->db->query("UPDATE `".$this->table."` SET `".$this->fields["position"]."` = ".$position." WHERE `".$this->fields["id"]."` = ".$cid);
continue;
}
$this->db->query("UPDATE `".$this->table."` SET `".$this->fields["parent_id"]."` = ".$map[(int)$child[$this->fields["left"]]]." WHERE `".$this->fields["id"]."` = ".$cid);
for($i = $child[$this->fields["left"]] + 1; $i < $child[$this->fields["right"]]; $i++) {
$map[$i] = $cid;
}
}
}
function _reconstruct() {
$this->db->query("" .
"CREATE TEMPORARY TABLE `temp_tree` (" .
"`".$this->fields["id"]."` INTEGER NOT NULL, " .
"`".$this->fields["parent_id"]."` INTEGER NOT NULL, " .
"`". $this->fields["position"]."` INTEGER NOT NULL, " .
"`". $this->fields["IDE_plans"]."` INTEGER NOT NULL" .
") type=HEAP"
);
$this->db->query("" .
"INSERT INTO `temp_tree` " .
"SELECT " .
"`".$this->fields["id"]."`, " .
"`".$this->fields["parent_id"]."`, " .
"`".$this->fields["position"]."`, " .
"`".$this->fields["IDE_plans"]."` " .
"FROM `".$this->table."`
WHERE `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans()
);
$this->db->query("" .
"CREATE TEMPORARY TABLE `temp_stack` (" .
"`".$this->fields["id"]."` INTEGER NOT NULL, " .
"`".$this->fields["left"]."` INTEGER, " .
"`".$this->fields["right"]."` INTEGER, " .
"`".$this->fields["level"]."` INTEGER, " .
"`stack_top` INTEGER NOT NULL, " .
"`".$this->fields["parent_id"]."` INTEGER, " .
"`".$this->fields["position"]."` INTEGER, " .
"`".$this->fields["IDE_plans"]."` INTEGER " .
") type=HEAP"
);
$counter = 2;
$this->db->query("SELECT COUNT(*)
FROM temp_tree
WHERE `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans());
$this->db->nextr();
$maxcounter = (int) $this->db->f(0) * 2;
$currenttop = 1;
$this->db->query("" .
"INSERT INTO `temp_stack` " .
"SELECT " .
"`".$this->fields["id"]."`, " .
"1, " .
"NULL, " .
"0, " .
"1, " .
"`".$this->fields["parent_id"]."`, " .
"`".$this->fields["position"]."` " .
"FROM `temp_tree` " .
"WHERE `".$this->fields["parent_id"]."` = 0
AND `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans()
);
$this->db->query("DELETE FROM `temp_tree` WHERE `".$this->fields["parent_id"]."` = 0
AND `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans()
);
while ($counter <= $maxcounter) {
$this->db->query("" .
"SELECT " .
"`temp_tree`.`".$this->fields["id"]."` AS tempmin, " .
"`temp_tree`.`".$this->fields["parent_id"]."` AS pid, " .
"`temp_tree`.`".$this->fields["position"]."` AS lid, " .
"`temp_tree`.`".$this->fields["IDE_plans"]."` AS idplan " .
"FROM `temp_stack`, `temp_tree` " .
"WHERE " .
"`temp_stack`.`".$this->fields["id"]."` = `temp_tree`.`".$this->fields["parent_id"]."` AND " .
"`temp_stack`.`stack_top` = ".$currenttop."
AND `".$this->fields["IDE_plans"]."` = " .$this->getIdePlans().
" ORDER BY `temp_tree`.`".$this->fields["position"]."` ASC LIMIT 1"
);
if ($this->db->nextr()) {
$tmp = $this->db->f("tempmin");
$q = "INSERT INTO temp_stack (stack_top, `".$this->fields["id"]."`, `".$this->fields["left"]."`, `".$this->fields["right"]."`, `".$this->fields["level"]."`, `".$this->fields["parent_id"]."`,
`".$this->fields["position"]."`, `".$this->fields["IDE_plans"]."`)
VALUES(".($currenttop + 1).", ".$tmp.", ".$counter.", NULL, ".$currenttop.", ".$this->db->f("pid").", ".$this->db->f("lid").", ".$this->db->f("idplan").")";
$this->db->query($q);
$this->db->query("DELETE FROM `temp_tree` WHERE `".$this->fields["id"]."` = ".$tmp);
$counter++;
$currenttop++;
}
else {
$this->db->query("" .
"UPDATE temp_stack SET " .
"`".$this->fields["right"]."` = ".$counter.", " .
"`stack_top` = -`stack_top` " .
"WHERE `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans()."
AND `stack_top` = ".$currenttop
);
$counter++;
$currenttop--;
}
}
$temp_fields = $this->fields;
unset($temp_fields["parent_id"]);
unset($temp_fields["position"]);
unset($temp_fields["left"]);
unset($temp_fields["right"]);
unset($temp_fields["level"]);
unset($temp_fields["IDE_plans"]);
if(count($temp_fields) > 1) {
$this->db->query("" .
"CREATE TEMPORARY TABLE `temp_tree2` " .
"SELECT `".implode("`, `", $temp_fields)."`
FROM `".$this->table."`
WHERE `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans()
);
}
//$this->db->query("TRUNCATE TABLE `".$this->table."`");
$this->db->query("" .
"INSERT INTO ".$this->table." (" .
"`".$this->fields["id"]."`, " .
"`".$this->fields["parent_id"]."`, " .
"`".$this->fields["position"]."`, " .
"`".$this->fields["left"]."`, " .
"`".$this->fields["right"]."`, " .
"`".$this->fields["level"]."`, " .
"`".$this->fields["IDE_plans"]."` " .
") " .
"SELECT " .
"`".$this->fields["id"]."`, " .
"`".$this->fields["parent_id"]."`, " .
"`".$this->fields["position"]."`, " .
"`".$this->fields["left"]."`, " .
"`".$this->fields["right"]."`, " .
"`".$this->fields["level"]."`, " .
"`".$this->fields["IDE_plans"]."` " .
"FROM temp_stack " .
"ORDER BY `".$this->fields["id"]."`"
);
if(count($temp_fields) > 1) {
$sql = "" .
"UPDATE `".$this->table."` v, `temp_tree2` SET v.`".$this->fields["id"]."` = v.`".$this->fields["id"]."` ";
foreach($temp_fields as $k => $v) {
if($k == "id") continue;
$sql .= ", v.`".$v."` = `temp_tree2`.`".$v."` ";
}
$sql .= " WHERE v.`".$this->fields["id"]."` = `temp_tree2`.`".$this->fields["id"]."` ";
$this->db->query($sql);
}
}
function _analyze() {
$report = array();
$this->db->query("" .
"SELECT " .
"`".$this->fields["left"]."` FROM `".$this->table."` s " .
"WHERE " .
"`".$this->fields["parent_id"]."` = 0
AND `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans()
);
$this->db->nextr();
if($this->db->nf() == 0) {
$report[] = "[FAIL]\tNo root node.";
}
else {
$report[] = ($this->db->nf() > 1) ? "[FAIL]\tMore than one root node." : "[OK]\tJust one root node.";
}
$report[] = ($this->db->f(0) != 1) ? "[FAIL]\tRoot node's left index is not 1." : "[OK]\tRoot node's left index is 1.";
$this->db->query("" .
"SELECT " .
"COUNT(*) FROM `".$this->table."` s " .
"WHERE " .
"`".$this->fields["parent_id"]."` != 0
AND `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans().
" AND " .
"(SELECT COUNT(*) FROM `".$this->table."` WHERE `".$this->fields["id"]."` = s.`".$this->fields["parent_id"]."`) = 0 AND `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans());
$this->db->nextr();
$report[] = ($this->db->f(0) > 0) ? "[FAIL]\tMissing parents." : "[OK]\tNo missing parents.";
$this->db->query("SELECT MAX(`".$this->fields["right"]."`) FROM `".$this->table."` WHERE `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans());
$this->db->nextr();
$n = $this->db->f(0);
$this->db->query("SELECT COUNT(*) FROM `".$this->table."` WHERE `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans());
$this->db->nextr();
$c = $this->db->f(0);
$report[] = ($n/2 != $c) ? "[FAIL]\tRight index does not match node count." : "[OK]\tRight index matches count.";
$this->db->query("" .
"SELECT COUNT(`".$this->fields["id"]."`) FROM `".$this->table."` s " .
"WHERE
`".$this->fields["IDE_plans"]."` = ".$this->getIdePlans().
" AND (SELECT COUNT(*) FROM `".$this->table."` WHERE " .
"`".$this->fields["right"]."` < s.`".$this->fields["right"]."` AND " .
"`".$this->fields["left"]."` > s.`".$this->fields["left"]."` AND " .
"`".$this->fields["level"]."` = s.`".$this->fields["level"]."` + 1 AND ".
"`".$this->fields["IDE_plans"]."` = ".$this->getIdePlans().
") != " .
"(SELECT COUNT(*) FROM `".$this->table."` WHERE " .
"`".$this->fields["parent_id"]."` = s.`".$this->fields["id"]."` AND
`".$this->fields["IDE_plans"]."` = ".$this->getIdePlans().
") "
);
$this->db->nextr();
$report[] = ($this->db->f(0) > 0) ? "[FAIL]\tAdjacency and nested set do not match." : "[OK]\tNS and AJ match";
return implode("<br />",$report);
}
function _dump($output = false) {
$nodes = array();
$this->db->query("SELECT * FROM ".$this->table."
WHERE `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans()."
ORDER BY `".$this->fields["left"]."`");
while($this->db->nextr()) $nodes[] = $this->db->get_row("assoc");
if($output) {
echo "<pre>";
foreach($nodes as $node) {
echo str_repeat(" ",(int)$node[$this->fields["level"]] * 2);
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 />";
}
echo str_repeat("-",40);
echo "</pre>";
}
return $nodes;
}
function _drop() {
//$this->db->query("TRUNCATE TABLE `".$this->table."`");
/*$this->db->query("" .
"INSERT INTO `".$this->table."` (" .
"`".$this->fields["id"]."`, " .
"`".$this->fields["parent_id"]."`, " .
"`".$this->fields["position"]."`, " .
"`".$this->fields["left"]."`, " .
"`".$this->fields["right"]."`, " .
"`".$this->fields["level"]."` " .
") " .
"VALUES (" .
"1, " .
"0, " .
"0, " .
"1, " .
"2, " .
"0 ".
")");*/
}
}
class json_tree extends _tree_struct {
function __construct($table = "plans_item", $fields = array(), $add_fields = array("title" => "title", "type" => "type")) {
parent::__construct($table, $fields);
$this->fields = array_merge($this->fields, $add_fields);
$this->add_fields = $add_fields;
}
function create_node($data) {
$id = parent::_create((int)$data[$this->fields["id"]], (int)$data[$this->fields["position"]]);
if($id) {
$data["id"] = $id;
$this->set_data($data);
return "{ \"status\" : 1, \"id\" : ".(int)$id." }";
}
return "{ \"status\" : 0 }";
}
function set_data($data) {
if(count($this->add_fields) == 0) { return "{ \"status\" : 1 }"; }
$s = "UPDATE `".$this->table."` SET `".$this->fields["id"]."` = `".$this->fields["id"]."` ";
foreach($this->add_fields as $k => $v) {
if(isset($data[$k])) $s .= ", `".$this->fields[$v]."` = \"".$this->db->escape($data[$k])."\" ";
else $s .= ", `".$this->fields[$v]."` = `".$this->fields[$v]."` ";
}
$s .= "WHERE `".$this->fields["id"]."` = ".(int)$data["id"];
$this->db->query($s);
return "{ \"status\" : 1 }";
}
function rename_node($data) { return $this->set_data($data); }
function move_node($data) {
$id = parent::_move((int)$data["id"], (int)$data["ref"], (int)$data["position"], (int)$data["copy"]);
if(!$id) return "{ \"status\" : 0 }";
if((int)$data["copy"] && count($this->add_fields)) {
$ids = array_keys($this->_get_children($id, true));
$data = $this->_get_children((int)$data["id"], true);
$i = 0;
foreach($data as $dk => $dv) {
$s = "UPDATE `".$this->table."` SET `".$this->fields["id"]."` = `".$this->fields["id"]."` ";
foreach($this->add_fields as $k => $v) {
if(isset($dv[$k])) $s .= ", `".$this->fields[$v]."` = \"".$this->db->escape($dv[$k])."\" ";
else $s .= ", `".$this->fields[$v]."` = `".$this->fields[$v]."` ";
}
$s .= "WHERE `".$this->fields["id"]."` = ".$ids[$i];
$this->db->query($s);
$i++;
}
}
return "{ \"status\" : 1, \"id\" : ".$id." }";
}
function remove_node($data) {
$id = parent::_remove((int)$data["id"]);
return "{ \"status\" : 1 }";
}
function get_children($data) {
$tmp = $this->_get_children((int)$data["id"]);
if((int)$data["id"] === 1 && count($tmp) === 0) {
//$this->_create_default();
$tmp = $this->_get_children((int)$data["id"]);
}
$result = array();
if((int)$data["id"] === 0) return json_encode($result);
foreach($tmp as $k => $v) {
$result[] = array(
"attr" => array("id" => "node_".$k, "rel" => $v[$this->fields["type"]]),
"data" => $v[$this->fields["title"]],
"state" => ((int)$v[$this->fields["right"]] - (int)$v[$this->fields["left"]] > 1) ? "closed" : ""
);
}
return json_encode($result);
}
function search($data) {
$this->db->query("SELECT `".$this->fields["left"]."`, `".$this->fields["right"]."`
FROM `".$this->table."`
WHERE `".$this->fields["title"]."` LIKE '%".$this->db->escape($data["search_str"])."%'
AND `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans());
if($this->db->nf() === 0) return "[]";
//$q = "SELECT DISTINCT `".$this->fields["id"]."` FROM `".$this->table."` WHERE 0 ";
$q = "SELECT DISTINCT `".$this->fields["id"]."` FROM `".$this->table."` WHERE `".$this->fields["IDE_plans"]."` = ".$this->getIdePlans();
while($this->db->nextr()) {
$q .= " OR (`".$this->fields["left"]."` < ".(int)$this->db->f(0)." AND `".$this->fields["right"]."` > ".(int)$this->db->f(1).") ";
}
$result = array();
$this->db->query($q);
while($this->db->nextr()) { $result[] = "#node_".$this->db->f(0); }
return json_encode($result);
}
function _create_default() {
//$this->_drop();
$this->create_node(array(
"id" => 1,
"position" => 0,
"title" => "C:",
"type" => "drive"
));
$this->create_node(array(
"id" => 1,
"position" => 1,
"title" => "D:",
"type" => "drive"
));
$this->create_node(array(
"id" => 2,
"position" => 0,
"title" => "_demo",
"type" => "folder"
));
$this->create_node(array(
"id" => 2,
"position" => 1,
"title" => "_docs",
"type" => "folder"
));
$this->create_node(array(
"id" => 4,
"position" => 0,
"title" => "index.html",
"type" => "default"
));
$this->create_node(array(
"id" => 5,
"position" => 1,
"title" => "doc.html",
"type" => "default"
));
}
}
?> |
Bdd:
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| DROP TABLE IF EXISTS `plans_item`;
CREATE TABLE IF NOT EXISTS `plans_item` (
`id` int(10) unsigned NOT NULL auto_increment,
`IDE_plans` int(10) unsigned default NULL,
`parent_id` int(10) unsigned default NULL,
`position` bigint(20) unsigned default NULL,
`left` bigint(20) unsigned default NULL,
`right` bigint(20) unsigned default NULL,
`level` bigint(20) unsigned default NULL,
`title` varchar(255) default NULL COMMENT 'Intitulé pour 1 item',
`type` varchar(255) default NULL COMMENT 'type pour affichage icone',
`type_item` varchar(255) default NULL COMMENT 'type pour item: catégorie || terme',
`muid` int(10) unsigned default NULL,
`mtime` timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`cuid` int(10) unsigned default NULL,
`ctime` timestamp NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Items des plans de lecture' AUTO_INCREMENT=10 ;
--
-- Contenu de la table `plans_item`
--
INSERT INTO `plans_item` (`IDE_plans`, `parent_id`, `position`, `left`, `right`, `level`, `title`, `type`, `type_item`, `muid`, `mtime`, `cuid`, `ctime`) VALUES
( 3, 0, 0, 1, 18, 0, NULL, NULL, NULL, NULL, '2011-09-12 14:21:49', NULL, '0000-00-00 00:00:00'),
(3, 1, 0, 2, 3, 1, 'Esthétique et critique', 'folder', 'Fragment', 90, '2011-09-12 14:56:10', 90, '2011-09-12 14:21:40'),
( 3, 1, 1, 4, 17, 1, 'Histoire et idées scientifiques', 'folder', 'Fragment', 90, '2011-09-12 14:56:10', 90, '2011-09-12 14:21:40'),
(3, 2, 0, 5, 10, 2, 'Esthétique', 'folder', 'Fragment', 90, '2011-09-12 14:56:59', 90, '2011-09-12 14:21:49'),
(3, 2, 1, 11, 16, 2, 'Critique', 'folder', 'Fragment', 90, '2011-09-12 14:59:06', 90, '2011-09-12 14:21:49'),
(3, 3, 0, 6, 7, 3, "Contradictions de l'histoire", 'folder', 'Fragment', 90, '2011-09-12 14:32:34', 90, '2011-09-12 14:21:49'),
(3, 3, 1, 8, 9, 2, 'Idées scientifiques', 'default', 'Fragment', 90, '2011-09-12 15:06:43', 90, '2011-09-12 14:21:49'),
(3, 3, 2, 12, 13, 2, "Histoire", 'default', 'Fragment', 90, '2011-09-12 15:25:16', 90, '2011-09-12 14:47:00');
INSERT INTO `plans_item` (`ID`, `IDE_plans`, `parent_id`, `position`, `left`, `right`, `level`, `title`, `type`, `type_item`, `muid`, `mtime`, `cuid`, `ctime`) VALUES
(9, 1, 0, 0, 1, 18, 0, NULL, NULL, NULL, NULL, '2011-09-12 14:21:49', NULL, '0000-00-00 00:00:00'),
(10, 1, 9, 0, 2, 3, 1, 'manu rentre chez toi', 'folder', 'Fragment', 90, '2011-09-12 14:56:10', 90, '2011-09-12 14:21:40'),
(11, 1, 9, 1, 4, 17, 1, 'je pensais même que soufrir, ça pouvait pas arriver', 'folder', 'Fragment', 90, '2011-09-12 14:56:10', 90, '2011-09-12 14:21:40'),
(12, 1, 10, 0, 5, 10, 2, 'Les copains qui reviennent', 'folder', 'Fragment', 90, '2011-09-12 14:56:59', 90, '2011-09-12 14:21:49'),
(13, 1, 10, 1, 11, 16, 2, 'une gonzesse de perdue', 'folder', 'Fragment', 90, '2011-09-12 14:59:06', 90, '2011-09-12 14:21:49'),
(14, 1, 11, 0, 6, 7, 3, "Déconne pas Manu", 'folder', 'Fragment', 90, '2011-09-12 14:32:34', 90, '2011-09-12 14:21:49'),
(15, 1, 11, 1, 8, 9, 2, 'elle a mis sur le mur', 'default', 'Fragment', 90, '2011-09-12 15:06:43', 90, '2011-09-12 14:21:49'),
(16, 1, 11, 2, 12, 13, 2, "une photo de Rimbaud", 'default', 'Fragment', 90, '2011-09-12 15:25:16', 90, '2011-09-12 14:47:00'); |
Résultat, pour l'instant:
pour mon plan 3 (IDE_plans=3), l'arbre s'affiche sans problème.
Pour les autres valeurs (IDE_plans=1), l'arbre n'est pas affiché.
Merci à tous ceux qui auront pris la peine de lire tout cela.
__________________
"le rhume distillation - L’estomac est la cucurbite, le cerveau le chapiteau, & le nez le réfrigérant par lequel s’écoule goutte à goutte le produit de la distillation."
de Avicenne
Le monde se divise en deux: ceux qui utilisent le tag et les autres.
Un problème? venez en découdre!
|