Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add location of items card for dashboard #18469

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/pics/charts/map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/Glpi/Dashboard/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,13 @@ public function getAllDasboardCards($force = false): array
]
];

$cards["items_map"] = [
'widgettype' => ["map"],
'label' => __("Location of items"),
'group' => __('Others'),
'provider' => "Glpi\\Dashboard\\Provider::getItemsCoordinates",
];

if (GLPI_ENVIRONMENT_TYPE !== GLPI::ENV_DEVELOPMENT) {
// Do not cache dashboard cards on `development` envs
$GLPI_CACHE->set(self::getAllDashboardCardsCacheKey(), $cards);
Expand Down
71 changes: 71 additions & 0 deletions src/Glpi/Dashboard/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use Glpi\DBAL\QueryExpression;
use Glpi\DBAL\QueryFunction;
use Glpi\DBAL\QuerySubQuery;
use Location;
use Session;
use Stat;
use Ticket;
Expand Down Expand Up @@ -1702,6 +1703,76 @@ public static function getTicketSummary(array $params = [])
}


public static function getItemsCoordinates(array $params = []): array
{
global $CFG_GLPI;

$DB = DBConnection::getReadConnection();
$default_params = [
'label' => "",
'icon' => "",
'apply_filters' => [],
'limit' => 5000,
];
$params = array_merge($default_params, $params);

$data = [];
$nb_added = 0;
foreach ($CFG_GLPI['location_types'] as $itemtype) {
if (!$DB->fieldExists($itemtype::getTable(), 'locations_id') || !$DB->fieldExists($itemtype::getTable(), 'name')) {
continue;
}

$i_table = $itemtype::getTable();
$l_table = Location::getTable();
$iterator = $DB->request([
'SELECT' => [
"$i_table.id",
"$i_table.name",
"$l_table.latitude",
"$l_table.longitude",
],
'FROM' => $l_table,
'INNER JOIN' => [
$i_table => [
'ON' => [
$i_table => getForeignKeyFieldForItemType(Location::class),
$l_table => 'id',
]
],
],
'WHERE' => [
'latitude' => ['!=', ''],
'longitude' => ['!=', ''],
],
]);

foreach ($iterator as $result) {
if ($nb_added >= $params['limit']) {
break 2;
}
$data[] = [
'id' => $result['id'],
'type' => $itemtype::getTypeName(1),
'url' => $itemtype::getFormURLWithID($result['id']),
'icon' => $itemtype::getIcon(),
'name' => $result['name'],
'latitude' => $result['latitude'],
'longitude' => $result['longitude'],
];

$nb_added++;
}
}

return [
'data' => $data,
'label' => $params['label'],
'icon' => $params['icon'],
];
}


public static function formatMonthyearDates(string $monthyear): array
{
$rawdate = explode('-', $monthyear);
Expand Down
109 changes: 105 additions & 4 deletions src/Glpi/Dashboard/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
use Laminas\Json\Expr as Json_Expr;
use Laminas\Json\Json;
use Mexitek\PHPColors\Color;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\MarkdownConverter;
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
use Plugin;
use Symfony\Component\DomCrawler\Crawler;
use Search;
Expand Down Expand Up @@ -288,6 +284,13 @@ public static function getAllTypes(): array
'width' => 3,
'height' => 4,
],
'map' => [
'label' => __("Map"),
'function' => 'Glpi\\Dashboard\\Widget::map',
'image' => $CFG_GLPI['root_doc'] . '/pics/charts/map.png',
'width' => 4,
'height' => 4,
]
];

$more_types = Plugin::doHookFunction(Hooks::DASHBOARD_TYPES);
Expand Down Expand Up @@ -1849,6 +1852,104 @@ public static function articleList(array $params): string
}


public static function map(array $params): string
{
$default = [
'data' => [],
'label' => '',
'alt' => '',
'url' => '',
'color' => '',
'icon' => '',
'limit' => 99999,
'class' => "map",
'rand' => mt_rand(),
'filters' => [],
];
$p = array_merge($default, $params);

$color = new Color($p['color']);
$is_light = $color->isLight();

$fg_color = Toolbox::getFgColor($p['color'], $is_light ? 65 : 40);
$id = "map-" . $p['rand'];
$class = $p['class'];
$class .= count($p['filters']) > 0 ? " filter-" . implode(' filter-', $p['filters']) : "";

$points = json_encode($p['data'] ?? []);

$html = <<<HTML
<div
class="card map {$class}"
id="{$id}"
style="background-color: {$p['color']}; color: {$fg_color}">
<div id="map_container-{$p['rand']}" class="card-body px-0"></div>
<span class="main-label">{$p['label']}</a></span>
<i class="main-icon {$p['icon']}"></i>
</div>
HTML;
$js = <<<JAVASCRIPT
$(function () {
const map = initMap($('#map_container-{$p['rand']}'), 'map', 'full');

const _markers = L.markerClusterGroup({
iconCreateFunction: (cluster) => {
const markers = cluster.getAllChildMarkers();
let n = markers.length;

let c = ' marker-cluster-';
if (n < 10) {
c += 'small';
} else if (n < 100) {
c += 'medium';
} else {
c += 'large';
}

return new L.DivIcon({
html: '<div><span>' + n + '</span></div>',
className: 'marker-cluster' + c,
iconSize: new L.Point(40, 40)
});
}
});

const points = {$points};
$.each(points, (index, point) => {
const _icon = L.divIcon({
'className': 'badge bg-blue-lt',
'iconSize': [24, 20],
'html': '<i class="' + point.icon + '"></i>',
});
const _title = `
<a href='\${point.url}'><i class="\${point.icon}"></i>\${point.type} - \${point.name}</a>
`;

const _marker = L.marker([point.latitude, point.longitude], {
icon: _icon,
title: point.name
});
_marker.count = point.count;
_marker.bindPopup(_title);
_markers.addLayer(_marker);

});

map.addLayer(_markers);
map.fitBounds(
_markers.getBounds(), {
padding: [50, 50],
maxZoom: 12
}
);
});
JAVASCRIPT;
$js = \Html::scriptBlock($js);

return $html . $js;
}


public static function getPalette(string $palette_name, int $nb_series = 0): array
{
$palette_obj = new Palette($palette_name);
Expand Down
Loading