Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
enh(DT): fix search filter for recurrent downtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
sc979 committed Feb 19, 2019
1 parent 60965b4 commit 8a97b81
Showing 1 changed file with 47 additions and 39 deletions.
86 changes: 47 additions & 39 deletions www/class/centreonDowntime.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright 2005-2015 Centreon
* Centreon is developped by : Julien Mathis and Romain Le Merlus under
* Copyright 2005-2019 Centreon
* Centreon is developed by : Julien Mathis and Romain Le Merlus under
* GPL Licence 2.0.
*
* This program is free software; you can redistribute it and/or modify it under
Expand Down Expand Up @@ -84,7 +84,7 @@ public function initPeriods()
. 'FROM downtime_period ';

$res = $this->db->query($query);
while ($row = $res->fetchRow()) {
while ($row = $res->fetch()) {
$this->periods[$row['dt_id']][] = $row;
}
}
Expand All @@ -105,7 +105,7 @@ public function setSearch($search = '')
}

/**
* Get the number of rows for display, with applied search filter
* Get the number of rows to display, when a search filter is applied
*
* @return int The number of rows
*/
Expand All @@ -116,13 +116,17 @@ public function getNbRows()
return $this->nbRows;
}
/* Get the number of rows with a COUNT(*) */
$query = "SELECT COUNT(*) FROM downtime WHERE " . $this->search;
$query = "SELECT COUNT(*) FROM downtime" . ($this->search != '' ? ' WHERE :search' : '');
try {
$res = $this->db->query($query);
$res = $this->db->prepare($query);
if ($this->search != '') {
$res->bindValue(':search', $this->search, PDO::PARAM_STR);
}
$res->execute();
} catch (\PDOException $e) {
return 0;
}
$row = $res->fetchRow();
$row = $res->fetch();
$res->closeCursor();
return $row["COUNT(*)"];
}
Expand All @@ -149,20 +153,20 @@ public function getNbRows()
public function getList($num, $limit, $type = null)
{
if ($type == "h") {
$query = "SELECT SQL_CALC_FOUND_ROWS downtime.dt_id, dt_name, dt_description, dt_activate FROM downtime
WHERE (downtime.dt_id IN(SELECT dt_id FROM downtime_host_relation)
OR downtime.dt_id IN (SELECT dt_id FROM downtime_hostgroup_relation)) " .
$query = "SELECT SQL_CALC_FOUND_ROWS downtime.dt_id, dt_name, dt_description, dt_activate FROM downtime " .
"WHERE (downtime.dt_id IN(SELECT dt_id FROM downtime_host_relation) " .
"OR downtime.dt_id IN (SELECT dt_id FROM downtime_hostgroup_relation)) " .
($this->search == '' ? "" : " AND ") . $this->search .
" ORDER BY dt_name LIMIT " . $num * $limit . ", " . $limit;
} elseif ($type == "s") {
$query = "SELECT SQL_CALC_FOUND_ROWS downtime.dt_id, dt_name, dt_description, dt_activate FROM downtime
WHERE (downtime.dt_id IN (SELECT dt_id FROM downtime_service_relation)
OR downtime.dt_id IN (SELECT dt_id FROM downtime_servicegroup_relation)) " .
$query = "SELECT SQL_CALC_FOUND_ROWS downtime.dt_id, dt_name, dt_description, dt_activate FROM downtime " .
"WHERE (downtime.dt_id IN (SELECT dt_id FROM downtime_service_relation) " .
"OR downtime.dt_id IN (SELECT dt_id FROM downtime_servicegroup_relation)) " .
($this->search == '' ? "" : " AND ") . $this->search .
" ORDER BY dt_name LIMIT " . $num * $limit . ", " . $limit;
} else {
$query = "SELECT SQL_CALC_FOUND_ROWS downtime.dt_id, dt_name, dt_description, dt_activate
FROM downtime " . ($this->search == '' ? "" : "WHERE ") . $this->search .
$query = "SELECT SQL_CALC_FOUND_ROWS downtime.dt_id, dt_name, dt_description, dt_activate FROM downtime " .
($this->search == '' ? "" : "WHERE ") . $this->search .
" ORDER BY dt_name LIMIT " . $num * $limit . ", " . $limit;
}
try {
Expand All @@ -171,7 +175,7 @@ public function getList($num, $limit, $type = null)
return array();
}
$list = array();
while ($row = $res->fetchRow()) {
while ($row = $res->fetch()) {
$list[] = $row;
}
$res->closeCursor();
Expand Down Expand Up @@ -229,13 +233,15 @@ public function getPeriods($id)
*/
public function getInfos($id)
{
$query = "SELECT dt_name, dt_description, dt_activate FROM downtime WHERE dt_id=" . $id;
$query = "SELECT dt_name, dt_description, dt_activate FROM downtime WHERE dt_id= :id";
try {
$res = $this->db->query($query);
$res = $this->db->prepare($query);
$res->bindValue(':id', $id, PDO::PARAM_INT);
$res->execute();
} catch (\PDOException $e) {
return array('name' => '', 'description' => '', 'activate' => '');
}
$row = $res->fetchRow();
$row = $res->fetch();
return array(
'name' => $row['dt_name'],
'description' => $row['dt_description'],
Expand Down Expand Up @@ -270,21 +276,23 @@ public function getRelations($id)
foreach (array_keys($list) as $type) {
switch ($type) {
case 'host':
$query = "SELECT host_host_id as obj_id FROM downtime_host_relation WHERE dt_id = ";
$query = "SELECT host_host_id as obj_id FROM downtime_host_relation WHERE dt_id = :id";
break;
case 'hostgrp':
$query = "SELECT hg_hg_id as obj_id FROM downtime_hostgroup_relation WHERE dt_id = ";
$query = "SELECT hg_hg_id as obj_id FROM downtime_hostgroup_relation WHERE dt_id = :id";
break;
case 'svc':
$query = "SELECT CONCAT(host_host_id, CONCAT('-', service_service_id)) as obj_id
FROM downtime_service_relation WHERE dt_id = ";
$query = "SELECT CONCAT(host_host_id, CONCAT('-', service_service_id)) as obj_id " .
"FROM downtime_service_relation WHERE dt_id = :id";
break;
case 'svcgrp':
$query = "SELECT sg_sg_id as obj_id FROM downtime_servicegroup_relation WHERE dt_id = ";
$query = "SELECT sg_sg_id as obj_id FROM downtime_servicegroup_relation WHERE dt_id = :id";
break;
}
$res = $this->db->query($query . $id);
while ($row = $res->fetchRow()) {
$res = $this->db->prepare($query);
$res->bindValue(':id', $id, PDO::PARAM_INT);
$res->execute();
while ($row = $res->fetch()) {
$list[$type][] = $row['obj_id'];
}
$res->closeCursor();
Expand All @@ -307,7 +315,7 @@ public function getHostDowntimes()

try {
$res = $this->db->query($query);
while ($row = $res->fetchRow()) {
while ($row = $res->fetch()) {
$hostDowntimes[] = $row;
}
} catch (\PDOException $e) {
Expand Down Expand Up @@ -350,11 +358,11 @@ public function getServiceDowntimes()

try {
$res = $this->db->query($query);
while ($row = $res->fetchRow()) {
while ($row = $res->fetch()) {
$serviceDowntimes[] = $row;
}
} catch (\PDOException $e) {
// Nothind to do
// Nothing to do
}

return $serviceDowntimes;
Expand All @@ -377,11 +385,11 @@ public function getHostgroupDowntimes()

try {
$res = $this->db->query($query);
while ($row = $res->fetchRow()) {
while ($row = $res->fetch()) {
$hostgroupDowntimes[] = $row;
}
} catch (\PDOException $e) {
// Nothind to do
// Nothing to do
}

return $hostgroupDowntimes;
Expand Down Expand Up @@ -421,7 +429,7 @@ public function getServicegroupDowntimes()

try {
$res = $this->db->query($query);
while ($row = $res->fetchRow()) {
while ($row = $res->fetch()) {
$servicegroupDowntimes[] = $row;
}
} catch (\PDOException $e) {
Expand Down Expand Up @@ -479,7 +487,7 @@ public function duplicate($ids, $nb)
} catch (\PDOException $e) {
return;
}
$row = $res->fetchRow();
$row = $res->fetch();
$dt_name = $row['dt_name'];
$dt_desc = $row['dt_description'];
$dt_activate = $row['dt_activate'];
Expand All @@ -488,7 +496,7 @@ public function duplicate($ids, $nb)
/* Find the index for duplicate name */
$query = "SELECT COUNT(*) as nb FROM downtime WHERE dt_name = '" . $dt_name . "_" . $index . "'";
$res = $this->db->query($query);
$row = $res->fetchRow();
$row = $res->fetch();
if ($row["nb"] == 0) {
/* Insert the new downtime */
$rq = "INSERT INTO downtime (dt_name, dt_description, dt_activate)
Expand All @@ -501,7 +509,7 @@ public function duplicate($ids, $nb)
/* Get the new downtime id */
$query = "SELECT dt_id FROM downtime WHERE dt_name = '" . $dt_name . "_" . $index . "'";
$res = $this->db->query($query);
$row = $res->fetchRow();
$row = $res->fetch();
$res->closeCursor();
$id_new = $row['dt_id'];
/* Copy the periods for new downtime */
Expand All @@ -511,7 +519,7 @@ public function duplicate($ids, $nb)
SELECT " . $id_new . ", dtp_start_time, dtp_end_time, dtp_day_of_week, dtp_month_cycle,
dtp_day_of_month, dtp_fixed, dtp_duration, dtp_activate
FROM downtime_period WHERE dt_id = " . $id;
$res = $this->db->query($query);
$this->db->query($query);

/*
* Duplicate Relations for hosts
Expand All @@ -531,7 +539,7 @@ public function duplicate($ids, $nb)
$this->db->query("INSERT INTO downtime_service_relation
(dt_id, host_host_id, service_service_id)
SELECT $id_new, host_host_id, service_service_id
FROM downtime_service_relation WHERE dt_id = '$id'");
FROM downtime_service_relation WHERE dt_id = '$id'");

/*
* Duplicate Relations for servicegroups
Expand Down Expand Up @@ -578,7 +586,7 @@ public function add($name, $desc, $activate)
if ($error || $res->rowCount() == 0) {
return false;
}
$row = $res->fetchRow();
$row = $res->fetch();
return $row['dt_id'];
}

Expand Down Expand Up @@ -685,7 +693,7 @@ public function addPeriod($id, $infos)
$infos['duration'] . ")";
break;
}
$res = $this->db->query($query);
$this->db->query($query);
}

/**
Expand Down

0 comments on commit 8a97b81

Please sign in to comment.