You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2017-12-01 00:51:43

bugless
Guest
Registered: 2010-01-24
Posts: 17

Help needed with custom report

Hello.

I'm trying to make a custom report through glpi-reports plugin. I'm taking as direct base the "List of equipment by location" report since this is the one with the most columns showing by default. I just need to add some more columns that are not there.

So I created a new directory called "customrep" inside /usr/share/glpi/plugins/reports/report directory, and put there the 2 required files: an already modified copy of listequipmentbylocation.php renamed as customrep.php, and the language file.

File customrep.en_GB.php:

<?php
$LANG['plugin_reports']['listequiploc'] = 'List equipment';

File customrep.php:

1   <?php
2   //Left this line just in case version number is needed
3   //version Id: listequipmentbylocation.php 319 2015-10-02 10:21:16Z yllen
4
5   $USEDBREPLICATE        = 1;
6   $DBCONNECTION_REQUIRED = 0;
7
8   include ("../../../../inc/includes.php");
9
10  //TRANS: The name of the report = List of equipments by location
11  $report = new PluginReportsAutoReport(__('listequiploc_report_title', 'reports'));
12  $loc    = new PluginReportsLocationCriteria($report);
13
14  $report->setColumns(array(new PluginReportsColumnType('itemtype', __('Type')),
15                            new PluginReportsColumnTypeLink('items_id', __('Item'),
16                                                            'itemtype', array('with_comment' => 1)),
17                            new PluginReportsColumn('serial', __('Serial number')),
18                            new PluginReportsColumn('otherserial', __('Inventory number')),
19                            new PluginReportsColumn('states_id', __('Status')),
20                            new PluginReportsColumnModelType('models_id', __('Model'),
21                                                             'itemtype', array('with_comment' => 1)),
22                            new PluginReportsColumnTypeType('types_id', __('Type'),
23                                                            'itemtype', array('with_comment' => 1))));
24
25  //Display criterias form is needed
26  $report->displayCriteriasForm();
27
28  //If criterias have been validated
29  if ($report->criteriasValidated()) {
30     $report->setSubNameAuto();
31
32     $query = getSqlSubRequest("Computer",$loc,new Computer());
33     foreach($CFG_GLPI["infocom_types"] as $itemtype) {
34        $obj = new $itemtype;
35        if ($obj->isField('locations_id')) {
36           $query.= "UNION (".getSqlSubRequest($itemtype,$loc,$obj).")";
37        }
38     }
39     $report->setGroupBy("entity","itemtype");
40     $report->setSqlRequest($query);
41     $report->execute();
42  }
43  else {
44     Html::footer();
45  }
46
47
48  function getSqlSubRequest($itemtype,$loc,$obj) {
49
50     $table     = getTableForItemType($itemtype);
51     $models_id = getForeignKeyFieldForTable(getTableForItemType($itemtype.'Model'));
52     $types_id  = getForeignKeyFieldForTable(getTableForItemType($itemtype.'Type'));
53     $fields    = array('name'        => 'name',
54                        'serial'      => 'serial',
55                        'otherserial' => 'otherserial',
56                        'states_id'   => 'states_id',
57                        $models_id    => 'models_id',
58                        $types_id     => 'types_id');
59
60     $query_where = "SELECT '$itemtype' AS itemtype,
61                            `$table`.`id` AS items_id,
62                            `$table`.`locations_id`";
63
64     foreach ($fields as $field => $alias) {
65        if ($obj->isField($field)) {
66           $query_where .= ", `$table`.`$field` AS $alias";
67        } else {
68           $query_where .= ", '' AS $alias";
69        }
70     }
71
72     $query_where .= " FROM `$table` ";
73
74     if ($obj->isEntityAssign()) {
75        $query_where .= getEntitiesRestrictRequest('WHERE', "$table");
76     } else {
77        $query_where .= 'WHERE 1';
78     }
79
80     if ($obj->maybeTemplate()) {
81        $query_where .= " AND `is_template`='0'";
82     }
83
84     if ($obj->maybeDeleted()) {
85        $query_where .= " AND `is_deleted`='0'";
86     }
87
88     $query_where .= $loc->getSqlCriteriasRestriction();
89
90     return $query_where;
91  }

I numbered the lines in this one for better clarity.
I have never handled php coding before so I'm total novice, though I already read glpi-reports docs.

First I wanted to add the Status column, so I tried by adding lines 19 and 56. Checked in glpi and the column got added indeed, but all I see is a number '1' in all the fields instead of the actual status text.

What am I doing wrong, or what else do I need?
Do I face the same problem if trying to add any other additional column?

Thanks.

Offline

#2 2017-12-07 10:23:52

remi
GLPI-DEV
From: Champagne
Registered: 2007-04-28
Posts: 7,099
Website

Re: Help needed with custom report

See the "printers" report which have the state column.


Dév. Fedora 25 - PHP 5.4/5.5/5.6/7.0/7.1/7.2 - MariaDB 10.1 - GLPI master
Certifié ITILv3 - RPM pour Fedora, RHEL et CentOS sur https://blog.remirepo.net/

Offline

#3 2017-12-07 22:03:33

bugless
Guest
Registered: 2010-01-24
Posts: 17

Re: Help needed with custom report

First some corrections:
in the language file it's "customrep" instead of "listequiploc", and just the same for line 11 in customrep.php

Printers report has "state" as field, but none of these report's fields works in customrep.php, report displays just blank fields in glpi.
I also searched for printer report's field names directly in maruadb database, and either they're non existent or they exist but in tables such as glpi_entitites, glpi_crontasks, glpi_plugins, and so, but neither related to computers, printers, or asset-related.
Printers report uses noin-existent fields! How the **** does it work?
Regardless it doesn't work for customrep.php

Also, when using customrep in glpi I see its title is displayed as "customrep_report_title" instead of just "customrep". This should have to do with line 11 in customrep.php, but clueless about hot to fix.

Finally, how can I add more columns to customrep such as manufacturer, operating system, comments, and location itself? Not even status is working!

Wish yllen can be back online, because remi is not willing to help (perhaps he got bothered enough in irc...)

Offline

#4 2017-12-09 07:56:31

bugless
Guest
Registered: 2010-01-24
Posts: 17

Re: Help needed with custom report

Truth to be told what I want to do is just a "global report", a simple report with no need to ask for any criteria beforehand but just displaying *all* global items with *all* common columns for everything even if some of them don't apply for a certain kind of item (say, the O.S. field for printer items just displaying blank).

At first I found the closest one doing what I needed was precisely the By location report, I just needed to *not* specify any location (i.e., leaving it as ---) in order to display everything globally. I found it was just missing some more columns.
I thought this should just be a matter of *slightly* modifying List of equipment by location report, for which no one has helped...

I already searched around the forums. Other users chose to make report from scratch following this link https://forge.glpi-project.org/projects … eateReport
But that looks already outdated: autoreport class is little to no documented, there are missing columns to be covered such as ModelType and TypeType, nothing that could explain what the "#$% getSqlSubRequest() function is for.......

Offline

#5 Yesterday 23:18:02

emmahersen
Guest
Registered: 2017-11-20
Posts: 6

Re: Help needed with custom report

Look at plugins/reports/report/location/Location.php =>

1. you create a new dir in plugins/reports/report/location/MyReport
2. you create a new file MyReport.php
3. in this file, you just define your columns and your SQL request

Offline

Board footer

Powered by FluxBB