From 9ac8403965f71ccd049211b33bffb821d70a9bc2 Mon Sep 17 00:00:00 2001 From: Nicolas Domenech Date: Wed, 19 Feb 2025 09:20:37 +0100 Subject: [PATCH 1/2] #41 [Shortener] add: rework extrafields + lib --- admin/setup.php | 53 ++---- class/actions_easyurl.class.php | 60 +----- class/shortener.class.php | 2 +- core/modules/modEasyURL.class.php | 88 ++++++--- core/substitutions/functions_easyurl.lib.php | 26 ++- ...ce_99_modEasyurl_EasyurlTriggers.class.php | 27 ++- langs/fr_FR/easyurl.lang | 3 +- lib/easyurl_function.lib.php | 175 ++++++++++-------- view/easyurltools.php | 2 +- 9 files changed, 202 insertions(+), 234 deletions(-) diff --git a/admin/setup.php b/admin/setup.php index 321754f..0e0c91e 100644 --- a/admin/setup.php +++ b/admin/setup.php @@ -35,6 +35,7 @@ // Load EasyURL libraries require_once __DIR__ . '/../lib/easyurl.lib.php'; +require_once __DIR__ . '/../lib/easyurl_function.lib.php'; // Global variables definitions global $conf, $db, $langs, $user; @@ -58,45 +59,23 @@ */ if ($action == 'set_config') { - $URLYourlsAPI = GETPOST('url_yourls_api'); - $signatureTokenYourlsAPI = GETPOST('signature_token_yourls_api'); - $defaultOriginalURL = GETPOST('default_original_url'); - - if (dol_strlen($URLYourlsAPI) > 0) { - dolibarr_set_const($db, 'EASYURL_URL_YOURLS_API', $URLYourlsAPI, 'chaine', 0, '', $conf->entity); - } - if (dol_strlen($signatureTokenYourlsAPI) > 0) { - dolibarr_set_const($db, 'EASYURL_SIGNATURE_TOKEN_YOURLS_API', $signatureTokenYourlsAPI, 'chaine', 0, '', $conf->entity); - } - - if (dol_strlen($URLYourlsAPI) > 0 && dol_strlen($signatureTokenYourlsAPI) > 0) { - - // Init the CURL session - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, GETPOST('url_yourls_api')); - curl_setopt($ch, CURLOPT_HEADER, 0); - 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 - 'signature' => getDolGlobalString('EASYURL_SIGNATURE_TOKEN_YOURLS_API'), - 'format' => 'json' - ]); - - $data = curl_exec($ch); - $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - - // 400 when no actions set and credentials ok - if ($statusCode != 400) { - setEventMessage('WarningApiCredentialsIncorrect', 'warnings'); + $APISettings = ['url_yourls_api', 'signature_token_yourls_api', 'default_original_url']; + foreach ($APISettings as $APISetting) { + $APISettingValue = GETPOST($APISetting); + $confName = 'EASYURL_' . dol_strtoupper($APISetting); + if (dol_strlen($APISettingValue) > 0) { + dolibarr_set_const($db, $confName, $APISettingValue, 'chaine', 0, '', $conf->entity); } } - dolibarr_set_const($db, 'EASYURL_DEFAULT_ORIGINAL_URL', $defaultOriginalURL, 'chaine', 0, '', $conf->entity); + $curlPostFields = ['action' => 'version']; + $data = init_easy_url_curl($curlPostFields); + if (empty($data)) { + setEventMessages('ErrorEasyURLAPIConfig', [], 'errors'); + } else { + setEventMessages('SavedConfig', []); + } - setEventMessage('SavedConfig'); header('Location: ' . $_SERVER['PHP_SELF']); exit; } @@ -133,12 +112,12 @@ print ''; print '' . $langs->trans('URLYourlsAPIDescription') . ''; -print ''; +print ''; print ''; print ''; print '' . $langs->trans('SignatureTokenYourlsAPIDescription') . ''; -print ''; +print ''; print ''; print ''; diff --git a/class/actions_easyurl.class.php b/class/actions_easyurl.class.php index 2e7f2e3..9cb8cae 100644 --- a/class/actions_easyurl.class.php +++ b/class/actions_easyurl.class.php @@ -73,36 +73,7 @@ public function __construct(DoliDB $db) */ public function printCommonFooter(array $parameters): int { - global $object, $langs; - - if (in_array($parameters['currentcontext'], ['propalcard', 'ordercard', 'invoicecard', 'contractcard', 'interventioncard'])) { - if ($object->status > $object::STATUS_DRAFT) { - $cssPath = dol_buildpath('/saturne/css/saturne.min.css', 1); - print ''; - - $pictoPath = dol_buildpath('/easyurl/img/easyurl_color.png', 1); - $picto = img_picto('', $pictoPath, '', 1, 0, 0, '', 'pictoModule'); - $urlTypes = ['payment', 'signature']; - foreach ($urlTypes as $urlType) { - $shortenerData = get_easy_url_link($object, $urlType); - $jQueryElement = '.' . $object->element . '_extras_easy_url_' . $urlType . '_link'; - if ($shortenerData->statusCode != 200 && getDolGlobalInt('EASYURL_MANUAL_GENERATION')) { - $output = $picto; - $output .= ''; - $output .= img_picto($langs->trans('SetEasyURLLink'), 'fontawesome_fa-redo_fas_#444', 'class="paddingright pictofixedwidth valignmiddle"') . ''; - $output .= '' . img_picto($langs->trans('GetEasyURLErrors'), 'fontawesome_fa-exclamation-triangle_fas_#bc9526') . ''; - } - if (!empty($object->array_options['options_easy_url_' . $urlType . '_link']) && $shortenerData->statusCode == 200) { - $output = showValueWithClipboardCPButton($object->array_options['options_easy_url_' . $urlType . '_link'], 0, 'none'); - } ?> - - '; - - $pictoPath = dol_buildpath('/easyurl/img/easyurl_color.png', 1); - $picto = img_picto('', $pictoPath, '', 1, 0, 0, '', 'pictoModule'); - $urlTypes = ['payment', 'signature']; - foreach ($urlTypes as $urlType) { - $jQueryElement = 'easy_url_' . $urlType . '_link'; ?> - - - id); - exit; - } - } - require_once __DIR__ . '/../../saturne/lib/object.lib.php'; $objectsMetadata = saturne_get_objects_metadata(); diff --git a/class/shortener.class.php b/class/shortener.class.php index c6a037b..9e774c1 100644 --- a/class/shortener.class.php +++ b/class/shortener.class.php @@ -553,7 +553,7 @@ public function displayObjectDetails(CommonObject $object): string $out .= '' . ($user->conf->EASYURL_SHOW_QRCODE ? saturne_show_medias_linked('easyurl', $conf->easyurl->multidir_output[$conf->entity] . '/shortener/' . $shortener->ref . '/qrcode/', 'small', 1, 0, 0, 0, 80, 80, 0, 0, 1, 'shortener/'. $shortener->ref . '/qrcode/', $shortener, '', 0, 0) : $shortener->showOutputField($this->fields['short_url'], 'short_url', $shortener->short_url)) . ''; $out .= '' . $shortener->showOutputField($this->fields['original_url'], 'original_url', $shortener->original_url) . ''; if (getDolGlobalInt('EASYURL_SHOW_API_INFOS')) { - $shortenerData = get_easy_url_link($object, 'all'); + $shortenerData = get_easy_url_link($shortener->short_url); $out .= '' . (isset($shortenerData->link) && isset($shortenerData->link->clicks) ? $shortenerData->link->clicks : 0) . ''; } if ($user->hasRight('easyurl', 'shortener', 'write')) { diff --git a/core/modules/modEasyURL.class.php b/core/modules/modEasyURL.class.php index 0c9868b..a195bf8 100644 --- a/core/modules/modEasyURL.class.php +++ b/core/modules/modEasyURL.class.php @@ -393,7 +393,7 @@ public function __construct($db) */ public function init($options = ''): int { - global $conf; + global $conf, $user; // Permissions $this->remove($options); @@ -408,35 +408,65 @@ public function init($options = ''): int dolibarr_set_const($this->db, 'EASYURL_VERSION', $this->version, 'chaine', 0, '', $conf->entity); dolibarr_set_const($this->db, 'EASYURL_DB_VERSION', $this->version, 'chaine', 0, '', $conf->entity); - // Create extrafields during init - require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php'; - $extraFields = new ExtraFields($this->db); - - // Propal extrafields - $extraFields->update('easy_url_signature_link', 'EasyUrlSignatureLink', 'url', '', 'propal', 0, 0, 2000, '', '', '', 5, 'EasyUrlLinkHelp', '', '', 0, 'easyurl@easyurl'); - $extraFields->addExtraField('easy_url_signature_link', 'EasyUrlSignatureLink', 'url', 2000, '', 'propal', 0, 0, '', '', '', '', 5, 'EasyUrlLinkHelp', '', 0, 'easyurl@easyurl'); - - // Order extrafields - $extraFields->update('easy_url_payment_link', 'EasyUrlPaymentLink', 'url', '', 'commande', 0, 0, 2000, '', '', '', 5, 'EasyUrlLinkHelp', '', '', 0, 'easyurl@easyurl'); - $extraFields->addExtraField('easy_url_payment_link', 'EasyUrlPaymentLink', 'url', 2000, '', 'commande', 0, 0, '', '', '', '', 5, 'EasyUrlLinkHelp', '', 0, 'easyurl@easyurl'); - - // Invoice extrafields - $extraFields->update('easy_url_payment_link', 'EasyUrlPaymentLink', 'url', '', 'facture', 0, 0, 2000, '', '', '', 5, 'EasyUrlLinkHelp', '', '', 0, 'easyurl@easyurl'); - $extraFields->addExtraField('easy_url_payment_link', 'EasyUrlPaymentLink', 'url', 2000, '', 'facture', 0, 0, '', '', '', '', 5, 'EasyUrlLinkHelp', '', 0, 'easyurl@easyurl'); - - // Contract extrafields - $extraFields->update('easy_url_signature_link', 'EasyUrlSignatureLink', 'url', '', 'contrat', 0, 0, 2000, '', '', '', 5, 'EasyUrlLinkHelp', '', '', 0, 'easyurl@easyurl'); - $extraFields->addExtraField('easy_url_signature_link', 'EasyUrlSignatureLink', 'url', 2000, '', 'contrat', 0, 0, '', '', '', '', 5, 'EasyUrlLinkHelp', '', 0, 'easyurl@easyurl'); - - // Fiche inter extrafields - $extraFields->update('easy_url_signature_link', 'EasyUrlSignatureLink', 'url', '', 'fichinter', 0, 0, 2000, '', '', '', 5, 'EasyUrlLinkHelp', '', '', 0, 'easyurl@easyurl'); - $extraFields->addExtraField('easy_url_signature_link', 'EasyUrlSignatureLink', 'url', 2000, '', 'fichinter', 0, 0, '', '', '', '', 5, 'EasyUrlLinkHelp', '', 0, 'easyurl@easyurl'); + if (!getDolGlobalInt('EASYURL_BACKWARD_EXTRAFIELDS') && version_compare(getDolGlobalString('EASYURL_VERSION'), '1.1.0', '>=')) { + // Create extrafields during init + require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php'; + + require_once __DIR__ . '/../../lib/easyurl_function.lib.php'; + + $extraFields = new ExtraFields($this->db); + + $extraFields->update('easy_url_signature_link', 'EasyUrlSignatureLink', 'url', '', 'propal', 0, 0, 2000, '', '', '', 0, 'EasyUrlLinkHelp', '', '', 0, 'easyurl@easyurl'); + $extraFields->update('easy_url_payment_link', 'EasyUrlPaymentLink', 'url', '', 'commande', 0, 0, 2000, '', '', '', 0, 'EasyUrlLinkHelp', '', '', 0, 'easyurl@easyurl'); + $extraFields->update('easy_url_payment_link', 'EasyUrlPaymentLink', 'url', '', 'facture', 0, 0, 2000, '', '', '', 0, 'EasyUrlLinkHelp', '', '', 0, 'easyurl@easyurl'); + $extraFields->update('easy_url_signature_link', 'EasyUrlSignatureLink', 'url', '', 'contrat', 0, 0, 2000, '', '', '', 0, 'EasyUrlLinkHelp', '', '', 0, 'easyurl@easyurl'); + $extraFields->update('easy_url_signature_link', 'EasyUrlSignatureLink', 'url', '', 'fichinter', 0, 0, 2000, '', '', '', 0, 'EasyUrlLinkHelp', '', '', 0, 'easyurl@easyurl'); + + // All element type extrafields + $shortenerUrlTypeDictionaries = saturne_fetch_dictionary('c_shortener_url_type'); + $objectsMetadata = saturne_get_objects_metadata(); + foreach ($objectsMetadata as $objectMetadata) { + $objects = saturne_fetch_all_object_type($objectMetadata['class_name'], '', '', 0, 0, [], 'AND', true); + if (is_array($objects) && !empty($objects)) { + foreach ($objects as $object) { + $urlTypes = ['signature', 'payment']; + foreach ($urlTypes as $urlType) { + if (isset($object->array_options['options_easy_url_' . $urlType . '_link']) && !empty($object->array_options['options_easy_url_' . $urlType . '_link'])) { + $shortener = new Shortener($this->db); + + $result = $shortener->fetch('', '', ' AND short_url = "' . $object->array_options['options_easy_url_' . $urlType . '_link'] . '"'); + if ($result == 0) { + $shortenerData = get_easy_url_link($object->array_options['options_easy_url_' . $urlType . '_link']); + + $shortener->ref = $shortener->getNextNumRef(); + $shortener->ref_ext = 'easy_url_' . $urlType . '_link'; + $shortener->entity = $conf->entity; + $shortener->status = Shortener::STATUS_ASSIGN; + $shortener->label = $shortenerData->title; + $shortener->short_url = $object->array_options['options_easy_url_' . $urlType . '_link']; + $shortener->original_url = $shortenerData->url; + if (is_array($shortenerUrlTypeDictionaries) && !empty($shortenerUrlTypeDictionaries)) { + foreach ($shortenerUrlTypeDictionaries as $shortenerUrlTypeDictionary) { + if ($shortenerUrlTypeDictionary->ref == ucfirst($urlType)) { + $shortener->type = $shortenerUrlTypeDictionary->id; + break; + } + } + } + $shortener->methode = 'yourls'; + $shortener->element_type = $objectMetadata['tab_type']; + $shortener->fk_element = $object->id; + + $shortener->create($user); + } + } + } + } + } + $extraFields->update('easy_url_all_link', 'EasyUrlAllLink', 'url', '', $objectMetadata['table_element'], 0, 0, 2100, '', '', '', 0, 'EasyUrlAllLinkHelp', '', '', 0, 'easyurl@easyurl'); + } - // All element type extrafields - $objectsMetadata = saturne_get_objects_metadata(); - foreach($objectsMetadata as $objectMetadata) { - $extraFields->update('easy_url_all_link', 'EasyUrlAllLink', 'url', '', $objectMetadata['table_element'], 0, 0, 2100, '', '', '', 5, 'EasyUrlAllLinkHelp', '', '', 0, 'easyurl@easyurl'); - $extraFields->addExtraField('easy_url_all_link', 'EasyUrlAllLink', 'url', 2100, '', $objectMetadata['table_element'], 0, 0, '', '', '', '', 5, 'EasyUrlAllLinkHelp', '', 0, 'easyurl@easyurl'); + dolibarr_set_const($this->db, 'EASYURL_BACKWARD_EXTRAFIELDS', 1, 'integer', 0, '', $conf->entity); } return $this->_init($sql, $options); diff --git a/core/substitutions/functions_easyurl.lib.php b/core/substitutions/functions_easyurl.lib.php index 3630a3e..cb003e8 100644 --- a/core/substitutions/functions_easyurl.lib.php +++ b/core/substitutions/functions_easyurl.lib.php @@ -33,22 +33,18 @@ */ function easyurl_completesubstitutionarray(&$substitutionarray, $langs, $object) { + global $db; + $langs->load('easyurl@easyurl'); - if (isset($object->element)) { - switch ($object->element) { - case 'propal' : - case 'contrat' : - case 'fichinter' : - $substitutionarray['__EASY_URL_SIGNATURE_LINK__'] = $object->array_options['options_easy_url_signature_link']; - break; - case 'commande' : - case 'facture' : - $substitutionarray['__EASY_URL_PAYMENT_LINK__'] = $object->array_options['options_easy_url_payment_link']; - break; - default : - $substitutionarray['__EASY_URL_SIGNATURE_LINK__'] = $langs->trans('EasyUrlSignatureLink'); - $substitutionarray['__EASY_URL_PAYMENT_LINK__'] = $langs->trans('EasyUrlPaymentLink'); - break; + + require_once __DIR__ . '/../../class/shortener.class.php'; + + $shortener = new Shortener($db); + + $shorteners = $shortener->fetchAll('', '', 0,0, ['customsql' => 'fk_element = ' . $object->id . ' AND type > 0']); + if (is_array($shorteners) && !empty($shorteners)) { + foreach ($shorteners as $shortener) { + $substitutionarray['__EASY_URL_' . dol_strtoupper(getDictionaryValue('c_shortener_url_type', 'label', $shortener->type)) . '_LINK__'] = $shortener->short_url; } } } diff --git a/core/triggers/interface_99_modEasyurl_EasyurlTriggers.class.php b/core/triggers/interface_99_modEasyurl_EasyurlTriggers.class.php index 77ff755..4e30a86 100644 --- a/core/triggers/interface_99_modEasyurl_EasyurlTriggers.class.php +++ b/core/triggers/interface_99_modEasyurl_EasyurlTriggers.class.php @@ -118,13 +118,29 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf case 'CONTRACT_VALIDATE': case 'FICHINTER_VALIDATE': if (getDolGlobalInt('EASYURL_AUTOMATIC_GENERATION')) { - set_easy_url_link($object, 'signature'); + require_once __DIR__ . '/../../class/shortener.class.php'; + + $shortener = new Shortener($this->db); + + $shortener->ref = $shortener->getNextNumRef(); + $shortener->methode = 'yourls'; + + $shortener->create($user); + set_easy_url_link($shortener, 'signature', $object); } break; case 'ORDER_VALIDATE': case 'BILL_VALIDATE': if (getDolGlobalInt('EASYURL_AUTOMATIC_GENERATION')) { - set_easy_url_link($object, 'payment'); + require_once __DIR__ . '/../../class/shortener.class.php'; + + $shortener = new Shortener($this->db); + + $shortener->ref = $shortener->getNextNumRef(); + $shortener->methode = 'yourls'; + + $shortener->create($user); + set_easy_url_link($shortener, 'payment', $object); } break; @@ -137,13 +153,6 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf // MODIFY case 'SHORTENER_MODIFY' : if (!empty($object->element_type)) { - $objectsMetadata = saturne_get_objects_metadata($object->element_type); - $className = $objectsMetadata['class_name']; - $objectLinked = new $className($this->db); - $objectLinked->fetch($object->fk_element); - $objectLinked->array_options['options_easy_url_all_link'] = $object->short_url; - $objectLinked->updateExtraField('easy_url_all_link'); - $object->status = Shortener::STATUS_ASSIGN; $object->setValueFrom('status', $object->status, '', null, 'int'); } else { diff --git a/langs/fr_FR/easyurl.lang b/langs/fr_FR/easyurl.lang index da000a5..cdf8900 100644 --- a/langs/fr_FR/easyurl.lang +++ b/langs/fr_FR/easyurl.lang @@ -44,7 +44,8 @@ AutomaticEasyUrlGeneration = Génération automatique des URLs raccou AutomaticEasyUrlGenerationDescription = Crée automatiquement l'URL raccourcie lors de la validation d'un objet
(proposition commerciale, commande, facture) ManualEasyUrlGeneration = Génération manuelle des URLs raccourcies ManualEasyUrlGenerationDescription = Ajout d'un bouton pour créer une URL raccourcie sur un objet
(proposition commerciale, commande, facture) -WarningApiCredentialsIncorrect = Les informations d'API sont incorrects +ErrorEasyURLAPIConfig = Les informations d'API sont incorrects +ErrorMissingEasyURLAPIConfig = Les informations d'API ne sont pas configurées # Public interface - Interface publique OfAssignShortener = d'affectation de raccourcissement d'URL diff --git a/lib/easyurl_function.lib.php b/lib/easyurl_function.lib.php index a73118d..4eb1128 100644 --- a/lib/easyurl_function.lib.php +++ b/lib/easyurl_function.lib.php @@ -23,15 +23,26 @@ /** * Init easy url curl - * @param array $curlPostFields cURL post fields - * @param string $urlMethod Url method - * @return CurlHandle|false $ch cURL handle on success, false on errors + * @param array $curlPostFields cURL post fields + * @param string $urlMethod Url method + * @return int|stdClass 0 < on error, data object on success */ function init_easy_url_curl(array $curlPostFields, string $urlMethod = 'yourls') { + global $langs; + + // Check configuration + $urlAPI = getDolGlobalString('EASYURL_URL_' . dol_strtoupper($urlMethod) . '_API'); + $signatureToken = getDolGlobalString('EASYURL_SIGNATURE_TOKEN_' . dol_strtoupper($urlMethod) . '_API'); + $checkConf = $urlAPI && $signatureToken; + if (!$checkConf) { + setEventMessages($langs->trans('ErrorMissingEasyURLAPIConfig'), [], 'errors'); + return -1; + } + // Init the CURL session $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, getDolGlobalString('EASYURL_URL_' . dol_strtoupper($urlMethod) . '_API')); + curl_setopt($ch, CURLOPT_URL, $urlAPI); 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); @@ -40,7 +51,7 @@ function init_easy_url_curl(array $curlPostFields, string $urlMethod = 'yourls') case 'yourls' : // Data to POST $defaultCurlPostFields = [ - 'signature' => getDolGlobalString('EASYURL_SIGNATURE_TOKEN_YOURLS_API'), + 'signature' => $signatureToken, 'format' => 'json' ]; $curlPostFields = array_merge($curlPostFields, $defaultCurlPostFields); @@ -50,30 +61,35 @@ function init_easy_url_curl(array $curlPostFields, string $urlMethod = 'yourls') break; } - return $ch; + // Fetch and return content + $data = curl_exec($ch); + curl_close($ch); + + // Do something with the result + return json_decode($data); } /** * Set easy url link * - * @param CommonObject $object Object - * @param string $urlType Url type - * @param string $urlMethod Url method - * @return int|object $data Data error after curl + * @param Shortener $shortener Shortener + * @param string $urlType Url type + * @param CommonObject|null $object Object + * @param string $urlMethod Url method + * @return int|object $data Data error after curl */ -function set_easy_url_link(CommonObject $object, string $urlType, string $urlMethod = 'yourls') +function set_easy_url_link(Shortener $shortener, string $urlType, CommonObject $object = null, string $urlMethod = 'yourls') { global $conf, $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 ((($urlType == 'payment' && $useOnlinePayment) || $urlType == 'signature' || $urlType == 'none') && $checkConf) { + if (($urlType == 'payment' && $useOnlinePayment) || $urlType == 'signature' || $urlType == 'none') { // 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'; - $object->fetch($object->id); + $shortener->fetch($shortener->id); switch ($object->element) { case 'propal' : $type = 'proposal'; @@ -94,20 +110,37 @@ function set_easy_url_link(CommonObject $object, string $urlType, string $urlMet switch ($urlType) { case 'payment' : $onlineUrl = getOnlinePaymentUrl(0, $type, $object->ref); + if ($type == 'proposal') { + $type = 'propal'; + } + $shortener->element_type = $type; + $shortener->fk_element = $object->id; + $shortener->status = $shortener::STATUS_ASSIGN; break; case 'signature' : $onlineUrl = getOnlineSignatureUrl(0, $type, $object->ref); + if ($type == 'proposal') { + $type = 'propal'; + } + $shortener->element_type = $type; + $shortener->fk_element = $object->id; + $shortener->status = $shortener::STATUS_ASSIGN; break; default : - if (property_exists($object, 'original_url') && dol_strlen($object->original_url) > 0) { - $onlineUrl = $object->original_url; + if (property_exists($shortener, 'original_url') && dol_strlen($shortener->original_url) > 0) { + $onlineUrl = $shortener->original_url; } else { $onlineUrl = getDolGlobalString('EASYURL_DEFAULT_ORIGINAL_URL'); } break; } - $title = dol_sanitizeFileName(dol_strtolower($conf->global->MAIN_INFO_SOCIETE_NOM . '-' . $object->ref) . (getDolGlobalInt('EASYURL_USE_SHA_URL') ? '-' . generate_random_id(8) : '')); + $title = getDolGlobalInt('EASYURL_USE_MAIN_INFO_SOCIETE_NAME') ? dol_strtolower($conf->global->MAIN_INFO_SOCIETE_NOM) : ''; + $title .= getDolGlobalInt('EASYURL_USE_MAIN_INFO_SOCIETE_NAME') && getDolGlobalInt('EASYURL_USE_SHORTENER_REF') ? '-' : ''; + $title .= getDolGlobalInt('EASYURL_USE_SHORTENER_REF') ? dol_strtolower($shortener->ref) : ''; + $title .= (getDolGlobalInt('EASYURL_USE_MAIN_INFO_SOCIETE_NAME') || getDolGlobalInt('EASYURL_USE_SHORTENER_REF')) && getDolGlobalInt('EASYURL_USE_SHA_URL') ? '-' : ''; + $title .= getDolGlobalInt('EASYURL_USE_SHA_URL') ? generate_random_id(8) : ''; + $title = dol_sanitizeFileName($title); $curlPostFields = [ 'action' => 'shorturl', @@ -115,98 +148,76 @@ function set_easy_url_link(CommonObject $object, string $urlType, string $urlMet 'keyword' => $title, 'url' => $onlineUrl ]; - $ch = init_easy_url_curl($curlPostFields, $urlMethod); - - // Fetch and return content - $data = curl_exec($ch); - curl_close($ch); - - // Do something with the result - $data = json_decode($data); + $data = init_easy_url_curl($curlPostFields, $urlMethod); if ($data != null && $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'; - - $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'; - - $imageData = $barcode->getBarcodePngData(); - $imageData = imagecreatefromstring($imageData); - imagepng($imageData, $file); + $shortenerUrlTypeDictionaries = saturne_fetch_dictionary('c_shortener_url_type'); + if (is_array($shortenerUrlTypeDictionaries) && !empty($shortenerUrlTypeDictionaries)) { + foreach ($shortenerUrlTypeDictionaries as $shortenerUrlTypeDictionary) { + if ($shortenerUrlTypeDictionary->ref == ucfirst($urlType)) { + $shortener->type = $shortenerUrlTypeDictionary->id; + break; + } + } + } + + if ($shortener->status == $shortener::STATUS_DRAFT) { + $shortener->status = $shortener::STATUS_VALIDATED; } + $shortener->label = $title; + $shortener->short_url = $data->shorturl; + $shortener->original_url = $onlineUrl; + $shortener->update($user, true); + + require_once TCPDF_PATH . 'tcpdf_barcodes_2d.php'; + + $barcode = new TCPDF2DBarcode($shortener->short_url, 'QRCODE,L'); + + dol_mkdir($conf->easyurl->multidir_output[$conf->entity] . '/shortener/' . $shortener->ref . '/qrcode/'); + $file = $conf->easyurl->multidir_output[$conf->entity] . '/shortener/' . $shortener->ref . '/qrcode/' . 'barcode_' . $shortener->ref . '.png'; + + $imageData = $barcode->getBarcodePngData(); + $imageData = imagecreatefromstring($imageData); + imagepng($imageData, $file); + + setEventMessages($langs->trans('SetEasyURLSuccess'), []); return 1; } else { setEventMessages($langs->trans('SetEasyURLErrors'), [$data->message], 'errors'); return $data; } + } else { + return -1; } } /** * get easy url link * - * @param CommonObject $object Object - * @param string $urlType Url type - * @return stdClass|int 0 < on error, data object on success + * @param string $shortUrl Short url + * @return int|stdClass 0 < on error, data object on success */ -function get_easy_url_link(CommonObject $object, string $urlType) +function get_easy_url_link(string $shortUrl) { - $useOnlinePayment = (isModEnabled('paypal') || isModEnabled('stripe') || isModEnabled('paybox')); - $checkConf = getDolGlobalString('EASYURL_URL_YOURLS_API') && getDolGlobalString('EASYURL_SIGNATURE_TOKEN_YOURLS_API'); - if ((($urlType == 'payment' && $useOnlinePayment) || $urlType == 'signature' || $urlType == 'all') && $checkConf) { - $object->fetch($object->id); - - $curlPostFields = [ - 'action' => 'url-stats', - 'shorturl' => $object->array_options['options_easy_url_' . $urlType . '_link'] - ]; - $ch = init_easy_url_curl($curlPostFields); - - // Fetch and return content - $data = curl_exec($ch); - curl_close($ch); - - // Do something with the result - return json_decode($data); - } else { - return -1; - } + $curlPostFields = [ + 'action' => 'url-stats', + 'shorturl' => $shortUrl + ]; + return init_easy_url_curl($curlPostFields); } /** * Update easy url link * * @param CommonObject $object Object - * @return int 0 < on error, 1 = statusCode 200, 0 = other statusCode (ex : 404) + * @return int|stdClass 0 < on error, data object on success */ -function update_easy_url_link(CommonObject $object): int +function update_easy_url_link(CommonObject $object) { $curlPostFields = [ 'action' => 'update', 'shorturl' => $object->short_url, 'url' => $object->original_url ]; - $ch = init_easy_url_curl($curlPostFields); - - // 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; + return init_easy_url_curl($curlPostFields); } diff --git a/view/easyurltools.php b/view/easyurltools.php index acf65c6..6d79c65 100644 --- a/view/easyurltools.php +++ b/view/easyurltools.php @@ -80,7 +80,7 @@ $shortener->create($user); // 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); + $result = set_easy_url_link($shortener, 'none', null, $urlMethode); if (!empty($result) && is_object($result)) { $urlParametersOut .= '?success=false&nb_url=' . GETPOST('nb_url') . '&successType=shortener'; } else { From 7ba7015f88aa998d5084f5d8998dd719b40f6e3c Mon Sep 17 00:00:00 2001 From: Nicolas Domenech Date: Wed, 19 Feb 2025 11:25:52 +0100 Subject: [PATCH 2/2] #41[admin] fix: improve check on detect Yourls API --- admin/setup.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/setup.php b/admin/setup.php index 0e0c91e..03a56e9 100644 --- a/admin/setup.php +++ b/admin/setup.php @@ -70,7 +70,7 @@ $curlPostFields = ['action' => 'version']; $data = init_easy_url_curl($curlPostFields); - if (empty($data)) { + if (empty($data) || (isset($data->errorCode) && $data->errorCode != 200)) { setEventMessages('ErrorEasyURLAPIConfig', [], 'errors'); } else { setEventMessages('SavedConfig', []);