Skip to content

Commit

Permalink
Eoxia#70 [Class/Js] fix: bad method for creating
Browse files Browse the repository at this point in the history
  • Loading branch information
evarisk-kilyan committed Sep 12, 2024
1 parent 5cb4879 commit d918ca4
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 135 deletions.
27 changes: 16 additions & 11 deletions class/exportshortenerdocument.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,25 @@ public function generateFile()

require_once __DIR__ . '/../class/shortener.class.php';

$data = json_decode($this->json, true);
$shortener = new Shortener($this->db);
$shorteners = $shortener->fetchAll('', '', 0, 0, ['t.fk_export_shortener' => $this->id]);
$shorteners = $shortener->fetchAll('', '', $data['number_shortener_url'], 0, ['customsql' => 't.rowid >=' . $data['first_shortener_id']]);

$upload_dir = $conf->easyurl->multidir_output[$conf->entity] . '/' . $this->element;
dol_mkdir($upload_dir);
$fileName = dol_print_date(dol_now(), 'dayhourxcard') . '_' . $this->element . '.csv';
$fp = fopen($upload_dir . '/' . $fileName, 'w');
fputcsv($fp, [1 => 'ref' . ';' . 'label' . ';' . 'original_url' . ';' . 'short_url']);
foreach ($shorteners as $key => $link) {
fputcsv($fp, [$key => $link->ref . ';' . $link->label . ';' . $link->original_url . ';' . $link->short_url]);
if (is_array($shorteners) && !empty($shorteners)) {
$upload_dir = $conf->easyurl->multidir_output[$conf->entity] . '/' . $this->element;
if (!dol_is_dir($upload_dir)) {
dol_mkdir($upload_dir);
}
$fileName = dol_print_date(dol_now(), 'dayhourxcard') . '_' . $this->element . '.csv';
$fp = fopen($upload_dir . '/' . $fileName, 'w');
fputcsv($fp, [1 => 'ref' . ';' . 'label' . ';' . 'original_url' . ';' . 'short_url']);
foreach ($shorteners as $key => $shortener) {
fputcsv($fp, [$key => $shortener->ref . ';' . str_replace(' ', '-', $shortener->label) . ';' . $shortener->original_url . ';' . $shortener->short_url]);
}
fclose($fp);
$this->last_main_doc = $fileName;
$this->update($user);
}
fclose($fp);
$this->last_main_doc = $fileName;
$this->update($user);
}

}
2 changes: 1 addition & 1 deletion js/easyurl.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 12 additions & 36 deletions js/modules/exportshortener.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* \brief JavaScript exportshortener file for module EasyURL
*/

'use strict'

/**
* Init exportshortener JS
*
Expand Down Expand Up @@ -59,11 +61,10 @@ window.easyurl.exportshortener.init = function() {
* @returns {void}
*/
window.easyurl.exportshortener.event = function() {
$(document).on('click', '#generate-url-from .button-save', window.easyurl.exportshortener.buttonSave);
};

/**
* ExportShortener button save
* ExportShortener create shortener
*
* @memberof EasyURL_ExportShortener
*
Expand All @@ -72,48 +73,23 @@ window.easyurl.exportshortener.event = function() {
*
* @returns {void}
*/
window.easyurl.exportshortener.buttonSave = function(e) {
e.preventDefault();
let form = new FormData($('#generate-url-from')[0]);
form.set('current_nb', '0');
let url = new URL(document.URL);
url.searchParams.append('action', form.get('action'));
url.searchParams.append('token', form.get('token'));
window.easyurl.exportshortener.generateExport = function(nbUrl) {

window.easyurl.exportshortener.createLink(form, url, 0);
}
let token = window.saturne.toolbox.getToken();
let querySeparator = window.saturne.toolbox.getQuerySeparator(document.URL);

/**
* ExportShortener create link
*
* @memberof EasyURL_ExportShortener
*
* @since 1.1.0
* @version 1.1.0
*
* @returns {void}
*/
window.easyurl.exportshortener.createLink = function(form, url, current) {
form.set('current_nb', current.toString());
$.ajax({
method: 'POST',
url: url.toString(),
data: JSON.stringify(Object.fromEntries(form)),
url: document.URL + querySeparator + 'action=generate_export&nb_url=' + nbUrl + '&token=' + token,
processData: false,
contentType: false,
success: function (resp) {
if (current == 0) {
$('.tab-export tr:nth-child(1)').after($(resp).find('.tab-export tr:nth-child(2)'));
let newFormData = new FormData($(resp).find('#generate-url-from')[0]);
form.set('export_id', newFormData.get('export_id'));
} else {
$('.tab-export tr:nth-child(2)').replaceWith($(resp).find('.tab-export tr:nth-child(2)'));
}
$('.global-infos').replaceWith($(resp).find('.global-infos'));
if (current < form.get('nb_url') - 1) {
window.easyurl.exportshortener.createLink(form, url, current + 1);
}
window.saturne.loader.remove($('#generate-url-from .button-save'));
$('#global-infos').replaceWith($(resp).find('#global-infos'));
$('#shortener-export-table').replaceWith($(resp).find('#shortener-export-table'));
}
});
}



55 changes: 55 additions & 0 deletions js/modules/shortener.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ window.easyurl.shortener.event = function() {
$(document).on('click', '.show-qrcode', window.easyurl.shortener.showQRCode);
$(document).on('change', '#fromid', window.easyurl.shortener.reloadAssignShortenerView);
$(document).on('click', '.button-save.assign-button', window.easyurl.shortener.assignShortener);
$(document).on('submit', '#generate-url-from', window.easyurl.shortener.buttonSave);
};

/**
Expand Down Expand Up @@ -177,3 +178,57 @@ window.easyurl.shortener.assignShortener = function() {
error: function() {}
});
};

/**
* ExportShortener button save
*
* @memberof EasyURL_Shortener
*
* @since 1.1.0
* @version 1.1.0
*
* @returns {void}
*/
window.easyurl.shortener.buttonSave = function(e) {
e.preventDefault();
let form = new FormData($('#generate-url-from')[0]);
let nbUrl = parseInt(form.get('nb_url'));

window.saturne.loader.display($('#generate-url-from .button-save'));
window.easyurl.shortener.createShortener(form, 1, nbUrl);
}

/**
* ExportShortener create link
*
* @memberof EasyURL_Shortener
*
* @since 1.1.0
* @version 1.1.0
*
* @returns {void}
*/
window.easyurl.shortener.createShortener = function(form, current, nbUrl) {
let token = window.saturne.toolbox.getToken();
let querySeparator = window.saturne.toolbox.getQuerySeparator(document.URL);

$.ajax({
method: 'POST',
url: document.URL + querySeparator + 'action=generate_url&nb_url=' + current + '&token=' + token,
data: JSON.stringify(Object.fromEntries(form)),
processData: false,
contentType: false,
success: function (resp) {
let success = $(resp).find('input[name="success"]').val();

$('#global-infos').replaceWith($(resp).find('#global-infos'));
if (success) {
if (current < nbUrl) {
window.easyurl.shortener.createShortener(form, ++current, nbUrl);
} else {
window.easyurl.exportshortener.generateExport(nbUrl);
}
}
}
});
}
5 changes: 3 additions & 2 deletions langs/fr_FR/easyurl.lang
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ ExportEnd = ID fin URL
ExportDate = Date export
ExportOrigin = URL d'origine
ExportConsume = Nombre d'URL consommé
ExportGenerating = Export en cours de %d urls !
ExportSuccess = Export réussi de %d urls !
ExportGenerating = Export en cours de %d URLs !
ExportSuccess = Export réussi des URLs !
ExportError = Erreur lors de la génération de l'URL %d;

#
# Shortener - Raccourcissement d'URL
Expand Down
Loading

0 comments on commit d918ca4

Please sign in to comment.