Commit adf2869a authored by Johan Cwiklinski's avatar Johan Cwiklinski Committed by Johan Cwiklinski

Convert report to iterator

parent f0bb7817
......@@ -44,18 +44,35 @@ if (isset($_POST["locations_id"]) && $_POST["locations_id"]) {
echo "<div class='center spaced'><h2>".sprintf(__('Network report by location: %s'), $name).
"</h2></div>";
Report::reportForNetworkInformations("`glpi_locations`
INNER JOIN `glpi_netpoints`
ON (`glpi_netpoints`.`locations_id`
= `glpi_locations`.`id`)
INNER JOIN `glpi_networkportethernets`
ON (`glpi_networkportethernets`.`netpoints_id`
= `glpi_netpoints`.`id`)",
"PORT_1.`id` = `glpi_networkportethernets`.`networkports_id`",
getRealQueryForTreeItem("glpi_locations", $_POST["locations_id"]),
"`glpi_locations`.`completename`, PORT_1.`name`",
"`glpi_netpoints`.`name` AS extra, ",
Netpoint::getTypeName());
$where = [];
if (!empty($_POST['locations_id'])) {
$sons = getSonsOf('glpi_locations', $_POST['locations_id']);
$where = ['glpi_locations.id' => $sons];
}
Report::reportForNetworkInformations(
'glpi_locations', //from
['PORT_1' => 'id', 'glpi_networkportethernets' => 'networkports_id'], //joincrit
$where, //where
['glpi_netpoints.name AS extra'], //select
[], //left join
[
'glpi_netpoints' => [
'ON' => [
'glpi_netpoints' => 'locations_id',
'glpi_locations' => 'id'
]
],
'glpi_networkportethernets' => [
'ON' => [
'glpi_networkportethernets' => 'netpoints_id',
'glpi_netpoints' => 'id'
]
]
], //inner join
['glpi_locations.completename', 'PORT_1.name'], //order
Netpoint::getTypeName()
);
Html::footer();
......
......@@ -45,18 +45,30 @@ if (isset($_POST["prise"]) && $_POST["prise"]) {
echo "<div class='center spaced'><h2>".sprintf(__('Network report by outlet: %s'), $name).
"</h2></div>";
Report::reportForNetworkInformations("`glpi_netpoints`
LEFT JOIN `glpi_locations`
ON (`glpi_locations`.`id`
= `glpi_netpoints`.`locations_id`)
INNER JOIN `glpi_networkportethernets`
ON (`glpi_networkportethernets`.`netpoints_id`
= `glpi_netpoints`.`id`)",
"PORT_1.`id` = `glpi_networkportethernets`.`networkports_id`",
"`glpi_netpoints`.`id` = '".$_POST["prise"]."'",
'',
"`glpi_locations`.`completename` AS extra,",
Location::getTypeName());
Report::reportForNetworkInformations(
'glpi_netpoints', //from
['PORT_1' => 'id', 'glpi_networkportethernets' => 'networkports_id'], //joincrit
['glpi_netpoints.id' => $_POST["prise"]], //where
['glpi_locations.completename AS extra'], //select
[
'glpi_locations' => [
'ON' => [
'glpi_locations' => 'id',
'glpi_netpoints' => 'locations_id'
]
]
], //left join
[
'glpi_networkportethernets' => [
'ON' => [
'glpi_networkportethernets' => 'netpoints_id',
'glpi_netpoints' => 'id'
]
]
], //inner join
[], //order
Location::getTypeName()
);
Html::footer();
......
......@@ -47,11 +47,11 @@ if (isset($_POST["switch"]) && $_POST["switch"]) {
$name = Dropdown::getDropdownName("glpi_networkequipments", $_POST["switch"]);
echo "<div class='center spaced'><h2>".sprintf(__('Network report by hardware: %s'), $name).
"</h2></div>";
Report::reportForNetworkInformations("`glpi_networkequipments` AS ITEM",
"PORT_1.`itemtype` = 'NetworkEquipment'
AND PORT_1.`items_id` = ITEM.`id`",
"ITEM.`id` = '".$_POST["switch"]."'");
Report::reportForNetworkInformations(
'glpi_networkequipments AS ITEM', //from
['PORT_1' => 'items_id', 'ITEM' => 'id', ['AND' => ['PORT_1.itemtype' => 'NetworkEquipment']]], //joincrit
['ITEM.id' => $_POST['switch']] //where
);
Html::footer();
......
......@@ -313,72 +313,144 @@ class Report extends CommonGLPI{
/**
* @since 0.84
* Get report informations
*
* @param string $from From table
* @param array $joincrit Join criteria
* @param array $where Where clause
* @param array $select Extra select clause
* @param array $leftjoin Extra LEFT JOIN clause
* @param array $innerjoin Extra INNER JOIN clause
* @param array $order Order clause
* @param string $extra ?
*
* @param $networkport_prefix
* @param $networkport_crit
* @param $where_crit
* @param $order (default '')
* @param $field (default '')
* @param $extra (default '')
* @return void
*
* @since 10.0.0
**/
static function reportForNetworkInformations($networkport_prefix, $networkport_crit,
$where_crit, $order = '', $field = '', $extra = '') {
static function reportForNetworkInformations(
$from,
array $joincrit,
array $where = [],
array $select = [],
array $leftjoin = [],
array $innerjoin = [],
array $order = [],
$extra = ''
) {
global $DB;
// This SQL request matches the NetworkPort, then its NetworkName and IPAddreses. It also
// match opposite NetworkPort, then its NetworkName and IPAddresses.
// Results are groupes by NetworkPort. Then all IPs are concatenated by comma as separator.
$query = "SELECT $field
PORT_1.`itemtype` AS itemtype_1,
PORT_1.`items_id` AS items_id_1,
PORT_1.`id` AS id_1,
PORT_1.`name` AS port_1,
PORT_1.`mac` AS mac_1,
PORT_1.`logical_number` AS logical_1,
GROUP_CONCAT(ADDR_1.`name` SEPARATOR ',') AS ip_1,
PORT_2.`itemtype` AS itemtype_2,
PORT_2.`items_id` AS items_id_2,
PORT_2.`id` AS id_2,
PORT_2.`name` AS port_2,
PORT_2.`mac` AS mac_2,
GROUP_CONCAT(ADDR_2.`name` SEPARATOR ',') AS ip_2
FROM $networkport_prefix
INNER JOIN `glpi_networkports` AS PORT_1
ON ($networkport_crit
AND PORT_1.`is_deleted` = 0)
LEFT JOIN `glpi_networknames` AS NAME_1
ON (NAME_1.`itemtype` = 'NetworkPort'
AND PORT_1.`id` = NAME_1.`items_id`
AND NAME_1.`is_deleted` = 0)
LEFT JOIN `glpi_ipaddresses` AS ADDR_1
ON (ADDR_1.`itemtype` = 'NetworkName'
AND NAME_1.`id` = ADDR_1.`items_id`
AND ADDR_1.`is_deleted` = 0)
LEFT JOIN `glpi_networkports_networkports` AS LINK
ON (LINK.`networkports_id_1` = PORT_1.`id`
OR LINK.`networkports_id_2` = PORT_1.`id`)
LEFT JOIN `glpi_networkports` AS PORT_2
ON (PORT_2.`id` = IF(LINK.`networkports_id_1` = PORT_1.`id`,
LINK.`networkports_id_2`,
LINK.`networkports_id_1`))
LEFT JOIN `glpi_networknames` AS NAME_2
ON (NAME_2.`itemtype` = 'NetworkPort'
AND PORT_2.`id` = NAME_2.`items_id`
AND NAME_2.`is_deleted` = 0)
LEFT JOIN `glpi_ipaddresses` AS ADDR_2
ON (ADDR_2.`itemtype` = 'NetworkName'
AND NAME_2.`id` = ADDR_2.`items_id`
AND ADDR_2.`is_deleted` = 0)
WHERE $where_crit
GROUP BY PORT_1.`id`";
if (!empty($order)) {
$query .= "ORDER BY $order";
if (count($joincrit) === 3) {
$andcrit = array_pop($joincrit);
$andcrit['AND']['PORT_1.is_deleted'] = 0;
$joincrit[] = $andcrit;
} else {
$joincrit[]['AND']['PORT_1.is_deleted'] = 0;
}
$criteria = [
'SELECT' => array_merge([
'PORT_1.`itemtype` AS itemtype_1',
'PORT_1.`items_id` AS items_id_1',
'PORT_1.`id` AS id_1',
'PORT_1.`name` AS port_1',
'PORT_1.`mac` AS mac_1',
'PORT_1.`logical_number` AS logical_1',
new QueryExpression('GROUP_CONCAT(' . $DB->quoteName('ADDR_1.name') . ' SEPARATOR ' . $DB->quote(',') . ') AS ' . $DB->quoteName('ip_1')),
'PORT_2.`itemtype` AS itemtype_2',
'PORT_2.`items_id` AS items_id_2',
'PORT_2.`id` AS id_2',
'PORT_2.`name` AS port_2',
'PORT_2.`mac` AS mac_2',
new QueryExpression('GROUP_CONCAT(' . $DB->quoteName('ADDR_2.name') .' SEPARATOR ' . $DB->quote(',') . ') AS ' . $DB->quoteName('ip_2'))
], $select),
'FROM' => $from,
'INNER JOIN' => $innerjoin + [
'glpi_networkports AS PORT_1' => [
'ON' => $joincrit
]
],
'LEFT JOIN' => [
'glpi_networknames AS NAME_1' => [
'ON' => [
'PORT_1' => 'id',
'NAME_1' => 'items_id', [
'AND' => [
'NAME_1.itemtype' => 'NetworkPort',
'NAME_1.is_deleted' => 0
]
]
]
],
'glpi_ipaddresses AS ADDR_1' => [
'ON' => [
'NAME_1' => 'id',
'ADDR_1' => 'items_id', [
'AND' => [
'ADDR_1.itemtype' => 'NetworkName',
'ADDR_1.is_deleted' => 0
]
]
]
],
'glpi_networkports_networkports AS LINK' => [
'ON' =>[
'LINK' => 'networkports_id_1',
'PORT_1' => 'id', [
'OR' => [
'LINK.networkports_id_2' => $DB->quoteName('PORT_1.id')
]
]
]
],
'glpi_networkports AS PORT_2' => [
'ON' => [
'PORT_2' => 'id',
new QueryExpression(
'IF(' . $DB->quoteName('LINK.networkports_id_1') . ' = ' . $DB->quoteName('PORT_1.id') . ', ' .
$DB->quoteName('LINK.networkports_id_2') . ', ' .
$DB->quoteName('LINK.networkports_id_1') . ')'
)
]
],
'glpi_networknames AS NAME_2' => [
'ON' => [
'PORT_2' => 'id',
'NAME_2' => 'items_id', [
'AND' => [
'NAME_2.itemtype' => 'NetworkPort',
'NAME_2.is_deleted' => 0
]
]
]
],
'glpi_ipaddresses AS ADDR_2' => [
'ON' => [
'NAME_2' => 'id',
'ADDR_2' => 'items_id', [
'AND' => [
'ADDR_2.itemtype' => 'NetworkName',
'ADDR_2.is_deleted' => 0
]
]
]
]
] + $leftjoin,
'WHERE' => $where,
'GROUPBY' => ['PORT_1.id']
];
if (count($order)) {
$criteria['ORDER'] = $order;
}
$result = $DB->request($query);
if ($result->numrows() > 0) {
$iterator = $DB->request($criteria);
if (count($iterator)) {
echo "<table class='tab_cadre_fixehov'>";
echo "<tr>";
if (!empty($extra)) {
......@@ -405,7 +477,7 @@ class Report extends CommonGLPI{
echo "<th>".__('Device name')."</th>";
echo "</tr>\n";
foreach ($result as $line) {
while ($line = $iterator->next()) {
echo "<tr class='tab_bg_1'>";
// To ensure that the NetworkEquipment remain the first item, we test its type
......
......@@ -429,6 +429,21 @@ class DBmysqlIterator extends DbTestCase {
'SELECT * FROM `foo` LEFT JOIN (SELECT * FROM `bar`) AS `t2` ON (`t2`.`id` = `foo`.`fk`)'
);
$it = $this->it->execute(
'foo', [
'LEFT JOIN' => [
'bar' => [
'ON' => [
'foo' => 'id',
new \QueryExpression('IF(something, do, dont)')
]
]
]
]
);
$this->string($it->getSql())->isIdenticalTo(
'SELECT * FROM `foo` LEFT JOIN `bar` ON (`foo`.`id` = IF(something, do, dont))'
);
}
public function testHaving() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment