diff --git a/class/actions_tinyurl.class.php b/class/actions_tinyurl.class.php index fb2c68a..b796d90 100644 --- a/class/actions_tinyurl.class.php +++ b/class/actions_tinyurl.class.php @@ -21,6 +21,9 @@ * \brief TinyURL hook overload */ +// Load TinyURL libraries +require_once __DIR__ . '/../lib/tinyurl_function.lib.php'; + /** * Class ActionsTinyurl */ @@ -74,14 +77,18 @@ public function printCommonFooter(array $parameters): int if (in_array($parameters['currentcontext'], ['propalcard', 'ordercard', 'invoicecard'])) { if ($object->status > $object::STATUS_DRAFT) { - $jQueryElement = '.' . $object->element . '_extras_tiny_url_link'; - $output = ''; - $output .= img_picto($langs->trans('SetTinyURLLink'), 'fontawesome_fa-redo_fas_#444', 'class="paddingright pictofixedwidth valignmiddle"') . ''; ?> - - element . '_extras_tiny_url_link'; + $output = ''; + $output .= img_picto($langs->trans('SetTinyURLLink'), 'fontawesome_fa-redo_fas_#444', 'class="paddingright pictofixedwidth valignmiddle"') . ''; + $output .= '' . img_picto($langs->trans('GetTinyURLErrors'), 'fontawesome_fa-exclamation-triangle_fas_#bc9526') . ''; ?> + + id); diff --git a/langs/fr_FR/tinyurl.lang b/langs/fr_FR/tinyurl.lang index 8834879..1f114a2 100644 --- a/langs/fr_FR/tinyurl.lang +++ b/langs/fr_FR/tinyurl.lang @@ -46,3 +46,5 @@ TinyUrlLinkHelp = Transformation d'une URL en short URL (Lien de paiement) SetTinyURLLink = Créer short URL SetTinyURLSuccess = Création réussie d'une short URL SetTinyURLErrors = Erreur : création d'une short URL +GetTinyURLErrors = Erreur : short URL non trouvée + diff --git a/lib/tinyurl_function.lib.php b/lib/tinyurl_function.lib.php index fdabb86..f1744d5 100644 --- a/lib/tinyurl_function.lib.php +++ b/lib/tinyurl_function.lib.php @@ -37,9 +37,20 @@ function set_tiny_url_link(CommonObject $object) { require_once DOL_DOCUMENT_ROOT . '/core/lib/ticket.lib.php'; $object->fetch($object->id); - $onlinePaymentURL = getOnlinePaymentUrl(0, 'invoice', $object->ref); + switch ($object->element) { + case 'commande' : + $type = 'order'; + break; + case 'facture' : + $type = 'invoice'; + break; + default : + $type = $object->element; + break; + } + $onlinePaymentURL = getOnlinePaymentUrl(0, $type, $object->ref); - $title = dol_sanitizeFileName(strtolower($conf->global->MAIN_INFO_SOCIETE_NOM) . '-' . strtolower($object->ref) . (getDolGlobalInt('TINYURL_USE_SHA_URL') ? '-' . generate_random_id(8) : '')); + $title = dol_sanitizeFileName(dol_strtolower($conf->global->MAIN_INFO_SOCIETE_NOM) . '-' . strtolower($object->ref) . (getDolGlobalInt('TINYURL_USE_SHA_URL') ? '-' . generate_random_id(8) : '')); // Init the CURL session $ch = curl_init(); @@ -63,12 +74,52 @@ function set_tiny_url_link(CommonObject $object) { // Do something with the result $data = json_decode($data); - $object->array_options['options_tiny_url_link'] = $data->shorturl; - $object->update($user, false); if ($data->status == 'success') { + $object->array_options['options_tiny_url_link'] = $data->shorturl; + $object->update($user, false); setEventMessage($langs->trans('SetTinyURLSuccess')); } else { setEventMessage($langs->trans('SetTinyURLErrors'), 'errors'); } } } + +/** + * get tiny url link + * + * @param CommonObject $object Object + * @return int 0 < on error, 1 = statusCode 200, 0 = other statusCode (ex : 404) + */ +function get_tiny_url_link(CommonObject $object) { + global $conf; + + $useOnlinePayment = (isModEnabled('paypal') || isModEnabled('stripe') || isModEnabled('paybox')); + $checkConf = getDolGlobalString('TINYURL_URL_YOURLS_API') && getDolGlobalString('TINYURL_SIGNATURE_TOKEN_YOURLS_API'); + if ($useOnlinePayment && $checkConf) { + $object->fetch($object->id); + + // Init the CURL session + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $conf->global->TINYURL_URL_YOURLS_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 + curl_setopt($ch, CURLOPT_POSTFIELDS, [ // Data to POST + 'action' => 'url-stats', + 'signature' => $conf->global->TINYURL_SIGNATURE_TOKEN_YOURLS_API, + 'format' => 'json', + 'shorturl' => $object->array_options['options_tiny_url_link'] + ]); + + // Fetch and return content + $data = curl_exec($ch); + curl_close($ch); + + // Do something with the result + $data = json_decode($data); + return $data->statusCode == 200 ? 1 : 0; + } else { + return -1; + } +}