Skip to content

Commit

Permalink
Eoxia#41 [Mod] add: backward extrafields
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-eoxia committed Apr 5, 2024
1 parent 6706e62 commit c698d45
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 154 deletions.
60 changes: 1 addition & 59 deletions class/actions_easyurl.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<link href="' . $cssPath . '" rel="stylesheet">';

$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 .= '<a class="reposition editfielda" href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=set_easy_url&url_type=' . $urlType . '&token=' . newToken() . '">';
$output .= img_picto($langs->trans('SetEasyURLLink'), 'fontawesome_fa-redo_fas_#444', 'class="paddingright pictofixedwidth valignmiddle"') . '</a>';
$output .= '</span>' . img_picto($langs->trans('GetEasyURLErrors'), 'fontawesome_fa-exclamation-triangle_fas_#bc9526') . '</span>';
}
if (!empty($object->array_options['options_easy_url_' . $urlType . '_link']) && $checkEasyUrlLink > 0) {
$output = showValueWithClipboardCPButton($object->array_options['options_easy_url_' . $urlType . '_link'], 0, 'none');
} ?>
<script>
var objectElement = <?php echo "'" . $jQueryElement . "'"; ?>;
jQuery(objectElement).prepend(<?php echo json_encode($output); ?>);
</script>
<?php
}
}
}
global $object;

require_once __DIR__ . '/../../saturne/lib/object.lib.php';

Expand Down Expand Up @@ -130,26 +101,6 @@ public function printCommonFooter(array $parameters): int
}
}

if (in_array($parameters['currentcontext'], ['propallist', 'orderlist', 'invoicelist'])) {
$cssPath = dol_buildpath('/saturne/css/saturne.min.css', 1);
print '<link href="' . $cssPath . '" rel="stylesheet">';

$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'; ?>

<script>
var objectElement = <?php echo "'" . $jQueryElement . "'"; ?>;
var outJS = <?php echo json_encode($picto); ?>;
var cell = $('.liste > tbody > tr.liste_titre').find('th[data-titlekey="' + objectElement + '"]');
cell.prepend(outJS);
</script>
<?php
}
}

return 0; // or return 1 to replace standard code
}

Expand All @@ -165,15 +116,6 @@ public function doActions(array $parameters, $object, string $action): int
{
global $conf, $user;

if (in_array($parameters['currentcontext'], ['propalcard', 'ordercard', 'invoicecard', 'contractcard', 'interventioncard'])) {
if ($action == 'set_easy_url') {
set_easy_url_link($object, GETPOST('url_type'));

header('Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
exit;
}
}

require_once __DIR__ . '/../../saturne/lib/object.lib.php';

$objectsMetadata = saturne_get_objects_metadata();
Expand Down
3 changes: 0 additions & 3 deletions class/shortener.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
84 changes: 55 additions & 29 deletions core/modules/modEasyURL.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public function __construct($db)
*/
public function init($options = ''): int
{
global $conf;
global $conf, $user;

// Permissions
$this->remove($options);
Expand All @@ -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);
Expand Down
25 changes: 11 additions & 14 deletions core/substitutions/functions_easyurl.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
27 changes: 18 additions & 9 deletions core/triggers/interface_99_modEasyurl_EasyurlTriggers.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down
Loading

0 comments on commit c698d45

Please sign in to comment.