$dsn['user'] = '******'; $dsn['pass'] = ''; $dsn['host'] = 'localhost'; $dsn['db'] = 'test'; $dsn['charset'] = 'utf8'; $dsn['errmode'] = 'exception'; define('DEBUG_MODE', false); $db = new SafeMySQL($dsn); $sql = 'SET NAMES utf8'; $db->query($sql); $tree_params = array('table' => 'test_sections', 'id' => 'section_id', 'left' => 'section_left', 'right' => 'section_right', 'level' => 'section_level'); $dbtree = new DbTreeExt($tree_params, $db); /* ------------------------ NAVIGATOR ------------------------ */ $navigator = 'You are here: '; if (!empty($_GET['section_id'])) { $parents = $dbtree->Parents((int) $_GET['section_id'], array('section_id', 'section_name')); foreach ($parents as $item) { if (@$_GET['section_id'] != $item['section_id']) { $navigator .= '<a href="dbtree.php?mode=' . $_GET['mode'] . '§ion_id=' . $item['section_id'] . '">' . $item['section_name'] . '</a> > '; } else { $navigator .= '<strong>' . $item['section_name'] . '</strong>'; } } } /* ------------------------ BRANCH ------------------------ */ if (!empty($_GET['mode']) && 'branch' == $_GET['mode']) { if (!isset($_GET['section_id'])) { $_GET['section_id'] = 1; } // Prepare data to view ajar tree $branch = $dbtree->Branch((int) $_GET['section_id'], array('section_id', 'section_level', 'section_name'));
/* ------------------------ MOVE 1 ------------------------ */ // Method 1: Swapping nodes within the same level and limits of one parent with all its children. if (!empty($_GET['action']) && 'move_1' == $_GET['action']) { // Change node ($_GET['section_id']) position and all its childrens to // before or after ($_POST['position']) node 2 ($_POST['section2_id']) $dbtree->ChangePositionAll((int) $_GET['section_id'], (int) $_POST['section2_id'], $_POST['position']); header('Location:dbtree_demo.php'); exit; } /* ------------------------ MOVE FORM------------------------ */ // Move section form if (!empty($_GET['action']) && 'move' == $_GET['action']) { // Prepare the restrictive data for the first method: // Swapping nodes within the same level and limits of one parent with all its children $current_section = $dbtree->GetNode((int) $_GET['section_id']); $parents = $dbtree->Parents((int) $_GET['section_id'], array('section_id'), array('and' => array('section_level = ' . ($current_section['section_level'] - 1)))); $item = current($parents); $branch = $dbtree->Branch($item['section_id'], array('section_id', 'section_name'), array('and' => array('section_level = ' . $current_section['section_level']))); // Create form ?> <table border="1" cellpadding="5" align="center"> <tr> <td> Move section </td> </tr> <tr> <td> <form action="dbtree_demo.php?action=move_1§ion_id=<?php echo $_GET['section_id']; ?>