Skip to content

Commit

Permalink
[Accident] add: workstop medias actions
Browse files Browse the repository at this point in the history
  • Loading branch information
evarisk-theo committed Dec 2, 2021
1 parent bdca814 commit ee1d61f
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 44 deletions.
124 changes: 90 additions & 34 deletions accident_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,13 @@
// Add file in ticket form
if ($action == 'sendfile') {
include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$objectlineid = GETPOST('objectlineid');
if ($objectlineid > 0) {
$objectline->fetch($objectlineid);
$folder = $objectline->ref;
} else {
$folder = 'temp';
}
$object->fetch($id);

if (!empty($_FILES) && !empty($_FILES['files']['name'][0])) {
Expand All @@ -585,7 +592,7 @@
}
}

$filedir = $conf->digiriskdolibarr->multidir_output[isset($object->entity) ? $object->entity : 1] . '/accident/'. $object->ref . '/workstop/temp';
$filedir = $conf->digiriskdolibarr->multidir_output[isset($object->entity) ? $object->entity : 1] . '/accident/'. $object->ref . '/workstop/' . $folder;
if (!file_exists($filedir))
{
if (dol_mkdir($filedir) < 0)
Expand All @@ -604,6 +611,38 @@
}
}

if ($action == 'removefile') {
include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$filetodelete = GETPOST('filetodelete');
$objectlineid = GETPOST('objectlineid');
if ($objectlineid > 0) {
$objectline->fetch($objectlineid);
$folder = $objectline->ref;
} else {
$folder = 'temp';
}


$accident_upload_dir = $conf->digiriskdolibarr->multidir_output[$conf->entity] . '/accident/' . $object->ref . '/workstop/' . $folder . '/';
//Add files linked
$fileList = dol_dir_list($accident_upload_dir);

if (is_file($accident_upload_dir . $filetodelete)) {
dol_delete_file($accident_upload_dir . $filetodelete);

$thumbsList = dol_dir_list($accident_upload_dir . 'thumbs/');
if (!empty($thumbsList)) {
foreach ($thumbsList as $thumb) {
if (preg_match('/'. preg_split('/\./', $filetodelete)[0] . '/', $thumb['name'])) {
dol_delete_file($accident_upload_dir . 'thumbs/' . $thumb['name'] );
}
}
}
}
$action = '';
}


// Actions to send emails
$triggersendname = 'FIREPERMIT_SENTBYMAIL';
$mode = 'emailfromthirdparty';
Expand Down Expand Up @@ -971,33 +1010,50 @@

$coldisplay++;
print '<td>'; ?>
<?php $fileLinkedList = dol_dir_list($conf->digiriskdolibarr->multidir_output[isset($conf->entity) ? $conf->entity : 1] . '/accident/' . $object->ref . '/workstop/'. $item->ref .'/'); ?>
<div class="wpeo-table table-flex table-3">
<?php
if (!empty($fileLinkedList)) {
foreach ($fileLinkedList as $fileLinked) {
if (preg_split('/\./', $fileLinked['name'])[1] == 'png' || preg_split('/\./', $fileLinked['name'])[1] == 'jpg' || preg_split('/\./', $fileLinked['name'])[1] == 'jpeg') :
?>
<div class="table-row">
<div class="table-cell">
<?php print '<img class="photo" width="50" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=digiriskdolibarr&entity='.$conf->entity.'&file='.urlencode('/accident/'. $object->ref . '/workstop/'. $item->ref .'/thumbs/' . preg_split('/\./', $fileLinked['name'])[0] . '_mini.'. preg_split('/\./', $fileLinked['name'])[1]).'" title="'.dol_escape_htmltag($alt).'">'; ?>
</div>
<div class="table-cell">
<?php print preg_replace('/_mini/','', $fileLinked['name']); ?>
</div>
</div> <?php
elseif ($fileLinked['type'] != 'dir') : ?>
<div class="table-row">
<div class="table-cell table-padding-100">
<i class="fas fa-file"></i>
</div>
<div class="table-cell">
<?php print preg_replace('/_mini/','', $fileLinked['name']); ?>
</div>
</div>
<div class="wpeo-gridlayout grid-2">
<span class="form-label"><?php print $langs->trans("FilesLinked"); ?></span>
<label class="wpeo-button button-blue" for="sendfile">
<i class="fas fa-image button-icon"></i>
<span class="button-label"><?php print $langs->trans('AddDocument'); ?></span>
<input type="file" name="userfile[]" class="sendfile" multiple="multiple" id="sendfile" onchange="window.eoxiaJS.accident.tmpStockFile(<?php echo $item->id ?>)" style="display: none"/>
</label>
</div>
<div id="sendFileForm<?php echo $item->id ?>">
<div id="fileLinkedTable<?php echo $item->id ?>" class="tableforinputfields objectline" value="<?php echo 0 ?>">
<?php $fileLinkedList = dol_dir_list($conf->digiriskdolibarr->multidir_output[isset($conf->entity) ? $conf->entity : 1] . '/accident/' . $object->ref . '/workstop/'. $item->ref .'/'); ?>
<div class="wpeo-table table-flex table-3 objectline" value="<?php echo $item->id ?>"">
<?php
endif;
}
if (!empty($fileLinkedList)) {
foreach ($fileLinkedList as $fileLinked) {
if (preg_split('/\./', $fileLinked['name'])[1] == 'png' || preg_split('/\./', $fileLinked['name'])[1] == 'jpg' || preg_split('/\./', $fileLinked['name'])[1] == 'jpeg') :
?>
<div class="table-row">
<div class="table-cell">
<?php print '<img class="photo" width="50" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=digiriskdolibarr&entity=' . $conf->entity . '&file=' . urlencode('/accident/' . $object->ref . '/workstop/' . $item->ref . '/thumbs/' . preg_split('/\./', $fileLinked['name'])[0] . '_mini.' . preg_split('/\./', $fileLinked['name'])[1]) . '" title="' . dol_escape_htmltag($alt) . '">'; ?>
</div>
<div class="table-cell">
<?php print preg_replace('/_mini/', '', $fileLinked['name']); ?>
</div>
<div class="table-cell table-50 table-end table-padding-0">
<?php print '<div class="linked-file-delete-workstop wpeo-button button-square-50 button-transparent" value="' . $fileLinked['name'] . '"><i class="fas fa-trash button-icon"></i></div>'; ?>
</div>
</div> <?php
elseif ($fileLinked['type'] != 'dir') : ?>
<div class="table-row">
<div class="table-cell table-padding-100">
<i class="fas fa-file"></i>
</div>
<div class="table-cell">
<?php print preg_replace('/_mini/', '', $fileLinked['name']); ?>
</div>
<div class="table-cell table-50 table-end table-padding-0">
<?php print '<div class="linked-file-delete-workstop wpeo-button button-square-50 button-transparent" value="' . $fileLinked['name'] . '"><i class="fas fa-trash button-icon"></i></div>'; ?>
</div>
</div>
<?php
endif;
} ?> </div>
</div> <?php
} else {
?>
<div class="table-row">
Expand Down Expand Up @@ -1032,7 +1088,7 @@
$coldisplay++;
print '<td>'; ?>
<?php $fileLinkedList = dol_dir_list($conf->digiriskdolibarr->multidir_output[isset($conf->entity) ? $conf->entity : 1] . '/accident/' . $object->ref . '/workstop/'. $item->ref .'/'); ?>
<div class="wpeo-table table-flex table-3">
<div class="wpeo-table table-flex table-3 objectline" value="<?php echo $item->id ?>">
<?php
if (!empty($fileLinkedList)) {
foreach ($fileLinkedList as $fileLinked) {
Expand Down Expand Up @@ -1094,7 +1150,7 @@
}
print '</tr>';
}
if ($object->status == 1 && $permissiontoadd) {
if ($object->status == 1 && $permissiontoadd && $action != 'editline') {
print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="addLine">';
Expand All @@ -1121,12 +1177,12 @@
<label class="wpeo-button button-blue" for="sendfile">
<i class="fas fa-image button-icon"></i>
<span class="button-label"><?php print $langs->trans('AddDocument'); ?></span>
<input type="file" name="userfile[]" multiple="multiple" id="sendfile" onchange="window.eoxiaJS.accident.tmpStockFile()" style="display: none"/>
<input type="file" name="userfile[]" class="sendfile" multiple="multiple" id="sendfile" onchange="window.eoxiaJS.accident.tmpStockFile(<?php echo 0 ?>)" style="display: none"/>
</label>
</div>

<div id="sendFileForm">
<div id="fileLinkedTable" class="tableforinputfields">
<div id="sendFileForm<?php echo 0 ?>">
<div id="fileLinkedTable<?php echo 0 ?>" class="tableforinputfields objectline" value="<?php echo 0 ?>">
<?php $fileLinkedList = dol_dir_list($conf->digiriskdolibarr->multidir_output[isset($conf->entity) ? $conf->entity : 1] . '/accident/' . $object->ref . '/workstop/temp/'); ?>
<div class="wpeo-table table-flex table-3">
<?php
Expand All @@ -1142,7 +1198,7 @@
<?php print preg_replace('/_mini/','', $fileLinked['name']); ?>
</div>
<div class="table-cell table-50 table-end table-padding-0">
<?php print '<div class="linked-file-delete wpeo-button button-square-50 button-transparent" value="'. $fileLinked['name'] .'"><i class="fas fa-trash button-icon"></i></div>'; ?>
<?php print '<div class="linked-file-delete-workstop wpeo-button button-square-50 button-transparent" value="'. $fileLinked['name'] .'"><i class="fas fa-trash button-icon"></i></div>'; ?>
</div>
</div> <?php
elseif ($fileLinked['type'] != 'dir') : ?>
Expand All @@ -1154,7 +1210,7 @@
<?php print preg_replace('/_mini/','', $fileLinked['name']); ?>
</div>
<div class="table-cell table-50 table-end table-padding-0">
<?php print '<div class="linked-file-delete wpeo-button button-square-50 button-transparent" value="'. $fileLinked['name'] .'"><i class="fas fa-trash button-icon"></i></div>'; ?>
<?php print '<div class="linked-file-delete-workstop wpeo-button button-square-50 button-transparent" value="'. $fileLinked['name'] .'"><i class="fas fa-trash button-icon"></i></div>'; ?>
</div>
</div>
<?php
Expand Down
57 changes: 47 additions & 10 deletions js/digiriskdolibarr.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -3253,16 +3253,16 @@
/**
* Initialise l'objet "accident" ainsi que la méthode "init" obligatoire pour la bibliothèque EoxiaJS.
*
* @since 1.1.0
* @version 1.1.0
* @since 8.5.0
* @version 8.5.0
*/
window.eoxiaJS.accident = {};

/**
* La méthode appelée automatiquement par la bibliothèque EoxiaJS.
*
* @since 1.1.0
* @version 1.1.0
* @since 8.5.0
* @version 8.5.0
*
* @return {void}
*/
Expand All @@ -3273,18 +3273,29 @@
/**
* La méthode contenant tous les évènements pour les accidents.
*
* @since 1.1.0
* @version 1.1.0
* @since 8.5.0
* @version 8.5.0
*
* @return {void}
*/
window.eoxiaJS.accident.event = function() {
jQuery( document ).on( 'submit', '#sendfile', window.eoxiaJS.accident.tmpStockFile );
jQuery( document ).on( 'submit', ' .sendfile', window.eoxiaJS.accident.tmpStockFile );
jQuery( document ).on( 'click', ' .linked-file-delete-workstop', window.eoxiaJS.accident.removeFile );

};

window.eoxiaJS.accident.tmpStockFile = function() {
/**
* Upload automatiquement le(s) fichier(s) séelectionnés dans digiriskdolibarr/accident/accident_ref/workstop/__REF__ (temp ou ref du workstop)
*
* @since 8.5.0
* @version 8.5.0
*
* @return {void}
*/
window.eoxiaJS.accident.tmpStockFile = function(id) {
var files = $('#sendfile').prop('files');


const formData = new FormData();
for (let i = 0; i < files.length; i++) {
let file = files[i]
Expand All @@ -3293,18 +3304,44 @@


$.ajax({
url: document.URL + '&action=sendfile',
url: document.URL + '&action=sendfile&objectlineid=' + id,
type: "POST",
processData: false,
contentType: false,
data: formData,
success: function ( ) {
$('#sendFileForm').load(document.URL+ ' #fileLinkedTable')
$('#sendFileForm' + id).load(document.URL + ' #fileLinkedTable' + id)
},
error: function ( ) {
}
});
};

/**
* Supprime le fichier séelectionné dans digiriskdolibarr/accident/accident_ref/workstop/__REF__ (temp ou ref du workstop)
*
* @since 8.5.0
* @version 8.5.0
*
* @return {void}
*/

window.eoxiaJS.accident.removeFile = function( event ) {
let filetodelete = $(this).attr('value');
filetodelete = filetodelete.replace('_mini', '')
let objectlineid = $(this).closest('.objectline').attr('value')

$.ajax({
url: document.URL + '&action=removefile&filetodelete='+filetodelete+'&objectlineid='+objectlineid,
type: "POST",
processData: false,
contentType: false,
success: function ( ) {
$('#sendFileForm' + objectlineid).load(document.URL + ' #fileLinkedTable' + objectlineid)
},
error: function ( ) {
}
});
};

/**
Expand Down

0 comments on commit ee1d61f

Please sign in to comment.