glpi / src / vigilo_hooks.php @ 0e8eed1f
History | View | Annotate | Download (5.77 KB)
1 |
<?php
|
---|---|
2 |
|
3 |
class VigiloHooks |
4 |
{ |
5 |
public function saveHost($host, $dir_type) |
6 |
{ |
7 |
$dirs = array(VIGILO_CONFDIR, $dir_type, "managed"); |
8 |
$confdir = implode(DIRECTORY_SEPARATOR, $dirs); |
9 |
$file = $confdir . DIRECTORY_SEPARATOR . $host->getName() . ".xml"; |
10 |
|
11 |
if (!file_exists($confdir)) { |
12 |
mkdir($confdir, 0770, true); |
13 |
} |
14 |
|
15 |
$res = file_put_contents($file, $host, LOCK_EX); |
16 |
if (false !== $res) { |
17 |
chgrp($file, "vigiconf"); |
18 |
chmod($file, 0660); |
19 |
} |
20 |
} |
21 |
|
22 |
public function updateGroups() |
23 |
{ |
24 |
$host = new VigiloLocation(); |
25 |
$this->saveHost($host, "groups"); |
26 |
} |
27 |
|
28 |
public function addComputer($computer) |
29 |
{ |
30 |
global $DB; |
31 |
|
32 |
if (!$computer->getField("is_template")) { |
33 |
$template_id = PluginVigiloVigiloTemplate::getVigiloTemplateNameByID( |
34 |
$computer->getField("vigilo_template") |
35 |
); |
36 |
|
37 |
if (!empty($template_id)) { |
38 |
$query = "UPDATE glpi_computers |
39 |
SET vigilo_template = '" . $template_id . |
40 |
"' WHERE id = " . $computer->getField("id") . ";"; |
41 |
$DB->queryOrDie($query, "update vigilo_template field"); |
42 |
} |
43 |
|
44 |
$query = "UPDATE glpi_computers |
45 |
SET is_dynamic = ' 1
|
46 |
' WHERE id = " . $computer->getField("id") . ";"; |
47 |
$DB->queryOrDie($query, "update vigilo_template field"); |
48 |
|
49 |
$host = new VigiloHost($computer); |
50 |
$this->saveHost($host, "hosts"); |
51 |
} |
52 |
} |
53 |
|
54 |
public function addNetworkEquipment($networkequipment) |
55 |
{ |
56 |
if (!$networkequipment->getField("is_template")) { |
57 |
$host = new VigiloNetworkEquipment($networkequipment); |
58 |
$this->saveHost($host, "hosts"); |
59 |
} |
60 |
} |
61 |
|
62 |
public function addPrinter($printer) |
63 |
{ |
64 |
if (!$printer->getField("is_template")) { |
65 |
$host = new VigiloPrinter($printer); |
66 |
$this->saveHost($host, "hosts"); |
67 |
} |
68 |
} |
69 |
|
70 |
public function delete($computer) |
71 |
{ |
72 |
$this->unmonitor($computer->fields["name"]); |
73 |
} |
74 |
|
75 |
public function update($computer) |
76 |
{ |
77 |
if (isset($computer->oldvalues["name"])) { |
78 |
$this->unmonitor($computer->oldvalues["name"]); |
79 |
} |
80 |
} |
81 |
|
82 |
public function updateComputer($computer) |
83 |
{ |
84 |
$this->update($computer); |
85 |
$this->addComputer($computer); |
86 |
} |
87 |
|
88 |
public function updateNetworkEquipment($networkEquipment) |
89 |
{ |
90 |
$this->update($networkEquipment); |
91 |
$this->addNetworkEquipment($networkEquipment); |
92 |
} |
93 |
|
94 |
public function updatePrinter($printer) |
95 |
{ |
96 |
$this->update($printer); |
97 |
$this->addPrinter($printer); |
98 |
} |
99 |
|
100 |
public function unmonitor($host) |
101 |
{ |
102 |
$dirs = array(VIGILO_CONFDIR, "hosts", "managed", $host . ".xml"); |
103 |
$filename = implode(DIRECTORY_SEPARATOR, $dirs); |
104 |
if (file_exists($filename)) { |
105 |
unlink($filename); |
106 |
} |
107 |
} |
108 |
|
109 |
public function manageComputerSoftwareVersion($computer_software_version) |
110 |
{ |
111 |
$computer = new Computer(); |
112 |
$computer->getFromDB($computer_software_version->getField("computers_id")); |
113 |
$this->updateComputer($computer); |
114 |
} |
115 |
|
116 |
public function manageSoftwares($software) |
117 |
{ |
118 |
$softwareVer = new SoftwareVersion(); |
119 |
$idSoftwareVersion = $softwareVer->find('softwares_id=' . $software->getID()); |
120 |
foreach ($idSoftwareVersion as $idVersion) { |
121 |
if (!$idVersion['id']) { |
122 |
continue;
|
123 |
} |
124 |
|
125 |
$computerVer = new Computer_SoftwareVersion(); |
126 |
$goodField = 'softwareversions_id=' . $idVersion['id']; |
127 |
$updateComp = $computerVer->find($goodField); |
128 |
|
129 |
foreach ($updateComp as $idComputer) { |
130 |
if (-1 === $idComputer['computers_id']) { |
131 |
continue;
|
132 |
} |
133 |
|
134 |
$computer = new Computer(); |
135 |
$computer->getFromDB($idComputer['computers_id']); |
136 |
$this->updateComputer($computer); |
137 |
} |
138 |
} |
139 |
} |
140 |
|
141 |
public function manageDisks($disk) |
142 |
{ |
143 |
$id = $disk->getField('computers_id'); |
144 |
$computer = new Computer(); |
145 |
$computer->getFromDB($id); |
146 |
$this->updateComputer($computer); |
147 |
} |
148 |
|
149 |
public function manageAddresses($address) |
150 |
{ |
151 |
$id = $address->getField('mainitems_id'); |
152 |
$comp = new Computer(); |
153 |
$comp->getFromDB($id); |
154 |
$this->updateComputer($comp); |
155 |
} |
156 |
|
157 |
public function manageNetworks($network) |
158 |
{ |
159 |
$id = $network->getField('items_id'); |
160 |
$itemtype = $network->getField('itemtype'); |
161 |
if ($itemtype === 'Computer') { |
162 |
$comp = new Computer(); |
163 |
$comp->getFromDB($id); |
164 |
$this->updateComputer($comp); |
165 |
} elseif ($itemtype === 'NetworkEquipment') { |
166 |
$ne = new NetworkEquipment(); |
167 |
$ne->getFromDB($id); |
168 |
$this->updateNetworkEquipment($ne); |
169 |
} elseif ($itemtype === 'Printer') { |
170 |
$printer = new Printer(); |
171 |
$printer->getFromDB($id); |
172 |
$this->updatePrinter($printer); |
173 |
} |
174 |
} |
175 |
|
176 |
// @codingStandardsIgnoreStart
|
177 |
public function plugin_vigilo_getAddSearchOptions($itemtype) |
178 |
{ |
179 |
// Le nom de la méthode est imposé par GLPI.
|
180 |
// @codingStandardsIgnoreEnd
|
181 |
$options = array(); |
182 |
|
183 |
if ($itemtype == 'Computer' || $itemtype == 'PluginVigiloComputer') { |
184 |
$options['7007']['table'] = 'glpi_computers'; |
185 |
$options['7007']['field'] = 'vigilo_template'; |
186 |
$options['7007']['name'] = 'vigilo_template'; |
187 |
$options['7007']['massiveaction'] = 'TRUE'; |
188 |
$options['7007']['datatype'] = 'dropdown'; |
189 |
} |
190 |
|
191 |
return $options; |
192 |
} |
193 |
} |