Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / vigilo_hooks.php @ ad6689da

History | View | Annotate | Download (5.96 KB)

1
<?php
2

    
3
class VigiloHooks
4
{
5
    private $confdir;
6

    
7
    public function __construct($confdir = "/etc/vigilo/vigiconf/conf.d")
8
    {
9
        spl_autoload_register('vigilo_autoloader');
10
        $this->confdir = $confdir;
11
    }
12

    
13
    public function saveHost($host, $dir_type)
14
    {
15
        $dirs       = array($this->confdir, $dir_type, "managed");
16
        $confdir    = implode(DIRECTORY_SEPARATOR, $dirs);
17
        $file       = $confdir . DIRECTORY_SEPARATOR . $host->getName() . ".xml";
18

    
19
        if (!file_exists($confdir)) {
20
            mkdir($confdir, 0770, true);
21
        }
22

    
23
        $res = file_put_contents($file, $host, LOCK_EX);
24
        if (false !== $res) {
25
            chgrp($file, "vigiconf");
26
            chmod($file, 0660);
27
        }
28
    }
29

    
30
    public function updateGroups()
31
    {
32
        $host       = new VigiloLocation();
33
        $this->saveHost($host, "groups");
34
    }
35

    
36
    public function addComputer($computer)
37
    {
38
        global $DB;
39

    
40
        if (!$computer->getField("is_template")) {
41
            $template_id = PluginVigiloVigiloTemplate::getVigiloTemplateNameByID(
42
                $computer->getField("vigilo_template")
43
            );
44

    
45
            if (!empty($template_id)) {
46
                $query = "UPDATE glpi_computers
47
                          SET vigilo_template = '" . $template_id .
48
                         "' WHERE id = " . $computer->getField("id") . ";";
49
                $DB->queryOrDie($query, "update vigilo_template field");
50
            }
51

    
52
            $query = "UPDATE glpi_computers
53
                      SET is_dynamic = ' 1
54
                      ' WHERE id = " . $computer->getField("id") . ";";
55
            $DB->queryOrDie($query, "update vigilo_template field");
56

    
57
            $host = new VigiloHost($computer);
58
            $this->saveHost($host, "hosts");
59
        }
60
    }
61

    
62
    public function addNetworkEquipment($networkequipment)
63
    {
64
        if (!$networkequipment->getField("is_template")) {
65
            $host = new VigiloNetworkEquipment($networkequipment);
66
            $this->saveHost($host, "hosts");
67
        }
68
    }
69

    
70
    public function addPrinter($printer)
71
    {
72
        if (!$printer->getField("is_template")) {
73
            $host = new VigiloPrinter($printer);
74
            $this->saveHost($host, "hosts");
75
        }
76
    }
77

    
78
    public function delete($computer)
79
    {
80
        $this->unmonitor($computer->fields["name"]);
81
    }
82

    
83
    public function update($computer)
84
    {
85
        if (isset($computer->oldvalues["name"])) {
86
            $this->unmonitor($computer->oldvalues["name"]);
87
        }
88
    }
89

    
90
    public function updateComputer($computer)
91
    {
92
        $this->update($computer);
93
        $this->addComputer($computer);
94
    }
95

    
96
    public function updateNetworkEquipment($networkEquipment)
97
    {
98
        $this->update($networkEquipment);
99
        $this->addNetworkEquipment($networkEquipment);
100
    }
101

    
102
    public function updatePrinter($printer)
103
    {
104
        $this->update($printer);
105
        $this->addPrinter($printer);
106
    }
107

    
108
    public function unmonitor($host)
109
    {
110
        $dirs = array($this->confdir, "hosts", "managed", $host . ".xml");
111
        $filename = implode(DIRECTORY_SEPARATOR, $dirs);
112
        if (file_exists($filename)) {
113
            unlink($filename);
114
        }
115
    }
116

    
117
    public function manageComputerSoftwareVersion($computer_software_version)
118
    {
119
        $computer = new Computer();
120
        $computer->getFromDB($computer_software_version->getField("computers_id"));
121
        $this->updateComputer($computer);
122
    }
123

    
124
    public function manageSoftwares($software)
125
    {
126
        $softwareVer = new SoftwareVersion();
127
        $idSoftwareVersion = $softwareVer->find('softwares_id=' . $software->getID());
128
        foreach ($idSoftwareVersion as $idVersion) {
129
            if (!$idVersion['id']) {
130
                continue;
131
            }
132

    
133
            $computerVer = new Computer_SoftwareVersion();
134
            $goodField   = 'softwareversions_id=' . $idVersion['id'];
135
            $updateComp  = $computerVer->find($goodField);
136

    
137
            foreach ($updateComp as $idComputer) {
138
                if (-1 === $idComputer['computers_id']) {
139
                    continue;
140
                }
141

    
142
                $computer = new Computer();
143
                $computer->getFromDB($idComputer['computers_id']);
144
                $this->updateComputer($computer);
145
            }
146
        }
147
    }
148

    
149
    public function manageDisks($disk)
150
    {
151
        $id = $disk->getField('computers_id');
152
        $computer = new Computer();
153
        $computer->getFromDB($id);
154
        $this->updateComputer($computer);
155
    }
156

    
157
    public function manageAddresses($address)
158
    {
159
        $id = $address->getField('mainitems_id');
160
        $comp = new Computer();
161
        $comp->getFromDB($id);
162
        $this->updateComputer($comp);
163
    }
164

    
165
    public function manageNetworks($network)
166
    {
167
        $id = $network->getField('items_id');
168
        $itemtype = $network->getField('itemtype');
169
        if ($itemtype === 'Computer') {
170
            $comp = new Computer();
171
            $comp->getFromDB($id);
172
            $this->updateComputer($comp);
173
        } elseif ($itemtype === 'NetworkEquipment') {
174
            $ne = new NetworkEquipment();
175
            $ne->getFromDB($id);
176
            $this->updateNetworkEquipment($ne);
177
        } elseif ($itemtype === 'Printer') {
178
            $printer = new Printer();
179
            $printer->getFromDB($id);
180
            $this->updatePrinter($printer);
181
        }
182
    }
183

    
184
    // @codingStandardsIgnoreStart
185
    public function plugin_vigilo_getAddSearchOptions($itemtype)
186
    {
187
        // Le nom de la méthode est imposé par GLPI.
188
        // @codingStandardsIgnoreEnd
189
        $options = array();
190

    
191
        if ($itemtype == 'Computer' || $itemtype == 'PluginVigiloComputer') {
192
            $options['7007']['table']          = 'glpi_computers';
193
            $options['7007']['field']          = 'vigilo_template';
194
            $options['7007']['name']           = 'vigilo_template';
195
            $options['7007']['massiveaction']  = 'TRUE';
196
            $options['7007']['datatype']       = 'dropdown';
197
        }
198

    
199
        return $options;
200
    }
201
}