Skip to content

Commit 532c38f

Browse files
feat: add possibility to export projects as JSON (#69)
* feat: add possibility to export projects as JSON * Apply suggestions from code review Co-authored-by: Adrien Clairembault <[email protected]> * fix: remove useless header_remove and move header in front file * feat: Adding project name in the exported file name --------- Co-authored-by: Adrien Clairembault <[email protected]>
1 parent 5701bb6 commit 532c38f

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- Added the ability to export gantt view data in JSON format
1213
- Use real dates as priority over planned dates for tasks and projects

front/projectsexport.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------
5+
*
6+
* GLPI - Gestionnaire Libre de Parc Informatique
7+
*
8+
* http://glpi-project.org
9+
*
10+
* @copyright 2015-2024 Teclib' and contributors.
11+
* @copyright 2003-2014 by the INDEPNET Development Team.
12+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
13+
*
14+
* ---------------------------------------------------------------------
15+
*
16+
* LICENSE
17+
*
18+
* This file is part of GLPI.
19+
*
20+
* This program is free software: you can redistribute it and/or modify
21+
* it under the terms of the GNU General Public License as published by
22+
* the Free Software Foundation, either version 3 of the License, or
23+
* (at your option) any later version.
24+
*
25+
* This program is distributed in the hope that it will be useful,
26+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
27+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28+
* GNU General Public License for more details.
29+
*
30+
* You should have received a copy of the GNU General Public License
31+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
32+
*
33+
* ---------------------------------------------------------------------
34+
*/
35+
36+
use GlpiPlugin\Gantt\ProjectsExport;
37+
38+
include('../../../inc/includes.php');
39+
40+
$project_id = $_GET['project_id'] ?? -1;
41+
42+
// Check right
43+
Session::checkRightsOr(Project::$rightname, [Project::READALL, Project::READMY]);
44+
45+
// Get the project name
46+
$project = new Project();
47+
$project->getFromDB($project_id);
48+
$project_name = $project->fields['name'] ?? 'projects';
49+
$project_name = preg_replace('/[^a-zA-Z0-9_\-]/', '', $project_name); // Remove special characters
50+
51+
header('Content-Type: application/json');
52+
header('Content-Disposition: attachment; filename="gantt-data-' . $project_name . '.json"');
53+
54+
echo (new ProjectsExport($project_id))->json();

src/ProjectsExport.php

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------
5+
*
6+
* GLPI - Gestionnaire Libre de Parc Informatique
7+
*
8+
* http://glpi-project.org
9+
*
10+
* @copyright 2015-2024 Teclib' and contributors.
11+
* @copyright 2003-2014 by the INDEPNET Development Team.
12+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
13+
*
14+
* ---------------------------------------------------------------------
15+
*
16+
* LICENSE
17+
*
18+
* This file is part of GLPI.
19+
*
20+
* This program is free software: you can redistribute it and/or modify
21+
* it under the terms of the GNU General Public License as published by
22+
* the Free Software Foundation, either version 3 of the License, or
23+
* (at your option) any later version.
24+
*
25+
* This program is distributed in the hope that it will be useful,
26+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
27+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28+
* GNU General Public License for more details.
29+
*
30+
* You should have received a copy of the GNU General Public License
31+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
32+
*
33+
* ---------------------------------------------------------------------
34+
*/
35+
36+
namespace GlpiPlugin\Gantt;
37+
38+
final class ProjectsExport
39+
{
40+
private int $project_id;
41+
42+
public function __construct(int $project_id) {
43+
$this->project_id = $project_id;
44+
}
45+
46+
private function loadProjectsData(): array
47+
{
48+
$items = [];
49+
$factory = new DataFactory();
50+
$factory->getItemsForProject($items, $this->project_id);
51+
52+
$id_refs = [];
53+
$to_export = [];
54+
foreach ($items as $item) {
55+
switch ($item->type) {
56+
case 'project':
57+
$project = [
58+
'id' => $item->id,
59+
'name' => $item->text,
60+
'start_date' => $item->start_date,
61+
'end_date' => $item->end_date,
62+
];
63+
64+
$id_refs[$item->id] = array_merge(
65+
$id_refs[$item->id] ?? [],
66+
$project
67+
);
68+
if ($item->parent == 0) {
69+
$to_export[] = &$id_refs[$item->id];
70+
} else {
71+
$id_refs[$item->parent]['projects'][] = &$id_refs[$item->id];
72+
}
73+
break;
74+
case 'task':
75+
$task = [
76+
'id' => $item->linktask_id,
77+
'name' => $item->text,
78+
'start_date' => $item->start_date,
79+
'end_date' => $item->end_date,
80+
'progress' => $item->progress,
81+
];
82+
83+
$id_refs[$item->id] = array_merge(
84+
$id_refs[$item->id] ?? [],
85+
$task
86+
);
87+
$id_refs[$item->parent]['tasks'][] = &$id_refs[$item->id];
88+
break;
89+
case 'milestone':
90+
$milestone = [
91+
'id' => $item->linktask_id,
92+
'name' => $item->text,
93+
'date' => $item->start_date,
94+
];
95+
96+
$id_refs[$item->id] = array_merge(
97+
$id_refs[$item->id] ?? [],
98+
$milestone
99+
);
100+
$id_refs[$item->parent]['milestones'][] = &$id_refs[$item->id];
101+
break;
102+
}
103+
}
104+
105+
return $to_export;
106+
}
107+
108+
public function json(): string
109+
{
110+
return json_encode($this->loadProjectsData());
111+
}
112+
}

templates/view.html.twig

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@
3737
</a>
3838
</li>
3939

40+
<li class="gantt-menu-item gantt-menu-item-right">
41+
<div class="btn-group flex-wrap" role="group">
42+
<span class="btn bg-blue-lt pe-none" aria-disabled="true">
43+
<i class="ti ti-file-export"></i>
44+
<span>{{ __('Export', 'gantt') }}</span>
45+
</span>
46+
47+
<a href="{{ get_plugin_web_dir('gantt') ~ '/front/projectsexport.php?project_id=' ~ id }}" target="_blank" class="btn btn-outline-secondary">{{ __('JSON', 'gantt') }}</a>
48+
</div>
49+
50+
</li>
51+
4052
<li class="gantt-menu-item gantt-menu-item-right">
4153
<div class="btn-group flex-wrap" role="group">
4254
<span class="btn bg-blue-lt pe-none" aria-disabled="true">

0 commit comments

Comments
 (0)