forked from OscarLoayzaB/relation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.php
476 lines (376 loc) · 14.8 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
<?php
/*
------------------------------------------------------------------------
Relation
Copyright (C) 2009-2014 by the Relation plugin Development Team.
https://forge.indepnet.net/projects/barscode
------------------------------------------------------------------------
LICENSE
This file is part of relation plugin project.
Plugin Relation is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Plugin Relation 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Plugin Relation. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------
@package Plugin Relation
@author David Durieux
@co-author
@copyright Copyright (c) 2009-2014 Relation plugin Development team
@license AGPL License 3.0 or (at your option) any later version
http://www.gnu.org/licenses/agpl-3.0-standalone.html
@link https://forge.indepnet.net/projects/barscode
@since 2009
------------------------------------------------------------------------
*/
// Define actions :
$plugin_relations_columnas = array();
// Install process for plugin : need to return true if succeeded
function plugin_relation_install() {
global $DB;
$migration = new Migration(PLUGIN_RELATIONS_VERSION);
if (!file_exists(GLPI_PLUGIN_DOC_DIR."/relation")) {
mkdir(GLPI_PLUGIN_DOC_DIR."/relation");
}
if (!TableExists("glpi_plugin_relation_relation")) { // not installed
$DB->runFile(GLPI_ROOT . '/plugins/relation/sql/script_carm.sql');
}
//CRI. Migrar Datos de 0836
if (TableExists("glpi_plugin_relations_relations")) {
plugin_relation_upgradetocarm();
}
include_once GLPI_ROOT.'/plugins/relation/inc/profile.class.php';
include_once GLPI_ROOT.'/plugins/relation/inc/config.class.php';
PluginRelationProfile::initProfile();
return true;
}
// Uninstall process for plugin : need to return true if succeeded
function plugin_relation_uninstall() {
global $DB;
$tables = array('glpi_plugin_relation_relations',
'glpi_plugin_relation_typerelations',
'glpi_plugin_relation_relationclases',
'glpi_plugin_relation_clases');
foreach ( $tables as $table ){
$DB->query("DROP TABLE `$table`");
}
$query = "DELETE FROM `glpi_displaypreferences`
WHERE `num` = 2250 OR `num` = 2251";
$DB->query($query);
include_once GLPI_ROOT.'/plugins/relation/inc/profile.class.php';
PluginRelationProfile::removeRights();
return true;
}
function plugin_relation_postinit() {
global $CFG_GLPI, $PLUGIN_HOOKS;
$PLUGIN_HOOKS['plugin_uninstall_after']['relation'] = array();
$PLUGIN_HOOKS['item_purge']['relation'] = array();
foreach (PluginRelationRelation::getTypes(true) as $type) {
$PLUGIN_HOOKS['item_purge']['relation'][$type]
= array('PluginRelationRelation','cleanForItem');
CommonGLPI::registerStandardTab($type, 'PluginRelationRelation');
}
//$PLUGIN_HOOKS['add_css']['relation']=array('dd.css');
//$PLUGIN_HOOKS['add_javascript']['relation']=array('jquery-1.9.0.min.js','jquery.dd.min.js');
}
////// SEARCH FUNCTIONS ///////
// Define search options
function plugin_relation_getAddSearchOptions($itemtype){
$sopt = array();
if (Session::haveRight("plugin_relation", CREATE)) {
$sopt['relation'] = 'Relaciones';
if ( in_array($itemtype, PluginRelationRelation::getTypes(true)) ){
$sopt[2250]['table'] = 'glpi_plugin_relation_relations';
$sopt[2250]['field'] = '';
$sopt[2250]['linkfield'] = 'parent';
$sopt[2250]['name'] = PluginRelationRelation::getTypeName(2)." - ".
__('Parent');
$sopt[2250]['datatype'] = 'itemlink';
//$sopt[2250]['datatype'] = 'dropdown';
$sopt[2250]['itemlink_type'] = $itemtype;
$sopt[2250]['forcegroupby'] = true;
$sopt[2250]['massiveaction'] = false;
$sopt[2251]['table'] = 'glpi_plugin_relation_relations';
$sopt[2251]['field'] = '';
$sopt[2251]['linkfield'] = 'children';
$sopt[2251]['name'] = PluginRelationRelation::getTypeName(2)." - ".
__('Children');
$sopt[2251]['forcegroupby'] = true;
//$sopt[2251]['datatype'] = 'dropdown';
$sopt[2251]['datatype'] = 'itemlink';
$sopt[2251]['itemlink_type'] = $itemtype;
$sopt[2251]['massiveaction'] = false;
//$sopt[2251]['link2'] = 'children2';
//$sopt[2251]["splititems"] = true;
}
}
return $sopt;
}
/*
Calidad CARM funcion: plugin_relation_addSelect
Modificado por Oscar, 25/10/2013
Para poder filtrar por nombre de los objetos relacionados, relacion directa e indirecta.
*/
function plugin_relation_addSelect($type,$ID,$num){
global $plugin_relations_columnas;
$out = '';
switch($ID) {
case 2250 :
$out = "GROUP_CONCAT(DISTINCT CONCAT(glpi_plugin_relation3.parent_type, '$$' , glpi_plugin_relation3.parent_id, '$$',glpi_plugin_relation3.relation_type)
SEPARATOR '$$$$') AS ITEM_$num, ";
//$plugin_relations_columnas[2250]="CONCAT(glpi_plugin_relations3.parent_type, '$$' , glpi_plugin_relations3.parent_id, '$$',glpi_plugin_relations3.relation_type)";
if (plugin_relation_parent_filterAnd($type)=="")
{
$plugin_relations_columnas[2250]="glpi_plugin_relation3.relation_type";
}
else
{
$plugin_relations_columnas[2250]=plugin_relation_parent_filterAnd($type);
}
break;
case 2251 :
$out = "GROUP_CONCAT(DISTINCT CONCAT(glpi_plugin_relation2.itemtype, '$$' , glpi_plugin_relation2.items_id, '$$',glpi_plugin_relation2.relation_type)
SEPARATOR '$$$$') AS ITEM_$num, ";
//$plugin_relations_columnas[2251]="CONCAT(glpi_plugin_relations2.itemtype, '$$' , glpi_plugin_relations2.items_id, '$$',glpi_plugin_relations2.relation_type)";
if (plugin_relation_children_filterAnd($type)=="")
{
$plugin_relations_columnas[2251]="glpi_plugin_relation2.relation_type";
}
else
{
$plugin_relations_columnas[2251]=plugin_relation_children_filterAnd($type);
}
break;
}
return $out;
}
function plugin_relation_parent_filterAnd($itemtype)
{
global $DB;
$sql = "select parent_type from glpi_plugin_relation_relations where itemtype = '".$itemtype."' group by parent_type";
$fstringsql = "";
$stringsql = "";
$result = $DB->query($sql);
if ($DB->numrows($result)){
$stringsqlcab = "concat_ws(' ',";
while ($data=$DB->fetch_array($result)){
if (getTableForItemType($data[0])=="glpi_users")
{
$stringsql .= getTableForItemType($data[0])."3.name,".getTableForItemType($data[0])."3.firstname,".getTableForItemType($data[0])."3.realname,";
}
else
{
$stringsql .= getTableForItemType($data[0])."3.name,";
}
}
$stringsqlpie= ")";
$stringsql=substr($stringsql,0,strlen($stringsql)-1);
$fstringsql=$stringsqlcab.$stringsql.$stringsqlpie;
}
return $fstringsql;
}
function plugin_relation_children_filterAnd($parent_type)
{
global $DB;
$sql = "select itemtype from glpi_plugin_relation_relations where parent_type = '".$parent_type."' group by itemtype";
$fstringsql = "";
$stringsql = "";
$result = $DB->query($sql);
if ($DB->numrows($result)){
$stringsqlcab = "concat_ws(' ',";
while ($data=$DB->fetch_array($result)){
if (getTableForItemType($data[0])=="glpi_users")
{
$stringsql .= getTableForItemType($data[0])."2.name,".getTableForItemType($data[0])."2.firstname,".getTableForItemType($data[0])."2.realname,";
}
else
{
$stringsql .= getTableForItemType($data[0])."2.name,";
}
}
$stringsqlpie= ")";
$stringsql=substr($stringsql,0,strlen($stringsql)-1);
$fstringsql=$stringsqlcab.$stringsql.$stringsqlpie;
}
return $fstringsql;
}
/*
Calidad CARM funcion: plugin_relations_addLeftJoin
Modificado por Oscar, 25/10/2013
Para poder filtrar por nombre de los objetos relacionados, relacion directa e indirecta.
*/
// Define how to join the tables when doing a search
function plugin_relation_addLeftJoin($type,$ref_table,$new_table,$linkfield,&$already_link_tables)
{
if ( $new_table == 'glpi_plugin_relation_relations')
{
if( $linkfield == 'parent' ){
$out = "LEFT JOIN glpi_plugin_relation_relations glpi_plugin_relation3
ON ( $ref_table.id = glpi_plugin_relation3.items_id and glpi_plugin_relation3.itemtype = '$type') \n ".plugin_relation_parent_obtenerLeftjoin($type);
}
else {
$out= "LEFT JOIN glpi_plugin_relation_relations glpi_plugin_relation2
ON ( $ref_table.id = glpi_plugin_relation2.parent_id and glpi_plugin_relation2.parent_type = '$type') \n ".plugin_relation_children_obtenerLeftjoin($type);
}
return $out;
}
}
function plugin_relation_parent_obtenerLeftjoin($itemtype)
{
global $DB;
$sql = "select parent_type from glpi_plugin_relation_relations where itemtype = '".$itemtype."' group by parent_type";
$stringsql = "";
$result = $DB->query($sql);
if ($DB->numrows($result)){
while ($data=$DB->fetch_array($result)){
$stringsql .= " LEFT JOIN ".getTableForItemType($data[0])." AS ".getTableForItemType($data[0])."3 ON ( ".getTableForItemType($data[0])."3.id = glpi_plugin_relation3.parent_id and glpi_plugin_relation3.parent_type = '".$data[0]."') \n";
}
}
return $stringsql;
}
/*
Calidad CARM
Añadido por Oscar, 25/10/2013
Para poder filtrar por nombre de los objetos relacionados, relacion directa e indirecta.
*/
function plugin_relation_children_obtenerLeftjoin($parent_type)
{
global $DB;
$sql = "select itemtype from glpi_plugin_relation_relations where parent_type = '".$parent_type."' group by itemtype";
$stringsql = "";
$result = $DB->query($sql);
if ($DB->numrows($result)){
while ($data=$DB->fetch_array($result)){
$stringsql .= " LEFT JOIN ".getTableForItemType($data[0])." AS ".getTableForItemType($data[0])."2 ON ( ".getTableForItemType($data[0])."2.id = glpi_plugin_relation2.items_id and glpi_plugin_relation2.itemtype = '".$data[0]."') \n";
}
}
return $stringsql;
}
function plugin_relation_addWhere($link,$nott,$type,$ID,$val) {
global $plugin_relations_columnas;
$out='';
switch($ID) {
case 2250: $out="$link ($plugin_relations_columnas[2250] LIKE '%$val%')"; break;
case 2251: $out="$link ($plugin_relations_columnas[2251] LIKE '%$val%')"; break;
}
return $out;
}
// Return the string that will be displayed in device views
function plugin_relation_giveItem($type,$ID,$data,$num) {
global $DB;
$searchopt = &Search::getOptions($type);
if (isset($searchopt[$ID]["itemlink_type"]))
{
$form = Toolbox::getItemTypeFormURL($searchopt[$ID]["itemlink_type"]);
}
$separator = "<br/>";
$dataraw = $data['raw'];
$split = explode("$$$$",$dataraw['ITEM_'.$num]);
$count = 0;
$string = "";
//print_r($split);
for ( $i = 0; $i < count($split); $i++ ){
if ( strlen(trim($split[$i])) > 0 ){
$item = explode("$$",$split[$i]);
if ( isset($item[0]) && isset($item[1]) && $item[1] > 0 ){
if ( $count ){
$string .= $separator;
}
$count++;
if ( ! class_exists($item[0]) ) {
continue;
}
$objeto = new $item[0]();
if ($objeto->getFromDB($item[1])) {
$nombre_objeto = $objeto->getNameID();
//if ( $objeto->canView() ) {
$form = Toolbox::getItemTypeFormURL($item[0]);
$string .= "<a id='PluginRelationRelation' href='";
$string .= $form . "?id=" . $item[1] . "'>";
switch($ID) {
case 2250:
$string .= $nombre_objeto. "</a>". " Class: ". $item[0] . " Relation type: ". PluginRelationRelation::getNombreTiporelacion($item[2], 1) ;
break;
case 2251:
$string .= $nombre_objeto. "</a>". " Class: ". $item[0] . " Relation type: ". PluginRelationRelation::getNombreTiporelacion($item[2], 0) ;
break;
}
//}
}
unset($objeto);
}
}
}
return $string;
}
function plugin_relation_upgradetocarm() {
global $DB;
//INICIALIZAR LAS TABLAS DEL PLUGIN CON LOS DATOS
if (TableExists("glpi_plugin_relations_clases")) {
$query = "TRUNCATE TABLE `glpi_plugin_relation_clases`";
$DB->query($query) or die($DB->error());
$query = "INSERT INTO `glpi_plugin_relation_clases` SELECT * FROM `glpi_plugin_relations_clases`";
$DB->query($query) or die($DB->error());
}
if (TableExists("glpi_plugin_relations_relations")) {
$query = "TRUNCATE TABLE `glpi_plugin_relation_relations`";
$DB->query($query) or die($DB->error());
$query = "INSERT INTO `glpi_plugin_relation_relations` SELECT * FROM `glpi_plugin_relations_relations`";
$DB->query($query) or die($DB->error());
}
if (TableExists("glpi_plugin_relations_tiposrelaciones")) {
$query = "TRUNCATE TABLE `glpi_plugin_relation_typerelations`";
$DB->query($query) or die($DB->error());
$query = "INSERT INTO `glpi_plugin_relation_typerelations` SELECT * FROM `glpi_plugin_relations_tiposrelaciones`";
$DB->query($query) or die($DB->error());
}
if (TableExists("glpi_plugin_relations_relacionesclases")) {
$query = "TRUNCATE TABLE `glpi_plugin_relation_relationclases`";
$DB->query($query) or die($DB->error());
$query = "INSERT INTO `glpi_plugin_relation_relationclases` SELECT * FROM `glpi_plugin_relations_relacionesclases`";
$DB->query($query) or die($DB->error());
}
if (TableExists("glpi_plugin_relations_profiles")) {
$query = "SELECT id, relations FROM `glpi_plugin_relations_profiles`";
$result = $DB->query($query) or die($DB->error());
if ($DB->numrows($result)){
while ($data=$DB->fetch_array($result)){
$query = "INSERT INTO `glpi_profilerights` (`profiles_id`, `name`, `rights`)
VALUES (".$data[0].",'plugin_relation',".plugin_relation_translatearight($data[1]).")";
$DB->query($query) or die($DB->error());
}
}
}
$tables = array(
// Propios del plugins
'glpi_plugin_relations_clases',
'glpi_plugin_relations_relations',
'glpi_plugin_relations_tiposrelaciones',
'glpi_plugin_relations_relacionesclases',
'glpi_plugin_relations_profiles'
);
foreach ($tables as $table) {
$DB->query("DROP TABLE `$table`");
}
}
function plugin_relation_translatearight($old_right) {
switch ($old_right) {
case '':
return 0;
case 'r' :
return READ;
case 'w':
return CREATE;
case '0':
case '1':
return $old_right;
default :
return 0;
}
}
?>