. * --------------------------------------------------------------------- */ namespace Glpi\Dashboard; use CommonGLPI; use ITILCategory; use RequestType; use Location; use Manufacturer; use Session; use Html; use Plugin; if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * Filter class **/ class Filter extends CommonGLPI { /** * Return all available filters * Plugins can hooks on this functions to add their own filters * * @return array of filters */ static function getAll(): array { $filters = [ 'dates' => __("Creation date"), 'dates_mod' => __("Last update"), 'itilcategory' => ITILCategory::getTypeName(Session::getPluralNumber()), 'requesttype' => RequestType::getTypeName(Session::getPluralNumber()), 'location' => Location::getTypeName(Session::getPluralNumber()), 'manufacturer' => Manufacturer::getTypeName(Session::getPluralNumber()), ]; $more_filters = Plugin::doHookFunction("dashboard_filters"); if (is_array($more_filters)) { $filters = array_merge($filters, $more_filters); } return $filters; } /** * Get HTML for a dates range filter * * @param string|array $values init the input with these values, will be a string if empty values * @param string $fieldname how is named the current date field * (used to specify creation date or last update) * * @return string */ static function dates($values = "", string $fieldname = 'dates'): string { // string mean empty value if (is_string($values)) { $values = []; } $rand = mt_rand(); $label = self::getAll()[$fieldname]; $field = Html::showDateField('filter-dates', [ 'value' => $values, 'rand' => $rand, 'range' => true, 'display' => false, 'calendar_btn' => false, 'placeholder' => $label, 'on_change' => "on_change_{$rand}(selectedDates, dateStr, instance)", ]); $js = << 0); } /** * Get HTML for a dates range filter. Same as date but for last update field * * @param string|array $values init the input with these values, will be a string if empty values * * @return string */ static function dates_mod($values): string { return self::dates($values, "dates_mod"); } static function itilcategory(string $value = "0"): string { return self::dropdown($value, 'itilcategory', ItilCategory::class); } static function requesttype(string $value = "0"): string { return self::dropdown($value, 'requesttype', RequestType::class); } static function location(string $value = "0"): string { return self::dropdown($value, 'location', Location::class); } static function manufacturer(string $value = "0"): string { return self::dropdown($value, 'manufacturer', Manufacturer::class); } static function dropdown( string $value = "0", string $fieldname = "", string $itemtype = "" ): string { $value = (int) $value; $rand = mt_rand(); $label = self::getAll()[$fieldname]; $field = $itemtype::dropdown([ 'name' => $fieldname, 'value' => $value, 'rand' => $rand, 'display' => false, 'display_emptychoice' => false, 'emptylabel' => '', 'placeholder' => $label, 'on_change' => "on_change_{$rand}()", 'allowClear' => true ]); $js = << 0) }; JAVASCRIPT; $js = Html::scriptBlock($js); return $js.self::field($fieldname, $field, $label, $value > 0); } /** * Get generic HTML for a filter * * @param string $id system name of the filter (ex "dates") * @param string $field html of the filter * @param string $label displayed label for the filter * * @return string the html for the complete field */ static function field( string $id = "", string $field = "", string $label = "", bool $filled = false ): string { $rand = mt_rand(); $class = $filled ? "filled" : ""; $html = << $field $label HTML; $js = << 0) { $('#filter-{$rand}').addClass('filled'); } else { $('#filter-{$rand}').removeClass('filled'); } $(this).width((str_len + 1) * 8 ); }); $('#filter-{$rand}') .hover(function() { $('.dashboard .card.filter-{$id}').addClass('filter-impacted'); }, function() { $('.dashboard .card.filter-{$id}').removeClass('filter-impacted'); }); }); JAVASCRIPT; $js = Html::scriptBlock($js); return $html.$js; } }