From a249450e9a99fe0bf3c5cb1fc4ca5955df2c7361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20David?= Date: Tue, 16 Jul 2024 17:55:20 +0200 Subject: [PATCH] #65 [Tools] add: saturne redirections interaction --- lib/easyurl_function.lib.php | 114 +++++++++++++++++++---------------- view/easyurltools.php | 2 +- 2 files changed, 64 insertions(+), 52 deletions(-) diff --git a/lib/easyurl_function.lib.php b/lib/easyurl_function.lib.php index 6138e67..db64fa1 100644 --- a/lib/easyurl_function.lib.php +++ b/lib/easyurl_function.lib.php @@ -31,16 +31,21 @@ */ function set_easy_url_link(CommonObject $object, string $urlType, string $urlMethod = 'yourls') { - global $conf, $langs, $user; + global $conf, $db, $langs, $user; $useOnlinePayment = (isModEnabled('paypal') || isModEnabled('stripe') || isModEnabled('paybox')); $checkConf = getDolGlobalString('EASYURL_URL_' . dol_strtoupper($urlMethod) . '_API') && getDolGlobalString('EASYURL_SIGNATURE_TOKEN_' . dol_strtoupper($urlMethod) . '_API'); + if ($urlMethod == 'dolibarr') { + $checkConf = true; + } if ((($urlType == 'payment' && $useOnlinePayment) || $urlType == 'signature' || $urlType == 'none') && $checkConf) { // Load Dolibarr libraries require_once DOL_DOCUMENT_ROOT . '/core/lib/payments.lib.php'; require_once DOL_DOCUMENT_ROOT . '/core/lib/signature.lib.php'; require_once DOL_DOCUMENT_ROOT . '/core/lib/ticket.lib.php'; + require_once __DIR__ . '/../../saturne/class/saturneredirection.class.php'; + $object->fetch($object->id); switch ($object->element) { case 'propal' : @@ -74,67 +79,74 @@ function set_easy_url_link(CommonObject $object, string $urlType, string $urlMet } break; } - $title = dol_sanitizeFileName(dol_strtolower($conf->global->MAIN_INFO_SOCIETE_NOM . '-' . $object->ref) . (getDolGlobalInt('EASYURL_USE_SHA_URL') ? '-' . generate_random_id(8) : '')); - // Init the CURL session - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, getDolGlobalString('EASYURL_URL_' . dol_strtoupper($urlMethod) . '_API')); - curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request - switch ($urlMethod) { - case 'yourls' : - curl_setopt($ch, CURLOPT_POSTFIELDS, [ // Data to POST - 'action' => 'shorturl', - 'signature' => getDolGlobalString('EASYURL_SIGNATURE_TOKEN_YOURLS_API'), - 'format' => 'json', - 'title' => $title, - 'keyword' => $title, - 'url' => $onlineUrl - ]); - break; - case 'wordpress' : - break; - } + if ($urlMethod == 'dolibarr') { + $saturneRedirection = new SaturneRedirection($db); + $saturneRedirection->from_url = '/' . $title; + $saturneRedirection->to_url = $onlineUrl; + $saturneRedirection->create($user); + } else { + // Init the CURL session + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, getDolGlobalString('EASYURL_URL_' . dol_strtoupper($urlMethod) . '_API')); + curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request + switch ($urlMethod) { + case 'yourls' : + curl_setopt($ch, CURLOPT_POSTFIELDS, [ // Data to POST + 'action' => 'shorturl', + 'signature' => getDolGlobalString('EASYURL_SIGNATURE_TOKEN_YOURLS_API'), + 'format' => 'json', + 'title' => $title, + 'keyword' => $title, + 'url' => $onlineUrl + ]); + break; + case 'wordpress' : + break; + } - // Fetch and return content - $data = curl_exec($ch); - curl_close($ch); + // Fetch and return content + $data = curl_exec($ch); + curl_close($ch); - // Do something with the result - $data = json_decode($data); + // Do something with the result + $data = json_decode($data); - if ($data->status == 'success') { - if ($urlType != 'none') { - $object->array_options['options_easy_url_' . $urlType . '_link'] = $data->shorturl; - $object->updateExtraField('easy_url_' . $urlType . '_link'); - setEventMessage($langs->trans('SetEasyURLSuccess')); - } else { - // Shortener object in 100% of cases - $object->status = $object::STATUS_VALIDATED; - $object->label = $title; - $object->short_url = $data->shorturl; - $object->original_url = $onlineUrl; - $object->update($user, true); + if ($data->status == 'success') { + if ($urlType != 'none') { + $object->array_options['options_easy_url_' . $urlType . '_link'] = $data->shorturl; + $object->updateExtraField('easy_url_' . $urlType . '_link'); + setEventMessage($langs->trans('SetEasyURLSuccess')); + } else { + // Shortener object in 100% of cases + $object->status = $object::STATUS_VALIDATED; + $object->label = $title; + $object->short_url = $data->shorturl; + $object->original_url = $onlineUrl; + $object->update($user, true); - require_once TCPDF_PATH . 'tcpdf_barcodes_2d.php'; + require_once TCPDF_PATH . 'tcpdf_barcodes_2d.php'; - $barcode = new TCPDF2DBarcode($object->short_url, 'QRCODE,L'); + $barcode = new TCPDF2DBarcode($object->short_url, 'QRCODE,L'); - dol_mkdir($conf->easyurl->multidir_output[$conf->entity] . '/shortener/' . $object->ref . '/qrcode/'); - $file = $conf->easyurl->multidir_output[$conf->entity] . '/shortener/' . $object->ref . '/qrcode/' . 'barcode_' . $object->ref . '.png'; + dol_mkdir($conf->easyurl->multidir_output[$conf->entity] . '/shortener/' . $object->ref . '/qrcode/'); + $file = $conf->easyurl->multidir_output[$conf->entity] . '/shortener/' . $object->ref . '/qrcode/' . 'barcode_' . $object->ref . '.png'; - $imageData = $barcode->getBarcodePngData(); - $imageData = imagecreatefromstring($imageData); - imagepng($imageData, $file); + $imageData = $barcode->getBarcodePngData(); + $imageData = imagecreatefromstring($imageData); + imagepng($imageData, $file); + } + return 1; + } else { + setEventMessage($langs->trans('SetEasyURLErrors'), 'errors'); + return $data; } - return 1; - } else { - setEventMessage($langs->trans('SetEasyURLErrors'), 'errors'); - return $data; } + } } diff --git a/view/easyurltools.php b/view/easyurltools.php index 3970085..48f8735 100644 --- a/view/easyurltools.php +++ b/view/easyurltools.php @@ -125,7 +125,7 @@ print '' . $langs->trans('Value') . ''; print ''; -$urlMethode = ['yourls' => 'YOURLS', 'wordpress' => 'WordPress']; +$urlMethode = ['yourls' => 'YOURLS', 'wordpress' => 'WordPress', 'dolibarr' => 'Dolibarr']; print ''; print $langs->trans('UrlMethode'); print '';