diff --git a/class/actions_easyurl.class.php b/class/actions_easyurl.class.php index 28e88cc..e221f10 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) { - $checkEasyUrlLink = get_easy_url_link($object, $urlType); - $jQueryElement = '.' . $object->element . '_extras_easy_url_' . $urlType . '_link'; - if ($checkEasyUrlLink == 0 && 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']) && $checkEasyUrlLink > 0) { - $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 a2754e9..4fc98e9 100644 --- a/class/shortener.class.php +++ b/class/shortener.class.php @@ -417,9 +417,6 @@ public function displayObjectDetails(CommonObject $object): string global $conf, $form, $langs, $user; switch ($object->element) { - case 'propal' : - $element_type = 'proposal'; - break; case 'commande' : $element_type = 'order'; break; diff --git a/core/modules/modEasyURL.class.php b/core/modules/modEasyURL.class.php index 769723f..a0851a0 100644 --- a/core/modules/modEasyURL.class.php +++ b/core/modules/modEasyURL.class.php @@ -384,7 +384,7 @@ public function __construct($db) */ public function init($options = ''): int { - global $conf; + global $conf, $user; // Permissions $this->remove($options); @@ -399,35 +399,61 @@ 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); + + // 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'], $urlType); + + $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->delete('easy_url_signature_link', $objectMetadata['table_element']); + $extraFields->delete('easy_url_payment_link', $objectMetadata['table_element']); + $extraFields->delete('easy_url_all_link', $objectMetadata['table_element']); + } - // 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 4db8cbe..cb003e8 100644 --- a/core/substitutions/functions_easyurl.lib.php +++ b/core/substitutions/functions_easyurl.lib.php @@ -33,21 +33,18 @@ */ function easyurl_completesubstitutionarray(&$substitutionarray, $langs, $object) { + global $db; + $langs->load('easyurl@easyurl'); - 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 c96a6c8..f73a871 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/lib/easyurl_function.lib.php b/lib/easyurl_function.lib.php index 6138e67..f6fcc01 100644 --- a/lib/easyurl_function.lib.php +++ b/lib/easyurl_function.lib.php @@ -24,12 +24,13 @@ /** * 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; @@ -41,7 +42,7 @@ function set_easy_url_link(CommonObject $object, string $urlType, string $urlMet 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'; @@ -62,20 +63,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); // Init the CURL session $ch = curl_init(); @@ -107,29 +125,33 @@ function set_easy_url_link(CommonObject $object, string $urlType, string $urlMet $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); - - 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); return 1; } else { setEventMessage($langs->trans('SetEasyURLErrors'), 'errors'); @@ -141,19 +163,17 @@ function set_easy_url_link(CommonObject $object, string $urlType, string $urlMet /** * get easy url link * - * @param CommonObject $object Object - * @param string $urlType Url type - * @return int 0 < on error, 1 = statusCode 200, 0 = other statusCode (ex : 404) + * @param string $shortUrl Short url + * @param string $urlType Url type + * @return int|object $data 0 < on error, data */ -function get_easy_url_link(CommonObject $object, string $urlType): int +function get_easy_url_link(string $shortUrl, string $urlType) { global $conf; $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') && $checkConf) { - $object->fetch($object->id); - // Init the CURL session $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $conf->global->EASYURL_URL_YOURLS_API); @@ -165,7 +185,7 @@ function get_easy_url_link(CommonObject $object, string $urlType): int 'action' => 'url-stats', 'signature' => $conf->global->EASYURL_SIGNATURE_TOKEN_YOURLS_API, 'format' => 'json', - 'shorturl' => $object->array_options['options_easy_url_' . $urlType . '_link'] + 'shorturl' => $shortUrl ]); // Fetch and return content @@ -174,7 +194,11 @@ function get_easy_url_link(CommonObject $object, string $urlType): int // Do something with the result $data = json_decode($data); - return $data->statusCode == 200 ? 1 : 0; + if ($data->statusCode == 200) { + return $data->link; + } else { + return -1; + } } else { return -1; } diff --git a/view/easyurltools.php b/view/easyurltools.php index 3970085..6fb62dc 100644 --- a/view/easyurltools.php +++ b/view/easyurltools.php @@ -75,7 +75,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)) { setEventMessage($result->message, 'errors'); $error++;