Skip to content

Commit

Permalink
New msg attachment linked to event (Dolibarr#29022)
Browse files Browse the repository at this point in the history
* New msg attached files and documents are linked to there event and not to the ticket and are accessible inside the linked files tab on the ticket page. for non private msg, they are set to be shared

* Fix ticket/document.php

* Fix ticket/document.php

* change comment

* Fixing style errors.

* fix style

* fix

* travis Fix

* Fix travis

* Fix sticler

* Fixing style errors.

* Remove useless code in files.lib.php

* Fix to not try adding linked files when they are linked to the message and not the ticket

* Commented deprecated part with linked files to messages

* Fix link of file linked to message

* Fix rename file

* comment delete

* Fix test dolibarr

* Fix merge

---------

Co-authored-by: stickler-ci <support@stickler-ci.com>
  • Loading branch information
MaximilienR-easya and stickler-ci authored Apr 24, 2024
1 parent e2b4638 commit 26920d2
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 7 deletions.
7 changes: 7 additions & 0 deletions htdocs/core/actions_linkedfiles.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@
if ($filenamefrom && $filenameto) {
$srcpath = $upload_dir.'/'.$filenamefrom;
$destpath = $upload_dir.'/'.$filenameto;
if ($modulepart == "ticket" && !dol_is_file($srcpath)) {
$srcbis = $conf->agenda->dir_output.'/'.GETPOST('section_dir').$filenamefrom;
if (dol_is_file($srcbis)) {
$srcpath = $srcbis;
$destpath = $conf->agenda->dir_output.'/'.GETPOST('section_dir').$filenameto;
}
}

$reshook = $hookmanager->initHooks(array('actionlinkedfiles'));
$parameters = array('filenamefrom' => $filenamefrom, 'filenameto' => $filenameto, 'upload_dir' => $upload_dir);
Expand Down
10 changes: 8 additions & 2 deletions htdocs/core/class/html.formfile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,9 @@ public function list_of_documents($filearray, $object, $modulepart, $param = '',
if (array_key_exists('rowid', $filearray[$key]) && $filearray[$key]['rowid'] > 0) {
$lastrowid = $filearray[$key]['rowid'];
}
$filepath = $relativepath.$file['name'];
$filepath = $file['level1name'].'/'.$file['name'];
$modulepart = basename(dirname($file['path']));
$relativepath = preg_replace('/\/(.+)/', '', $filepath) . '/';

$editline = 0;
$nboflines++;
Expand Down Expand Up @@ -1385,7 +1387,7 @@ public function list_of_documents($filearray, $object, $modulepart, $param = '',
print $relativepath;
}
//print dol_trunc($file['name'],$maxlength,'middle');
if (GETPOST('action', 'aZ09') == 'editfile' && $file['name'] == basename(GETPOST('urlfile', 'alpha'))) {
if (GETPOST('action', 'aZ09') == 'editfile' && $file['name'] == basename(GETPOST('urlfile', 'alpha')) && $file['level1name'] == dirname(GETPOST('urlfile', 'alpha'))) {
print '</a>';
$section_dir = dirname(GETPOST('urlfile', 'alpha'));
if (!preg_match('/\/$/', $section_dir)) {
Expand Down Expand Up @@ -1434,8 +1436,12 @@ public function list_of_documents($filearray, $object, $modulepart, $param = '',
if (!dol_is_file($file['path'].'/'.$smallfile)) {
$smallfile = getImageFileNameForSize($file['name'], '_small', '.png'); // For backward compatibility of old thumbs that were created with filename in lower case and with .png extension
}
if (!dol_is_file($file['path'].'/'.$smallfile)) {
$smallfile = getImageFileNameForSize($file['name'], ''); // This is in case no _small image exist
}
//print $file['path'].'/'.$smallfile.'<br>';


$urlforhref = getAdvancedPreviewUrl($modulepart, $relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']), 1, '&entity='.(empty($object->entity) ? $conf->entity : $object->entity));
if (empty($urlforhref)) {
$urlforhref = DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.(empty($object->entity) ? $conf->entity : $object->entity).'&file='.urlencode($relativepath.$fileinfo['filename'].'.'.strtolower($fileinfo['extension']));
Expand Down
9 changes: 9 additions & 0 deletions htdocs/core/lib/files.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,15 @@ function completeFileArrayWithDatabaseInfo(&$filearray, $relativedir)

$filearrayindatabase = array_merge($filearrayindatabase, dol_dir_list_in_database($relativedirold, '', null, 'name', SORT_ASC));
}
} elseif ($modulepart == 'ticket') {
foreach ($filearray as $key => $val) {
$rel_dir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT, '/').'/', '', $filearray[$key]['path']);
$rel_dir = preg_replace('/[\\/]$/', '', $rel_dir);
$rel_dir = preg_replace('/^[\\/]/', '', $rel_dir);
if ($rel_dir != $relativedir) {
$filearrayindatabase = array_merge($filearrayindatabase, dol_dir_list_in_database($rel_dir, '', null, 'name', SORT_ASC));
}
}
}

//var_dump($relativedir);
Expand Down
12 changes: 11 additions & 1 deletion htdocs/core/lib/ticket.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function ticketAdminPrepareHead()
*/
function ticket_prepare_head($object)
{
global $langs, $conf, $user;
global $langs, $conf, $user, $db;

$h = 0;
$head = array();
Expand All @@ -109,6 +109,16 @@ function ticket_prepare_head($object)
include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$upload_dir = $conf->ticket->dir_output."/".$object->ref;
$nbFiles = count(dol_dir_list($upload_dir, 'files'));
$sql = 'SELECT id FROM '.MAIN_DB_PREFIX.'actioncomm';
$sql .= " WHERE fk_element = ".(int) $object->id." AND elementtype = 'ticket'";
$resql = $db->query($sql);
if ($resql) {
$numrows = $db->num_rows($resql);
for ($i=0; $i < $numrows; $i++) {
$upload_msg_dir = $conf->agenda->dir_output.'/'.$db->fetch_row($resql)[0];
$nbFiles += count(dol_dir_list($upload_msg_dir, "files"));
}
}
$head[$h][0] = DOL_URL_ROOT.'/ticket/document.php?id='.$object->id;
$head[$h][1] = $langs->trans("Documents");
if ($nbFiles > 0) {
Expand Down
33 changes: 29 additions & 4 deletions htdocs/ticket/class/ticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1859,17 +1859,42 @@ public function createTicketMessage($user, $notrigger = 0, $filename_list = arra
$actioncomm->attachedfiles = $attachedfiles;
}

if (!empty($mimefilename_list) && is_array($mimefilename_list)) {
$actioncomm->note_private = dol_concatdesc($actioncomm->note_private, "\n".$langs->transnoentities("AttachedFiles").': '.implode(';', $mimefilename_list));
}

//if (!empty($mimefilename_list) && is_array($mimefilename_list)) {
// $actioncomm->note_private = dol_concatdesc($actioncomm->note_private, "\n".$langs->transnoentities("AttachedFiles").': '.implode(';', $mimefilename_list));
//}
$actionid = $actioncomm->create($user);
if ($actionid <= 0) {
$error++;
$this->error = $actioncomm->error;
$this->errors = $actioncomm->errors;
}

if ($actionid > 0) {
if (is_array($attachedfiles) && array_key_exists('paths', $attachedfiles) && count($attachedfiles['paths']) > 0) {
foreach ($attachedfiles['paths'] as $key => $filespath) {
$destdir = $conf->agenda->dir_output.'/'.$actionid;
$destfile = $destdir.'/'.$attachedfiles['names'][$key];
if (dol_mkdir($destdir) >= 0) {
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
dol_move($filespath, $destfile);
if ($actioncomm->code == "TICKET_MSG") {
$ecmfile = new EcmFiles($this->db);
$destdir = preg_replace('/^'.preg_quote(DOL_DATA_ROOT, '/').'/', '', $destdir);
$destdir = preg_replace('/[\\/]$/', '', $destdir);
$destdir = preg_replace('/^[\\/]/', '', $destdir);
$ecmfile->fetch(0, '', $destdir.'/'.$attachedfiles['names'][$key]);
require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
$ecmfile->share = getRandomPassword(true);
$result = $ecmfile->update($user);
if ($result < 0) {
setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
}
}
}
}
}
}

// Commit or rollback
if ($error) {
$this->db->rollback();
Expand Down
19 changes: 19 additions & 0 deletions htdocs/ticket/document.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,25 @@

// Build file list
$filearray = dol_dir_list($upload_dir, "files", 0, '', '\.meta$', $sortfield, (strtolower($sortorder) == 'desc' ? SORT_DESC : SORT_ASC), 1);
// same as above for every messages
$sql = 'SELECT id FROM '.MAIN_DB_PREFIX.'actioncomm';
$sql .= " WHERE fk_element = ".(int) $object->id." AND elementtype = 'ticket'";
$resql = $db->query($sql);
if ($resql) {
$file_msg_array = array();
$numrows = $db->num_rows($resql);
for ($i=0; $i < $numrows; $i++) {
$upload_msg_dir = $conf->agenda->dir_output.'/'.$db->fetch_row($resql)[0];
$file_msg = dol_dir_list($upload_msg_dir, "files", 0, '', '\.meta$', $sortfield, (strtolower($sortorder) == 'desc' ? SORT_DESC : SORT_ASC), 1);
if (count($file_msg)) {
$file_msg_array = array_merge($file_msg, $file_msg_array);
}
}
if (count($file_msg_array)) {
$filearray = array_merge($filearray, $file_msg_array);
}
}

$totalsize = 0;
foreach ($filearray as $key => $file) {
$totalsize += $file['size'];
Expand Down

0 comments on commit 26920d2

Please sign in to comment.