diff --git a/class/shortener.class.php b/class/shortener.class.php index 20b965f..46bd9f0 100644 --- a/class/shortener.class.php +++ b/class/shortener.class.php @@ -1,5 +1,5 @@ +/* Copyright (C) 2021-2024 EVARISK * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -107,23 +107,24 @@ class Shortener extends SaturneObject * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor */ public $fields = [ - 'rowid' => ['type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'visible' => 0, 'noteditable' => 1, 'index' => 1, 'comment' => 'Id'], - 'ref' => ['type' => 'varchar(128)', 'label' => 'Ref', 'enabled' => 1, 'position' => 10, 'notnull' => 1, 'visible' => 4, 'noteditable' => 1, 'default' => '(PROV)', 'index' =>1, 'searchall' => 1, 'showoncombobox' => 1, 'validate' => 1, 'comment' => 'Reference of object'], - 'ref_ext' => ['type' => 'varchar(128)', 'label' => 'RefExt', 'enabled' => 1, 'position' => 20, 'notnull' => 0, 'visible' => 0], - 'entity' => ['type' => 'integer', 'label' => 'Entity', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => 0, 'index' => 1], - 'date_creation' => ['type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'position' => 40, 'notnull' => 1, 'visible' => 2], - 'tms' => ['type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'position' => 50, 'notnull' => 0, 'visible' => 0], - 'import_key' => ['type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'position' => 60, 'notnull' => 0, 'visible' => 0], - 'status' => ['type' => 'smallint', 'label' => 'Status', 'enabled' => 1, 'position' => 160, 'notnull' => 1, 'visible' => 2, 'default' => 0, 'index' => 1, 'arrayofkeyval' => [0 => 'StatusDraft', 1 => 'ValidatePendingAssignment', 10 => 'Assign'], 'css' => 'minwidth100 maxwidth300 widthcentpercentminusxx'], - 'label' => ['type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'position' => 70, 'notnull' => 1, 'visible' => 5, 'searchall' => 1, 'css' => 'minwidth100 maxwidth300 widthcentpercentminusxx', 'cssview' => 'wordbreak', 'showoncombobox' => 2, 'validate' => 1], - 'short_url' => ['type' => 'url', 'label' => 'ShortUrl', 'enabled' => 1, 'position' => 80, 'notnull' => 0, 'visible' => 1, 'copytoclipboard' => 1, 'css' => 'minwidth100 maxwidth300 widthcentpercentminusxx nowrap'], - 'original_url' => ['type' => 'url', 'label' => 'OriginalUrl', 'enabled' => 1, 'position' => 90, 'notnull' => 0, 'visible' => 1, 'copytoclipboard' => 1, 'css' => 'minwidth100 maxwidth300 widthcentpercentminusxx nowrap'], - 'type' => ['type' => 'sellist:c_shortener_url_type:label', 'label' => 'UrlType', 'enabled' => 1, 'position' => 100, 'notnull' => 0, 'visible' => 1, 'css' => 'maxwidth150 widthcentpercentminusxx'], - 'methode' => ['type' => 'select', 'label' => 'UrlMethode', 'enabled' => 1, 'position' => 110, 'notnull' => 0, 'visible' => 5, 'arrayofkeyval' => ['' => '', 'yourls' => 'YOURLS', 'wordpress' => 'WordPress'], 'css' => 'maxwidth200 widthcentpercentminusxx', 'csslist' => 'minwidth150 center', 'help' => 'UrlMethodeDescription'], - 'element_type' => ['type' => 'select', 'label' => 'ElementType', 'enabled' => 1, 'position' => 120, 'notnull' => 0, 'visible' => 1, 'arrayofkeyval' => ['' => ''], 'css' => 'maxwidth150 widthcentpercentminusxx'], - 'fk_element' => ['type' => 'integer', 'label' => 'FkElement', 'enabled' => 1, 'position' => 130, 'notnull' => 0, 'visible' => 1, 'index' => 1, 'css' => 'minwidth200 maxwidth300 widthcentpercentminusxx'], - 'fk_user_creat' => ['type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'picto' => 'user', 'enabled' => 1, 'position' => 140, 'notnull' => 1, 'visible' => 0, 'foreignkey' => 'user.rowid'], - 'fk_user_modif' => ['type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'picto' => 'user', 'enabled' => 1, 'position' => 150, 'notnull' => 0, 'visible' => 0, 'foreignkey' => 'user.rowid'], + 'rowid' => ['type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'visible' => 0, 'noteditable' => 1, 'index' => 1, 'comment' => 'Id'], + 'ref' => ['type' => 'varchar(128)', 'label' => 'Ref', 'enabled' => 1, 'position' => 10, 'notnull' => 1, 'visible' => 4, 'noteditable' => 1, 'default' => '(PROV)', 'index' =>1, 'searchall' => 1, 'showoncombobox' => 1, 'validate' => 1, 'comment' => 'Reference of object'], + 'ref_ext' => ['type' => 'varchar(128)', 'label' => 'RefExt', 'enabled' => 1, 'position' => 20, 'notnull' => 0, 'visible' => 0], + 'entity' => ['type' => 'integer', 'label' => 'Entity', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => 0, 'index' => 1], + 'date_creation' => ['type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'position' => 40, 'notnull' => 1, 'visible' => 2], + 'tms' => ['type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'position' => 50, 'notnull' => 0, 'visible' => 0], + 'import_key' => ['type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'position' => 60, 'notnull' => 0, 'visible' => 0], + 'status' => ['type' => 'smallint', 'label' => 'Status', 'enabled' => 1, 'position' => 160, 'notnull' => 1, 'visible' => 2, 'default' => 0, 'index' => 1, 'arrayofkeyval' => [0 => 'StatusDraft', 1 => 'ValidatePendingAssignment', 10 => 'Assign'], 'css' => 'minwidth100 maxwidth300 widthcentpercentminusxx'], + 'label' => ['type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'position' => 70, 'notnull' => 1, 'visible' => 5, 'searchall' => 1, 'css' => 'minwidth100 maxwidth300 widthcentpercentminusxx', 'cssview' => 'wordbreak', 'showoncombobox' => 2, 'validate' => 1], + 'short_url' => ['type' => 'url', 'label' => 'ShortUrl', 'enabled' => 1, 'position' => 80, 'notnull' => 0, 'visible' => 1, 'copytoclipboard' => 1, 'css' => 'minwidth100 maxwidth300 widthcentpercentminusxx nowrap'], + 'original_url' => ['type' => 'url', 'label' => 'OriginalUrl', 'enabled' => 1, 'position' => 90, 'notnull' => 0, 'visible' => 1, 'copytoclipboard' => 1, 'css' => 'minwidth100 maxwidth300 widthcentpercentminusxx nowrap'], + 'type' => ['type' => 'sellist:c_shortener_url_type:label', 'label' => 'UrlType', 'enabled' => 1, 'position' => 100, 'notnull' => 0, 'visible' => 1, 'css' => 'maxwidth150 widthcentpercentminusxx'], + 'methode' => ['type' => 'select', 'label' => 'UrlMethode', 'enabled' => 1, 'position' => 110, 'notnull' => 0, 'visible' => 5, 'arrayofkeyval' => ['' => '', 'yourls' => 'YOURLS', 'wordpress' => 'WordPress'], 'css' => 'maxwidth200 widthcentpercentminusxx', 'csslist' => 'minwidth150 center', 'help' => 'UrlMethodeDescription'], + 'element_type' => ['type' => 'select', 'label' => 'ElementType', 'enabled' => 1, 'position' => 120, 'notnull' => 0, 'visible' => 1, 'arrayofkeyval' => ['' => ''], 'css' => 'maxwidth150 widthcentpercentminusxx'], + 'fk_element' => ['type' => 'integer', 'label' => 'FkElement', 'enabled' => 1, 'position' => 130, 'notnull' => 0, 'visible' => 1, 'index' => 1, 'css' => 'minwidth200 maxwidth300 widthcentpercentminusxx'], + 'fk_easyurl_urlexport' => ['type' => 'integer', 'label' => 'UrlExport', 'enabled' => 1, 'position' => 140, 'notnull' => 0, 'visible' => 0, 'foreignkey' => 'easyurl_urlexport.rowid'], + 'fk_user_creat' => ['type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'picto' => 'user', 'enabled' => 1, 'position' => 150, 'notnull' => 1, 'visible' => 0, 'foreignkey' => 'user.rowid'], + 'fk_user_modif' => ['type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'picto' => 'user', 'enabled' => 1, 'position' => 160, 'notnull' => 0, 'visible' => 0, 'foreignkey' => 'user.rowid'], ]; /** @@ -201,6 +202,11 @@ class Shortener extends SaturneObject */ public $fk_element; + /** + * @var int|null URL Export + */ + public $fk_easyurl_urlexport; + /** * @var int User ID */ diff --git a/class/urlexport.class.php b/class/urlexport.class.php new file mode 100644 index 0000000..9273455 --- /dev/null +++ b/class/urlexport.class.php @@ -0,0 +1,75 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file class/shortener.class.php + * \ingroup easyurl + * \brief This file is a CRUD class file for Shortener (Create/Read/Update/Delete) + */ + +// Load Saturne libraries +require_once __DIR__ . '/../../saturne/class/saturnedocuments.class.php'; + +/** + * Class for Shortener + */ +class UrlExport extends SaturneDocuments +{ + /** + * @var string Module name + */ + public $module = 'easyurl'; + + /** + * @var string Element type of object + */ + public $element = 'urlexport'; + + /** + * Constructor. + * + * @param DoliDb $db Database handler. + */ + public function __construct(DoliDB $db) + { + parent::__construct($db, $this->module, $this->element); + } + + public function generateFile() + { + + global $conf, $user; + + require_once __DIR__ . '/../class/shortener.class.php'; + + $shorteners = new Shortener($this->db); + $shorteners = $shorteners->fetchAll('', '', 0, 0, ['t.fk_easyurl_urlexport' => $this->id]); + + $upload_dir = $conf->easyurl->multidir_output[$conf->entity] . '/' . $this->element; + dol_mkdir($upload_dir); + $fileName = dol_print_date(dol_now(), 'dayxcard') . '_' . $this->ref . '_exporturl.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 . ';' . $shortener->label . ';' . $shortener->original_url . ';' . $shortener->short_url]); + } + fclose($fp); + $this->last_main_doc = $fileName; + $this->update($user); + } + +} diff --git a/js/easyurl.min.js b/js/easyurl.min.js index db68bdb..c2c986b 100644 --- a/js/easyurl.min.js +++ b/js/easyurl.min.js @@ -1 +1 @@ -"use strict";window.easyurl||(window.easyurl={},window.easyurl.scriptsLoaded=!1),window.easyurl.scriptsLoaded||(window.easyurl.init=function(){window.easyurl.load_list_script()},window.easyurl.load_list_script=function(){if(!window.easyurl.scriptsLoaded){let e=void 0,o=void 0;for(e in window.easyurl)for(o in window.easyurl[e].init&&window.easyurl[e].init(),window.easyurl[e])window.easyurl[e]&&window.easyurl[e][o]&&window.easyurl[e][o].init&&window.easyurl[e][o].init();window.easyurl.scriptsLoaded=!0}},window.easyurl.refresh=function(){let e=void 0,o=void 0;for(e in window.easyurl)for(o in window.easyurl[e].refresh&&window.easyurl[e].refresh(),window.easyurl[e])window.easyurl[e]&&window.easyurl[e][o]&&window.easyurl[e][o].refresh&&window.easyurl[e][o].refresh()},$(document).ready(window.easyurl.init)),window.easyurl.shortener={},window.easyurl.shortener.init=function(){window.easyurl.shortener.event()},window.easyurl.shortener.event=function(){$(document).on("click",".toggleObjectInfo",window.easyurl.shortener.toggleObjectInfo),$(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)},window.easyurl.shortener.toggleObjectInfo=function(){$(this).hasClass("fa-minus-square")?($(this).removeClass("fa-minus-square").addClass("fa-caret-square-down"),$(this).closest(".fiche").find(".ObjectInfo tbody").hide()):($(this).removeClass("fa-caret-square-down").addClass("fa-minus-square"),$(this).closest(".fiche").find(".ObjectInfo tbody").show())},window.easyurl.shortener.showQRCode=function(){var e=window.saturne.toolbox.getQuerySeparator(document.URL),o=window.saturne.toolbox.getToken();let n;n=$(this).hasClass("fa-toggle-off")?1:0,window.saturne.loader.display($(this)),$.ajax({url:document.URL+e+"action=show_qrcode&token="+o,type:"POST",processData:!1,data:JSON.stringify({showQRCode:n}),contentType:!1,success:function(e){window.location.reload()},error:function(){}})},window.easyurl.shortener.reloadAssignShortenerView=function(){var e=$(this).val(),o=window.saturne.toolbox.getToken(),n=window.saturne.toolbox.getQuerySeparator(document.URL);window.saturne.loader.display($(".assign-form")),$.ajax({url:document.URL+n+"fromid="+e+"&token="+o,type:"POST",processData:!1,contentType:!1,success:function(e){$(".assign-form").replaceWith($(e).find(".assign-form"))},error:function(){}})},window.easyurl.shortener.assignShortener=function(){var e=window.saturne.toolbox.getToken(),o=window.saturne.toolbox.getQuerySeparator(document.URL);$.ajax({url:document.URL+o+"view&token="+e,type:"POST",processData:!1,contentType:!1,success:function(){window.parent.jQuery("#idfordialogassignShortener").dialog("close")},error:function(){}})}; \ No newline at end of file +window.easyurl||(window.easyurl={},window.easyurl.scriptsLoaded=!1),window.easyurl.scriptsLoaded||(window.easyurl.init=function(){window.easyurl.load_list_script()},window.easyurl.load_list_script=function(){if(!window.easyurl.scriptsLoaded){let e=void 0,o=void 0;for(e in window.easyurl)for(o in window.easyurl[e].init&&window.easyurl[e].init(),window.easyurl[e])window.easyurl[e]&&window.easyurl[e][o]&&window.easyurl[e][o].init&&window.easyurl[e][o].init();window.easyurl.scriptsLoaded=!0}},window.easyurl.refresh=function(){let e=void 0,o=void 0;for(e in window.easyurl)for(o in window.easyurl[e].refresh&&window.easyurl[e].refresh(),window.easyurl[e])window.easyurl[e]&&window.easyurl[e][o]&&window.easyurl[e][o].refresh&&window.easyurl[e][o].refresh()},$(document).ready(window.easyurl.init)),window.easyurl.shortener={},window.easyurl.shortener.init=function(){window.easyurl.shortener.event()},window.easyurl.shortener.event=function(){$(document).on("click",".toggleObjectInfo",window.easyurl.shortener.toggleObjectInfo),$(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)},window.easyurl.shortener.toggleObjectInfo=function(){$(this).hasClass("fa-minus-square")?($(this).removeClass("fa-minus-square").addClass("fa-caret-square-down"),$(this).closest(".fiche").find(".ObjectInfo tbody").hide()):($(this).removeClass("fa-caret-square-down").addClass("fa-minus-square"),$(this).closest(".fiche").find(".ObjectInfo tbody").show())},window.easyurl.shortener.showQRCode=function(){var e=window.saturne.toolbox.getQuerySeparator(document.URL),o=window.saturne.toolbox.getToken();let r;r=$(this).hasClass("fa-toggle-off")?1:0,window.saturne.loader.display($(this)),$.ajax({url:document.URL+e+"action=show_qrcode&token="+o,type:"POST",processData:!1,data:JSON.stringify({showQRCode:r}),contentType:!1,success:function(e){window.location.reload()},error:function(){}})},window.easyurl.shortener.reloadAssignShortenerView=function(){var e=$(this).val(),o=window.saturne.toolbox.getToken(),r=window.saturne.toolbox.getQuerySeparator(document.URL);window.saturne.loader.display($(".assign-form")),$.ajax({url:document.URL+r+"fromid="+e+"&token="+o,type:"POST",processData:!1,contentType:!1,success:function(e){$(".assign-form").replaceWith($(e).find(".assign-form"))},error:function(){}})},window.easyurl.shortener.assignShortener=function(){var e=window.saturne.toolbox.getToken(),o=window.saturne.toolbox.getQuerySeparator(document.URL);$.ajax({url:document.URL+o+"view&token="+e,type:"POST",processData:!1,contentType:!1,success:function(){window.parent.jQuery("#idfordialogassignShortener").dialog("close")},error:function(){}})},window.easyurl.urlexport={},window.easyurl.urlexport.init=function(){window.easyurl.urlexport.event()},window.easyurl.urlexport.event=function(){$(".button-save","#generate-url-from").on("click",window.easyurl.urlexport.saveButton)},window.easyurl.urlexport.saveButton=function(e){e.preventDefault(),window.easyurl.urlexport.hideNotice();let o=$("#generate-url-from").serializeArray().find(e=>"nb_url"==e.name);$.ajax({method:"POST",url:"",data:$("#generate-url-from").serialize(),success:e=>{(new Date).toLocaleString("en-GB",{day:"2-digit",month:"2-digit",year:"numeric",hour:"2-digit",minute:"2-digit",hour12:!1}).replace(",","");$(''+e.ref+'---'+e.date+'-0
').insertAfter($("tr:nth-child(1)",".tab-export")),$(".button-save","#generate-url-from").addClass("wpeo-loader"),window.easyurl.urlexport.createUrls(e.data,0,parseInt(o.value))},error:function(e,o,r){window.easyurl.urlexport.displayNotice(e.responseJSON.title,e.responseJSON.message,"error"),$(".button-save","#generate-url-from").removeClass("wpeo-loader")}})},window.easyurl.urlexport.createUrls=function(o,r,t){r!=t?$.ajax({method:"POST",url:"",data:$("#generate-url-from").serialize()+"&export_url_id="+o,success:e=>{$("tr:nth-child(2)",".tab-export").find(".tab-count").html(r+1),0==r&&($("tr:nth-child(2)",".tab-export").find(".tab-first").html(e.data),$("tr:nth-child(2)",".tab-export").find(".tab-url").html(''+e.url+"")),$("tr:nth-child(2)",".tab-export").find(".tab-end").html(e.data),window.easyurl.urlexport.createUrls(o,r+1,t)},error:function(e,o,r){window.easyurl.urlexport.displayNotice(e.responseJSON.title,e.responseJSON.message,"error"),$("tr:nth-child(2)",".tab-export").remove(),$(".button-save","#generate-url-from").removeClass("wpeo-loader")}}):$.ajax({method:"POST",url:"",data:$("#generate-url-from").serialize()+"&export_url_id="+o+"&export_file=1",success:e=>{window.easyurl.urlexport.displayNotice(e.title,e.message,"success"),$("tr:nth-child(2)",".tab-export").find(".tab-download").html(e.download),$(".button-save","#generate-url-from").removeClass("wpeo-loader"),window.open(e.redirect,"_blank").focus()},error:function(e,o,r){window.easyurl.urlexport.displayNotice(e.responseJSON.title,e.responseJSON.message,"error"),$("tr:nth-child(2)",".tab-export").remove(),$(".button-save","#generate-url-from").removeClass("wpeo-loader")}})},window.easyurl.urlexport.displayNotice=function(e,o,r){var t=$(".global-infos");t.removeClass("notice-error"),t.removeClass("notice-info"),t.removeClass("notice-success"),t.removeClass("hidden"),t.addClass("notice-"+r),$("span",t).remove(),t.find(".notice-title").append(""+e+""),t.find(".notice-content").append(""+o+""),t.show()},window.easyurl.urlexport.hideNotice=function(){$(".global-infos").hide()}; \ No newline at end of file diff --git a/js/modules/urlexport.js b/js/modules/urlexport.js new file mode 100644 index 0000000..261496d --- /dev/null +++ b/js/modules/urlexport.js @@ -0,0 +1,197 @@ +/* Copyright (C) 2021-2024 EVARISK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Library javascript to enable Browser notifications + */ + +/** + * \file js/urlexport.js + * \ingroup easyurl + * \brief JavaScript shortener file for module EasyURL + */ + +/** + * Init UrlExport JS + * + * @memberof EasyURL_UrlExport + * + * @since 1.1.0 + * @version 1.1.0 + * + * @type {Object} + */ +window.easyurl.urlexport = {}; + +/** + * UrlExport init + * + * @memberof EasyURL_UrlExport + * + * @since 1.1.0 + * @version 1.1.0 + * + * @returns {void} + */ +window.easyurl.urlexport.init = function() { + window.easyurl.urlexport.event(); +}; + +/** + * UrlExport event + * + * @memberof EasyURL_UrlExport + * + * @since 1.1.0 + * @version 1.1.0 + * + * @returns {void} + */ +window.easyurl.urlexport.event = function() { + $('.button-save', '#generate-url-from').on('click', window.easyurl.urlexport.saveButton); +}; + +/** + * UrlExport Save Button action + * + * @memberof EasyURL_UrlExport + * + * @since 1.1.0 + * @version 1.1.0 + * + * @returns {void} + */ +window.easyurl.urlexport.saveButton = function(e) { + e.preventDefault(); + window.easyurl.urlexport.hideNotice(); + + let nbUrls = $('#generate-url-from').serializeArray().find((e) => e.name == 'nb_url') + + $.ajax({ + method: 'POST', + url: '', + data: $('#generate-url-from').serialize(), + success: (data) => { + let date = new Date().toLocaleString('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit', hour12: false }).replace(',', ''); + $('' + + '' + data.ref + '' + + '-' + + '-' + + '-' + + '' + data.date + '' + + '-' + + '0' + + '
' + + '').insertAfter($('tr:nth-child(1)', '.tab-export')); + $('.button-save', '#generate-url-from').addClass('wpeo-loader'); + window.easyurl.urlexport.createUrls(data.data, 0, parseInt(nbUrls.value)); + }, + error: function (r, s, e) { + window.easyurl.urlexport.displayNotice(r.responseJSON.title, r.responseJSON.message, 'error'); + $('.button-save', '#generate-url-from').removeClass('wpeo-loader'); + } + }) +} + +/** + * UrlExport Create shortlink url and export + * + * @memberof EasyURL_UrlExport + * + * @since 1.1.0 + * @version 1.1.0 + * + * @returns {void} + */ +window.easyurl.urlexport.createUrls = function (id, number, total) { + if (number != total) { + $.ajax({ + method: 'POST', + url: '', + data: $('#generate-url-from').serialize() + '&export_url_id='+id, + success: (data) => { + $('tr:nth-child(2)', '.tab-export').find('.tab-count').html(number + 1); + if (number == 0) { + $('tr:nth-child(2)', '.tab-export').find('.tab-first').html(data.data); + $('tr:nth-child(2)', '.tab-export').find('.tab-url').html('' + data.url + ''); + } + $('tr:nth-child(2)', '.tab-export').find('.tab-end').html(data.data); + window.easyurl.urlexport.createUrls(id, number + 1, total) + }, + error: function (r, s, e) { + window.easyurl.urlexport.displayNotice(r.responseJSON.title, r.responseJSON.message, 'error'); + $('tr:nth-child(2)', '.tab-export').remove(); + $('.button-save', '#generate-url-from').removeClass('wpeo-loader'); + } + }) + } else { + $.ajax({ + method: 'POST', + url: '', + data: $('#generate-url-from').serialize() + '&export_url_id='+id + "&export_file=1", + success: (e) => { + window.easyurl.urlexport.displayNotice(e.title, e.message, 'success'); + $('tr:nth-child(2)', '.tab-export').find('.tab-download').html(e.download); + $('.button-save', '#generate-url-from').removeClass('wpeo-loader'); + window.open(e.redirect, '_blank').focus(); + }, + error: function (r, s, e) { + window.easyurl.urlexport.displayNotice(r.responseJSON.title, r.responseJSON.message, 'error'); + $('tr:nth-child(2)', '.tab-export').remove(); + $('.button-save', '#generate-url-from').removeClass('wpeo-loader'); + } + }) + } +} + +/** + * UrlExport Display notice + * + * @memberof EasyURL_UrlExport + * + * @since 1.1.0 + * @version 1.1.0 + * + * @returns {void} + */ +window.easyurl.urlexport.displayNotice = function (title, message, type) { + let notice = $('.global-infos'); + + notice.removeClass('notice-error'); + notice.removeClass('notice-info'); + notice.removeClass('notice-success'); + notice.removeClass('hidden'); + notice.addClass('notice-' + type); + + $('span', notice).remove(); + + notice.find('.notice-title').append('' + title + ''); + notice.find('.notice-content').append('' + message + ''); + + notice.show(); +} + +/** + * UrlExport Hide notice + * + * @memberof EasyURL_UrlExport + * + * @since 1.1.0 + * @version 1.1.0 + * + * @returns {void} + */ +window.easyurl.urlexport.hideNotice = function () { + $('.global-infos').hide(); +} diff --git a/langs/fr_FR/easyurl.lang b/langs/fr_FR/easyurl.lang index ad9cad9..df774ad 100644 --- a/langs/fr_FR/easyurl.lang +++ b/langs/fr_FR/easyurl.lang @@ -68,6 +68,7 @@ GetEasyURLErrors = Erreur : URL raccourcie non trouvée # Data - Donnée GenerateUrlManagement = Gestion de la génération des raccourcissements d'URLs +GeneratedExport = Export déjà générés NbUrl = Nombre d'URL NbUrlDescription = Cette option permet de choisir le nombre d'URL à générée OriginalUrl = URL d'origine @@ -79,8 +80,16 @@ UrlMethode = Méthode d'API URL UrlMethodeDescription = Cette option permet de choisir la méthode d'API URL utilisée dans le raccourcissement des URLs
(deux valeurs possibles : YOURLS ou WordPress) GenerateUrlSuccess = Génération réussie des %d raccourcissements d'URLs OriginalUrlFail = Erreur(s) possible(s) :
- Nombre d'URL est vide
- l'URL d'origine est vide
- l'URL d'origine par défaut n'as pas été configurée +ExportSuccess = L'export a été rélisé avec succès ! DefaultOriginalUrlConfiguration = Vous n'avez pas configuré d'URL d'origine par défaut, veuillez cliquer ici pour vous rendre sur la page de configuration - +Success = Succès +ExportId = Id Export +ExportNumber = Nombre d'URL générées +ExportStart = ID début URL +ExportEnd = ID fin URL +ExportDate = Date export +ExportOrigin = URL d'origine +ExportConsume = Nombre d'URL consommé # # Shortener - Raccourcissement d'URL diff --git a/sql/update.sql b/sql/update.sql new file mode 100644 index 0000000..102dd9e --- /dev/null +++ b/sql/update.sql @@ -0,0 +1,18 @@ +-- Copyright (C) 2022-2024 EVARISK +-- +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see https://www.gnu.org/licenses/. + +-- 1.1.0 + +ALTER TABLE `llx_easyurl_shortener` ADD `fk_easyurl_urlexport` int DEFAULT NULL AFTER `fk_element`; diff --git a/view/easyurltools.php b/view/easyurltools.php index 3970085..561ba86 100644 --- a/view/easyurltools.php +++ b/view/easyurltools.php @@ -32,6 +32,7 @@ // Load EasyURL libraries require_once __DIR__ . '/../class/shortener.class.php'; +require_once __DIR__ . '/../class/urlexport.class.php'; require_once __DIR__ . '/../lib/easyurl_function.lib.php'; // Global variables definitions @@ -56,39 +57,95 @@ */ if ($action == 'generate_url' && $permissionToAdd) { + + header('Content-type: application/json'); + $error = 0; $urlMethode = GETPOST('url_methode'); $NbUrl = GETPOST('nb_url'); $originalUrl = GETPOST('original_url'); $urlParameters = GETPOST('url_parameters'); - if ((dol_strlen($originalUrl) > 0 || dol_strlen(getDolGlobalString('EASYURL_DEFAULT_ORIGINAL_URL')) > 0) && $NbUrl > 0) { - for ($i = 1; $i <= $NbUrl; $i++) { - $shortener = new Shortener($db); - $shortener->ref = $shortener->getNextNumRef(); - if (dol_strlen($originalUrl) > 0) { - $shortener->original_url = $originalUrl . $urlParameters; + $exportUrlId = GETPOST('export_url_id'); + $exportFile = GETPOST('export_file'); + + if ($exportUrlId == '') { + $exportUrl = new UrlExport($db); + $exportUrl->original_url = $originalUrl . $urlParameters; + + if ($exportUrl->create($user) == -1) { + http_response_code(500); + print json_encode(['message' => 'Error during exporting url parameters']); + exit; + } else { + http_response_code(201); + print json_encode(['message' => 'Ok', 'data' => $exportUrl->id, 'date' => dol_print_date($exportUrl->date_creation, 'dayhour'), 'ref' => $exportUrl->ref]); + exit; + } + } else { + if ($exportFile == '') { + if ((dol_strlen($originalUrl) > 0 || dol_strlen(getDolGlobalString('EASYURL_DEFAULT_ORIGINAL_URL')) > 0) && $NbUrl > 0) { + $shortener = new Shortener($db); + $shortener->ref = $shortener->getNextNumRef(); + $shortener->fk_easyurl_urlexport = $exportUrlId; + + if (dol_strlen($originalUrl) > 0) { + $shortener->original_url = $originalUrl . $urlParameters; + } else { + $shortener->original_url = getDolGlobalString('EASYURL_DEFAULT_ORIGINAL_URL') . $urlParameters; + } + $shortener->methode = $urlMethode; + + $shortener->create($user); + + $result = set_easy_url_link($shortener, 'none', $urlMethode); + if (!empty($result) && is_object($result)) { + http_response_code(500); + print json_encode(['message' => $result->message, 'title' => $langs->trans("Error")]); + exit; + } else { + http_response_code(201); + print json_encode(['message' => 'Ok', 'data' => $shortener->id, 'url' => $shortener->original_url]); + exit; + } } else { - $shortener->original_url = getDolGlobalString('EASYURL_DEFAULT_ORIGINAL_URL') . $urlParameters; + http_response_code(400); + print json_encode(['message' => $langs->trans('OriginalUrlFail'), 'title' => $langs->trans("Error")]); + exit; } - $shortener->methode = $urlMethode; + } else { + $exportUrl = new UrlExport($db); + $exportUrl = $exportUrl->fetchAll('', '', 0, 0, ['rowId' => $exportUrlId]); - $shortener->create($user); + if (count($exportUrl) != -1) { + if (empty($exportUrl)) { + http_response_code(404); + print json_encode(['message' => 'Error during exporting url parameters', 'title' => $langs->trans("Error")]); + exit; + } else { + $exportUrl = current($exportUrl); + $exportUrl->generateFile(); - // UrlType : none because we want mass generation url (all can be use but need to change this code) - $result = set_easy_url_link($shortener, 'none', $urlMethode); - if (!empty($result) && is_object($result)) { - setEventMessage($result->message, 'errors'); - $error++; + $uploadDir = $conf->easyurl->multidir_output[$conf->entity ?? 1]; + $fileDir = $uploadDir . '/' . $exportUrl->element; + if (dol_is_file($fileDir . '/' . $exportUrl->last_main_doc)) { + $documentUrl = DOL_URL_ROOT . '/document.php'; + $fileUrl = $documentUrl . '?modulepart=easyurl&file=' . urlencode($exportUrl->element . '/' . $exportUrl->last_main_doc); + http_response_code(201); + print json_encode(['message' => $langs->trans('ExportSuccess'), 'title' => $langs->trans("Success"), 'download' => '', 'redirect' => dol_buildpath('/custom/easyurl/view/shortener/shortener_list.php', 1)]); + exit; + } else { + http_response_code(500); + print json_encode(['message' => 'Error during exporting url parameters', 'title' => $langs->trans("Error")]); + exit; + } + } + } else { + http_response_code(500); + print json_encode(['message' => 'Error during exporting url parameters', 'title' => $langs->trans("Error")]); + exit; } } - if ($error == 0) { - setEventMessage($langs->trans('GenerateUrlSuccess', $i - 1)); - } - } else { - setEventMessage($langs->trans('OriginalUrlFail'), 'errors'); } - header('Location: ' . $_SERVER['PHP_SELF']); - exit; } /* @@ -112,6 +169,15 @@ +
+
+
+
+ +'; + print load_fiche_titre($langs->trans('GenerateUrlManagement'), '', ''); print '
'; @@ -150,9 +216,50 @@ print ''; print ''; -print $form->buttonsSaveCancel('Generate', ''); +print '
'; +print $form->buttonsSaveCancel('Generate', '', [], true); +print '
'; print '
'; +print load_fiche_titre($langs->trans('GeneratedExport'), '', ''); +print ''; + +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +$urlExport = new UrlExport($db); +$urlExport = $urlExport->fetchAll('DESC', 'rowid'); + +foreach ($urlExport as $row) { + $shortener = new Shortener($db); + $shortener = $shortener->fetchAll('', '', 0, 0, ['t.fk_easyurl_urlexport' => $row->id]); + + $uploadDir = $conf->easyurl->multidir_output[$conf->entity ?? 1]; + $fileDir = $uploadDir . '/' . $row->element; + if (dol_is_file($fileDir . '/' . $row->last_main_doc)) { + $documentUrl = DOL_URL_ROOT . '/document.php'; + $fileUrl = $documentUrl . '?modulepart=easyurl&file=' . urlencode($row->element . '/' . $row->last_main_doc); + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } +} +print '
' . $langs->trans('ExportId') . '' . $langs->trans('ExportNumber') . '' . $langs->trans('ExportStart') . '' . $langs->trans('ExportEnd') . '' . $langs->trans('ExportDate') . '' . $langs->trans('ExportOrigin') . '' . $langs->trans('ExportConsume') . '
' . $row->ref . '' . count($shortener) . '' . current($shortener)->id . '' . end($shortener)->id . '' . dol_print_date($row->date_creation, 'dayhour') . '' . current($shortener)->original_url . '' . count(array_filter($shortener, function($elem) {return $elem->status == 0;})) . '
'; + // End of page llxFooter(); $db->close();