Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / hook.php @ c482ac76

History | View | Annotate | Download (6.12 KB)

1
<?php
2

    
3
require __DIR__ . DIRECTORY_SEPARATOR .'autoloader.php';
4
class VigiloHooks
5
{
6
    private $confdir;
7

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

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

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

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

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

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

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

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

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

    
60
    public function addNetworkEquipment($networkequipment)
61
    {
62
        if ($networkequipment->getField("is_template")==0) {
63
            global $DB;
64

    
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")==0) {
73
            global $DB;
74

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

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

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

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

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

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

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

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

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

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

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

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

    
190
    public function plugin_vigilo_getAddSearchOptions($itemtype)
191
    {
192
        $options = array();
193
        if ($itemtype == 'Computer' or $itemtype == 'PluginVigiloComputer')
194
        {
195
            $options['7007']['table']          = 'glpi_computers';
196
            $options['7007']['field']          = 'vigilo_template';
197
            $options['7007']['name']           = 'vigilo_template';
198
            $options['7007']['massiveaction']  = 'TRUE';
199
            $options['7007']['datatype']       = 'dropdown';
200
            return $options;
201
        }
202
    }
203
}