ryxeo-glpi-git / inc / rule.dictionnary.software.class.php @ b67d8923
Historique | Voir | Annoter | Télécharger (14,6 ko)
1 |
<?php
|
---|---|
2 |
|
3 |
/*
|
4 |
* @version $Id: rule.dictionnary.software.class.php 7884 2009-01-23 19:16:34Z remi $
|
5 |
-------------------------------------------------------------------------
|
6 |
GLPI - Gestionnaire Libre de Parc Informatique
|
7 |
Copyright (C) 2003-2009 by the INDEPNET Development Team.
|
8 |
|
9 |
http://indepnet.net/ http://glpi-project.org
|
10 |
-------------------------------------------------------------------------
|
11 |
|
12 |
LICENSE
|
13 |
|
14 |
This file is part of GLPI.
|
15 |
|
16 |
GLPI is free software; you can redistribute it and/or modify
|
17 |
it under the terms of the GNU General Public License as published by
|
18 |
the Free Software Foundation; either version 2 of the License, or
|
19 |
(at your option) any later version.
|
20 |
|
21 |
GLPI is distributed in the hope that it will be useful,
|
22 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
23 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
24 |
GNU General Public License for more details.
|
25 |
|
26 |
You should have received a copy of the GNU General Public License
|
27 |
along with GLPI; if not, write to the Free Software
|
28 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
29 |
--------------------------------------------------------------------------
|
30 |
*/
|
31 |
|
32 |
// ----------------------------------------------------------------------
|
33 |
// Original Author of file: Walid Nouh
|
34 |
// Purpose of file:
|
35 |
// ----------------------------------------------------------------------
|
36 |
if (!defined('GLPI_ROOT')) { |
37 |
die("Sorry. You can't access directly to this file"); |
38 |
} |
39 |
|
40 |
class DictionnarySoftwareCollection extends RuleCachedCollection { |
41 |
|
42 |
/**
|
43 |
* Constructor
|
44 |
**/
|
45 |
function DictionnarySoftwareCollection() { |
46 |
|
47 |
$this->rule_type = RULE_DICTIONNARY_SOFTWARE; |
48 |
$this->rule_class_name = 'DictionnarySoftwareRule'; |
49 |
$this->stop_on_first_match = true; |
50 |
$this->right = "rule_dictionnary_software"; |
51 |
|
52 |
//Init cache system values
|
53 |
$this->initCache("glpi_rule_cache_software", |
54 |
array ("name" => "old_value","manufacturer" => "manufacturer"), |
55 |
array ("name" => "new_value","version" => "version","manufacturer" => "new_manufacturer","_ignore_ocs_import"=>"ignore_ocs_import")); |
56 |
} |
57 |
|
58 |
function getTitle() { |
59 |
global $LANG; |
60 |
return $LANG["rulesengine"][35]; |
61 |
} |
62 |
function cleanTestOutputCriterias($output){ |
63 |
|
64 |
//If output array contains keys begining with _ : drop it
|
65 |
foreach($output as $criteria => $value){ |
66 |
if ($criteria[0]=='_' && $criteria != '_ignore_ocs_import' ){ |
67 |
unset($output[$criteria]); |
68 |
} |
69 |
} |
70 |
return $output; |
71 |
} |
72 |
|
73 |
function warningBeforeReplayRulesOnExistingDB($target){ |
74 |
global $LANG,$CFG_GLPI; |
75 |
echo "<form name='testrule_form' id='softdictionnary_confirmation' method='post' action=\"".$target."\">\n"; |
76 |
echo "<div class='center'>"; |
77 |
echo "<table class='tab_cadre_fixe'>"; |
78 |
echo "<tr><th colspan='2'><strong>" .$LANG["rulesengine"][92]. "</strong></th</tr>"; |
79 |
echo "<tr><td align='center' class='tab_bg_2'>"; |
80 |
echo "<img src=\"".$CFG_GLPI["root_doc"]."/pics/warning.png\"></td>"; |
81 |
echo "<td align='center' class='tab_bg_2'>".$LANG["rulesengine"][93]. "</td></tr>\n"; |
82 |
echo "<tr><th colspan='2'><strong>" .$LANG["rulesengine"][95]. "</strong></th</tr>"; |
83 |
echo "<tr><td align='center' class='tab_bg_2'>".$LANG["rulesengine"][96]."</td>"; |
84 |
echo "<td align='center' class='tab_bg_2'>"; |
85 |
dropdownValue("glpi_dropdown_manufacturer","manufacturer"); |
86 |
echo"</td></tr>\n"; |
87 |
|
88 |
echo "<tr><td align='center' class='tab_bg_2' colspan='2'><input type='submit' name='replay_rule' value=\"" . $LANG["buttons"][2] . "\" class='submit'><input type='hidden' name='replay_confirm' value='replay_confirm'</td></tr>"; |
89 |
echo "</table>"; |
90 |
echo "</div></form>"; |
91 |
return true; |
92 |
} |
93 |
|
94 |
function replayRulesOnExistingDB($offset=0,$maxtime=0, $items = array (),$params=0) { |
95 |
global $DB; |
96 |
if (isCommandLine()){
|
97 |
echo "replayRulesOnExistingDB started : " . date("r") . "\n"; |
98 |
} |
99 |
|
100 |
|
101 |
if (count($items) == 0) { |
102 |
//Select all the differents software
|
103 |
$sql = "SELECT DISTINCT glpi_software.name, glpi_dropdown_manufacturer.name AS manufacturer," . |
104 |
" glpi_software.FK_glpi_enterprise as FK_glpi_enterprise " .
|
105 |
"FROM glpi_software LEFT JOIN glpi_dropdown_manufacturer " .
|
106 |
"ON glpi_dropdown_manufacturer.ID=glpi_software.FK_glpi_enterprise ";
|
107 |
|
108 |
if (isset($params['manufacturer'])&&$params['manufacturer'] > 0) { |
109 |
$sql.=" WHERE FK_glpi_enterprise='".$params['manufacturer']."'"; |
110 |
} |
111 |
if ($offset) { |
112 |
$sql .= " LIMIT $offset,999999999"; |
113 |
} |
114 |
|
115 |
$res = $DB->query($sql); |
116 |
$nb = $DB->numrows($res)+$offset; |
117 |
$i = $offset; |
118 |
$step=($nb>1000 ? 50 : ($nb>20 ? floor($DB->numrows($res)/20) : 1)); |
119 |
|
120 |
while ($input = $DB->fetch_array($res)) { |
121 |
if (!($i % $step)) { |
122 |
if (isCommandLine()) {
|
123 |
echo date("H:i:s") . " replayRulesOnExistingDB : $i/$nb (".round(memory_get_usage()/(1024*1024),2)." Mo)\n"; |
124 |
} else {
|
125 |
changeProgressBarPosition($i,$nb,"$i / $nb"); |
126 |
} |
127 |
} |
128 |
|
129 |
//If manufacturer is set, then first run the manufacturer's dictionnary
|
130 |
if (isset($input["manufacturer"])) |
131 |
$input["manufacturer"] = processManufacturerName($input["manufacturer"]); |
132 |
|
133 |
//Replay software dictionnary rules
|
134 |
$input=addslashes_deep($input); |
135 |
$res_rule = $this->processAllRules($input, array (), array ()); |
136 |
$res_rule = addslashes_deep($res_rule); |
137 |
|
138 |
//If the software's name or version has changed
|
139 |
if ((isset ($res_rule["name"]) && $res_rule["name"] != $input["name"]) || (isset ($res_rule["version"])) && $res_rule["version"] != '') |
140 |
{ |
141 |
$IDs = array(); |
142 |
//Find all the softwares in the database with the same name and manufacturer
|
143 |
$sql = "SELECT ID FROM `glpi_software` WHERE name='" . $input["name"] . "' AND FK_glpi_enterprise='" . $input["FK_glpi_enterprise"]."'"; |
144 |
$res_soft = $DB->query($sql); |
145 |
if ($DB->numrows($res_soft) > 0) |
146 |
{ |
147 |
//Store all the software's IDs in an array
|
148 |
while ($result = $DB->fetch_array($res_soft)) |
149 |
$IDs[] = $result["ID"]; |
150 |
|
151 |
//Replay dictionnary on all the softwares
|
152 |
$this->replayDictionnaryOnSoftwaresByID($IDs, $res_rule); |
153 |
} |
154 |
} |
155 |
|
156 |
$i++;
|
157 |
if ($maxtime) { |
158 |
$crt=explode(" ",microtime()); |
159 |
if ($crt[0]+$crt[1] > $maxtime) { |
160 |
break;
|
161 |
} |
162 |
} |
163 |
} // each distinct software
|
164 |
|
165 |
if (isCommandLine()) {
|
166 |
echo "replayRulesOnExistingDB : $i/$nb \n"; |
167 |
} else {
|
168 |
changeProgressBarPosition($i,$nb,"$i / $nb"); |
169 |
} |
170 |
|
171 |
} else {
|
172 |
$this->replayDictionnaryOnSoftwaresByID($items); |
173 |
return true; |
174 |
} |
175 |
if (isCommandLine())
|
176 |
echo "replayRulesOnExistingDB ended : " . date("r") . "\n"; |
177 |
|
178 |
return ($i==$nb ? -1 : $i); |
179 |
} |
180 |
|
181 |
/**
|
182 |
* Replay dictionnary on several softwares
|
183 |
* @param $IDs array of software IDs to replay
|
184 |
* @param $res_rule array of rule results
|
185 |
* @return Query result handler
|
186 |
*/
|
187 |
function replayDictionnaryOnSoftwaresByID($IDs, $res_rule=array()) { |
188 |
global $DB; |
189 |
|
190 |
$new_softs = array(); |
191 |
$delete_ids = array (); |
192 |
|
193 |
foreach ($IDs as $ID) { |
194 |
$res_soft = $DB->query("SELECT gs.ID AS ID, gs.name AS name, gs.FK_entities AS FK_entities, gm.name AS manufacturer |
195 |
FROM glpi_software AS gs LEFT JOIN glpi_dropdown_manufacturer AS gm ON gs.FK_glpi_enterprise = gm.ID
|
196 |
WHERE gs.is_template=0 AND gs.ID ='" . $ID."'"); |
197 |
|
198 |
if ($DB->numrows($res_soft)){ |
199 |
$soft = $DB->fetch_array($res_soft); |
200 |
|
201 |
//For each software
|
202 |
$this->replayDictionnaryOnOneSoftware($new_softs,$res_rule, $ID,$soft["FK_entities"], |
203 |
(isset($soft["name"])?$soft["name"]:''), |
204 |
(isset($soft["manufacturer"])?$soft["manufacturer"]:''), $delete_ids); |
205 |
} |
206 |
} |
207 |
|
208 |
//Delete software if needed
|
209 |
$this->putOldSoftsInTrash($delete_ids); |
210 |
} |
211 |
|
212 |
/**
|
213 |
* Replay dictionnary on one software
|
214 |
* @param $new_softs array containing new softwares already computed
|
215 |
* @param $res_rule array of rule results
|
216 |
* @param $ID ID of the software
|
217 |
* @param $entity working entity ID
|
218 |
* @param $name softwrae name
|
219 |
* @param $manufacturer manufacturer ID
|
220 |
* @param $soft_ids array containing replay software need to be trashed
|
221 |
*/
|
222 |
function replayDictionnaryOnOneSoftware(&$new_softs,$res_rule, $ID,$entity, $name, $manufacturer, & $soft_ids) { |
223 |
global $DB; |
224 |
|
225 |
$input["name"] = $name; |
226 |
$input["manufacturer"] = $manufacturer; |
227 |
$input=addslashes_deep($input); |
228 |
|
229 |
if (empty($res_rule)){ |
230 |
$res_rule = $this->processAllRules($input, array (), array ()); |
231 |
$res_rule=addslashes_deep($res_rule); |
232 |
} |
233 |
|
234 |
//Get all the different versions for a software
|
235 |
$result = $DB->query("SELECT ID, version FROM glpi_licenses WHERE sID='" . $ID."'"); |
236 |
while ($license = $DB->fetch_array($result)) { |
237 |
$input["version"]=addslashes($license["version"]); |
238 |
//Replay software dictionnary rules
|
239 |
|
240 |
//Software's name has changed
|
241 |
if (isset($res_rule["name"]) && $res_rule["name"] != $name) |
242 |
{ |
243 |
if (isset($res_rule["FK_glpi_enterprise"])) |
244 |
$manufacturer = getDropdownName("glpi_dropdown_manufacturer",$res_rule["FK_glpi_enterprise"]); |
245 |
//New software not already present in this entity
|
246 |
if (!isset($new_softs[$entity][$res_rule["name"]])){ |
247 |
// create new software or restore it from trash
|
248 |
$new_software_id = addSoftwareOrRestoreFromTrash($res_rule["name"],$manufacturer,$entity,'',IMPORT_TYPE_DICTIONNARY); |
249 |
$new_softs[$entity][$res_rule["name"]] = $new_software_id; |
250 |
} else {
|
251 |
$new_software_id = $new_softs[$entity][$res_rule["name"]]; |
252 |
} |
253 |
} |
254 |
else
|
255 |
$new_software_id = $ID; |
256 |
|
257 |
//if (isCommandLine())
|
258 |
// echo "replayDictionnaryOnOneSoftware".$ID."/".$entity."/".$name."/".(isset($res_rule["version"]) && $res_rule["version"] != '')."/".$manufacturer."\n";
|
259 |
|
260 |
$this->moveLicenses($ID, $new_software_id, $license["ID"], $input["version"], ((isset($res_rule["version"]) && $res_rule["version"] != '') ? $res_rule["version"] : $license["version"]), $entity); |
261 |
} |
262 |
$soft_ids[] = $ID; |
263 |
} |
264 |
|
265 |
/**
|
266 |
* Delete a list of softwares
|
267 |
* @param $soft_ids array containing replay software need to be trashed
|
268 |
*/
|
269 |
function putOldSoftsInTrash($soft_ids) { |
270 |
global $DB,$CFG_GLPI,$LANG; |
271 |
|
272 |
if (isCommandLine()) {
|
273 |
//echo "checkUnusedSoftwaresAndDelete ()\n";
|
274 |
} |
275 |
if (count($soft_ids) > 0) { |
276 |
|
277 |
$first = true; |
278 |
$ids = ""; |
279 |
foreach ($soft_ids as $soft_id) { |
280 |
$ids .= (!$first ? "," : "") . "'" . $soft_id . "'"; |
281 |
$first = false; |
282 |
} |
283 |
|
284 |
//Try to delete all the software that are not used anymore (which means that don't have license associated anymore)
|
285 |
$res_countsoftinstall = $DB->query("SELECT glpi_software.ID as ID, count( glpi_licenses.sID ) AS cpt " . |
286 |
"FROM `glpi_software` LEFT JOIN glpi_licenses ON glpi_licenses.sID = glpi_software.ID " .
|
287 |
"WHERE glpi_software.ID IN (" . $ids . ") AND deleted=0 GROUP BY glpi_software.ID HAVING cpt=0 ORDER BY cpt"); |
288 |
|
289 |
$software = new Software; |
290 |
while ($soft = $DB->fetch_array($res_countsoftinstall)) { |
291 |
putSoftwareInTrash($soft["ID"], $LANG["rulesengine"][87], IMPORT_TYPE_DICTIONNARY); |
292 |
} |
293 |
} |
294 |
} |
295 |
|
296 |
/**
|
297 |
* Change software's name, and move licenses if needed
|
298 |
* @param $ID old software ID
|
299 |
* @param $new_software_id new software ID
|
300 |
* @param $license_id license ID to move
|
301 |
* @param $old_version old version
|
302 |
* @param $new_version new version
|
303 |
* @param $entity entity ID
|
304 |
*/
|
305 |
function moveLicenses($ID,$new_software_id, $license_id, $old_version, $new_version, $entity) { |
306 |
global $DB; |
307 |
|
308 |
$new_licenseID = $this->licenseExists($new_software_id, $license_id,$new_version); |
309 |
|
310 |
//A license does not exist
|
311 |
if ($new_licenseID == -1){ |
312 |
//Transfer licenses from old software to new software for a specific version
|
313 |
$DB->query("UPDATE glpi_licenses SET version='" . $new_version . "', sID='" . $new_software_id . "' WHERE sID='" . $ID."' AND version='".$old_version."'"); |
314 |
} else {
|
315 |
//Change ID of the license in glpi_inst_software
|
316 |
$DB->query("UPDATE glpi_inst_software SET license='" . $new_licenseID . "' WHERE license='" . $ID."'"); |
317 |
|
318 |
//Delete old license
|
319 |
$old_license = new License; |
320 |
$old_license->delete(array("ID"=>$license_id)); |
321 |
} |
322 |
} |
323 |
|
324 |
/**
|
325 |
* Check if a license exists
|
326 |
* @param $software_id software ID
|
327 |
* @param $license_id license ID to search
|
328 |
* @param $version license version
|
329 |
*/
|
330 |
function licenseExists($software_id, $license_id, $version) { |
331 |
global $DB; |
332 |
|
333 |
$license = new License; |
334 |
$license->getFromDB($license_id); |
335 |
|
336 |
//Check if the version exists
|
337 |
$sql = "SELECT * FROM glpi_licenses WHERE sID=" . $software_id . " AND version='" . $version . "' AND (serial='free' OR serial='global')"; |
338 |
|
339 |
//Unset unnecessary fields
|
340 |
unset ($license->fields["ID"]); |
341 |
unset ($license->fields["version"]); |
342 |
unset ($license->fields["sID"]); |
343 |
|
344 |
//Add all license's fields to the request
|
345 |
foreach ($license->fields as $field => $value) |
346 |
$sql .= " AND " . $field . "='" . $value . "'"; |
347 |
|
348 |
$res_version = $DB->query($sql); |
349 |
return (!$DB->numrows($res_version)?-1:$DB->result($res_version, 0, "ID")); |
350 |
} |
351 |
|
352 |
} |
353 |
|
354 |
/**
|
355 |
* Rule class store all informations about a GLPI rule :
|
356 |
* - description
|
357 |
* - criterias
|
358 |
* - actions
|
359 |
*
|
360 |
**/
|
361 |
class DictionnarySoftwareRule extends RuleCached { |
362 |
|
363 |
/**
|
364 |
* Constructor
|
365 |
**/
|
366 |
function DictionnarySoftwareRule() { |
367 |
$this->table = "glpi_rules_descriptions"; |
368 |
$this->type = -1; |
369 |
$this->rule_type = RULE_DICTIONNARY_SOFTWARE; |
370 |
$this->right = "rule_dictionnary_software"; |
371 |
$this->can_sort = true; |
372 |
} |
373 |
|
374 |
function getTitle() { |
375 |
global $LANG; |
376 |
return $LANG["rulesengine"][35]; |
377 |
} |
378 |
|
379 |
function maxActionsCount() { |
380 |
return 3; |
381 |
} |
382 |
|
383 |
function showCacheRuleHeader() |
384 |
{ |
385 |
global $LANG; |
386 |
echo "<th colspan='6'>".$LANG["rulesengine"][100]." : ".$this->fields["name"]."</th></tr>"; |
387 |
echo "<tr>"; |
388 |
echo "<td class='tab_bg_1'>".$LANG["rulesengine"][104]."</td>"; |
389 |
echo "<td class='tab_bg_1'>".$LANG["common"][5]." ".$LANG["rulesengine"][108]."</td>"; |
390 |
echo "<td class='tab_bg_1'>".$LANG["rulesengine"][105]."</td>"; |
391 |
echo "<td class='tab_bg_1'>".$LANG["rulesengine"][78]."</td>"; |
392 |
echo "<td class='tab_bg_1'>".$LANG["common"][5]."</td>"; |
393 |
echo "<td class='tab_bg_1'>".$LANG["rulesengine"][39]." ".$LANG["ocsng"][7]."</td>"; |
394 |
echo "</tr>"; |
395 |
} |
396 |
|
397 |
function showCacheRuleDetail($fields) |
398 |
{ |
399 |
global $LANG; |
400 |
echo "<td class='tab_bg_2'>".$fields["old_value"]."</td>"; |
401 |
echo "<td class='tab_bg_2'>".$fields["manufacturer"]."</td>"; |
402 |
echo "<td class='tab_bg_2'>".($fields["new_value"]!=''?$fields["new_value"]:$LANG["rulesengine"][106])."</td>"; |
403 |
echo "<td class='tab_bg_2'>".($fields["version"]!=''?$fields["version"]:$LANG["rulesengine"][106])."</td>"; |
404 |
echo "<td class='tab_bg_2'>".((isset($fields["new_manufacturer"]) && $fields["new_manufacturer"]!='')?getDropdownName("glpi_dropdown_manufacturer",$fields["new_manufacturer"]):$LANG["rulesengine"][106])."</td>"; |
405 |
echo "<td class='tab_bg_2'>"; |
406 |
if ($fields["ignore_ocs_import"]==''){ |
407 |
echo " "; |
408 |
} else {
|
409 |
echo getYesNo($fields["ignore_ocs_import"]); |
410 |
} |
411 |
echo "</td>"; |
412 |
} |
413 |
} |
414 |
|
415 |
?>
|