3434
3535namespace Glpi \Controller \Knowbase ;
3636
37+ use Document ;
3738use Document_Item ;
39+ use Glpi \Application \View \TemplateRenderer ;
3840use Glpi \Controller \AbstractController ;
3941use Glpi \Controller \CrudControllerTrait ;
4042use Glpi \Exception \Http \AccessDeniedHttpException ;
@@ -73,7 +75,8 @@ public function __invoke(int $id, Request $request): JsonResponse
7375 throw new BadRequestHttpException ();
7476 }
7577
76- $ linked_count = 0 ;
78+ $ linked = [];
79+ $ twig = TemplateRenderer::getInstance ();
7780
7881 foreach ($ documents_ids as $ doc_id ) {
7982 $ doc_id = (int ) $ doc_id ;
@@ -82,18 +85,40 @@ public function __invoke(int $id, Request $request): JsonResponse
8285 }
8386
8487 try {
85- $ this ->add (Document_Item::class, [
88+ $ doc_item = $ this ->add (Document_Item::class, [
8689 'documents_id ' => $ doc_id ,
8790 'itemtype ' => KnowbaseItem::class,
8891 'items_id ' => $ id ,
8992 ]);
90- $ linked_count ++;
9193 } catch (\RuntimeException ) {
9294 // Skip documents that fail (duplicates, permission issues, etc.)
9395 continue ;
9496 }
97+
98+ $ document = new Document ();
99+ if (!$ document ->getFromDB ($ doc_id )) {
100+ continue ;
101+ }
102+
103+ $ filename = (string ) ($ document ->fields ['filename ' ] ?? '' );
104+ $ extension = strtolower (pathinfo ($ filename , PATHINFO_EXTENSION ));
105+ $ styles = KnowbaseItem::getDocumentIconAndColor ($ extension );
106+
107+ $ linked [] = [
108+ 'html ' => $ twig ->render ('pages/tools/kb/document_badge.html.twig ' , [
109+ 'doc ' => [
110+ 'assoc_id ' => $ doc_item ->getID (),
111+ 'id ' => $ doc_id ,
112+ 'filename ' => $ filename ,
113+ 'download_url ' => $ document ->getDownloadUrl (),
114+ 'icon_class ' => $ styles ['icon_class ' ],
115+ 'color_class ' => $ styles ['color_class ' ],
116+ ],
117+ 'can_edit ' => true ,
118+ ]),
119+ ];
95120 }
96121
97- return new JsonResponse (['linked_count ' => $ linked_count ]);
122+ return new JsonResponse (['documents ' => $ linked ]);
98123 }
99124}
0 commit comments