Commit 2365e15a authored by Johan Cwiklinski's avatar Johan Cwiklinski Committed by Johan Cwiklinski
Browse files

Convert to iterator, replace hardcoded backquotes

Backport from master
parent 6c3325bc
......@@ -63,6 +63,8 @@ The present file will list all changes made to the project; according to the
- `Item_SoftwareVersion::displaySoftsByLicense` renamed to `Item_SoftwareVersion::displaySoftwareByLicense`
- `Item_SoftwareVersion::cloneComputer` renamed to `Item_SoftwareVersion::cloneItem`
- `Transfer::transferComputerSoftwares` renamed to `Transfer::transferItemSoftwares`
- 'getRealQueryForTreeItem()'
- ``getCommonSelect`` and ``getCommonLeftJoin()`` from ``Ticket``, ``Change`` and ``Problem`` are replaced with ``getCommonCriteria()`` compliant with db iterator
#### Removed
......
......@@ -48,69 +48,98 @@ if (class_exists($_POST["itemtype"])
&& isset($_POST["item"])) {
$table = getTableForItemType($_POST["itemtype"]);
$joins = [];
$name_field = new QueryExpression("'' AS " . $DB->quoteName('npname'));
if ($_POST['instantiation_type'] == 'NetworkPortEthernet') {
$npname_field = "`glpi_netpoints`.`name`";
$JOINS = "LEFT JOIN `glpi_networkportethernets`
ON (`glpi_networkportethernets`.`id` = `glpi_networkports`.`id`)
LEFT JOIN `glpi_netpoints`
ON (`glpi_netpoints`.`id`=`glpi_networkportethernets`.`netpoints_id`)";
} else {
$npname_field = "''";
$JOINS = "";
$name_field = 'glpi_netpoints.name AS npname';
$joins = [
'glpi_networkportethernets' => [
'ON' => [
'glpi_networkportethernets' => 'id',
'glpi_networkports' => 'id'
]
],
'glpi_netpoints' => [
'ON' => [
'glpi_networkportethernets' => 'netpoints_id',
'glpi_netpoints' => 'id'
]
]
];
}
$query = "SELECT DISTINCT `glpi_networkports_networkports`.`id` AS wid,
`glpi_networkports`.`id` AS did,
`$table`.`name` AS cname,
`glpi_networkports`.`name` AS nname,
$npname_field AS npname
FROM `$table`
LEFT JOIN `glpi_networkports`
ON (`glpi_networkports`.`items_id` = '".$_POST['item']."'
AND `glpi_networkports`.`itemtype` = '".$_POST["itemtype"]."'
AND `glpi_networkports`.`items_id` = `$table`.`id`
AND `glpi_networkports`.`instantiation_type` = '".$_POST['instantiation_type']."')
LEFT JOIN `glpi_networkports_networkports`
ON (`glpi_networkports_networkports`.`networkports_id_1` = `glpi_networkports`.`id`
OR `glpi_networkports_networkports`.`networkports_id_2`=`glpi_networkports`.`id`)
$JOINS
WHERE `glpi_networkports_networkports`.`id` IS NULL
AND `glpi_networkports`.`id` IS NOT NULL
AND `glpi_networkports`.`id` <> '".$_POST['networkports_id']."'
AND `$table`.`is_deleted` = 0
AND `$table`.`is_template` = 0
ORDER BY `glpi_networkports`.`id`";
$result = $DB->query($query);
$criteria = [
'SELECT' => [
'glpi_networkports_networkports.id AS wid',
'glpi_networkports.id AS did',
"$table.name AS cname",
'glpi_networkports.name AS nname',
$name_field
],
'DISTINCT' => true,
'FROM' => $table,
'LEFT JOIN' => [
'glpi_networkports' => [
'ON' => [
'glpi_networkports' => 'items_id',
$table => 'id', [
'AND' => [
'glpi_networkports.items_id' => $_POST['item'],
'glpi_networkports.itemtype' => $_POST["itemtype"],
'glpi_networkports.instantiation_type' => $_POST['instantiation_type']
]
]
]
],
'glpi_networkports_networkports' => [
'ON' => [
'glpi_networkports_networkports' => 'networkports_id_1',
'glpi_networkports' => 'id', [
'OR' => [
'glpi_networkports_networkports.networkports_id_2' => $DB->quoteName('glpi_networkports.id')
]
]
]
]
] + $joins,
'WHERE' => [
'glpi_networkports_networkports.id' => null,
'NOT' => ['glpi_networkports.id' => null],
'glpi_networkports.id' => ['<>', $_POST['networkports_id']],
"$table.is_deleted" => 0,
"$table.is_template" => 0
],
'ORDERBY' => 'glpi_networkports.id'
];
$iterator = $Db->request($criteria);
echo "<br>";
$values = [];
if ($DB->numrows($result)) {
while ($data = $DB->fetchAssoc($result)) {
// Device name + port name
$output = $output_long = $data['cname'];
while ($data = $iterator->next()) {
// Device name + port name
$output = $output_long = $data['cname'];
if (!empty($data['nname'])) {
$output = sprintf(__('%1$s - %2$s'), $output, $data['nname']);
//TRANS: %1$s is device name, %2$s is port name
$output_long = sprintf(__('%1$s - The port %2$s'), $output_long, $data['nname']);
}
if (!empty($data['nname'])) {
$output = sprintf(__('%1$s - %2$s'), $output, $data['nname']);
//TRANS: %1$s is device name, %2$s is port name
$output_long = sprintf(__('%1$s - The port %2$s'), $output_long, $data['nname']);
}
// display netpoint (which will be copied)
if (!empty($data['npname'])) {
$output = sprintf(__('%1$s - %2$s'), $output, $data['npname']);
//TRANS: %1$s is a string (device name - port name...), %2$s is network outlet name
$output_long = sprintf(__('%1$s - Network outlet %2$s'), $output_long, $data['npname']);
}
$ID = $data['did'];
// display netpoint (which will be copied)
if (!empty($data['npname'])) {
$output = sprintf(__('%1$s - %2$s'), $output, $data['npname']);
//TRANS: %1$s is a string (device name - port name...), %2$s is network outlet name
$output_long = sprintf(__('%1$s - Network outlet %2$s'), $output_long, $data['npname']);
}
$ID = $data['did'];
if ($_SESSION["glpiis_ids_visible"] || empty($output) || empty($output_long)) {
$output = sprintf(__('%1$s (%2$s)'), $output, $ID);
$output_long = sprintf(__('%1$s (%2$s)'), $output_long, $ID);
}
$values[$ID] = $output_long;
if ($_SESSION["glpiis_ids_visible"] || empty($output) || empty($output_long)) {
$output = sprintf(__('%1$s (%2$s)'), $output, $ID);
$output_long = sprintf(__('%1$s (%2$s)'), $output_long, $ID);
}
$values[$ID] = $output_long;
}
Dropdown::showFromArray($_POST['myname'], $values, ['display_emptychoice' => true]);
}
......@@ -45,4 +45,4 @@ if ($_POST['id'] > 0) {
$field->getEmpty();
$field->fields['itemtype'] = $_POST['itemtype'];
}
$field->selectCriterias($field);
$field->selectCriterias();
......@@ -44,41 +44,43 @@ if ($_POST['softwares_id'] > 0) {
$_POST['value'] = 0;
}
$where = '';
$where = [];
if (isset($_POST['used'])) {
$used = $_POST['used'];
if (count($used)) {
$where = " AND `glpi_softwareversions`.`id` NOT IN ('".implode("','", $used)."')";
$where = ['NOT' => ['glpi_softwareversions.id' => $used]];
}
}
// Make a select box
$query = "SELECT DISTINCT `glpi_softwareversions`.*,
`glpi_states`.`name` AS sname
FROM `glpi_softwareversions`
LEFT JOIN `glpi_states` ON (`glpi_softwareversions`.`states_id` = `glpi_states`.`id`)
WHERE `glpi_softwareversions`.`softwares_id` = '".$_POST['softwares_id']."'
$where
ORDER BY `name`";
$result = $DB->query($query);
$number = $DB->numrows($result);
$iterator = $DB->request([
'SELECT' => ['glpi_softwareversions.*', 'glpi_states.name AS sname'],
'DISTINCT' => true,
'FROM' => 'glpi_softwareversions',
'LEFT JOIN' => [
'glpi_states' => [
'ON' => [
'glpi_softwareversions' => 'states_id',
'glpi_states' => 'id'
]
]
],
'WHERE' => ['glpi_softwareversions.softwares_id' => $_POST['softwares_id']] + $where
]);
$number = count($iterator);
$values = [];
if ($number) {
while ($data = $DB->fetchAssoc($result)) {
$ID = $data['id'];
$output = $data['name'];
if (empty($output) || $_SESSION['glpiis_ids_visible']) {
$output = sprintf(__('%1$s (%2$s)'), $output, $ID);
}
if (!empty($data['sname'])) {
$output = sprintf(__('%1$s - %2$s'), $output, $data['sname']);
}
$values[$ID] = $output;
while ($data = $iterator->next()) {
$ID = $data['id'];
$output = $data['name'];
if (empty($output) || $_SESSION['glpiis_ids_visible']) {
$output = sprintf(__('%1$s (%2$s)'), $output, $ID);
}
if (!empty($data['sname'])) {
$output = sprintf(__('%1$s - %2$s'), $output, $data['sname']);
}
$values[$ID] = $output;
}
Dropdown::showFromArray($_POST['myname'], $values, ['display_emptychoice' => true]);
......
......@@ -65,8 +65,6 @@ if (isset($_POST["itemtype"])
$search = $search[$_POST["id_field"]];
$FIELDNAME_PRINTED = false;
echo "<table class='tab_glpi' width='100%'><tr><td>";
$plugdisplay = false;
......@@ -101,9 +99,7 @@ if (isset($_POST["itemtype"])
echo $item->getValueToSelect($search, $fieldname, $values, $options);
}
if (!$FIELDNAME_PRINTED) {
echo "<input type='hidden' name='field' value='$fieldname'>";
}
echo "<input type='hidden' name='field' value='$fieldname'>";
echo "</td>";
if ($inline) {
echo "<td><input type='submit' name='massiveaction' class='submit' value='$submitname'></td>";
......
......@@ -44,21 +44,20 @@ if ($_POST['softwares_id'] > 0) {
$_POST['value'] = 0;
}
$restrict = getEntitiesRestrictRequest(' AND', 'glpi_softwarelicenses', 'entities_id',
$_POST['entity_restrict'], true);
// Make a select box
$query = "SELECT DISTINCT *
FROM `glpi_softwarelicenses`
WHERE `glpi_softwarelicenses`.`softwares_id` = '".$_POST['softwares_id']."'
$restrict
ORDER BY `name`";
$result = $DB->query($query);
$number = $DB->numrows($result);
$iterator = $DB->request([
'DISTINCT' => true,
'FROM' => 'glpi_softwarelicenses',
'WHERE' => [
'glpi_softwarelicenses.softwares_id' => (int)$_POST['softwares_id']
] + getEntitiesRestrictCriteria('glpi_softwarelicenses', 'entities_id', $_POST['entity_restrict'], true),
'ORDERBY' => 'name'
]);
$number = count($iterator);
$values = [];
if ($number) {
while ($data = $DB->fetchAssoc($result)) {
while ($data = $iterator->next()) {
$ID = $data['id'];
$output = $data['name'];
......
......@@ -79,32 +79,43 @@ if (isset($_GET['node'])) {
}
} else { // standard node
$node_id = $_GET['node'];
$query = "SELECT ent.`id`, ent.`name`, ent.`sons_cache`, count(sub_entities.id) as nb_subs
FROM `glpi_entities` as ent
LEFT JOIN `glpi_entities` as sub_entities
ON sub_entities.entities_id = ent.id
WHERE ent.`entities_id` = '$node_id'
GROUP BY ent.`id`, ent.`name`, ent.`sons_cache`
ORDER BY `name`";
$iterator = $DB->request([
'SELECT' => [
'ent.id',
'ent.name',
'ent.sons_cache',
'COUNT' => 'sub_entities.id AS nb_subs'
],
'FROM' => 'glpi_entities AS ent',
'LEFT JOIN' => [
'glpi_entities AS sub_entities' => [
'ON' => [
'sub_entities' => 'entities_id',
'ent' => 'id'
]
]
],
'WHERE' => ['ent.entities_id' => $node_id],
'GROUPBY' => ['ent.id', 'ent.name', 'ent.sons_cache'],
'ORDERBY' => 'name'
]);
if ($result = $DB->query($query)) {
while ($row = $DB->fetchAssoc($result)) {
$path = [
'id' => $row['id'],
'text' => $row['name']
];
while ($row = $iterator->next()) {
$path = [
'id' => $row['id'],
'text' => $row['name']
];
if ($row['nb_subs'] > 0) {
//apend a i tag (one of shortest tags) to have the is_recursive link
$path['text'].= '<i/>';
$path['children'] = true;
if ($row['nb_subs'] > 0) {
//apend a i tag (one of shortest tags) to have the is_recursive link
$path['text'].= '<i/>';
$path['children'] = true;
if (isset($ancestors[$row['id']])) {
$path['state']['opened'] = 'true';
}
if (isset($ancestors[$row['id']])) {
$path['state']['opened'] = 'true';
}
$nodes[] = $path;
}
$nodes[] = $path;
}
}
echo json_encode($nodes);
......
......@@ -48,12 +48,14 @@ if ($_POST['items_id']
$linktype = $devicetype::getItem_DeviceType();
if (count($linktype::getSpecificities())) {
$keys = array_keys($linktype::getSpecificities());
array_walk($keys, function (&$val) use ($DB) { return $DB->quoteName($val); });
$name_field = new QueryExpression(
"CONCAT_WS(' - ', `" . implode('`, `', array_keys($linktype::getSpecificities())) . "`)"
. "AS `name`"
"CONCAT_WS(' - ', " . implode(', ', $keys) . ")"
. "AS ".$DB->quoteName("name")
);
} else {
$name_field = "id AS name";
$name_field = 'id AS name';
}
$result = $DB->request(
[
......
......@@ -70,11 +70,12 @@ function xmlbackup() {
$result = $DB->listTables();
$i = 0;
$query = [];
while ($line = $result->next()) {
$table = $line['TABLE_NAME'];
$query[$i] = "SELECT *
FROM `$table`";
FROM ".$DB->quoteName($table);
$i++;
}
......@@ -147,9 +148,9 @@ function get_content($DB, $table, $from, $limit) {
if ($iterator->count()) {
while ($row = $iterator->next()) {
$insert = "INSERT INTO `$table` VALUES (";
$insert = "INSERT INTO ".$DB->quoteName($table)." VALUES (";
foreach ($row as $field_key => $field_val) {
foreach ($row as $field_val) {
if (is_null($field_val)) {
$insert .= "NULL,";
} else if ($field_val != "") {
......@@ -174,9 +175,9 @@ function get_content($DB, $table, $from, $limit) {
function get_def($DB, $table) {
$def = "### Dump table $table\n\n";
$def .= "DROP TABLE IF EXISTS `$table`;\n";
$def .= "DROP TABLE IF EXISTS ".$DB->quoteName($table).";\n";
$query = "SHOW CREATE TABLE `$table`";
$query = "SHOW CREATE TABLE ".$DB->quoteName($table);
$result = $DB->query($query);
$DB->query("SET SESSION sql_quote_show_create = 1");
$row = $DB->fetchRow($result);
......@@ -339,6 +340,7 @@ function backupMySql($DB, $dumpFile, $duree, $rowlimit) {
$result = $DB->listTables();
$numtab = 0;
$tables = [];
while ($t = $result->next()) {
$tables[$numtab] = $t['TABLE_NAME'];
$numtab++;
......@@ -549,8 +551,10 @@ if (isset($_GET["file"]) && ($_GET["file"] != "") && is_file(GLPI_DUMP_DIR . "/"
} else {
// Compatiblity for old version for utf8 complete conversion
$cnf = new Config();
$input['id'] = 1;
$input['utf8_conv'] = 1;
$input = [
'id' => 1,
'utf8_conv' => 1,
];
$cnf->update($input);
}
}
......
......@@ -52,122 +52,148 @@ if ((isset($_POST["item_type"][0]) && ($_POST["item_type"][0] == '0'))
if (isset($_POST["item_type"]) && is_array($_POST["item_type"])) {
$query = [];
$all_criteria = [];
foreach ($_POST["item_type"] as $key => $val) {
if (in_array($val, $items)) {
$itemtable = getTableForItemType($val);
$order = "itemdeleted DESC,";
if (($val == 'Project')
|| ($val == 'SoftwareLicense')) {
$select = $join = '';
if ($val == 'SoftwareLicense') {
$select = "`glpi_infocoms`.`buy_date`,
`glpi_infocoms`.`warranty_duration`,";
$join = " LEFT JOIN `glpi_infocoms`
ON (`glpi_infocoms`.`itemtype` = '$val'
AND `$itemtable`.`id` = `glpi_infocoms`.`items_id`)";
$order = '';
}
if ($val == 'Project') {
$select = "`$itemtable`.`is_deleted` AS itemdeleted,";
}
$query[$val] = "SELECT `$itemtable`.`name` AS itemname,
`glpi_contracttypes`.`name` AS type,
$select
`glpi_contracts`.`begin_date`,
`glpi_contracts`.`duration`,
`glpi_entities`.`completename` AS entname,
`glpi_entities`.`id` AS entID
FROM `glpi_contracts_items`
INNER JOIN `glpi_contracts`
ON (`glpi_contracts_items`.`contracts_id` = `glpi_contracts`.`id`)
INNER JOIN `$itemtable`
ON (`glpi_contracts_items`.`itemtype` = '$val'
AND `$itemtable`.`id` = `glpi_contracts_items`.`items_id`)
$join
LEFT JOIN `glpi_contracttypes`
ON (`glpi_contracts`.`contracttypes_id` = `glpi_contracttypes`.`id`)
LEFT JOIN `glpi_entities`
ON (`$itemtable`.`entities_id` = `glpi_entities`.`id`) ".
getEntitiesRestrictRequest("WHERE", $itemtable);
if (isset($_POST["year"][0]) && ($_POST["year"][0] != 0)) {
$query[$val] .= " AND ( ";
$first = true;
foreach ($_POST["year"] as $val2) {
if (!$first) {
$query[$val] .= " OR ";
} else {
$first = false;
}
if ($val == 'Project') {
$query[$val] .= " YEAR(`glpi_contracts`.`begin_date`) = '$val2'";
}
if ($val == 'SoftwareLicense') {
$query[$val] .= " YEAR(`glpi_infocoms`.`buy_date`) = '$val2'
OR YEAR(`glpi_contracts`.`begin_date`) = '$val2'";
}
if (!in_array($val, $items)) {
continue;
}
$itemtable = getTableForItemType($val);
$criteria = [
'SELECT' => [
'glpi_contracttypes.name AS type',
'glpi_contracts.duration',
'glpi_entities.completename AS entname',
'glpi_entities.id AS entID',
'glpi_contracts.begin_date'
],
'FROM' => 'glpi_contracts_items',
'INNER JOIN' => [
'glpi_contracts' => [
'ON' => [
'glpi_contracts_items' => 'contracts_id',
'glpi_contracts' => 'id'
]
],
$itemtable => [
'ON' => [
$itemtable => 'id',
'glpi_contracts_items' => 'items_id', [
'AND' => [
'glpi_contracts_items.itemtype' => $val
]
]
]
]
],
'LEFT JOIN' => [
'glpi_contracttypes' => [
'ON' => [
'glpi_contracts' => 'contracttypes_id',
'glpi_contracttypes' => 'id'
]
],
'glpi_entities' => [
'ON' => [
$itemtable => 'entities_id',
'glpi_entities' => 'id'
]
]
],
'WHERE' => getEntitiesRestrictCriteria($itemtable),
'ORDERBY' => ["entname ASC", 'itemdeleted DESC', "itemname ASC"]
];
if ($DB->fieldExists($itemtable, 'name')) {
$criteria['SELECT'][] = "$itemtable.name AS itemname";
} else {
$criteria['SELECT'][] = new QueryExpression("'' AS ".$DB->quoteName('itemname'));
}
if (($val == 'Project')
|| ($val == 'SoftwareLicense')) {
if ($val == 'SoftwareLicense') {
$criteria['ORDERBY'] = ["entname ASC", "itemname ASC"];
$criteria['SELECT'] = array_merge(
$criteria['SELECT'],
['glpi_infocoms.buy_date', 'glpi_infocoms.warranty_duration']
);
$criteria['LEFT JOIN']['glpi_infocoms'] = [
'ON' => [
'glpi_infocoms' => 'items_id',
$itemtable => 'id', [
'AND' => [
'glpi_infocoms.itemtype' => $val
]
]
]
];
}
if ($val == 'Project') {
$criteria['SELECT'][] = "$itemtable.is_deleted AS itemdeleted";
}
if (isset($_POST["year"][0]) && ($_POST["year"][0] != 0)) {
$ors = [];
foreach ($_POST["year"] as $val2) {
$ors[] = new QueryExpression('YEAR('.$DB->quoteName('glpi_contracts.begin_date').') = '.$DB->quote($val2));
if ($val == 'SoftwareLicense') {
$ors[] = new QueryExpression('YEAR('.$DB->quoteName('glpi_infocoms.buy_date').') = '.$DB->quote($val2));
}
$query[$val] .= ")";
}
if (count($ors)) {
$criteria['WHERE'][] = ['OR' => $ors];
}
}
} else {
$query[$val] = "SELECT `$itemtable`.`name` AS itemname,
`$itemtable`.`is_deleted` AS itemdeleted,
`glpi_locations`.`completename` AS location,
`glpi_contracttypes`.`name` AS type,
`glpi_infocoms`.`buy_date`,
`glpi_infocoms`.`warranty_duration`,
`glpi_contracts`.`begin_date`,
`glpi_contracts`.`duration`,
`glpi_entities`.`completename` AS entname,
`glpi_entities`.`id` AS entID
FROM `glpi_contracts_items`
INNER JOIN `glpi_contracts`
ON (`glpi_contracts_items`.`contracts_id` = `glpi_contracts`.`id`)
INNER JOIN `$itemtable`