diff --git a/setup.php b/setup.php index 63d027c..b7ded57 100644 --- a/setup.php +++ b/setup.php @@ -34,6 +34,7 @@ use GlpiPlugin\Example\Dropdown; use GlpiPlugin\Example\DeviceCamera; use GlpiPlugin\Example\Example; +use GlpiPlugin\Example\Filters\ComputerModelFilter; use GlpiPlugin\Example\ItemForm; use GlpiPlugin\Example\RuleTestCollection; use GlpiPlugin\Example\Showtabitem; @@ -232,6 +233,11 @@ function plugin_init_example() { // add new cards to dashboard grid $PLUGIN_HOOKS['dashboard_types']['example'] = [Example::class, 'dashboardTypes']; $PLUGIN_HOOKS['dashboard_cards']['example'] = [Example::class, 'dashboardCards']; + + // Dashboard filter + $PLUGIN_HOOKS[Hooks::DASHBOARD_FILTERS]['example'] = [ + ComputerModelFilter::class + ]; } diff --git a/src/Filters/ComputerModelFilter.php b/src/Filters/ComputerModelFilter.php new file mode 100644 index 0000000..e1ea1c1 --- /dev/null +++ b/src/Filters/ComputerModelFilter.php @@ -0,0 +1,99 @@ +. + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2006-2022 by Example plugin team. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/example + * ------------------------------------------------------------------------- + */ + +namespace GlpiPlugin\Example\Filters; + +use ComputerModel; +use DBmysql; +use Glpi\Dashboard\Filters\AbstractFilter; + +class ComputerModelFilter extends AbstractFilter +{ + public static function getName(): string + { + return __("Computer model"); + } + + public static function getId(): string + { + return "plugin_example_computer_model"; + } + + public static function canBeApplied(string $table): bool + { + global $DB; + + return $DB->fieldExists($table, ComputerModel::getForeignKeyField()); + } + + public static function getHtml($value): string + { + return self::displayList( + self::getName(), + is_string($value) ? $value : "", + self::getId(), + ComputerModel::class + ); + } + + public static function getCriteria(string $table, $value): array + { + if ((int) $value > 0) { + $field = ComputerModel::getForeignKeyField(); + return [ + "WHERE" => [ + "$table.$field" => (int) $value + ] + ]; + } + + return []; + } + + public static function getSearchCriteria(string $table, $value): array + { + if ((int) $value > 0) { + return [ + [ + 'link' => 'AND', + 'searchtype' => 'equals', + 'value' => (int) $value, + 'field' => self::getSearchOptionID( + $table, + ComputerModel::getForeignKeyField(), + ComputerModel::getTable() + ), + ] + ]; + } + + return []; + } +}