Skip to content

Commit 06b0ff0

Browse files
committed
Add injection class for GLPI 11
1 parent 1292d99 commit 06b0ff0

4 files changed

Lines changed: 195 additions & 7 deletions

File tree

‎hook.php‎

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
use Glpi\Plugin\Hooks;
3131
use GlpiPlugin\Addressing\Addressing;
32+
use GlpiPlugin\Addressing\AddressingInjection;
3233
use GlpiPlugin\Addressing\Filter;
3334
use GlpiPlugin\Addressing\PingInfo;
3435
use GlpiPlugin\Addressing\Profile;
@@ -174,6 +175,78 @@ function plugin_addressing_install()
174175
}
175176
}
176177

178+
// Version 3.2.2 - Migration des IDs de recherche 1000/1001 vers 100/101
179+
$num_mapping = [1000 => 100, 1001 => 101];
180+
181+
$has_old_prefs = $DB->request([
182+
'COUNT' => 'cpt',
183+
'FROM' => 'glpi_displaypreferences',
184+
'WHERE' => [
185+
'itemtype' => Addressing::class,
186+
'num' => array_keys($num_mapping),
187+
],
188+
])->current()['cpt'] > 0;
189+
190+
if ($has_old_prefs) {
191+
foreach ($num_mapping as $old_num => $new_num) {
192+
$conflicts = $DB->request([
193+
'SELECT' => ['users_id', 'interface'],
194+
'FROM' => 'glpi_displaypreferences',
195+
'WHERE' => [
196+
'itemtype' => Addressing::class,
197+
'num' => $new_num,
198+
],
199+
]);
200+
201+
foreach ($conflicts as $conflict) {
202+
$DB->delete('glpi_displaypreferences', [
203+
'itemtype' => Addressing::class,
204+
'num' => $old_num,
205+
'users_id' => $conflict['users_id'],
206+
'interface' => $conflict['interface'],
207+
]);
208+
}
209+
210+
$DB->update('glpi_displaypreferences', ['num' => $new_num], [
211+
'itemtype' => Addressing::class,
212+
'num' => $old_num,
213+
]);
214+
}
215+
216+
$saved_searches = $DB->request([
217+
'FROM' => 'glpi_savedsearches',
218+
'WHERE' => ['itemtype' => Addressing::class],
219+
]);
220+
221+
foreach ($saved_searches as $saved_search) {
222+
parse_str($saved_search['query'], $params);
223+
$modified = false;
224+
225+
if (isset($params['criteria']) && is_array($params['criteria'])) {
226+
foreach ($params['criteria'] as &$criterion) {
227+
if (isset($criterion['field']) && isset($num_mapping[(int) $criterion['field']])) {
228+
$criterion['field'] = (string) $num_mapping[(int) $criterion['field']];
229+
$modified = true;
230+
}
231+
}
232+
unset($criterion);
233+
}
234+
235+
if (isset($params['sort']) && isset($num_mapping[(int) $params['sort']])) {
236+
$params['sort'] = (string) $num_mapping[(int) $params['sort']];
237+
$modified = true;
238+
}
239+
240+
if ($modified) {
241+
$DB->update('glpi_savedsearches', [
242+
'query' => http_build_query($params),
243+
], [
244+
'id' => $saved_search['id'],
245+
]);
246+
}
247+
}
248+
}
249+
177250
if ($update) {
178251
$query_ = "SELECT *
179252
FROM `glpi_plugin_addressing_profiles` ";
@@ -200,7 +273,7 @@ function plugin_addressing_install()
200273
//Add all rights for current user profile
201274
Profile::createFirstAccess($_SESSION['glpiactiveprofile']['id']);
202275
//Drop old profile table : not used anymore
203-
$migration = new Migration("2.5.0");
276+
$migration = new Migration(PLUGIN_ADDRESSING_VERSION);
204277
$migration->dropTable('glpi_plugin_addressing_profiles');
205278
CronTask::Register(PingInfo::class, 'UpdatePing', DAY_TIMESTAMP);
206279

@@ -215,7 +288,7 @@ function plugin_addressing_uninstall()
215288
{
216289
global $DB;
217290

218-
$migration = new Migration("2.5.0");
291+
$migration = new Migration(PLUGIN_ADDRESSING_VERSION);
219292
$tables = ["glpi_plugin_addressing_addressings",
220293
"glpi_plugin_addressing_configs",
221294
"glpi_plugin_addressing_filters",
@@ -388,7 +461,7 @@ function plugin_addressing_dynamicReport($params)
388461
function plugin_addressing_addOrderBy($itemtype, $ID, $order, $key)
389462
{
390463
if ($itemtype == Addressing::class
391-
&& ($ID == 1000 || $ID == 1001)) {
464+
&& ($ID == 100 || $ID == 101)) {
392465
return "ORDER BY INET_ATON(ITEM_$key) $order";
393466
}
394467
}
@@ -404,3 +477,9 @@ function plugin_addressing_postinit()
404477
= [PingInfo::class, 'cleanForItem'];
405478
}
406479
}
480+
481+
function plugin_datainjection_populate_addressing()
482+
{
483+
global $INJECTABLE_TYPES;
484+
$INJECTABLE_TYPES[AddressingInjection::class] = 'addressing';
485+
}

‎setup.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ function plugin_init_addressing()
7373
$PLUGIN_HOOKS[Hooks::POST_ITEM_FORM]['addressing'] = [PingInfo::class,
7474
'getPingResponseForItem'];
7575

76+
if (class_exists(Addressing::class)) { // only if plugin activated
77+
$PLUGIN_HOOKS['plugin_datainjection_populate']['addressing']
78+
= 'plugin_datainjection_populate_addressing';
79+
}
80+
7681
// Add specific files to add to the header : javascript or css
7782
if (isset($_SESSION['glpiactiveprofile']['interface'])
7883
&& $_SESSION['glpiactiveprofile']['interface'] == 'central') {
@@ -98,3 +103,4 @@ function plugin_version_addressing()
98103
],
99104
]];
100105
}
106+

‎src/Addressing.php‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,18 @@ public function rawSearchOptions()
155155
];
156156

157157
$tab[] = [
158-
'id' => '1000',
158+
'id' => '100',
159159
'table' => $this->getTable(),
160160
'field' => 'begin_ip',
161161
'name' => __('First IP', 'addressing'),
162-
// 'nosearch' => true,
163162
'massiveaction' => false,
164163
];
165164

166165
$tab[] = [
167-
'id' => '1001',
166+
'id' => '101',
168167
'table' => $this->getTable(),
169168
'field' => 'end_ip',
170169
'name' => __('Last IP', 'addressing'),
171-
// 'nosearch' => true,
172170
'massiveaction' => false,
173171
];
174172

‎src/AddressingInjection.php‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/*
3+
* @version $Id$
4+
-------------------------------------------------------------------------
5+
addressing plugin for GLPI
6+
Copyright (C) 2009-2022 by the addressing Development Team.
7+
8+
https://github.com/pluginsGLPI/addressing
9+
-------------------------------------------------------------------------
10+
11+
LICENSE
12+
13+
This file is part of addressing.
14+
15+
addressing is free software; you can redistribute it and/or modify
16+
it under the terms of the GNU General Public License as published by
17+
the Free Software Foundation; either version 2 of the License, or
18+
(at your option) any later version.
19+
20+
addressing is distributed in the hope that it will be useful,
21+
but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
GNU General Public License for more details.
24+
25+
You should have received a copy of the GNU General Public License
26+
along with addressing. If not, see <http://www.gnu.org/licenses/>.
27+
--------------------------------------------------------------------------
28+
*/
29+
30+
namespace GlpiPlugin\Addressing;
31+
32+
use AllowDynamicProperties;
33+
use PluginDatainjectionCommonInjectionLib;
34+
use PluginDatainjectionInjectionInterface;
35+
use Search;
36+
37+
/**
38+
* Class AddressingInjection
39+
*/
40+
#[AllowDynamicProperties]
41+
class AddressingInjection extends Addressing implements PluginDatainjectionInjectionInterface
42+
{
43+
public static function getTable($classname = null)
44+
{
45+
return Addressing::getTable();
46+
}
47+
48+
/**
49+
* @return bool
50+
*/
51+
public function isPrimaryType()
52+
{
53+
return true;
54+
}
55+
56+
/**
57+
* @return array
58+
*/
59+
public function connectedTo()
60+
{
61+
return [];
62+
}
63+
64+
/**
65+
* @param string $primary_type
66+
* @return array
67+
*/
68+
public function getOptions($primary_type = '')
69+
{
70+
71+
$tab = Search::getOptions(get_parent_class($this));
72+
73+
//Specific to location
74+
75+
$tab[5]['linkfield'] = 'locations_id';
76+
//$blacklist = PluginDatainjectionCommonInjectionLib::getBlacklistedOptions();
77+
//Remove some options because some fields cannot be imported
78+
$notimportable = [8, 14, 30, 80];
79+
$options['ignore_fields'] = $notimportable;
80+
$options['displaytype'] = ["dropdown" => [2, 5, 6, 7],
81+
"multiline_text" => [3],
82+
"bool" => [4]];
83+
84+
$tab = PluginDatainjectionCommonInjectionLib::addToSearchOptions($tab, $options, $this);
85+
86+
return $tab;
87+
}
88+
89+
/**
90+
* Standard method to add an object into glpi
91+
* WILL BE INTEGRATED INTO THE CORE IN 0.80
92+
* @param array| $values
93+
* @param array| $options
94+
* @return array array of IDs of newly created objects : for example array(Computer=>1, Networkport=>10)
95+
* @internal param fields $values to add into glpi
96+
* @internal param options $options used during creation
97+
*/
98+
public function addOrUpdateObject($values = [], $options = [])
99+
{
100+
$lib = new PluginDatainjectionCommonInjectionLib($this, $values, $options);
101+
$lib->processAddOrUpdate();
102+
return $lib->getInjectionResults();
103+
}
104+
105+
}

0 commit comments

Comments
 (0)