Exemple #1
0
function repairTree()
{
    $tbl_mdb_names = claro_sql_get_main_tbl();
    $tbl_category = $tbl_mdb_names['category'];
    //  get  list of all node
    $sql = " SELECT code, code_P, treePos, name, nb_childs\n               FROM `" . $tbl_category . "`\n               ORDER BY `treePos`";
    $catList = claro_sql_query_fetch_all($sql);
    $newTreePos = 1;
    $listSize = count($catList);
    // foreach node check code_parent,  treepos and nbchilds
    foreach ($catList as $cat) {
        $newCatList[$cat['code']] = $cat;
        $parentCatData = get_cat_data(get_cat_id_from_code($cat['code_P']));
        if ($cat['treePos'] < $parentCatData['treePos']) {
            $newCatList[$cat['code']]['newCode_P'] = ' root ';
            $newCatList[$cat['code']]['newTreePos'] = $listSize--;
        } else {
            if (!is_null($cat['code_P']) && !get_cat_id_from_code($cat['code_P'])) {
                $newCatList[$cat['code']]['newCode_P'] = ' root ';
                $newCatList[$cat['code']]['newTreePos'] = $listSize--;
            } else {
                $newCatList[$cat['code']]['newTreePos'] = $newTreePos++;
                $newCatList[$cat['code']]['newNb_childs'] = countChild($cat['code']);
            }
        }
    }
    reset($newCatList);
    $node_moved = false;
    // rescan node list  and  update data if difference was detected.
    foreach ($newCatList as $cat) {
        if (isset($cat['newCode_P']) && $cat['code_P'] != $cat['newCode_P']) {
            $sql = "UPDATE  `" . $tbl_category . "` " . ($cat['newCode_P'] == ' root ' ? "   SET code_P = null " : "   SET code_P = " . (int) $cat['newCode_P']) . " WHERE code = '" . claro_sql_escape($cat['code']) . "'";
            $node_moved = true;
            // repair ownance but brok countchild
            claro_sql_query($sql);
        }
        if (isset($cat['newNb_childs']) && $cat['nb_childs'] != $cat['newNb_childs']) {
            $sql = "UPDATE  `" . $tbl_category . "` " . "   SET nb_childs = " . (int) $cat['newNb_childs'] . " WHERE code = '" . claro_sql_escape($cat['code']) . "'";
            claro_sql_query($sql);
        }
        if ($cat['treePos'] != $cat['newTreePos']) {
            $sql = "UPDATE  `" . $tbl_category . "` " . "   SET treePos = " . (int) $cat['newTreePos'] . " WHERE code = '" . claro_sql_escape($cat['code']) . "'";
            claro_sql_query($sql);
        }
    }
    if ($node_moved) {
        return claro_failure::set_failure('node_moved');
    } else {
        return true;
    }
}
Exemple #2
0
 // The treePos max
 $maxTree = search_max_tree_pos();
 // The treePos of her and his childeren = max(treePos)+i
 $i = 1;
 while ($i <= $facultyEdit['nb_childs'] + 1) {
     $sql_TempTree = " UPDATE `" . $tbl_course_node . "`\n                                        SET treePos=" . $maxTree . "+" . $i . "\n                                        WHERE treePos = " . (int) $facultyEdit['treePos'] . "+" . $i . " - 1";
     claro_sql_query($sql_TempTree);
     $i++;
 }
 // Change treePos of the faculty they have a treePos > treePos of the last child
 $sql_ChangeTree = " UPDATE `" . $tbl_course_node . "`\n                                       SET treePos = treePos - " . (int) $facultyEdit['nb_childs'] . "-1\n                                       WHERE treePos > " . (int) $treePosLastChild . " AND treePos <= " . (int) $maxTree;
 claro_sql_query($sql_ChangeTree);
 // if the father isn't root
 if ($_REQUEST['fatherCat'] != 'NULL') {
     // Search treePos of the new father
     $newFather = get_cat_data(get_cat_id_from_code($_REQUEST['fatherCat']));
     //Ajoute a tous les treePos apres le nouveau pere le nombre d enfant + 1 de celui qu on deplace
     $sql_ChangeTree = " UPDATE `" . $tbl_course_node . "`\n                                          SET treePos=treePos + " . (int) $facultyEdit['nb_childs'] . " + 1\n                                          WHERE treePos > " . (int) $newFather['treePos'] . " and treePos <= " . (int) $maxTree;
     claro_sql_query($sql_ChangeTree);
     // the new treePos is the treePos of the new father+1
     $newTree = $newFather['treePos'] + 1;
 } else {
     // The new treePos is the last treePos exist
     $newTree = $maxTree;
 }
 // Change the treePos of her and his childeren
 $i = 0;
 while ($i <= $facultyEdit['nb_childs']) {
     $sql_ChangeTree = " UPDATE `" . $tbl_course_node . "`\n                                           SET treePos=" . $newTree . "+" . $i . "\n                                           WHERE treePos=" . $maxTree . "+" . $i . "+1";
     claro_sql_query($sql_ChangeTree);
     $i++;