Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / vigilo_hooks.php @ 1be75264

History | View | Annotate | Download (5.91 KB)

1
<?php
2

    
3
class VigiloHooks
4
{
5
    private $confdir;
6

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
198
        return $options;
199
    }
200
}