Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / plugins / vigilo / inc / template.class.php @ e1c378c0

History | View | Annotate | Download (2.98 KB)

1
<?php
2

    
3
class PluginVigiloTemplate extends CommonDBTM
4
{
5
    public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
6
    {
7
        return 'Vigilo';
8
    }
9

    
10
    public static function showForm($params)
11
    {
12
        global $DB, $CFG_GLPI;
13

    
14
        $item = $params['item'];
15
        $options = $params['options'];
16

    
17
        if (!in_array($item::getType(), array('Computer', 'Printer', 'NetworkEquipment'))) {
18
            return;
19
        }
20

    
21
        $opts = array(
22
            "name" => "vigilo_template",
23
            "value" => 0,
24
            "url" => $CFG_GLPI["root_doc"] . "/plugins/vigilo/ajax/getTemplates.php"
25
        );
26

    
27
        $id = $item->getID();
28
        $query = <<<SQL
29
SELECT `template`
30
FROM glpi_plugin_vigilo_template
31
WHERE `id` = $id;
32
SQL;
33
        $result = $DB->query($query);
34
        if ($result) {
35
            $tpl        = $DB->result($result, 0, "template");
36
            $templates  = static::getTemplates();
37
            $index      = array_search($tpl, $templates, true);
38
            if (false !== $index) {
39
                $opts['value']      = $index;
40
                $opts['emptylabel'] = $tpl;
41
            }
42
        }
43

    
44
        echo "<tr><th colspan='4'>Vigilo</th></tr>";
45
        echo '<tr>';
46
        echo '<td><label for="vigilo_template">Vigilo Template</td>';
47
        echo '<td>';
48
        Dropdown::show(__CLASS__, $opts);
49
        echo '</td></tr>';
50
    }
51

    
52
    public static function filterFile($path)
53
    {
54
        $tpl_dir    = VIGILO_CONFDIR . DIRECTORY_SEPARATOR . 'hosttemplates';
55
        $pattern    = '#^(([^.][^/]+/)*[^.][^/]+\.xml)$#i';
56
        // FIXME : l'utilisation de substr()+strlen() n'est pas idéale ici.
57
        return preg_match($pattern, substr($path->getPathname(), strlen($tpl_dir)));
58
    }
59

    
60
    public static function getTemplates()
61
    {
62
        $tpl_dir    = VIGILO_CONFDIR . DIRECTORY_SEPARATOR . 'hosttemplates';
63
        $templates  = array();
64
        $pattern    = "/<template\\s+(?:.*?\\s+)?name=(['\"])(\w+)\\1>/";
65
        $dir_it     = new RecursiveDirectoryIterator($tpl_dir, RecursiveDirectoryIterator::SKIP_DOTS);
66
        $it_it      = new RecursiveIteratorIterator($dir_it);
67
        $files      = new CallbackFilterIterator($it_it, array(__CLASS__, 'filterFile'));
68

    
69
        foreach ($files as $file) {
70
            $file = $file->getPathname();
71

    
72
            if (!is_file($file)) {
73
                continue;
74
            }
75

    
76
            $ok = preg_match_all($pattern, file_get_contents($file), $matches);
77
            if (!$ok) {
78
                continue;
79
            }
80

    
81
            foreach ($matches[2] as $match) {
82
                $templates[] = $match;
83
            }
84
        }
85

    
86
        $templates = array_unique($templates);
87
        sort($templates, SORT_REGULAR);
88
        array_unshift($templates, '-----');
89
        return $templates;
90
    }
91

    
92
    public static function getAjaxTemplates()
93
    {
94
        $res = array();
95
        foreach (static::getTemplates() as $index => $tpl) {
96
            $res[] = array("id" => $index, "text" => $tpl);
97
        }
98
        return $res;
99
    }
100
}