Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree sort mechanism does not take sites into account #4241

Closed
browniebraun opened this issue Apr 22, 2021 · 2 comments
Closed

Tree sort mechanism does not take sites into account #4241

browniebraun opened this issue Apr 22, 2021 · 2 comments
Labels
bug Undesired behaviour unverified Some days we don't have a clue

Comments

@browniebraun
Copy link
Member

Describe the bug

Sites added to a branch will always be unsorted.

To Reproduce

Create some site records, add them to a tree branch, change sorting and see that nothing happens.

Expected behavior

Sites will be sorted in the same way like graphs, hosts or branches.

Changes

Should not be so difficult to add from my point of view:

in /lib/api_tree.php, function api_tree_sort_branch()

  1. extend SQL statement at line 872-877
    $sort_array = array_rekey(db_fetch_assoc_prepared('SELECT id, title FROM graph_tree_items AS gti WHERE parent = ? AND graph_tree_id = ? AND local_graph_id = 0 AND host_id = 0
    to $sort_array = array_rekey(db_fetch_assoc_prepared('SELECT id, title FROM graph_tree_items AS gti WHERE parent = ? AND graph_tree_id = ? AND local_graph_id = 0 AND host_id = 0 AND site_id = 0
    to exclude site records from branch sorting.

  2. add new code for site sorting after previous part at line ~ 906

` if ($sort_style == TREE_ORDERING_ALPHABETIC) {
$order_by = 'ORDER BY s.name';
} else {
$order_by = 'ORDER BY position';
}

    $sort_array = array_rekey(db_fetch_assoc_prepared('SELECT s.name, gti.id
            FROM graph_tree_items AS gti
            INNER JOIN sites AS s
            ON s.id = gti.site_id
            WHERE parent = ?
            AND graph_tree_id = ?
            AND local_graph_id = 0
            AND site_id > 0 ' . $order_by, array($parent, $tree_id)), 'id', 'name');

    if (cacti_sizeof($sort_array)) {
            if ($sort_style == TREE_ORDERING_NUMERIC) {
                    asort($sort_array, SORT_NUMERIC);
            } elseif ($sort_style == TREE_ORDERING_ALPHABETIC) {
                    // Let's let the database do it!
            } elseif ($sort_style == TREE_ORDERING_NATURAL) {
                    if (defined('SORT_FLAG_CASE')) {
                            asort($sort_array, SORT_NATURAL | SORT_FLAG_CASE);
                    } else {
                            natcasesort($sort_array);
                    }
            }

            foreach($sort_array as $id => $element) {
                    $sort = db_fetch_cell_prepared('SELECT sort_children_type FROM graph_tree_items WHERE id = ?', array($id));
                    if ($sort == TREE_ORDERING_INHERIT) {
                            $first_child = db_fetch_cell_prepared('SELECT id FROM graph_tree_items WHERE parent = ? ORDER BY position LIMIT 1', array($id));
                            if (!empty($first_child)) {
                                    $level++;
                                    api_tree_sort_branch($first_child, $tree_id, false);
                            }
                    }

                    db_execute_prepared('UPDATE graph_tree_items SET position = ? WHERE id = ?', array($sequence, $id));
                    $sequence++;
            }
    }

`

@browniebraun browniebraun added bug Undesired behaviour unverified Some days we don't have a clue labels Apr 22, 2021
@TheWitness
Copy link
Member

Can you create a pull request?

browniebraun added a commit that referenced this issue Apr 22, 2021
TheWitness added a commit that referenced this issue Apr 22, 2021
Just some cleanup.  No programmatic changes
@netniV
Copy link
Member

netniV commented May 21, 2021

This was in the 1.2.17 release wasn't it? If so, it's missing the 1.2.17 milestone marker.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Undesired behaviour unverified Some days we don't have a clue
Projects
None yet
Development

No branches or pull requests

3 participants