Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / vigilo_hooks.php @ 077e4de7

History | View | Annotate | Download (6.13 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 ($res !== false) {
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") == 0) {
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") == 0) {
65
            global $DB;
66

    
67
            $host = new VigiloNetworkEquipment($networkequipment);
68
            $this->saveHost($host, "hosts");
69
        }
70
    }
71

    
72
    public function addPrinter($printer)
73
    {
74
        if ($printer->getField("is_template") == 0) {
75
            global $DB;
76

    
77
            $host = new VigiloPrinter($printer);
78
            $this->saveHost($host, "hosts");
79
        }
80
    }
81

    
82
    public function delete($computer)
83
    {
84
        $this->unmonitor($computer->fields["name"]);
85
    }
86

    
87
    public function update($computer)
88
    {
89
        global $PLUGIN_HOOKS, $DB;
90
        if (isset($computer->oldvalues["name"])) {
91
            $this->unmonitor($computer->oldvalues["name"]);
92
        }
93
    }
94

    
95
    public function updateComputer($computer)
96
    {
97
        $this->update($computer);
98
        $this->addComputer($computer);
99
    }
100

    
101
    public function updateNetworkEquipment($networkEquipment)
102
    {
103
        $this->update($networkEquipment);
104
        $this->addNetworkEquipment($networkEquipment);
105
    }
106

    
107
    public function updatePrinter($printer)
108
    {
109
        $this->update($printer);
110
        $this->addPrinter($printer);
111
    }
112

    
113
    public function unmonitor($host)
114
    {
115
        $dirs = array($this->confdir, "hosts", "managed", $host . ".xml");
116
        $filename = implode(DIRECTORY_SEPARATOR, $dirs);
117
        if (file_exists($filename)) {
118
            unlink($filename);
119
        }
120
    }
121

    
122
    public function manageComputerSoftwareVersion($computer_software_version)
123
    {
124
        global $DB;
125
        $computer=new Computer();
126
        $computer->getFromDB($computer_software_version->getField("computers_id"));
127
        $this->updateComputer($computer);
128
    }
129

    
130
    public function manageSoftwares($software)
131
    {
132
        global $DB;
133
        $softwareVer=new SoftwareVersion();
134
        $idSoftwareVersion=$softwareVer->find('softwares_id=' . $software->getID());
135
        foreach ($idSoftwareVersion as $idVersion) {
136
            if ($idVersion['id']) {
137
                $computerVer=new Computer_SoftwareVersion();
138
                $goodField='softwareversions_id=' . $idVersion['id'];
139
                $updateComp=$computerVer->find($goodField);
140
                foreach ($updateComp as $idComputer) {
141
                    if ($idComputer['computers_id'] != -1) {
142
                        $computer=new Computer();
143
                        $computer->getFromDB($idComputer['computers_id']);
144
                        $this->updateComputer($computer);
145
                    }
146
                }
147
            }
148
        }
149
    }
150

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

    
160
    public function manageAddresses($address)
161
    {
162
        global $DB;
163
        $id = $address->getField('mainitems_id');
164
        $comp = new Computer();
165
        $comp->getFromDB($id);
166
        $this->updateComputer($comp);
167
    }
168

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

    
189
    // @codingStandardsIgnoreStart
190
    public function plugin_vigilo_getAddSearchOptions($itemtype)
191
    {
192
        // Le nom de la méthode est imposé par GLPI.
193
        // @codingStandardsIgnoreEnd
194
        $options = array();
195
        if ($itemtype == 'Computer' || $itemtype == 'PluginVigiloComputer') {
196
            $options['7007']['table']          = 'glpi_computers';
197
            $options['7007']['field']          = 'vigilo_template';
198
            $options['7007']['name']           = 'vigilo_template';
199
            $options['7007']['massiveaction']  = 'TRUE';
200
            $options['7007']['datatype']       = 'dropdown';
201
        }
202
        return $options;
203
    }
204
}