glpi-plugin-auf : ajustement des rapports
[auf-serveur.git] / glpi-plugin-auf / plugin / report.computers.php
CommitLineData
f87696f8
P
1<?php
2//Options for GLPI 0.71 and newer : need slave db to access the report
3$USEDBREPLICATE=1;
4$DBCONNECTION_REQUIRED=0;
5
6include ("../../inc/includes.php");
7
8Session::checkRight("reports", "r");
9Session::checkRight("computer", "r");
10
11#Html::header(Report::getTypeName(2), $_SERVER['PHP_SELF'], "utils", "report");
12Html::header("Ordinateurs par implantation", $_SERVER['PHP_SELF'], "utils", "report");
13Report::title();
14
15$computer = new Computer();
16
f87696f8
P
17$itemtype = 'Computer';
18$table_item = getTableForItemType($itemtype);
19
c2b8eea0
P
20$svg_colors = Array('red'=>'#ffa0a0', 'green'=>'#a0ffa0', 'yellow'=>'#ffdd80', 'blue'=>'#a0a0ff');
21
f87696f8 22function svg_circle($color, $size=4) {
c2b8eea0
P
23 global $svg_colors;
24 if (array_key_exists($color, $svg_colors)) { $color = $svg_colors[$color]; }
f87696f8 25 $r = (int)$size; $cx = $r+1; $cy = $r+1;
b38636fc 26 $svg = '<svg width="'.(2+2*$r).'" height="'.(2+2*$r).'" style="margin: 1px; vertical-align: middle;">';
f87696f8
P
27 $svg .= "<circle cx='$cx' cy='$cy' r='$r' stroke='black' stroke-width='0' fill='$color' />";
28 $svg .= '</svg>';
29 return $svg;
30}
31
32function svg_pie_chart($values, $size=16) {
c2b8eea0
P
33 global $svg_colors;
34 $color_names = Array('red', 'green', 'yellow', 'blue'); $color_idx = 0;
f87696f8
P
35 $total = 0.0; foreach ($values as $v) { $total += $v; }
36 $r = (int)$size; $cx = $r+1; $cy = $r+1; $rad = 2*pi()/$total; $old_a = 0;
b38636fc 37 $svg = '<svg width="'.(2+2*$r).'" height="'.(2+2*$r).'" style="margin: 2px; vertical-align: middle;">';
f87696f8
P
38 foreach ($values as $a) {
39 $x1 = $cx + $r * cos(-$old_a * $rad);
40 $y1 = $cy + $r * sin(-$old_a * $rad);
41 $x2 = $cx + $r * cos(-($old_a + $a) * $rad);
42 $y2 = $cy + $r * sin(-($old_a + $a) * $rad);
43 $big = ($a > ($total/2)) ? '1' : '0';
c2b8eea0 44 $color = $svg_colors[$color_names[$color_idx++]];
f87696f8
P
45 $svg .= "<path d=\"M$cx,$cy L$x1,$y1 A$r,$r 0 $big,0 $x2,$y2 z\" style=\"stroke: 0 black; fill: $color;\" />";
46 $old_a += $a;
47 }
48 $svg .= '</svg>';
49 return $svg;
50}
51
67d402c6
P
52// echo "\n<span class='big b' style='color: red;'>ATTENTION : ce rapport est en cours de refonte, merci de ne pas tenir compte des données affichées ci-dessous.</span><br><br>";
53
749e76d6 54//echo "\n<span class='big b'>Ordinateurs par implantation</span><br><br>";
f87696f8
P
55echo "<table class='tab_cadrehov'>";
56echo "<tr class='tab_bg_1'><th rowspan='2' colspan='2'>Implantation</th>";
c2b8eea0 57echo "<th colspan='2'>".svg_circle('red')." Serveurs</th>";
67d402c6
P
58echo "<th colspan='2'>".svg_circle('green')." Postes internes</th>";
59echo "<th colspan='2'>".svg_circle('yellow')." Postes publics</th>";
60echo "<th rowspan='2'>Total</th>";
61echo "<th rowspan='2'>".svg_circle('blue')." Virtuels</th>";
62echo "<th rowspan='2'>Autres<br /><span style='font-size: 75%'>(sans statut)</span></th></tr>";
f87696f8
P
63echo "<tr class='tab_bg_1'><th>Total</th><th>Production</th>";
64echo "<th>Total</th><th>Attribué</th><th>Total</th><th>Attribué</th></tr>\n";
65
749e76d6
P
66$grand_serveurs_total = 0; $grand_serveurs_prod = 0;
67$grand_internes_total = 0; $grand_internes_prod = 0;
68$grand_publics_total = 0; $grand_publics_prod = 0;
67d402c6
P
69$grand_total = 0;
70$grand_virtuels_total = 0; $grand_autres_total = 0;
f87696f8 71# récupération de la liste des régions
f87696f8
P
72$query = "SELECT id FROM glpi_entities WHERE (id=0 OR entities_id=0)";
73$query .= " ".getEntitiesRestrictRequest("AND", "glpi_entities")." ORDER BY name";
74$region_result = $DB->query($query);
75# parcours des régions
76while ($region_data = $DB->fetch_assoc($region_result)) {
77 $region_id = $region_data['id'];
78 if ($region_id != 0) {
79 $region_name = getTreeLeafValueName("glpi_entities", $region_id);
80 } else {
81 $region_name = "AUF";
82 }
83 $table_lines = Array();
84 $region_serveurs_total = 0; $region_serveurs_prod = 0;
85 $region_internes_total = 0; $region_internes_prod = 0;
86 $region_publics_total = 0; $region_publics_prod = 0;
87 $region_total = 0; $region_virtuels_total = 0;
88 $region_autres_total = 0;
89
90 # récupération de la liste des implantations
91 $query = "SELECT id,level FROM glpi_entities WHERE ";
92 if ($region_id != 0) {
93 $query .= getRealQueryForTreeItem("glpi_entities", $region_id, "id");
94 } else {
95 $query .= "id=0";
96 }
97 $query .= " ".getEntitiesRestrictRequest("AND", "glpi_entities")." ORDER BY name";
98 $implant_result = $DB->query($query);
99 # parcours des implantations
100 while ($implant_data = $DB->fetch_assoc($implant_result)) {
101 $implant_id = $implant_data['id'];
102 if ($implant_id != 0) {
103 $implant_name = getTreeLeafValueName("glpi_entities", $implant_id);
104 } else {
105 $implant_name = "<i>(non classé)</i>";
106 }
107
108 # récupération de la liste des systèmes de l'implantation
109 $nb_serveurs_total = 0; $nb_serveurs_prod = 0;
110 $nb_internes_total = 0; $nb_internes_prod = 0;
111 $nb_publics_total = 0; $nb_publics_prod = 0;
112 $nb_virtuels_total = 0; $nb_autres_total = 0;
67d402c6
P
113 $query = "SELECT s.completename AS sname, ac.est_virtuel AS virtuel,
114 acc.name AS categorie, acu.name AS utilisation
f87696f8 115 FROM `$table_item` c
f87696f8 116 LEFT JOIN glpi_states s ON (c.states_id = s.id)
67d402c6
P
117 LEFT JOIN glpi_plugin_auf_computers ac ON (c.id = ac.id)
118 LEFT JOIN glpi_plugin_auf_categories acc ON (ac.auf_categories_id = acc.id)
119 LEFT JOIN glpi_plugin_auf_utilisations acu ON (ac.auf_utilisations_id = acu.id)
f87696f8 120 WHERE c.is_deleted = '0' AND c.is_template = '0'
67d402c6 121 AND c.entities_id = '".$implant_id."'";
f87696f8
P
122 $result = $DB->query($query);
123 while ($data=$DB->fetch_assoc($result)) {
67d402c6
P
124 // on ne compte pas les sorties d'inventaire
125 if (preg_match("/^sortie/i", $data['sname'])) {
126 continue;
127 }
128 // serveurs
129 if (preg_match("/^infra/i", $data['categorie'])) {
130 $nb_serveurs_total++;
131 if (preg_match("/^production/i", $data['utilisation']) &&
132 !preg_match("/panne/i", $data['sname']) ) {
133 $nb_serveurs_prod++;
134 }
135 }
136 // postes internes
137 elseif (preg_match("/^poste.*interne/i", $data['categorie'])) {
138 $nb_internes_total++;
139 if (preg_match("/^production/i", $data['utilisation']) &&
140 !preg_match("/panne/i", $data['sname']) ) {
141 $nb_internes_prod++;
142 }
143 }
144 // postes publics
145 elseif (preg_match("/^poste.*public/i", $data['categorie'])) {
146 $nb_publics_total++;
147 if (preg_match("/^production/i", $data['utilisation']) &&
148 !preg_match("/panne/i", $data['sname']) ) {
149 $nb_publics_prod++;
150 }
151 }
152 // autres
153 else {
154 // machines virtuelles
155 if ($data['virtuel']) {
156 $nb_virtuels_total++;
157 // autres
158 } else {
159 $nb_autres_total++;
160 }
f87696f8
P
161 }
162 } // systèmes dans l'implantation
163
67d402c6 164 $total = $nb_serveurs_total + $nb_internes_total + $nb_publics_total;
f87696f8
P
165 if ($total > 0) {
166 $spaces = "";
167 for ($i=1;$i<$implant_data['level'];$i++) { $spaces .= "&nbsp;&nbsp;"; }
67d402c6
P
168 $line = "<tr><td class='tab_bg_1'>$spaces$implant_name</td>";
169 $line .= "<td class='numeric'>$nb_serveurs_total</td>";
170 $line .= "<td class='numeric'>$nb_serveurs_prod</td>";
171 $line .= "<td class='numeric'>$nb_internes_total</td>";
172 $line .= "<td class='numeric'>$nb_internes_prod</td>";
173 $line .= "<td class='numeric'>$nb_publics_total</td>";
174 $line .= "<td class='numeric'>$nb_publics_prod</td>";
175 $line .= "<td class='numeric tab_bg_1'>$total</td>";
176 $line .= "<td class='numeric'>$nb_virtuels_total</td>";
177 $line .= "<td class='numeric'>$nb_autres_total</td></tr>";
f87696f8
P
178 $table_lines[] = $line;
179 }
180 $region_serveurs_total += $nb_serveurs_total;
181 $region_serveurs_prod += $nb_serveurs_prod;
182 $region_internes_total += $nb_internes_total;
183 $region_internes_prod += $nb_internes_prod;
184 $region_publics_total += $nb_publics_total;
185 $region_publics_prod += $nb_publics_prod;
186 $region_total += $total;
187 $region_virtuels_total += $nb_virtuels_total;
188 $region_autres_total += $nb_autres_total;
189 //$table_lines[] = "<tr><td>".$implant_name."</td></tr>";
190 } // fin des implantations de la région
191
192 $line = "<tr class='tab_bg_1 b'><td>Sous-total</td>";
67d402c6
P
193 $line .= "<td class='numeric'>$region_serveurs_total</td>";
194 $line .= "<td class='numeric'>$region_serveurs_prod</td>";
195 $line .= "<td class='numeric'>$region_internes_total</td>";
196 $line .= "<td class='numeric'>$region_internes_prod</td>";
197 $line .= "<td class='numeric'>$region_publics_total</td>";
198 $line .= "<td class='numeric'>$region_publics_prod</td>";
199 $line .= "<td class='numeric tab_bg_1'>$region_total</td>";
200 $line .= "<td class='numeric'>$region_virtuels_total</td>";
201 $line .= "<td class='numeric'>$region_autres_total</td></tr>";
f87696f8
P
202 $table_lines[] = $line;
203
204 $region_name = svg_pie_chart(Array($region_serveurs_total, $region_internes_total, $region_publics_total, $region_virtuels_total), 16) . $region_name;
205 $table_lines[0] = str_replace("<tr>", "<tr><td class='tab_bg_1 b' rowspan='".count($table_lines)."'>$region_name</td>", $table_lines[0]);
67d402c6
P
206 if ($region_id != 0) {
207 echo "<tr><th colspan='11'></th></tr>\n";
208 }
f87696f8
P
209 echo implode("\n", $table_lines);
210
211 $grand_serveurs_total += $region_serveurs_total;
212 $grand_serveurs_prod += $region_serveurs_prod;
213 $grand_internes_total += $region_internes_total;
214 $grand_internes_prod += $region_internes_prod;
215 $grand_publics_total += $region_publics_total;
216 $grand_publics_prod += $region_publics_prod;
217 $grand_total += $region_total;
218 $grand_virtuels_total += $region_virtuels_total;
219 $grand_autres_total += $region_autres_total;
220} // fin des régions
221
222// echo "<tr class='tab_bg_1'><td colspan='8'>&nbsp;</td></tr>";
223
224/*
225# total operating systems
226$query = "SELECT COUNT(DISTINCT fia.`tag`) FROM `".$table_item."`
227 LEFT JOIN `glpi_computers_items` ci
228 ON (ci.`itemtype` = '".$itemtype."'
229 AND ci.`items_id` = `".$table_item."`.`id`)
230 LEFT JOIN `glpi_plugin_fusioninventory_agents` fia
231 ON (fia.`computers_id` = `".$table_item."`.`id`)
232 WHERE `".$table_item."`.`is_deleted` = '0'
233 AND `".$table_item."`.`is_template` = '0' ".
234 getEntitiesRestrictRequest("AND", $table_item);
235$result = $DB->query($query);
236$grand_total = $DB->result($result, 0, 0);
237*/
238$line = "<tr class='tab_bg_1'><td class='b' colspan='2'>Total général</td>";
67d402c6
P
239$line .= "<td class='numeric b'>$grand_serveurs_total</td>";
240$line .= "<td class='numeric b'>$grand_serveurs_prod</td>";
241$line .= "<td class='numeric b'>$grand_internes_total</td>";
242$line .= "<td class='numeric b'>$grand_internes_prod</td>";
243$line .= "<td class='numeric b'>$grand_publics_total</td>";
244$line .= "<td class='numeric b'>$grand_publics_prod</td>";
245$line .= "<td class='numeric b'>$grand_total</td>";
246$line .= "<td class='numeric b'>$grand_virtuels_total</td>";
247$line .= "<td class='numeric b'>$grand_autres_total</td></tr>";
248echo "<tr><th colspan='11'></th></tr>\n";
f87696f8
P
249echo $line;
250
251echo "</table>";
252
253Html::footer();
254?>