dolibarr 24.0.0-beta
shareddocuments.controller.class.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (c) 2025 Schaffhauser sébastien <sebastien@webmaster67.fr>
5 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
9 */
10
11require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
12
13require_once __DIR__ . '/abstractdocument.controller.class.php';
14
25{
27 public $sanitized_subdir = '';
28
34 public function checkAccess()
35 {
36 $this->accessRight = getDolGlobalInt('WEBPORTAL_SHARED_DOCUMENT_ACCESS');
37 return parent::checkAccess();
38 }
39
45 public function action()
46 {
47 global $langs;
49 if (!$context->controllerInstance->checkAccess()) {
50 return -1;
51 }
52
53 $current_subdir = $_POST['subdir'] ?? $_GET['subdir'] ?? '';
54 if (!empty($current_subdir)) {
55 $parts = explode('/', $current_subdir);
56 $safe_parts = array();
57 foreach ($parts as $part) {
58 if ($part !== '.' && $part !== '..') {
59 $safe_parts[] = dol_sanitizeFileName($part);
60 }
61 }
62 $this->sanitized_subdir = implode('/', $safe_parts);
63 }
64
65 $context->title = html_entity_decode($langs->trans('SharedDocuments'));
66 $context->desc = $langs->trans('ListOfSharedDocuments');
67 $context->menu_active[] = 'shared_documents';
68
69 return 1;
70 }
71
77 public function display()
78 {
79 global $conf, $langs;
81 if (!$context->controllerInstance->checkAccess()) {
82 $this->display404();
83 return;
84 }
85
86 $this->loadTemplate('header');
87 $this->loadTemplate('menu');
88 $this->loadTemplate('hero-header-banner');
89
90 print '<main class="container">';
91
92 // 1. Manage the current subfolder from the class property
93 $sanitized_subdir = $this->sanitized_subdir;
94
95 // 2. Prepare the paths
96 $shared_dir_name = getDolGlobalString('WEBPORTAL_SHARED_DOCS_DIR', 'Documentscomptes');
97 $base_dir_ged_partage = $conf->ecm->dir_output . '/' . $shared_dir_name;
98 // The full path now includes the visited subfolder
99 $current_dir_ged_partage = $base_dir_ged_partage . '/' . $sanitized_subdir;
100
101 // 3. List ALL contents (files AND folders) of the current directory
102 $itemList = dol_dir_list($current_dir_ged_partage, 'all', 0, '', '', 'name', SORT_ASC);
103 if (is_array($itemList)) {
104 foreach ($itemList as $key => $item) {
105 // If the item is a file and its size is empty...
106 if ($item['type'] === 'file' && empty($item['size'])) {
107 $full_file_path = $current_dir_ged_partage . '/' . $item['name'];
108 // ... we recalculate its size and update the table.
109 // The @ avoids an error if the file is unreadable.
110 $itemList[$key]['size'] = @filesize($full_file_path);
111 }
112 }
113 }
114 // 4. Build the Breadcrumb
115 $baseUrl = $_SERVER['PHP_SELF'].'?controller=shareddocuments';
116 $breadcrumbs = '<nav aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="'.$baseUrl.'">'.dol_htmlentities($langs->trans("Home")).'</a></li>';
117 $path_so_far = '';
118 if (!empty($sanitized_subdir)) {
119 foreach (explode('/', $sanitized_subdir) as $part) {
120 $path_so_far .= (empty($path_so_far) ? '' : '/') . $part;
121 $breadcrumbs .= '<li class="breadcrumb-item"><a href="'.$baseUrl.'&subdir='.$path_so_far.'">'.dol_htmlentities($part).'</a></li>';
122 }
123 }
124 $breadcrumbs .= '</ol></nav>';
125
126 // Show breadcrumbs
127 print $breadcrumbs;
128
129 // 5. Define functions to build navigation and download links
130 $linkBuilder = array(
131 'dir' =>
135 function (array $dir) use ($baseUrl, $sanitized_subdir) {
136 $new_subdir = (!empty($sanitized_subdir) ? $sanitized_subdir . '/' : '') . $dir['name'];
137 return $baseUrl . '&subdir=' . urlencode($new_subdir);
138 },
139 'file' =>
143 function (array $file) use ($shared_dir_name, $sanitized_subdir) {
144 $file_path = $shared_dir_name . '/' . (!empty($sanitized_subdir) ? $sanitized_subdir . '/' : '') . $file['name'];
145 return DOL_URL_ROOT . '/document.php?modulepart=ecm&file=' . urlencode($file_path);
146 }
147 );
148 // 6. Call the new display method
150 html_entity_decode($langs->trans('SharedDocuments')),
151 $itemList,
152 $langs->trans('ThisDirectoryIsEmpty'),
153 $linkBuilder
154 );
155
156 print '</main>';
157
158 $this->loadTemplate('footer');
159 }
160}
displayDocumentTable($title, $itemList, $emptyMessage, array $linkBuilder)
Renders an HTML file browser table for a given list of files and directories.
static getInstance()
Singleton method to create one instance of this object.
display404()
Display error template.
loadTemplate($templateName, $vars=false)
Load a template .tpl file.
Class for SharedDocumentsController.
action()
Action method is called before html output.
checkAccess()
Check access rights for this page.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:64
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
$context
@method int call_trigger(string $triggerName, ?User $user)
Definition logout.php:42