This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
hook.php
780 lines (658 loc) · 30.1 KB
/
hook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
<?php
/*
* @version $Id: HEADER 15930 2011-10-30 15:47:55Z tsmr $
-------------------------------------------------------------------------
databases plugin for GLPI
Copyright (C) 2009-2016 by the databases Development Team.
https://github.com/InfotelGLPI/databases
-------------------------------------------------------------------------
LICENSE
This file is part of databases.
databases is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
databases is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with databases. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
/**
* @return bool
*/
function plugin_databases_install() {
global $DB;
include_once(PLUGIN_DATABASES_DIR . "/inc/profile.class.php");
$update = false;
if (!$DB->tableExists("glpi_plugin_sgbd") && !$DB->tableExists("glpi_plugin_databases_databases")) {
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/empty-2.3.2.sql");
} else if ($DB->tableExists("glpi_plugin_sgbd") && !$DB->tableExists("glpi_plugin_sgbd_instances")) {
$update = true;
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-1.1.sql");
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-1.2.0.sql");
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-1.2.1.sql");
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-1.3.0.sql");
} else if ($DB->tableExists("glpi_plugin_sgbd") && !$DB->tableExists("glpi_dropdown_plugin_sgbd_category")) {
$update = true;
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-1.2.0.sql");
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-1.2.1.sql");
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-1.3.0.sql");
} else if ($DB->tableExists("glpi_plugin_sgbd") && !$DB->fieldExists("glpi_plugin_sgbd", "helpdesk_visible")) {
$update = true;
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-1.2.1.sql");
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-1.3.0.sql");
} else if (!$DB->tableExists("glpi_plugin_databases_databases")) {
$update = true;
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-1.3.0.sql");
}
//from 1.3 version
if ($DB->tableExists("glpi_plugin_databases_databases")
&& !$DB->fieldExists("glpi_plugin_databases_databases", "users_id_tech")
&& !$DB->fieldExists("glpi_plugin_databases_databases", "users_id")) {
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-1.5.0.sql");
}
if ($DB->tableExists("glpi_plugin_databases_databases")
&& !$DB->fieldExists("glpi_plugin_databases_databases", "users_id")) {
$DB->runFile(PLUGIN_DATABASES_DIR . "/sql/update-2.2.2.sql");
}
if ($DB->tableExists("glpi_plugin_databases_profiles")) {
$notepad_tables = ['glpi_plugin_databases_databases'];
foreach ($notepad_tables as $t) {
// Migrate data
if ($DB->fieldExists($t, 'notepad')) {
$query = "SELECT id, notepad
FROM `$t`
WHERE notepad IS NOT NULL
AND notepad <>'';";
foreach ($DB->request($query) as $data) {
$iq = "INSERT INTO `glpi_notepads`
(`itemtype`, `items_id`, `content`, `date`, `date_mod`)
VALUES ('PluginDatabasesDatabase', '" . $data['id'] . "',
'" . addslashes($data['notepad']) . "', NOW(), NOW())";
$DB->queryOrDie($iq, "0.85 migrate notepad data");
}
$query = "ALTER TABLE `glpi_plugin_databases_databases` DROP COLUMN `notepad`;";
$DB->query($query);
}
}
}
if ($update) {
$query_ = "SELECT *
FROM `glpi_plugin_databases_profiles` ";
$result_ = $DB->query($query_);
if ($DB->numrows($result_) > 0) {
while ($data = $DB->fetchArray($result_)) {
$query = "UPDATE `glpi_plugin_databases_profiles`
SET `profiles_id` = '" . $data["id"] . "'
WHERE `id` = '" . $data["id"] . "';";
$DB->query($query);
}
}
$query = "ALTER TABLE `glpi_plugin_databases_profiles`
DROP `name` ;";
$DB->query($query);
$query = "SELECT `entities_id`,`is_recursive`,`id` FROM `glpi_plugin_databases_databases` ";
$result = $DB->query($query);
$number = $DB->numrows($result);
if ($number) {
while ($data = $DB->fetchArray($result)) {
$query = "UPDATE `glpi_plugin_databases_instances`
SET `entities_id` = '" . $data["entities_id"] . "'
AND `is_recursive` = '" . $data["is_recursive"] . "'
WHERE `plugin_databases_databases_id` = '" . $data["id"] . "' ";
$DB->query($query) or die($DB->error());
$query = "UPDATE `glpi_plugin_databases_scripts`
SET `entities_id` = '" . $data["entities_id"] . "'
AND `is_recursive` = '" . $data["is_recursive"] . "'
WHERE `plugin_databases_databases_id` = '" . $data["id"] . "' ";
$DB->query($query) or die($DB->error());
}
}
Plugin::migrateItemType(
[2400 => 'PluginDatabasesDatabase'],
["glpi_savedsearches", "glpi_savedsearches_users", "glpi_displaypreferences",
"glpi_documents_items", "glpi_infocoms", "glpi_logs", "glpi_items_tickets"],
["glpi_plugin_databases_databases_items"]);
Plugin::migrateItemType(
[1200 => "PluginAppliancesAppliance", 1300 => "PluginWebapplicationsWebapplication"],
["glpi_plugin_databases_databases_items"]);
}
PluginDatabasesProfile::initProfile();
PluginDatabasesProfile::createFirstAccess($_SESSION['glpiactiveprofile']['id']);
$migration = new Migration("1.7.0");
$migration->dropTable('glpi_plugin_databases_profiles');
return true;
}
/**
* @return bool
*/
function plugin_databases_uninstall() {
global $DB;
include_once(PLUGIN_DATABASES_DIR . "/inc/profile.class.php");
include_once(PLUGIN_DATABASES_DIR . "/inc/menu.class.php");
$tables = ["glpi_plugin_databases_databases",
"glpi_plugin_databases_databasetypes",
"glpi_plugin_databases_databasecategories",
"glpi_plugin_databases_servertypes",
"glpi_plugin_databases_scripttypes",
"glpi_plugin_databases_instances",
"glpi_plugin_databases_scripts",
"glpi_plugin_databases_databases_items"];
foreach ($tables as $table) {
$DB->query("DROP TABLE IF EXISTS `$table`;");
}
//old versions
$tables = ["glpi_plugin_sgbd",
"glpi_dropdown_plugin_sgbd_type",
"glpi_dropdown_plugin_sgbd_server_type",
"glpi_plugin_sgbd_device",
"glpi_plugin_sgbd_profiles",
"glpi_dropdown_plugin_sgbd_script_type",
"glpi_plugin_sgbd_instances",
"glpi_plugin_sgbd_scripts",
"glpi_dropdown_plugin_sgbd_category",
"glpi_plugin_databases_profiles"];
foreach ($tables as $table) {
$DB->query("DROP TABLE IF EXISTS `$table`;");
}
$tables_glpi = ["glpi_displaypreferences",
"glpi_documents_items",
"glpi_savedsearches",
"glpi_logs",
"glpi_items_tickets",
"glpi_notepads",
"glpi_dropdowntranslations",
"glpi_impactitems"];
foreach ($tables_glpi as $table_glpi) {
$DB->query("DELETE FROM `$table_glpi` WHERE `itemtype` LIKE 'PluginDatabases%' ;");
}
$DB->query("DELETE
FROM `glpi_impactrelations`
WHERE `itemtype_source` IN ('PluginDatabasesDatabase')
OR `itemtype_impacted` IN ('PluginDatabasesDatabase')");
if (class_exists('PluginDatainjectionModel')) {
PluginDatainjectionModel::clean(['itemtype' => 'PluginDatabasesDatabase']);
}
//Delete rights associated with the plugin
$profileRight = new ProfileRight();
foreach (PluginDatabasesProfile::getAllRights() as $right) {
$profileRight->deleteByCriteria(['name' => $right['field']]);
}
PluginDatabasesMenu::removeRightsFromSession();
PluginDatabasesProfile::removeRightsFromSession();
return true;
}
function plugin_databases_postinit() {
global $PLUGIN_HOOKS;
$PLUGIN_HOOKS['item_purge']['databases'] = [];
foreach (PluginDatabasesDatabase::getTypes(true) as $type) {
$PLUGIN_HOOKS['item_purge']['databases'][$type]
= ['PluginDatabasesDatabase_Item', 'cleanForItem'];
CommonGLPI::registerStandardTab($type, 'PluginDatabasesDatabase_Item');
}
}
/**
* @param $types
*
* @return mixed
*/
function plugin_databases_AssignToTicket($types) {
if (Session::haveRight("plugin_databases_open_ticket", "1")) {
$types['PluginDatabasesDatabase'] = PluginDatabasesDatabase::getTypeName(2);
//$types['PluginDatabasesDatabase_Item'] = _n('Database item', 'Databases item', 2, 'databases');
}
return $types;
}
/*
function plugin_databases_AssignToTicketDropdown($data) {
global $DB, $CFG_GLPI;
if ($data['itemtype'] == 'PluginDatabasesDatabase') {
$table = getTableForItemType($data["itemtype"]);
$rand = mt_rand();
$field_id = Html::cleanId("dropdown_".$data['myname'].$rand);
$p = array('itemtype' => $data["itemtype"],
'entity_restrict' => $data['entity_restrict'],
'table' => $table,
'myname' => $data["myname"]);
if(isset($data["used"]) && !empty($data["used"])){
if(isset($data["used"][$data["itemtype"]])){
$p["used"] = $data["used"][$data["itemtype"]];
}
}
echo Html::jsAjaxDropdown($data['myname'], $field_id,
$CFG_GLPI['root_doc']."/ajax/getDropdownFindNum.php",
$p);
// Auto update summary of active or just solved tickets
$params = array('items_id' => '__VALUE__',
'itemtype' => $data['itemtype']);
Ajax::updateItemOnSelectEvent($field_id,"item_ticket_selection_information",
$CFG_GLPI["root_doc"]."/ajax/ticketiteminformation.php",
$params);
} else if ($data['itemtype'] == 'PluginDatabasesDatabase_Item') {
$sql = "SELECT `glpi_plugin_databases_databases`.`name`, "
. " `items_id`, `itemtype`, `glpi_plugin_databases_databases_items`.`id` "
. " FROM `glpi_plugin_databases_databases_items`"
. " LEFT JOIN `glpi_plugin_databases_databases`"
. " ON `plugin_databases_databases_id` = `glpi_plugin_databases_databases`.`id`";
$result = $DB->query($sql);
$elements = array();
while ($res = $DB->fetchArray($result)) {
$itemtype = $res['itemtype'];
$item = new $itemtype;
$item->getFromDB($res['items_id']);
$elements[$res['name']][$res['id']] = $item->getName();
}
Dropdown::showFromArray('items_id', $elements, array());
}
}
function plugin_databases_AssignToTicketDisplay($data) {
global $DB;
if ($data['itemtype'] == 'PluginDatabasesDatabase_Item') {
$paDatabase = new PluginDatabasesDatabase();
$item = new PluginDatabasesDatabase_Item();
$itemtype = $data['data']['itemtype'];
$iteminv = new $itemtype;
$iteminv->getFromDB($data['data']['items_id']);
$paDatabase->getFromDB($data['data']['plugin_databases_databases_id']);
echo "<tr class='tab_bg_1'>";
if ($data['canedit']) {
echo "<td width='10'>";
Html::showMassiveActionCheckBox('Item_Ticket', $data['data']["IDD"]);
echo "</td>";
}
$typename = "<i>".PluginDatabasesDatabase::getTypeName()."</i><br/>".
$iteminv->getTypeName();
echo "<td class='center top' rowspan='1'>".$typename."</td>";
echo "<td class='center'>";
echo "<i>".Dropdown::getDropdownName("glpi_entities", $paDatabase->fields['entities_id'])."</i>";
echo "<br/>";
echo Dropdown::getDropdownName("glpi_entities", $iteminv->fields['entities_id']);
echo "</td>";
$linkDatabase = Toolbox::getItemTypeFormURL('PluginDatabasesDatabase');
$namelinkDatabase = "<a href=\"".$linkDatabase."?id=".
$paDatabase->fields['id']."\">".$paDatabase->getName()."</a>";
$link = Toolbox::getItemTypeFormURL($data['data']['itemtype']);
$namelink = "<a href=\"".$link."?id=".$data['data']['items_id']."\">".$iteminv->getName()."</a>";
echo "<td class='center".
(isset($iteminv->fields['is_deleted']) && $iteminv->fields['is_deleted'] ? " tab_bg_2_2'" : "'");
echo "><i>".$namelinkDatabase."</i><br/>".$namelink;
echo "</td>";
echo "<td class='center'><i>".(isset($paDatabase->fields["serial"])? "".$paDatabase->fields["serial"]."" :"-").
"</i><br/>".(isset($iteminv->fields["serial"])? "".$iteminv->fields["serial"]."" :"-").
"</td>";
echo "<td class='center'>".
"<i>".(isset($iteminv->fields["otherserial"])? "".$iteminv->fields["otherserial"]."" :"-")."</i><br/>".
(isset($iteminv->fields["otherserial"])? "".$iteminv->fields["otherserial"]."" :"-")."</td>";
echo "</tr>";
return false;
}
return true;
}
function plugin_databases_AssignToTicketGiveItem($data) {
if ($data['itemtype'] == 'PluginDatabasesDatabase_Item') {
$paDatabase = new PluginDatabasesDatabase();
$paDatabase_item = new PluginDatabasesDatabase_Item();
$paDatabase_item->getFromDB($data['name']);
$itemtype = $paDatabase_item->fields['itemtype'];
$paDatabase->getFromDB($paDatabase_item->fields['plugin_databases_databases_id']);
$item = new $itemtype;
$item->getFromDB($paDatabase_item->fields['items_id']);
return $item->getLink(array('comments' => true))." (".
$paDatabase->getLink(array('comments' => true)).")";
}
}*/
// Define dropdown relations
/**
* @return array
*/
function plugin_databases_getDatabaseRelations() {
$plugin = new Plugin();
if ($plugin->isActivated("databases")) {
return ["glpi_entities" => [
"glpi_plugin_databases_databases" => "entities_id",
"glpi_plugin_databases_databasetypes" => "entities_id",
"glpi_plugin_databases_databasecategories" => "entities_id",
"glpi_plugin_databases_instances" => "entities_id",
"glpi_plugin_databases_scripts" => "entities_id"],
"glpi_plugin_databases_databasecategories" => [
"glpi_plugin_databases_databases" => "plugin_databases_databasecategories_id"],
"glpi_plugin_databases_databasetypes" => [
"glpi_plugin_databases_databases" => "plugin_databases_databasetypes_id"],
"glpi_users" => [
"glpi_plugin_databases_databases" => "users_id"],
"glpi_groups" => [
"glpi_plugin_databases_databases" => "groups_id"],
"glpi_plugin_databases_servertypes" => [
"glpi_plugin_databases_databases" => "plugin_databases_servertypes_id"],
"glpi_suppliers" => [
"glpi_plugin_databases_databases" => "suppliers_id"],
"glpi_manufacturers" => [
"glpi_plugin_databases_databases" => "manufacturers_id"],
"glpi_locations" => [
"glpi_plugin_databases_databases" => "locations_id"],
"glpi_plugin_databases_databases" => [
"glpi_plugin_databases_instances" => "plugin_databases_databases_id",
"glpi_plugin_databases_scripts" => "plugin_databases_databases_id",
"glpi_plugin_databases_databases_items" => "plugin_databases_databases_id"],
"glpi_plugin_databases_scripttypes" => [
"glpi_plugin_databases_scripts" => "plugin_databases_scripttypes_id"],
];
} else {
return [];
}
}
// Define Dropdown tables to be manage in GLPI :
/**
* @return array
*/
function plugin_databases_getDropdown() {
$plugin = new Plugin();
if ($plugin->isActivated("databases")) {
return ["PluginDatabasesDatabaseType" => PluginDatabasesDatabaseType::getTypeName(2),
"PluginDatabasesDatabaseCategory" => PluginDatabasesDatabaseCategory::getTypeName(2),
"PluginDatabasesServerType" => PluginDatabasesServerType::getTypeName(2),
"PluginDatabasesScriptType" => PluginDatabasesScriptType::getTypeName(2)];
} else {
return [];
}
}
////// SEARCH FUNCTIONS ///////() {
/**
* @param $itemtype
*
* @return array
*/
function plugin_databases_getAddSearchOptions($itemtype) {
$sopt = [];
if (in_array($itemtype, PluginDatabasesDatabase::getTypes(true))) {
if (Session::haveRight("plugin_databases", READ)) {
$sopt[2410]['table'] = 'glpi_plugin_databases_databases';
$sopt[2410]['field'] = 'name';
$sopt[2410]['name'] = PluginDatabasesDatabase::getTypeName(2) . " - " . __('Name');
$sopt[2410]['forcegroupby'] = true;
$sopt[2410]['datatype'] = 'itemlink';
$sopt[2410]['massiveaction'] = false;
$sopt[2410]['itemlink_type'] = 'PluginDatabasesDatabase';
$sopt[2410]['joinparams'] = ['beforejoin'
=> ['table' => 'glpi_plugin_databases_databases_items',
'joinparams' => ['jointype' => 'itemtype_item']]];
$sopt[2411]['table'] = 'glpi_plugin_databases_databasecategories';
$sopt[2411]['field'] = 'name';
$sopt[2411]['name'] = PluginDatabasesDatabase::getTypeName(2) . " - " . PluginDatabasesDatabaseCategory::getTypeName(1);
$sopt[2411]['forcegroupby'] = true;
$sopt[2411]['joinparams'] = ['beforejoin' => [
['table' => 'glpi_plugin_databases_databases',
'joinparams' => $sopt[2410]['joinparams']]]];
$sopt[2411]['datatype'] = 'dropdown';
$sopt[2411]['massiveaction'] = false;
$sopt[2412]['table'] = 'glpi_plugin_databases_servertypes';
$sopt[2412]['field'] = 'name';
$sopt[2412]['name'] = PluginDatabasesDatabase::getTypeName(2) . " - " . PluginDatabasesServerType::getTypeName(1);
$sopt[2412]['forcegroupby'] = true;
$sopt[2412]['joinparams'] = ['beforejoin' => [
['table' => 'glpi_plugin_databases_databases',
'joinparams' => $sopt[2410]['joinparams']]]];
$sopt[2412]['datatype'] = 'dropdown';
$sopt[2412]['massiveaction'] = false;
$sopt[2413]['table'] = 'glpi_plugin_databases_databasetypes';
$sopt[2413]['field'] = 'name';
$sopt[2413]['name'] = PluginDatabasesDatabase::getTypeName(2) . " - " . PluginDatabasesDatabaseType::getTypeName(1);
$sopt[2413]['forcegroupby'] = true;
$sopt[2413]['joinparams'] = ['beforejoin' => [
['table' => 'glpi_plugin_databases_databases',
'joinparams' => $sopt[2410]['joinparams']]]];
$sopt[2413]['datatype'] = 'dropdown';
$sopt[2413]['massiveaction'] = false;
}
}
/*if ($itemtype == 'Ticket') {
if (Session::haveRight("plugin_databases", READ)) {
$sopt[2414]['table'] = 'glpi_plugin_databases_databases';
$sopt[2414]['field'] = 'name';
$sopt[2414]['linkfield'] = 'items_id';
$sopt[2414]['datatype'] = 'itemlink';
$sopt[2414]['massiveaction'] = false;
$sopt[2414]['name'] = __('Database', 'databases')." - ".
__('Name');
}
}*/
return $sopt;
}
/**
* @param $type
* @param $ID
* @param $data
* @param $num
*
* @return string
*/
function plugin_databases_giveItem($type, $ID, $data, $num) {
global $DB;
$searchopt =& Search::getOptions($type);
$table = $searchopt[$ID]["table"];
$field = $searchopt[$ID]["field"];
$dbu = new DbUtils();
switch ($table . '.' . $field) {
case "glpi_plugin_databases_databases_items.items_id" :
$query_device = "SELECT DISTINCT `itemtype`
FROM `glpi_plugin_databases_databases_items`
WHERE `plugin_databases_databases_id` = '" . $data['id'] . "'
ORDER BY `itemtype`";
$result_device = $DB->query($query_device);
$number_device = $DB->numrows($result_device);
$out = '';
$databases = $data['id'];
if ($number_device > 0) {
for ($i = 0; $i < $number_device; $i++) {
$column = "name";
$itemtype = $DB->result($result_device, $i, "itemtype");
if (!class_exists($itemtype)) {
continue;
}
$item = new $itemtype();
if ($item->canView()) {
$table_item = $dbu->getTableForItemType($itemtype);
$query = "SELECT `" . $table_item . "`.*, `glpi_plugin_databases_databases_items`.`id` AS items_id, `glpi_entities`.`id` AS entity "
. " FROM `glpi_plugin_databases_databases_items`, `" . $table_item
. "` LEFT JOIN `glpi_entities` ON (`glpi_entities`.`id` = `" . $table_item . "`.`entities_id`) "
. " WHERE `" . $table_item . "`.`id` = `glpi_plugin_databases_databases_items`.`items_id`
AND `glpi_plugin_databases_databases_items`.`itemtype` = '$itemtype'
AND `glpi_plugin_databases_databases_items`.`plugin_databases_databases_id` = '" . $databases . "' "
. $dbu->getEntitiesRestrictRequest(" AND ", $table_item, '', '', $item->maybeRecursive());
if ($item->maybeTemplate()) {
$query .= " AND `" . $table_item . "`.`is_template` = '0'";
}
$query .= " ORDER BY `glpi_entities`.`completename`, `" . $table_item . "`.`$column`";
if ($result_linked = $DB->query($query)) {
if ($DB->numrows($result_linked)) {
$item = new $itemtype();
while ($data = $DB->fetchAssoc($result_linked)) {
if ($item->getFromDB($data['id'])) {
$out .= $item::getTypeName(1) . " - " . $item->getLink() . "<br>";
}
}
} else {
$out .= ' ';
}
}
} else {
$out .= ' ';
}
}
}
return $out;
break;
case 'glpi_plugin_databases_databases.name':
if ($type == 'Ticket') {
if ($data['raw']["ITEM_$num"] != '') {
$databases_id = explode('$$$$', $data['raw']["ITEM_$num"]);
} else {
$databases_id = explode('$$$$', $data['raw']["ITEM_" . $num . "_2"]);
}
$ret = [];
$paDatabase = new PluginDatabasesDatabase();
foreach ($databases_id as $ap_id) {
$paDatabase->getFromDB($ap_id);
$ret[] = $paDatabase->getLink();
}
return implode('<br>', $ret);
}
break;
}
return "";
}
////// SPECIFIC MODIF MASSIVE FUNCTIONS ///////
/**
* @param $type
*
* @return array
*/
function plugin_databases_MassiveActions($type) {
$plugin = new Plugin();
if ($plugin->isActivated('databases')) {
if (in_array($type, PluginDatabasesDatabase::getTypes(true))) {
return ['PluginDatabasesDatabase' . MassiveAction::CLASS_ACTION_SEPARATOR . 'plugin_databases__add_item' =>
__('Associate to the database', 'databases')];
}
}
return [];
}
/*
function plugin_databases_MassiveActionsDisplay($options=array()) {
$database=new PluginDatabasesDatabase;
if (in_array($options['itemtype'], PluginDatabasesDatabase::getTypes(true))) {
$database->dropdownDatabases("plugin_databases_databases_id");
echo "<input type=\"submit\" name=\"massiveaction\" class=\"submit\" value=\""._sx('button', 'Post')."\" >";
}
return "";
}
function plugin_databases_MassiveActionsProcess($data) {
$res = array('ok' => 0,
'ko' => 0,
'noright' => 0);
$database_item = new PluginDatabasesDatabase_Item();
switch ($data['action']) {
case "plugin_databases_add_item":
foreach ($data["item"] as $key => $val) {
if ($val == 1) {
$input = array('plugin_databases_databases_id' => $data['plugin_databases_databases_id'],
'items_id' => $key,
'itemtype' => $data['itemtype']);
if ($database_item->can(-1,'w',$input)) {
if ($database_item->can(-1,'w',$input)) {
$database_item->add($input);
$res['ok']++;
} else {
$res['ko']++;
}
} else {
$res['noright']++;
}
}
}
break;
}
return $res;
}
*/
function plugin_datainjection_populate_databases() {
global $INJECTABLE_TYPES;
$INJECTABLE_TYPES['PluginDatabasesDatabaseInjection'] = 'databases';
}
/*
function plugin_databases_addSelect($type,$id,$num) {
$searchopt = &Search::getOptions($type);
$table = $searchopt[$id]["table"];
$field = $searchopt[$id]["field"];
//echo "add select : ".$table.".".$field."<br/>";
switch ($type) {
case 'Ticket':
if ($table.".".$field == "glpi_plugin_databases_databases.name") {
return " GROUP_CONCAT(DISTINCT `glpi_plugin_databases_databases`.`id` SEPARATOR '$$$$') AS ITEM_$num, "
. " GROUP_CONCAT(DISTINCT `glpi_plugin_databases_databases_bis`.`id` SEPARATOR '$$$$') AS ITEM_".$num."_2,";
}
break;
}
}
function plugin_databases_addLeftJoin($itemtype,$ref_table,$new_table,$linkfield,&$already_link_tables) {
switch ($itemtype) {
case 'Ticket':
return " LEFT JOIN `glpi_plugin_databases_databases` AS glpi_plugin_databases_databases
ON (`glpi_items_tickets`.`items_id` = `glpi_plugin_databases_databases`.`id`
AND `glpi_items_tickets`.`itemtype`='PluginDatabasesDatabase')
LEFT JOIN `glpi_plugin_databases_databases_items`
ON (`glpi_items_tickets`.`items_id` = `glpi_plugin_databases_databases_items`.`id`
AND `glpi_items_tickets`.`itemtype`='PluginDatabasesDatabase_Item')
LEFT JOIN `glpi_plugin_databases_databases` AS glpi_plugin_databases_databases_bis
ON (`glpi_plugin_databases_databases_items`.`plugin_databases_databases_id` = `glpi_plugin_databases_databases_bis`.`id`)";
break;
}
return "";
}
function plugin_databases_addWhere($link,$nott,$type,$id,$val,$searchtype) {
$searchopt = &Search::getOptions($type);
$table = $searchopt[$id]["table"];
$field = $searchopt[$id]["field"];
switch ($type) {
case 'Ticket':
if ($table.".".$field == "glpi_plugin_databases_databases.name") {
$out = '';
switch ($searchtype) {
case "contains" :
$SEARCH = Search::makeTextSearch($val, $nott);
break;
case "equals" :
if ($nott) {
$SEARCH = " <> '$val'";
} else {
$SEARCH = " = '$val'";
}
break;
case "notequals" :
if ($nott) {
$SEARCH = " = '$val'";
} else {
$SEARCH = " <> '$val'";
}
break;
}
if (in_array($searchtype, array('equals', 'notequals'))) {
if ($table != getTableForItemType($type) || $type == 'States') {
$out = " $link (`glpi_plugin_databases_databases`.`id`".$SEARCH;
} else {
$out = " $link (`glpi_plugin_databases_databases`.`$field`".$SEARCH;
}
if ($searchtype=='notequals') {
$nott = !$nott;
}
// Add NULL if $val = 0 and not negative search
// Or negative search on real value
if ((!$nott && $val==0) || ($nott && $val != 0)) {
$out .= " OR `glpi_plugin_databases_databases`.`id` IS NULL";
}
// $out .= ')';
$out1 = $out;
$out = str_replace(" ".$link." (", " ".$link." ", $out);
} else {
$out = Search::makeTextCriteria("`glpi_plugin_databases_databases`.".$field,$val,$nott,$link);
$out1 = $out;
$out = preg_replace("/^ $link/", $link.' (', $out);
}
$out2 = $out." OR ";
$out2 .= str_replace("`glpi_plugin_databases_databases`",
"`glpi_plugin_databases_databases_bis`", $out1)." ";
$out2 = str_replace("OR AND", "OR", $out2);
$out2 = str_replace("OR OR", "OR", $out2);
$out2 = str_replace("AND OR", "OR", $out2);
$out2 = str_replace("OR AND", "OR", $out2);
$out2 = str_replace("OR OR", "OR", $out2);
$out2 = str_replace("AND OR", "OR", $out2);
return $out2.")";
}
break;
}
}
*/