Пример #1
0
}
if ($create_incident) {
    $id_inventory = (int) get_parameter('id_inventory');
    $inventories = array();
    if ($id_inventory) {
        if (!give_acl($config['id_user'], $id_inventory, "VR")) {
            audit_db($config['id_user'], $config["REMOTE_ADDR"], "ACL Violation", "Trying to access inventory #" . $id);
        } else {
            $inventories[$id_inventory] = get_db_value('name', 'tinventory', 'id', $id_inventory);
        }
    }
    $table_advanced->data[3][2] = print_select($inventories, 'incident_inventories', NULL, '', '', '', true, false, false, __('Objects affected'));
    $table_advanced->data[3][2] .= "&nbsp;&nbsp;<a href='javascript: incident_show_inventory_search(\"\",\"\",\"\",\"\",\"\",\"\");'>" . print_image('images/add.png', true, array('title' => __('Add'))) . "</a>";
    $table_advanced->data[3][2] .= "&nbsp;&nbsp;<a href='javascript: removeInventory();'>" . print_image('images/cross.png', true, array('title' => __('Remove'))) . "</a>";
} else {
    $inventories = get_inventories_in_incident($id);
    $table_advanced->data[3][2] = print_select($inventories, 'incident_inventories', NULL, '', '', '', true, false, false, __('Objects affected'), $blocked_incident);
    if (!$blocked_incident) {
        $table_advanced->data[3][2] .= "&nbsp;&nbsp;<a href='javascript: incident_show_inventory_search(\"\",\"\",\"\",\"\",\"\",\"\");'>" . print_image('images/add.png', true, array('title' => __('Add'))) . "</a>";
        $table_advanced->data[3][2] .= "&nbsp;&nbsp;<a href='javascript: removeInventory();'>" . print_image('images/cross.png', true, array('title' => __('Remove'))) . "</a>";
    }
}
foreach ($inventories as $inventory_id => $inventory_name) {
    $table_advanced->data[3][2] .= print_input_hidden("inventories[]", $inventory_id, true, 'selected-inventories');
}
// END TABLE ADVANCED
$table->colspan['row_advanced'][0] = 4;
$table->data['row_advanced'][0] = print_container('advanced_parameters_incidents_form', __('Advanced parameters'), print_table($table_advanced, true), 'closed', true, false);
$table->colspan[9][0] = 4;
$table->colspan[10][0] = 4;
$disabled_str = $disabled ? 'readonly="1"' : '';
$incident_details .= "</table>";
$left_side = print_container('incident_details', __('Details'), $incident_details, 'no');
/* Description */
$incident_description = clean_output_breaks($incident["descripcion"]);
$left_side .= print_container('incident_description', __('Description'), $incident_description);
// Advanced details
$editor = get_db_value_filter("nombre_real", "tusuario", array("id_usuario" => $incident["editor"]));
$creator_group = get_db_value_filter("nombre", "tgrupo", array("id_grupo" => $incident["id_group_creator"]));
if ($incident["sla_disabled"]) {
    $sla = __("Yes");
} else {
    $sla = __("No");
}
$task = incidents_get_incident_task_text($id);
$parent = __("Ticket") . " #" . $incident["id_parent"];
$objects = get_inventories_in_incident($id);
if ($objects) {
    $objects = implode(", ", $objects);
    $obj_table = "<td class='advanced_details_icons'>" . print_image('images/object.png', true) . "</td>";
    $obj_table .= "<td>" . __("Objects affected") . ":</td>";
    $obj_table .= "</tr>";
    $obj_table .= "<tr><td></td>";
    $obj_table .= "<td><table><tr><td class='advanced_details_icons'></td><td align='right'><b>" . $objects . "</b></td></tr></table></td>";
    $obj_table .= "</tr>";
} else {
    $objects = __("None");
    $obj_table = "<td class='advanced_details_icons'>" . print_image('images/object.png', true) . "</td>";
    $obj_table .= "<td><table><tr><td>" . __("There is no objects affected") . "</td></tr></table></td>";
}
$email_notify = $incident["notify_email"];
if ($email_notify) {
Пример #3
0
function incidents_get_filter_tickets_tree($filters, $mode = false, $limit = false)
{
    global $config;
    /* Set default values if none is set */
    $filters['string'] = isset($filters['string']) ? $filters['string'] : '';
    $filters['status'] = isset($filters['status']) ? $filters['status'] : -10;
    $filters['priority'] = isset($filters['priority']) ? $filters['priority'] : -1;
    $filters['id_group'] = isset($filters['id_group']) ? $filters['id_group'] : -1;
    $filters['id_company'] = isset($filters['id_company']) ? $filters['id_company'] : 0;
    $filters['id_inventory'] = isset($filters['id_inventory']) ? $filters['id_inventory'] : 0;
    $filters['id_incident_type'] = isset($filters['id_incident_type']) ? $filters['id_incident_type'] : 0;
    $filters['id_user'] = isset($filters['id_user']) ? $filters['id_user'] : '';
    $filters['id_user_or_creator'] = isset($filters['id_user_or_creator']) ? $filters['id_user_or_creator'] : '';
    $filters['from_date'] = isset($filters['from_date']) ? $filters['from_date'] : 0;
    $filters['first_date'] = isset($filters['first_date']) ? $filters['first_date'] : '';
    $filters['last_date'] = isset($filters['last_date']) ? $filters['last_date'] : '';
    $filters['id_creator'] = isset($filters['id_creator']) ? $filters['id_creator'] : '';
    $filters['editor'] = isset($filters['editor']) ? $filters['editor'] : '';
    $filters['closed_by'] = isset($filters['closed_by']) ? $filters['closed_by'] : '';
    $filters['resolution'] = isset($filters['resolution']) ? $filters['resolution'] : '';
    $filters["offset"] = isset($filters['offset']) ? $filters['offset'] : 0;
    $filters["group_by_project"] = isset($filters['group_by_project']) ? $filters['group_by_project'] : 0;
    $filters["id_task"] = isset($filters['id_task']) ? $filters['id_task'] : -1;
    $filters["sla_state"] = isset($filters['sla_state']) ? $filters['sla_state'] : 0;
    $filters["left_sla"] = isset($filters['left_sla']) ? $filters['left_sla'] : 0;
    $filters["right_sla"] = isset($filters['right_sla']) ? $filters['right_sla'] : 0;
    $filters["show_hierarchy"] = isset($filters['show_hierarchy']) ? $filters['show_hierarchy'] : 0;
    if (empty($filters['status'])) {
        $filters['status'] = implode(',', array_keys(get_indicent_status()));
    }
    // Not closed
    if ($filters["status"] == -10) {
        $filters['status'] = "1,2,3,4,5,6";
    }
    $resolutions = get_incident_resolutions();
    $sql_clause = '';
    if ($filters['priority'] != -1) {
        $sql_clause .= sprintf(' AND prioridad = %d', $filters['priority']);
    }
    if ($filters['id_group'] != 1) {
        if ($filters["show_hierarchy"]) {
            $children = groups_get_childrens($filters['id_group']);
            $ids = $filters['id_group'];
            foreach ($children as $child) {
                $ids .= "," . $child['id_grupo'];
            }
            $sql_clause .= " AND id_grupo IN (" . $ids . ")";
        } else {
            $sql_clause .= sprintf(' AND id_grupo = %d', $filters['id_group']);
        }
    }
    if (!empty($filters['id_user'])) {
        $sql_clause .= sprintf(' AND id_usuario = "%s"', $filters['id_user']);
    }
    if (!empty($filters['id_user_or_creator'])) {
        $sql_clause .= sprintf(' AND (id_usuario = "%s" OR id_creator = "%s")', $filters['id_user_or_creator'], $filters['id_user_or_creator']);
    }
    if (!empty($filters['resolution']) && $filters['resolution'] > -1) {
        $sql_clause .= sprintf(' AND resolution = %d', $filters['resolution']);
    }
    if ($filters['id_task'] == 0) {
        $sql_clause .= sprintf(' AND id_task = 0');
    } else {
        if ($filters['id_task'] != -1) {
            $sql_clause .= sprintf(' AND id_task = %d', $filters['id_task']);
        }
    }
    //Incident type 0 means all and incident type -1 means without type
    if ($filters["id_incident_type"] != -1) {
        if ($filters["id_incident_type"]) {
            $sql_clause .= sprintf(' AND id_incident_type = %d', $filters['id_incident_type']);
        }
        $incident_fields = array();
        foreach ($filters as $key => $value) {
            // If matchs an incident field, ad an element to the array with their real id and its data
            if (preg_match("/^type_field_/", $key)) {
                $incident_fields[preg_replace("/^type_field_/", "", $key)] = $value;
            }
        }
        foreach ($incident_fields as $id => $data) {
            if ($data !== "") {
                $sql_clause .= sprintf(' AND id_incidencia = ANY (SELECT id_incident
																	FROM tincident_field_data
																	WHERE id_incident_field = "%s"
																		AND data LIKE "%%%s%%")', $id, $data);
            }
        }
    }
    if (!empty($filters['from_date']) && $filters['from_date'] > 0) {
        $last_date_seconds = $filters['from_date'] * 24 * 60 * 60;
        $filters['first_date'] = date('Y-m-d H:i:s', time() - $last_date_seconds);
        $sql_clause .= sprintf(' AND inicio >= "%s"', $filters['first_date']);
        $filters['last_date'] = "";
    } else {
        if (!empty($filters['first_date'])) {
            $time = strtotime($filters['first_date']);
            //00:00:00 to set date at the beginig of the day
            $sql_clause .= sprintf(' AND inicio >= "%s"', date("Y-m-d 00:00:00", $time));
        }
        if (!empty($filters['last_date'])) {
            $time = strtotime($filters['last_date']);
            if (!empty($filters['first_date'])) {
                //23:59:59 to set date at the end of day
                $sql_clause .= sprintf(' AND inicio <= "%s"', date("Y-m-d 23:59:59", $time));
            } else {
                $time_from = strtotime($filters['first_date']);
                if ($time_from < $time) {
                    $sql_clause .= sprintf(' AND inicio <= "%s"', date("Y-m-d", $time));
                }
            }
        }
    }
    if (!empty($filters['id_creator'])) {
        $sql_clause .= sprintf(' AND id_creator = "%s"', $filters['id_creator']);
    }
    if (!empty($filters['editor'])) {
        $sql_clause .= sprintf(' AND editor = "%s"', $filters['editor']);
    }
    if (!empty($filters['closed_by'])) {
        $sql_clause .= sprintf(' AND closed_by = "%s"', $filters['closed_by']);
    }
    if (!empty($filters['sla_state'])) {
        switch ($filters['sla_state']) {
            case 0:
                $sla_filter = ' ';
                break;
            case 1:
                $sla_filter = "AND (sla_disabled = 0 AND affected_sla_id <> 0)";
                break;
            case 2:
                $sla_filter = "AND (sla_disabled = 0 AND affected_sla_id = 0)";
                break;
        }
    }
    if ($filters['order_by'] && !is_array($filters['order_by'])) {
        $order_by_array = json_decode(clean_output($filters["order_by"]), true);
    } else {
        $order_by_array = $filters['order_by'];
    }
    //Use config block size if no other was given
    if ($limit) {
        if (!isset($filters["limit"])) {
            $filters["limit"] = $config["block_size"];
        }
    }
    $order_by = "";
    if ($order_by_array) {
        foreach ($order_by_array as $key => $value) {
            if ($value) {
                $order_by .= " {$key} {$value}, ";
            }
        }
    }
    switch ($mode) {
        case 'count':
            //Just count items
            $sql = sprintf('SELECT COUNT(id_incidencia) FROM tincidencia FD
				WHERE estado IN (%s)
				%s
				AND (titulo LIKE "%%%s%%" OR descripcion LIKE "%%%s%%" 
				OR id_creator LIKE "%%%s%%" OR id_usuario LIKE "%%%s%%" 
				OR id_incidencia IN (SELECT id_incident FROM tincident_field_data WHERE data LIKE "%%%s%%"))
				%s', $filters['status'], $sql_clause, $filters['string'], $filters['string'], $filters['string'], $filters['string'], $filters['string'], $sla_filter);
            $count = get_db_value_sql($sql);
            if ($count === false) {
                return 0;
            }
            return $count;
            break;
        case 'tasks':
            $sql = sprintf('SELECT id_task FROM tincidencia FD
				WHERE estado IN (%s)
				%s
				AND (titulo LIKE "%%%s%%" OR descripcion LIKE "%%%s%%" 
				OR id_creator LIKE "%%%s%%" OR id_usuario LIKE "%%%s%%" 
				OR id_incidencia IN (SELECT id_incident FROM tincident_field_data WHERE data LIKE "%%%s%%"))
				%s
				GROUP BY id_task', $filters['status'], $sql_clause, $filters['string'], $filters['string'], $filters['string'], $filters['string'], $filters['string'], $sla_filter);
            $tasks = get_db_all_rows_sql($sql);
            if ($tasks === false) {
                return false;
            }
            return $tasks;
            break;
        case 'tickets':
        default:
            //Select all items and return all information
            $sql = sprintf('SELECT * FROM tincidencia FD
				WHERE estado IN (%s)
				%s
				AND (titulo LIKE "%%%s%%" OR descripcion LIKE "%%%s%%" 
				OR id_creator LIKE "%%%s%%" OR id_usuario LIKE "%%%s%%" 
				OR id_incidencia IN (SELECT id_incident FROM tincident_field_data WHERE data LIKE "%%%s%%"))
				%s
				ORDER BY %s actualizacion DESC', $filters['status'], $sql_clause, $filters['string'], $filters['string'], $filters['string'], $filters['string'], $filters['string'], $sla_filter, $order_by);
            $incidents = get_db_all_rows_sql($sql);
            if ($incidents === false) {
                return false;
            }
            $result = array();
            foreach ($incidents as $incident) {
                //Check external users ACLs
                $external_check = enterprise_hook("manage_external", array($incident));
                if ($external_check !== ENTERPRISE_NOT_HOOK && !$external_check) {
                    continue;
                } else {
                    //Normal ACL pass if IR for this group or if the user is the incident creator
                    //or if the user is the owner or if the user has workunits
                    $check_acl = enterprise_hook("incidents_check_incident_acl", array($incident));
                    if (!$check_acl) {
                        continue;
                    }
                }
                $inventories = get_inventories_in_incident($incident['id_incidencia'], false);
                if ($filters['id_inventory']) {
                    $found = false;
                    foreach ($inventories as $inventory) {
                        if ($inventory['id'] == $filters['id_inventory']) {
                            $found = true;
                            break;
                        }
                    }
                    if (!$found) {
                        continue;
                    }
                }
                if ($filters['id_company']) {
                    $found = false;
                    $user_creator = $incident['id_creator'];
                    $user_company = get_db_value('id_company', 'tusuario', 'id_usuario', $user_creator);
                    //If company do no match, dismiss incident
                    if ($filters['id_company'] != $user_company) {
                        continue;
                    }
                }
                if ($filters['left_sla']) {
                    $percent_sla_incident = format_numeric(get_sla_compliance_single_id($incident['id_incidencia']));
                    //If sla do not match, dismiss incident
                    if ($filters['left_sla'] > $percent_sla_incident) {
                        continue;
                    }
                }
                if ($filters['right_sla']) {
                    $percent_sla_incident = format_numeric(get_sla_compliance_single_id($incident['id_incidencia']));
                    //If sla do not match, dismiss incident
                    if ($filters['right_sla'] < $percent_sla_incident) {
                        continue;
                    }
                }
                array_push($result, $incident);
            }
            return $result;
            break;
    }
}
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

if (check_login () != 0) {
	audit_db ("Noauth", $config["REMOTE_ADDR"], "No authenticated access","Trying to access ticket viewer");
	require ("general/noaccess.php");
	exit;
}

require_once ('include/functions_inventories.php');
require_once ('include/functions_incidents.php');

$id_incident = $id;

$inventories = get_inventories_in_incident ($id_incident, false);

$table = new StdClass();
$table->class = 'listing';
$table->width = '100%';
$table->head = array ();
$table->head[0] = __('Relationship');
$table->head[1] = __('Company');
$table->head[2] = __('Contact');
$table->head[3] = __('Details');
$table->head[4] = __('Edit');
$table->size = array ();
$table->size[3] = '40px';
$table->size[4] = '40px';
$table->align[3] = 'center';
$table->align[4] = 'center';
Пример #5
0
function incidents_get_filter_tickets_tree($filters, $mode = false, $id_task = false)
{
    global $config;
    // TODO: Refactor to use the function 'filter_incidents'
    /* Set default values if none is set */
    $filters['inverse_filter'] = isset($filters['inverse_filter']) ? $filters['inverse_filter'] : false;
    $filters['string'] = isset($filters['string']) ? $filters['string'] : '';
    $filters['status'] = isset($filters['status']) ? $filters['status'] : 0;
    $filters['priority'] = isset($filters['priority']) ? $filters['priority'] : -1;
    $filters['id_group'] = isset($filters['id_group']) ? $filters['id_group'] : -1;
    $filters['id_company'] = isset($filters['id_company']) ? $filters['id_company'] : 0;
    $filters['id_inventory'] = isset($filters['id_inventory']) ? $filters['id_inventory'] : 0;
    $filters['id_incident_type'] = isset($filters['id_incident_type']) ? $filters['id_incident_type'] : 0;
    $filters['id_user'] = isset($filters['id_user']) ? $filters['id_user'] : '';
    $filters['id_user_or_creator'] = isset($filters['id_user_or_creator']) ? $filters['id_user_or_creator'] : '';
    $filters['from_date'] = isset($filters['from_date']) ? $filters['from_date'] : 0;
    $filters['first_date'] = isset($filters['first_date']) ? $filters['first_date'] : '';
    $filters['last_date'] = isset($filters['last_date']) ? $filters['last_date'] : '';
    $filters['id_creator'] = isset($filters['id_creator']) ? $filters['id_creator'] : '';
    $filters['editor'] = isset($filters['editor']) ? $filters['editor'] : '';
    $filters['closed_by'] = isset($filters['closed_by']) ? $filters['closed_by'] : '';
    $filters['resolution'] = isset($filters['resolution']) ? $filters['resolution'] : '';
    $filters["offset"] = isset($filters['offset']) ? $filters['offset'] : 0;
    $filters["group_by_project"] = isset($filters['group_by_project']) ? $filters['group_by_project'] : 0;
    $filters["id_task"] = isset($filters['id_task']) ? $filters['id_task'] : -1;
    $filters["sla_state"] = isset($filters['sla_state']) ? $filters['sla_state'] : 0;
    $filters["left_sla"] = isset($filters['left_sla']) ? $filters['left_sla'] : 0;
    $filters["right_sla"] = isset($filters['right_sla']) ? $filters['right_sla'] : 0;
    $filters["show_hierarchy"] = isset($filters['show_hierarchy']) ? $filters['show_hierarchy'] : 0;
    $filters["medals"] = isset($filters['medals']) ? $filters['medals'] : 0;
    $filters["parent_name"] = isset($filters['parent_name']) ? $filters['parent_name'] : '';
    ///// IMPORTANT: Write an inverse filter for every new filter /////
    $is_inverse = $filters['inverse_filter'];
    $sql_clause = '';
    // Status
    if (!empty($filters['status'])) {
        // Not closed
        if ($filters['status'] == -10) {
            if (!$is_inverse) {
                $sql_clause .= sprintf(' AND estado <> %d', STATUS_CLOSED);
            } else {
                $sql_clause .= sprintf(' AND estado = %d', STATUS_CLOSED);
            }
        } else {
            if (!$is_inverse) {
                $sql_clause .= sprintf(' AND estado = %d', $filters['status']);
            } else {
                $sql_clause .= sprintf(' AND estado <> %d', $filters['status']);
            }
        }
    }
    // Priority
    if ($filters['priority'] != -1) {
        if (!$is_inverse) {
            $sql_clause .= sprintf(' AND prioridad = %d', $filters['priority']);
        } else {
            $sql_clause .= sprintf(' AND prioridad <> %d', $filters['priority']);
        }
    }
    // Group
    if ($filters['id_group'] != 1) {
        if ($filters['show_hierarchy']) {
            $children = groups_get_childrens($filters['id_group']);
            $ids = $filters['id_group'];
            foreach ($children as $child) {
                $ids .= "," . $child['id_grupo'];
            }
            if (!$is_inverse) {
                $sql_clause .= sprintf(' AND id_grupo IN (%s)', $ids);
            } else {
                $sql_clause .= sprintf(' AND id_grupo NOT IN (%s)', $ids);
            }
        } else {
            if (!$is_inverse) {
                $sql_clause .= sprintf(' AND id_grupo = %d', $filters['id_group']);
            } else {
                $sql_clause .= sprintf(' AND id_grupo <> %d', $filters['id_group']);
            }
        }
    }
    // User
    if (!empty($filters['id_user'])) {
        if (!$is_inverse) {
            $sql_clause .= sprintf(' AND id_usuario = "%s"', $filters['id_user']);
        } else {
            $sql_clause .= sprintf(' AND id_usuario <> "%s"', $filters['id_user']);
        }
    }
    // User or creator
    if (!empty($filters['id_user_or_creator'])) {
        if (!$is_inverse) {
            $sql_clause .= sprintf(' AND (id_usuario = "%s" OR id_creator = "%s")', $filters['id_user_or_creator'], $filters['id_user_or_creator']);
        } else {
            $sql_clause .= sprintf(' AND (id_usuario <> "%s" AND id_creator <> "%s")', $filters['id_user_or_creator'], $filters['id_user_or_creator']);
        }
    }
    // Resolution
    if (!empty($filters['resolution']) && $filters['resolution'] > -1) {
        if (!$is_inverse) {
            $sql_clause .= sprintf(' AND resolution = %d', $filters['resolution']);
        } else {
            $sql_clause .= sprintf(' AND resolution <> %d', $filters['resolution']);
        }
    }
    // Task
    if ($id_task !== false) {
        // Don't apply the task inverse filter
        $sql_clause .= sprintf(' AND id_task = %d', $id_task);
    } else {
        if ($filters['id_task'] > 0) {
            if (!$is_inverse) {
                $sql_clause .= sprintf(' AND id_task = %d', $filters['id_task']);
            } else {
                $sql_clause .= sprintf(' AND id_task <> %d', $filters['id_task']);
            }
        }
    }
    // Incidents
    if (!empty($filters['id_incident_type']) && $filters['id_incident_type'] != -1) {
        if (!$is_inverse) {
            $sql_clause .= sprintf(' AND id_incident_type = %d', $filters['id_incident_type']);
        } else {
            $sql_clause .= sprintf(' AND id_incident_type <> %d', $filters['id_incident_type']);
        }
        // Incident fields
        $incident_fields = array();
        foreach ($filters as $key => $value) {
            // If matchs an incident field, ad an element to the array with their real id and its data
            if (preg_match('/^type_field_/', $key)) {
                $incident_fields[preg_replace('/^type_field_/', '', $key)] = $value;
            }
        }
        foreach ($incident_fields as $id => $data) {
            if (!empty($data)) {
                if (!$is_inverse) {
                    $sql_clause .= sprintf(' AND id_incidencia IN (SELECT id_incident
																	FROM tincident_field_data
																	WHERE id_incident_field = "%s"
																		AND data LIKE "%%%s%%")', $id, $data);
                } else {
                    $sql_clause .= sprintf(' AND id_incidencia NOT IN (SELECT id_incident
																	FROM tincident_field_data
																	WHERE id_incident_field = "%s"
																		AND data LIKE "%%%s%%")', $id, $data);
                }
            }
        }
    }
    // Date
    if (!empty($filters['from_date']) && $filters['from_date'] > 0) {
        $last_date_seconds = $filters['from_date'] * 24 * 60 * 60;
        $filters['first_date'] = date('Y-m-d H:i:s', time() - $last_date_seconds);
        if (!$is_inverse) {
            $sql_clause .= sprintf(' AND inicio >= "%s"', $filters['first_date']);
        } else {
            $sql_clause .= sprintf(' AND inicio < "%s"', $filters['first_date']);
        }
    } else {
        if (!empty($filters['first_date']) && !empty($filters['last_date'])) {
            // 00:00:00 to set date at the beginig of the day
            $start_time = strtotime($filters['first_date']);
            $start_date = date('Y-m-d 00:00:00', $start_time);
            // 23:59:59 to set date at the end of day
            $end_time = strtotime($filters['last_date']);
            $end_date = date('Y-m-d 23:59:59', $end_time);
            if (!$is_inverse) {
                $sql_clause .= sprintf(' AND inicio >= "%s"', $start_date);
                $sql_clause .= sprintf(' AND inicio <= "%s"', $end_date);
            } else {
                $sql_clause .= sprintf(' AND (inicio < "%s" OR inicio > "%s")', $start_date, $end_date);
            }
        } else {
            if (!empty($filters['first_date'])) {
                // 00:00:00 to set date at the beginig of the day
                $start_time = strtotime($filters['first_date']);
                $start_date = date('Y-m-d 00:00:00', $start_time);
                if (!$is_inverse) {
                    $sql_clause .= sprintf(' AND inicio >= "%s"', $start_date);
                } else {
                    $sql_clause .= sprintf(' AND inicio < "%s"', $start_date);
                }
            } else {
                if (!empty($filters['last_date'])) {
                    // 23:59:59 to set date at the end of day
                    $end_time = strtotime($filters['last_date']);
                    $end_date = date('Y-m-d 23:59:59', $end_time);
                    if (!$is_inverse) {
                        $sql_clause .= sprintf(' AND inicio <= "%s"', $end_date);
                    } else {
                        $sql_clause .= sprintf(' AND inicio > "%s"', $end_date);
                    }
                }
            }
        }
    }
    // Creator
    if (!empty($filters['id_creator'])) {
        if (!$is_inverse) {
            $sql_clause .= sprintf(' AND id_creator = "%s"', $filters['id_creator']);
        } else {
            $sql_clause .= sprintf(' AND id_creator <> "%s"', $filters['id_creator']);
        }
    }
    // Editor
    if (!empty($filters['editor'])) {
        if (!$is_inverse) {
            $sql_clause .= sprintf(' AND editor = "%s"', $filters['editor']);
        } else {
            $sql_clause .= sprintf(' AND editor <> "%s"', $filters['editor']);
        }
    }
    // Closed by
    if (!empty($filters['closed_by'])) {
        if (!$is_inverse) {
            $sql_clause .= sprintf(' AND closed_by = "%s"', $filters['closed_by']);
        } else {
            $sql_clause .= sprintf(' AND closed_by <> "%s"', $filters['closed_by']);
        }
    }
    // SLA
    $sla_filter = '';
    if (!empty($filters['sla_state'])) {
        $sla_fired_filter = 'AND (sla_disabled = 0 AND affected_sla_id <> 0)';
        $sla_not_fired_filter = 'AND (sla_disabled = 0 AND affected_sla_id = 0)';
        if ($filters['sla_state'] == 1) {
            $sla_filter = !$is_inverse ? $sla_fired_filter : $sla_not_fired_filter;
        } else {
            if ($filters['sla_state'] == 2) {
                $sla_filter = !$is_inverse ? $sla_not_fired_filter : $sla_fired_filter;
            }
        }
    }
    // Medals
    $medals_filter = '';
    if ($filters['medals']) {
        if ($filters['medals'] == 1) {
            if (!$is_inverse) {
                $medals_filter = 'AND gold_medals <> 0';
            } else {
                $medals_filter = 'AND gold_medals = 0';
            }
        } else {
            if ($filters['medals'] == 2) {
                if (!$is_inverse) {
                    $medals_filter = 'AND black_medals <> 0';
                } else {
                    $medals_filter = 'AND black_medals = 0';
                }
            }
        }
    }
    if (!empty($filters['parent_name'])) {
        $inventory_id = get_db_value('id', 'tinventory', 'name', $filters['parent_name']);
        if ($inventory_id) {
            if (!$is_inverse) {
                $sql_clause .= sprintf(' AND id_incidencia IN (SELECT id_incident FROM tincident_inventory WHERE
					id_inventory = %d)', $inventory_id);
            } else {
                $sql_clause .= sprintf(' AND id_incidencia NOT IN (SELECT id_incident FROM tincident_inventory WHERE
					id_inventory = %d)', $inventory_id);
            }
        }
    }
    if ($no_parents) {
        $sql_clause .= ' AND id_incidencia NOT IN (SELECT id_incidencia FROM tincidencia WHERE id_parent <> 0)';
    }
    // Order
    if ($filters['order_by'] && !is_array($filters['order_by'])) {
        $order_by_array = json_decode(clean_output($filters['order_by']), true);
    } else {
        $order_by_array = $filters['order_by'];
    }
    $order_by = '';
    if ($order_by_array) {
        foreach ($order_by_array as $key => $value) {
            if ($value) {
                $order_by .= " {$key} {$value}, ";
            }
        }
    }
    // Use config block size if no other was given
    if ($limit && !isset($filters['limit'])) {
        $filters['limit'] = $config['block_size'];
    }
    // Text filter
    $text_filter = '';
    if (!empty($filters['string'])) {
        if (!$is_inverse) {
            $text_filter = sprintf('AND (
				titulo LIKE "%%%s%%" OR descripcion LIKE "%%%s%%"
				OR id_creator LIKE "%%%s%%" OR id_usuario LIKE "%%%s%%"
				OR id_incidencia = %d
				OR id_incidencia IN (
					SELECT id_incident
					FROM tincident_field_data
					WHERE data LIKE "%%%s%%"))', $filters['string'], $filters['string'], $filters['string'], $filters['string'], $filters['string'], $filters['string']);
        } else {
            $text_filter = sprintf('AND (
				titulo NOT LIKE "%%%s%%" AND descripcion NOT LIKE "%%%s%%"
				AND id_creator NOT LIKE "%%%s%%" AND id_usuario NOT LIKE "%%%s%%"
				AND id_incidencia <> %d
				AND id_incidencia NOT IN (
					SELECT id_incident
					FROM tincident_field_data
					WHERE data LIKE "%%%s%%"))', $filters['string'], $filters['string'], $filters['string'], $filters['string'], $filters['string'], $filters['string']);
        }
    }
    switch ($mode) {
        case 'count':
            //Just count items
            $sql = sprintf('SELECT COUNT(id_incidencia) FROM tincidencia FD WHERE 1=1 %s %s %s %s', $sql_clause, $text_filter, $sla_filter, $medals_filter);
            return (int) get_db_value_sql($sql);
            break;
        case 'tasks':
            $sql = sprintf('SELECT id_task FROM tincidencia FD WHERE 1=1 %s %s %s %s GROUP BY id_task', $sql_clause, $text_filter, $sla_filter, $medals_filter);
            return get_db_all_rows_sql($sql);
            break;
        case 'tickets':
        default:
            //Select all items and return all information
            $sql = sprintf('SELECT * FROM tincidencia FD WHERE 1=1 %s %s %s %s ORDER BY %s actualizacion DESC', $sql_clause, $text_filter, $sla_filter, $medals_filter, $order_by);
            $incidents = get_db_all_rows_sql($sql);
            if ($incidents === false) {
                return false;
            }
            $result = array();
            foreach ($incidents as $incident) {
                //Check external users ACLs
                $standalone_check = enterprise_hook('manage_standalone', array($incident, 'read'));
                if ($standalone_check !== ENTERPRISE_NOT_HOOK && !$standalone_check) {
                    continue;
                } else {
                    // Normal ACL pass if IR for this group or if the user is the incident creator
                    // or if the user is the owner or if the user has workunits
                    $check_acl = enterprise_hook('incidents_check_incident_acl', array($incident));
                    if (!$check_acl) {
                        continue;
                    }
                }
                $inventories = get_inventories_in_incident($incident['id_incidencia'], false);
                // Inventory
                if ($filters['id_inventory']) {
                    $found = false;
                    foreach ($inventories as $inventory) {
                        if ($inventory['id'] == $filters['id_inventory']) {
                            $found = true;
                            break;
                        }
                    }
                    if (!$is_inverse && !$found) {
                        continue;
                    } else {
                        if ($is_inverse && $found) {
                            continue;
                        }
                    }
                }
                // Company
                if ($filters['id_company']) {
                    $found = false;
                    $user_creator = $incident['id_creator'];
                    $user_company = get_db_value('id_company', 'tusuario', 'id_usuario', $user_creator);
                    // Don't match, dismiss incident
                    if (!$is_inverse && $filters['id_company'] != $user_company) {
                        continue;
                    }
                    // Match, dismiss incident
                    if ($is_inverse && $filters['id_company'] == $user_company) {
                        continue;
                    }
                }
                // SLA
                if ($filters['left_sla']) {
                    $percent_sla_incident = format_numeric(get_sla_compliance_single_id($incident['id_incidencia']));
                    // Don't match, dismiss incident
                    if (!$is_inverse && $filters['left_sla'] > $percent_sla_incident) {
                        continue;
                    }
                    // Match, dismiss incident
                    if ($is_inverse && $filters['left_sla'] <= $percent_sla_incident) {
                        continue;
                    }
                }
                if ($filters['right_sla']) {
                    $percent_sla_incident = format_numeric(get_sla_compliance_single_id($incident['id_incidencia']));
                    // Don't match, dismiss incident
                    if (!$is_inverse && $filters['right_sla'] < $percent_sla_incident) {
                        continue;
                    }
                    // Match, dismiss incident
                    if ($is_inverse && $filters['right_sla'] >= $percent_sla_incident) {
                        continue;
                    }
                }
                array_push($result, $incident);
            }
            return $result;
            break;
    }
}