|
| 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 | +} |
0 commit comments