Skip to content

Commit 4899ba4

Browse files
Example for the new dashboard filters hooks
1 parent 99ccf29 commit 4899ba4

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

setup.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use GlpiPlugin\Example\Dropdown;
3535
use GlpiPlugin\Example\DeviceCamera;
3636
use GlpiPlugin\Example\Example;
37+
use GlpiPlugin\Example\Filters\ComputerModelFilter;
3738
use GlpiPlugin\Example\ItemForm;
3839
use GlpiPlugin\Example\RuleTestCollection;
3940
use GlpiPlugin\Example\Showtabitem;
@@ -232,6 +233,11 @@ function plugin_init_example() {
232233
// add new cards to dashboard grid
233234
$PLUGIN_HOOKS['dashboard_types']['example'] = [Example::class, 'dashboardTypes'];
234235
$PLUGIN_HOOKS['dashboard_cards']['example'] = [Example::class, 'dashboardCards'];
236+
237+
// Dashboard filter
238+
$PLUGIN_HOOKS[Hooks::DASHBOARD_FILTERS]['example'] = [
239+
ComputerModelFilter::class
240+
];
235241
}
236242

237243

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Example plugin for GLPI
6+
* -------------------------------------------------------------------------
7+
*
8+
* LICENSE
9+
*
10+
* This file is part of Example.
11+
*
12+
* Example is free software; you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License as published by
14+
* the Free Software Foundation; either version 2 of the License, or
15+
* (at your option) any later version.
16+
*
17+
* Example is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU General Public License
23+
* along with Example. If not, see <http://www.gnu.org/licenses/>.
24+
* -------------------------------------------------------------------------
25+
* @copyright Copyright (C) 2006-2022 by Example plugin team.
26+
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
27+
* @link https://github.com/pluginsGLPI/example
28+
* -------------------------------------------------------------------------
29+
*/
30+
31+
namespace GlpiPlugin\Example\Filters;
32+
33+
use ComputerModel;
34+
use DBmysql;
35+
use Glpi\Dashboard\Filters\AbstractFilter;
36+
37+
class ComputerModelFilter extends AbstractFilter
38+
{
39+
public static function getName(): string
40+
{
41+
return __("Computer model");
42+
}
43+
44+
public static function getId(): string
45+
{
46+
return "plugin_example_computer_model";
47+
}
48+
49+
public static function canBeApplied(string $table): bool
50+
{
51+
global $DB;
52+
53+
return $DB->fieldExists($table, ComputerModel::getForeignKeyField());
54+
}
55+
56+
public static function getHtml($value): string
57+
{
58+
return self::displayList(
59+
self::getName(),
60+
is_string($value) ? $value : "",
61+
self::getId(),
62+
ComputerModel::class
63+
);
64+
}
65+
66+
public static function getCriteria(string $table, $value): array
67+
{
68+
if ((int) $value > 0) {
69+
$field = ComputerModel::getForeignKeyField();
70+
return [
71+
"WHERE" => [
72+
"$table.$field" => (int) $value
73+
]
74+
];
75+
}
76+
77+
return [];
78+
}
79+
80+
public static function getSearchCriteria(string $table, $value): array
81+
{
82+
if ((int) $value > 0) {
83+
return [
84+
[
85+
'link' => 'AND',
86+
'searchtype' => 'equals',
87+
'value' => (int) $value,
88+
'field' => self::getSearchOptionID(
89+
$table,
90+
ComputerModel::getForeignKeyField(),
91+
ComputerModel::getTable()
92+
),
93+
]
94+
];
95+
}
96+
97+
return [];
98+
}
99+
}

0 commit comments

Comments
 (0)