Example #1
0
function api_graph_tree_item_list($graph_tree_id, $filter_array = "", $limit_sub_tree_id = "", $show_sub_tree_parent = false, $show_sub_tree_children = true, $current_page = 0, $rows_per_page = 0) {
	/* sanity checks */
	validate_id_die($graph_tree_id, "graph_tree_id");

	require_once(CACTI_BASE_PATH . "/lib/graph_tree/graph_tree_form.php");

	$sql_where = "";
	/* validation and setup for the WHERE clause */
	if ((is_array($filter_array)) && (sizeof($filter_array) > 0)) {
		/* validate each field against the known master field list */
		$field_errors = api_graph_tree_item_fields_validate(sql_filter_array_to_field_array($filter_array));

		/* if a field input error has occured, register the error in the session and return */
		if (sizeof($field_errors) > 0) {
			field_register_error($field_errors);
			return false;
		/* otherwise, form an SQL WHERE string using the filter fields */
		}else{
			$sql_where = sql_filter_array_to_where_string($filter_array, api_graph_tree_item_form_list(), false);
		}
	}

	$sql_limit = "";
	/* validation and setup for the LIMIT clause */
	if ((is_numeric($current_page)) && (is_numeric($rows_per_page)) && (!empty($current_page)) && (!empty($rows_per_page))) {
		$sql_limit = "limit " . ($rows_per_page * ($current_page - 1)) . ",$rows_per_page";
	}

	/* only show tree items under this item if specified */
	if (db_integer_validate($limit_sub_tree_id, false, false)) {
		$graph_tree_item = api_graph_tree_item_get($limit_sub_tree_id);
		$search_key = substr($graph_tree_item["order_key"], 0, (api_graph_tree_item_depth_get($graph_tree_item["order_key"]) * CHARS_PER_TIER));

		if ($show_sub_tree_children == true) {
			$sql_where .= "and graph_tree_items.order_key like '$search_key%%'";
		}else{
			$sql_where .= " and graph_tree_items.order_key like '$search_key" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', (MAX_TREE_DEPTH * CHARS_PER_TIER) - (strlen($search_key) + CHARS_PER_TIER)) . "'";
		}

		if ($show_sub_tree_parent == false) {
			$sql_where .= "and graph_tree_items.id != $limit_sub_tree_id";
		}
	}

	return db_fetch_assoc("select
		graph_tree_items.id,
		graph_tree_items.order_key,
		graph_tree_items.sort_children_type,
		graph_tree_items.device_grouping_type,
		graph_tree_items.item_type,
		graph_tree_items.item_value,
		graph.title_cache as graph_title,
		host.description as host_description,
		host.hostname as host_hostname
		from graph_tree_items
		left join graph on (graph_tree_items.item_value = graph.id and graph_tree_items.item_type = " . TREE_ITEM_TYPE_GRAPH . ")
		left join host on (graph_tree_items.item_value = host.id and graph_tree_items.item_type = " . TREE_ITEM_TYPE_HOST . ")
		where graph_tree_items.graph_tree_id = " . sql_sanitize($graph_tree_id) . "
		$sql_where
		order by graph_tree_items.order_key
		$sql_limit");
}
Example #2
0
function form_save()
{
    if ($_POST["action_post"] == "graph_tree_item_edit") {
        /* cache all post field values */
        init_post_field_cache();
        $form_graph_tree_item["graph_tree_id"] = $_POST["graph_tree_id"];
        $form_graph_tree_item["device_grouping_type"] = $_POST["device_grouping_type"];
        $form_graph_tree_item["parent_item_id"] = $_POST["parent_item_id"];
        $form_graph_tree_item["sort_children_type"] = $_POST["sort_children_type"];
        $form_graph_tree_item["item_type"] = $_POST["item_type"];
        $form_graph_tree_item["propagate_changes"] = html_boolean(isset($_POST["propagate_changes"]) ? $_POST["propagate_changes"] : "");
        if ($_POST["item_type"] == TREE_ITEM_TYPE_HEADER) {
            $form_graph_tree_item["item_value"] = $_POST["item_value_title"];
        } else {
            if ($_POST["item_type"] == TREE_ITEM_TYPE_GRAPH) {
                $form_graph_tree_item["item_value"] = $_POST["item_value_graph"];
            } else {
                if ($_POST["item_type"] == TREE_ITEM_TYPE_HOST) {
                    $form_graph_tree_item["item_value"] = $_POST["item_value_device"];
                }
            }
        }
        /* obtain a list of visible graph tree item fields on the form */
        $visible_fields = api_graph_tree_item_visible_field_list($_POST["item_type"]);
        /* all non-visible fields on the form should be discarded */
        foreach ($visible_fields as $field_name) {
            $v_form_graph_tree_item[$field_name] = $form_graph_tree_item[$field_name];
        }
        /* validate graph tree item preset fields */
        field_register_error(api_graph_tree_item_fields_validate($v_form_graph_tree_item, "|field|"));
        /* the header title textbox goes by a different name on the form */
        if (field_error_isset("item_value") && $_POST["item_type"] == TREE_ITEM_TYPE_HEADER) {
            field_register_error("item_value_title");
        }
        if (!is_error_message()) {
            $graph_tree_item_id = api_graph_tree_item_save($_POST["id"], $form_graph_tree_item);
        }
        if (is_error_message()) {
            header("Location: graph_trees_items.php?action=edit&tree_id=" . $_POST["graph_tree_id"] . (empty($graph_tree_item_id) ? "" : "&id={$graph_tree_item_id}"));
        } else {
            header("Location: graph_trees.php?action=edit&id=" . $_POST["graph_tree_id"]);
        }
        /* submit button on the actions area page */
    } else {
        if ($_POST["action_post"] == "box-1") {
            $selected_rows = explode(":", $_POST["box-1-action-area-selected-rows"]);
            if ($_POST["box-1-action-area-type"] == "remove") {
                foreach ($selected_rows as $graph_tree_item_id) {
                    api_graph_tree_item_remove($graph_tree_item_id);
                }
            }
            header("Location: graph_trees.php?action=edit&id=" . $_POST["graph_tree_id"]);
        }
    }
}