glpi / src / plugins / vigilo / inc / template.class.php @ 14b31af4
History | View | Annotate | Download (3.83 KB)
1 | 166be9d2 | Francois POIROTTE | <?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 | 79c0fe97 | Francois POIROTTE | if (array_key_exists('formfooter', $options) && false === $options['formfooter']) { |
22 | return;
|
||
23 | } |
||
24 | |||
25 | 166be9d2 | Francois POIROTTE | $opts = array( |
26 | "name" => "vigilo_template", |
||
27 | "value" => 0, |
||
28 | "url" => $CFG_GLPI["root_doc"] . "/plugins/vigilo/ajax/getTemplates.php" |
||
29 | ); |
||
30 | |||
31 | $id = $item->getID(); |
||
32 | $query = <<<SQL |
||
33 | SELECT `template`
|
||
34 | FROM glpi_plugin_vigilo_template
|
||
35 | cdde5484 | Francois POIROTTE | WHERE `id` = $id;
|
36 | 166be9d2 | Francois POIROTTE | SQL;
|
37 | $result = $DB->query($query); |
||
38 | if ($result) { |
||
39 | $tpl = $DB->result($result, 0, "template"); |
||
40 | $templates = static::getTemplates(); |
||
41 | $index = array_search($tpl, $templates, true); |
||
42 | if (false !== $index) { |
||
43 | $opts['value'] = $index; |
||
44 | $opts['emptylabel'] = $tpl; |
||
45 | } |
||
46 | } |
||
47 | |||
48 | a651e78d | Francois POIROTTE | echo "<tr><th colspan='4'>Vigilo NMS</th></tr>"; |
49 | 166be9d2 | Francois POIROTTE | echo '<tr>'; |
50 | echo '<td><label for="vigilo_template">Vigilo Template</td>'; |
||
51 | echo '<td>'; |
||
52 | Dropdown::show(__CLASS__, $opts); |
||
53 | echo '</td></tr>'; |
||
54 | } |
||
55 | |||
56 | public static function filterFile($path) |
||
57 | { |
||
58 | $tpl_dir = VIGILO_CONFDIR . DIRECTORY_SEPARATOR . 'hosttemplates'; |
||
59 | $pattern = '#^(([^.][^/]+/)*[^.][^/]+\.xml)$#i'; |
||
60 | // FIXME : l'utilisation de substr()+strlen() n'est pas idéale ici.
|
||
61 | return preg_match($pattern, substr($path->getPathname(), strlen($tpl_dir))); |
||
62 | } |
||
63 | |||
64 | public static function getTemplates() |
||
65 | { |
||
66 | $tpl_dir = VIGILO_CONFDIR . DIRECTORY_SEPARATOR . 'hosttemplates'; |
||
67 | $templates = array(); |
||
68 | a18806e5 | Francois POIROTTE | $pattern = "/<template\\s+(?:.*?\\s+)?name=(['\"])([^'\"]+)\\1>/"; |
69 | 166be9d2 | Francois POIROTTE | $dir_it = new RecursiveDirectoryIterator($tpl_dir, RecursiveDirectoryIterator::SKIP_DOTS); |
70 | $it_it = new RecursiveIteratorIterator($dir_it); |
||
71 | $files = new CallbackFilterIterator($it_it, array(__CLASS__, 'filterFile')); |
||
72 | |||
73 | foreach ($files as $file) { |
||
74 | $file = $file->getPathname(); |
||
75 | |||
76 | if (!is_file($file)) { |
||
77 | continue;
|
||
78 | } |
||
79 | |||
80 | $ok = preg_match_all($pattern, file_get_contents($file), $matches); |
||
81 | if (!$ok) { |
||
82 | continue;
|
||
83 | } |
||
84 | |||
85 | foreach ($matches[2] as $match) { |
||
86 | $templates[] = $match; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | $templates = array_unique($templates); |
||
91 | sort($templates, SORT_REGULAR); |
||
92 | array_unshift($templates, '-----'); |
||
93 | return $templates; |
||
94 | } |
||
95 | |||
96 | public static function getAjaxTemplates() |
||
97 | { |
||
98 | $res = array(); |
||
99 | foreach (static::getTemplates() as $index => $tpl) { |
||
100 | $res[] = array("id" => $index, "text" => $tpl); |
||
101 | } |
||
102 | return $res; |
||
103 | } |
||
104 | 925171f8 | Francois POIROTTE | |
105 | public static function getTemplateIndexForItem(CommonDBTM $item) |
||
106 | { |
||
107 | $tpl = self::getTemplateNameForItem($item); |
||
108 | if (null === $tpl) { |
||
109 | return null; |
||
110 | } |
||
111 | |||
112 | $templates = self::getTemplates(); |
||
113 | $index = array_search($tpl, $templates, true); |
||
114 | return (false !== $index ? $index : null); |
||
115 | } |
||
116 | |||
117 | public static function getTemplateNameForItem(CommonDBTM $item) |
||
118 | { |
||
119 | global $DB; |
||
120 | |||
121 | $id = $item->getID(); |
||
122 | $query = <<<SQL |
||
123 | SELECT `template`
|
||
124 | FROM glpi_plugin_vigilo_template
|
||
125 | WHERE `id` = $id;
|
||
126 | SQL;
|
||
127 | |||
128 | $result = $DB->query($query); |
||
129 | if (!$result) { |
||
130 | return null; |
||
131 | } |
||
132 | |||
133 | return $DB->result($result, 0, "template"); |
||
134 | } |
||
135 | 166be9d2 | Francois POIROTTE | } |