Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / hook.php @ e0c97dab

History | View | Annotate | Download (6.12 KB)

1
<?php
2

    
3
require __DIR__ . DIRECTORY_SEPARATOR .'autoloader.php';
4

    
5
class VigiloHooks
6
{
7
    private $confdir;
8

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

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

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

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

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

    
38
    public function addComputer($computer)
39
    {
40
        if ($computer->getField("is_template")==0) {
41
            global $DB;
42
            $template_id = PluginVigiloVigiloTemplate::getVigiloTemplateNameByID($computer->getField("vigilo_template"));
43

    
44
            if(!empty($template_id)) {
45
                $query = "UPDATE glpi_computers
46
                          SET vigilo_template = '" . PluginVigiloVigiloTemplate::getVigiloTemplateNameByID($computer->getField("vigilo_template")) .
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")==0) {
64
            global $DB;
65

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

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

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

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

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

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

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

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

    
112
    public function unmonitor($host)
113
    {
114
        $dirs = array($this->confdir, "hosts", "managed", $host . ".xml");
115
        $filename = implode(DIRECTORY_SEPARATOR, $dirs);
116
        if (file_exists($filename))
117
        {
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
        }
179
        else if ($itemtype === 'NetworkEquipment') {
180
            $ne=new NetworkEquipment();
181
            $ne->getFromDB($id);
182
            $this->updateNetworkEquipment($ne);
183
        }
184
        else if ($itemtype === 'Printer') {
185
            $printer=new Printer();
186
            $printer->getFromDB($id);
187
            $this->updatePrinter($printer);
188
        }
189
    }
190

    
191
    public function plugin_vigilo_getAddSearchOptions($itemtype)
192
    {
193
        $options = array();
194
        if ($itemtype == 'Computer' or $itemtype == 'PluginVigiloComputer')
195
        {
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
            return $options;
202
        }
203
    }
204
}