From 9556893e628dd90fab7a83bfeaf278547674d0da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Panasiewicz?= Date: Wed, 20 Jul 2022 20:30:01 +0200 Subject: [PATCH 1/7] criteria for entities --- inc/dropdowncriteria.class.php | 11 +++++-- inc/entitiescriteria.class.php | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 inc/entitiescriteria.class.php diff --git a/inc/dropdowncriteria.class.php b/inc/dropdowncriteria.class.php index 20b5139..02aac6d 100644 --- a/inc/dropdowncriteria.class.php +++ b/inc/dropdowncriteria.class.php @@ -272,16 +272,21 @@ public function displayDropdownCriteria() { * * @see plugins/reports/inc/PluginReportsAutoCriteria::getSqlCriteriasRestriction() **/ - public function getSqlCriteriasRestriction($link='AND') { + public function getSqlCriteriasRestriction($link='AND',$tableName='') { $dbu = new DbUtils(); if ($this->getParameterValue() || $this->searchzero) { + + if($tableName != '') { + $sqlField = "`" . $tableName . "`.`" . $sqlField ."` " + } + if (!$this->childrens) { - return $link . " " . $this->getSqlField() . "='" . $this->getParameterValue() . "' "; + return $link . " " . $sqlField . "='" . $this->getParameterValue() . "' "; } if ($this->getParameterValue()) { - return $link . " " . $this->getSqlField() . + return $link . " " . $sqlField . " IN (" . implode(',', $dbu->getSonsOf($this->getTable(), $this->getParameterValue())) . ") "; } diff --git a/inc/entitiescriteria.class.php b/inc/entitiescriteria.class.php new file mode 100644 index 0000000..bca4119 --- /dev/null +++ b/inc/entitiescriteria.class.php @@ -0,0 +1,57 @@ +. + + @package reports + @authors Nelly Mahu-Lasson, Remi Collet, Alexandre Delaunay + @copyright Copyright (c) 2009-2021 Reports plugin team + @license AGPL License 3.0 or (at your option) any later version + http://www.gnu.org/licenses/agpl-3.0-standalone.html + @link https://forge.glpi-project.org/projects/reports + @link http://www.glpi-project.org/ + @since 2009 + -------------------------------------------------------------------------- + */ + +/** + * Entities selection criteria + */ +class PluginReportsEntitiesCriteria extends PluginReportsDropdownCriteria { + + + /** + * @param $report + * @param $name (default 'entities_id') + * @param $label (default '') + **/ + function __construct($report, $name='entities_id', $label='') { + + parent::__construct($report, $name, 'glpi_entities', ($label ? $label : __('Entity'))); + } + + + /** + * @param $entity + **/ + public function setDefaultLocation($entity) { + $this->addParameter($this->name, $entity); + } + +} From 9eb8bf3f32b0aa21d9f2b5dbfd920a2c8e4aed15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Panasiewicz?= Date: Wed, 20 Jul 2022 20:50:59 +0200 Subject: [PATCH 2/7] for entities criteria (The "entities_id" column in the where clause is ambiguous) --- inc/dropdowncriteria.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/dropdowncriteria.class.php b/inc/dropdowncriteria.class.php index 02aac6d..2ccc893 100644 --- a/inc/dropdowncriteria.class.php +++ b/inc/dropdowncriteria.class.php @@ -273,13 +273,14 @@ public function displayDropdownCriteria() { * @see plugins/reports/inc/PluginReportsAutoCriteria::getSqlCriteriasRestriction() **/ public function getSqlCriteriasRestriction($link='AND',$tableName='') { - + $dbu = new DbUtils(); - + if ($this->getParameterValue() || $this->searchzero) { + $sqlField = $this->getSqlField(); if($tableName != '') { - $sqlField = "`" . $tableName . "`.`" . $sqlField ."` " + $sqlField = "`" . $tableName . "`.`" . $sqlField ."` "; } if (!$this->childrens) { @@ -295,5 +296,4 @@ public function getSqlCriteriasRestriction($link='AND',$tableName='') { // Zero => means ALL => no criteria return ''; } - } From cea2f56c3b63d1b1c2eb1ca08970731090938340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Panasiewicz?= Date: Thu, 21 Jul 2022 18:12:47 +0200 Subject: [PATCH 3/7] setDefaultEntity --- inc/entitiescriteria.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/entitiescriteria.class.php b/inc/entitiescriteria.class.php index bca4119..f535ffc 100644 --- a/inc/entitiescriteria.class.php +++ b/inc/entitiescriteria.class.php @@ -50,7 +50,7 @@ function __construct($report, $name='entities_id', $label='') { /** * @param $entity **/ - public function setDefaultLocation($entity) { + public function setDefaultEntity($entity) { $this->addParameter($this->name, $entity); } From 6cbeaeb3e58592fb888bc49b3cff1f77fe1ed976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Panasiewicz?= Date: Tue, 20 Sep 2022 14:18:28 +0200 Subject: [PATCH 4/7] Report - List of assets in entity --- .../assetsitemsinentity.config.php | 0 .../assetsitemsinentity.en_GB.php | 2 + .../assetsitemsinentity.en_US.php | 2 + .../assetsitemsinentity.php | 212 ++++++++++++++++++ .../assetsitemsinentity.pl_PL.php | 2 + 5 files changed, 218 insertions(+) create mode 100644 report/assetsitemsinentity/assetsitemsinentity.config.php create mode 100644 report/assetsitemsinentity/assetsitemsinentity.en_GB.php create mode 100644 report/assetsitemsinentity/assetsitemsinentity.en_US.php create mode 100644 report/assetsitemsinentity/assetsitemsinentity.php create mode 100644 report/assetsitemsinentity/assetsitemsinentity.pl_PL.php diff --git a/report/assetsitemsinentity/assetsitemsinentity.config.php b/report/assetsitemsinentity/assetsitemsinentity.config.php new file mode 100644 index 0000000..e69de29 diff --git a/report/assetsitemsinentity/assetsitemsinentity.en_GB.php b/report/assetsitemsinentity/assetsitemsinentity.en_GB.php new file mode 100644 index 0000000..d4d28d0 --- /dev/null +++ b/report/assetsitemsinentity/assetsitemsinentity.en_GB.php @@ -0,0 +1,2 @@ + Financial & Administratives Information -> Read +- Assets -> Read +- Tags -> Read +-------------------------------------------------------------------------- + */ + +$USEDBREPLICATE = 1; +$DBCONNECTION_REQUIRED = 0; + +include '../../../../inc/includes.php'; + +//TRANS: The name of the report = List of assets in entity +$report = new PluginReportsAutoReport( + __('List of equipment in entity', 'reports') +); +//TODO - multichoice for Entities +$entities = new PluginReportsEntitiesCriteria($report); + +// TODO - configuration for multiple assets, configuration for report +$ignored = [ + 'Cartridge', + 'CartridgeItem', + 'Consumable', + 'ConsumableItem', + 'Software', + 'Line', + 'Certificate', + 'Appliance', + 'Domain', + 'Item_DeviceSimcard', + 'SoftwareLicense', + 'Peripheral', + 'Phone', + 'Rack', + 'Enclosure', + 'PDU', + 'PassiveDCEquipment', +]; + +$report->setColumns([ + new PluginReportsColumnType('itemtype', __('Assets')), + new PluginReportsColumnLink('entityid', __('Entity'), 'Entity', [ + 'sorton' => 'glpi_entities.completename', + ]), + new PluginReportsColumnLink('locationid', __('Location'), 'Location', [ + 'sorton' => 'glpi_locations.completename', + ]), + new PluginReportsColumnTypeLink('items_id', __('Item'), 'itemtype', [ + 'with_comment' => 1, + ]), + new PluginReportsColumnDate('date_creation', __('Creation date'), [ + 'sorton' => 'date_creation', + ]), + new PluginReportsColumnDate( + 'lastinventorydate', + __('Date of last physical inventory'), + ['sorton' => 'lastinventorydate'] + ), + new PluginReportsColumnLink( + 'manufacturerid', + __('Manufacturer'), + 'Manufacturer', + ['sorton' => 'glpi_manufacturers.name'] + ), + new PluginReportsColumnModelType('models_id', __('Model'), 'itemtype', [ + 'with_comment' => 1, + ]), + new PluginReportsColumn('otherserial', __('Inventory number')), + new PluginReportsColumn('serial', __('Serial number')), + new PluginReportsColumn('invoicenumber', __('Invoice number')), + new PluginReportsColumn( + 'entitytag', + __('Type') . ' ' . _n('Entity', 'Entities', 2) + ), + new PluginReportsColumn('statename', __('Status')), + new PluginReportsColumnDate('date_mod', __('Last update')), + new PluginReportsColumnTypeType('types_id', __('Type'), 'itemtype', [ + 'with_comment' => 1, + ]), +]); + +//Display criterias form is needed +$report->displayCriteriasForm(); + +//If criterias have been validated +if ($report->criteriasValidated() && $entities->getParameterValue() != 0) { + $report->setSubNameAuto(); + $cfg_types = array_values(array_diff($CFG_GLPI['infocom_types'], $ignored)); + $query = getSqlSubRequest($cfg_types[0], $entities, new $cfg_types[0]()); + + foreach ($cfg_types as $itemtype) { + $obj = new $itemtype(); + if ( + $obj->isField('entities_id') && + $itemtype != $cfg_types[0] && + !in_array($itemtype, $ignored) + ) { + $query .= + 'UNION (' . getSqlSubRequest($itemtype, $entities, $obj) . ')'; + } + } + + $report->setGroupBy('entity', 'itemtype'); + //$report->getOrderBy('name'); + $report->setSqlRequest($query); + $report->execute(); +} else { + echo "

" . + __('Entity not selected', 'reports') . + '

'; + Html::footer(); +} + +function getSqlSubRequest($itemtype, $entities, $obj) { + $dbu = new DbUtils(); + + $table = $dbu->getTableForItemType($itemtype); + $models_id = $dbu->getForeignKeyFieldForTable( + $dbu->getTableForItemType($itemtype . 'Model') + ); + $types_id = $dbu->getForeignKeyFieldForTable( + $dbu->getTableForItemType($itemtype . 'Type') + ); + $fields = [ + 'name' => 'name', + 'serial' => 'serial', + 'otherserial' => 'otherserial', + 'states_id' => 'states_id', + 'date_creation' => 'date_creation', + 'date_mod' => 'date_mod', + 'locations_id' => 'locations_id', + 'entities_id' => 'entities_id', + 'manufacturers_id' => 'manufacturers_id', + 'id' => 'id', + $models_id => 'models_id', + $types_id => 'types_id', + ]; + + $query_where = "SELECT '$itemtype' AS itemtype, + `$table`.`id` AS items_id, + `$table`.`entities_id`"; + + $join = ''; + foreach ($fields as $field => $alias) { + if ($obj->isField($field)) { + if ($field == 'locations_id') { + $query_where .= ', `glpi_locations`.`id` AS locationid'; + $join .= " LEFT JOIN `glpi_locations`ON `glpi_locations`.`id` = `$table`.`locations_id` "; + } elseif ($field == 'entities_id') { + $query_where .= ', `glpi_entities`.`id` AS entityid'; + $join .= " LEFT JOIN `glpi_entities` ON `glpi_entities`.`id` = `$table`.`entities_id` "; + //Tag for entity + $query_where .= ', `glpi_plugin_tag_tags`.`name` AS entitytag'; + $join .= " LEFT JOIN `glpi_plugin_tag_tagitems` ON (`glpi_entities`.`id` = `glpi_plugin_tag_tagitems`.`items_id` + AND `glpi_plugin_tag_tagitems`.`itemtype` = 'Entity' ) "; + $join .= + ' LEFT JOIN `glpi_plugin_tag_tags` ON `glpi_plugin_tag_tagitems`.`plugin_tag_tags_id` = `glpi_plugin_tag_tags`.`id` '; + } elseif ($field == 'manufacturers_id') { + $query_where .= ', `glpi_manufacturers`.`id` AS manufacturerid'; + $join .= " LEFT JOIN `glpi_manufacturers` ON `glpi_manufacturers`.`id` = `$table`.`manufacturers_id` "; + } elseif ($field == 'id') { + $query_where .= ', `glpi_infocoms`.`bill` AS invoicenumber'; + $query_where .= + ', `glpi_infocoms`.`inventory_date` AS lastinventorydate'; + $join .= " LEFT JOIN `glpi_infocoms` ON ( `glpi_infocoms`.`items_id` = `$table`.`id` AND `glpi_infocoms`.`itemtype` = '$itemtype' ) "; + } elseif ($field == 'states_id') { + $query_where .= ', `glpi_states`.`name` AS statename'; + $join .= " LEFT JOIN `glpi_states` ON `glpi_states`.`id` = `$table`.`states_id` "; + } else { + $query_where .= ", `$table`.`$field` AS $alias"; + } + } else { + $query_where .= ", '' AS $alias"; + } + } + + $query_where .= " FROM `$table` + $join "; + + if ($obj->isEntityAssign()) { + $query_where .= $dbu->getEntitiesRestrictRequest('WHERE', "$table"); + } else { + $query_where .= 'WHERE 1'; + } + + if ($obj->maybeTemplate()) { + $query_where .= " AND `$table`.`is_template`='0'"; + } + + if ($obj->maybeDeleted()) { + $query_where .= " AND `$table`.`is_deleted`='0'"; + } + + $query_where .= $entities->getSqlCriteriasRestriction('AND', $table); + return $query_where; +} diff --git a/report/assetsitemsinentity/assetsitemsinentity.pl_PL.php b/report/assetsitemsinentity/assetsitemsinentity.pl_PL.php new file mode 100644 index 0000000..cec815b --- /dev/null +++ b/report/assetsitemsinentity/assetsitemsinentity.pl_PL.php @@ -0,0 +1,2 @@ + Date: Mon, 26 Sep 2022 11:57:16 +0200 Subject: [PATCH 5/7] Add ignore assets added in v10 (The used SELECT statements have a different number of columns ...) --- report/assetsitemsinentity/assetsitemsinentity.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/report/assetsitemsinentity/assetsitemsinentity.php b/report/assetsitemsinentity/assetsitemsinentity.php index 888f66d..b9d35e1 100644 --- a/report/assetsitemsinentity/assetsitemsinentity.php +++ b/report/assetsitemsinentity/assetsitemsinentity.php @@ -51,6 +51,8 @@ 'Enclosure', 'PDU', 'PassiveDCEquipment', + 'DatabaseInstance', + 'Cable' ]; $report->setColumns([ From 34eca63aabb929221828813103779bc5f5880bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Panasiewicz?= Date: Wed, 26 Oct 2022 12:26:09 +0200 Subject: [PATCH 6/7] Delete assetsitemsinentity.config.php --- report/assetsitemsinentity/assetsitemsinentity.config.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 report/assetsitemsinentity/assetsitemsinentity.config.php diff --git a/report/assetsitemsinentity/assetsitemsinentity.config.php b/report/assetsitemsinentity/assetsitemsinentity.config.php deleted file mode 100644 index e69de29..0000000 From 6f9d2fa248d3a2ff353bae83feb3ec1862ae8661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Panasiewicz?= Date: Wed, 10 May 2023 17:37:19 +0200 Subject: [PATCH 7/7] Remove locales --- report/assetsitemsinentity/assetsitemsinentity.en_GB.php | 2 -- report/assetsitemsinentity/assetsitemsinentity.en_US.php | 2 -- report/assetsitemsinentity/assetsitemsinentity.pl_PL.php | 2 -- 3 files changed, 6 deletions(-) delete mode 100644 report/assetsitemsinentity/assetsitemsinentity.en_GB.php delete mode 100644 report/assetsitemsinentity/assetsitemsinentity.en_US.php delete mode 100644 report/assetsitemsinentity/assetsitemsinentity.pl_PL.php diff --git a/report/assetsitemsinentity/assetsitemsinentity.en_GB.php b/report/assetsitemsinentity/assetsitemsinentity.en_GB.php deleted file mode 100644 index d4d28d0..0000000 --- a/report/assetsitemsinentity/assetsitemsinentity.en_GB.php +++ /dev/null @@ -1,2 +0,0 @@ -