From 64f58f3b5a7cb1b7f10f7f10364c08cda3ec44d4 Mon Sep 17 00:00:00 2001 From: Nicolas Domenech Date: Wed, 2 Aug 2023 22:28:16 +0200 Subject: [PATCH] #2 [Triggers] add: config yourls api and triggers bill validate --- admin/setup.php | 56 +++++++++- core/triggers/index.php | 2 + ...ce_99_modTinyurl_TinyurlTriggers.class.php | 103 ++++++++++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 core/triggers/index.php create mode 100644 core/triggers/interface_99_modTinyurl_TinyurlTriggers.class.php diff --git a/admin/setup.php b/admin/setup.php index d576741..f46fa03 100644 --- a/admin/setup.php +++ b/admin/setup.php @@ -57,6 +57,28 @@ * Actions */ +if ($action == 'set_config') { + $userNameYourlsAPI = GETPOST('username_yourls_api'); + $keyYourlsAPI = GETPOST('key_yourls_api'); + $signatureTokenYourlsAPI = GETPOST('signature_token_yourls_api'); + + if (dol_strlen($userNameYourlsAPI) > 0) { + dolibarr_set_const($db, 'TINYURL_USERNAME_YOURLS_API', $userNameYourlsAPI, 'chaine', 0, '', $conf->entity); + } + if (dol_strlen($keyYourlsAPI) > 0) { + dolibarr_set_const($db, 'TINYURL_KEY_YOURLS_API', $keyYourlsAPI, 'chaine', 0, '', $conf->entity); + } + if (dol_strlen($signatureTokenYourlsAPI) > 0) { + dolibarr_set_const($db, 'TINYURL_SIGNATURE_TOKEN_YOURLS_API', $signatureTokenYourlsAPI, 'chaine', 0, '', $conf->entity); + } + + setEventMessage('SavedConfig'); + header('Location: ' . $_SERVER['PHP_SELF']); + exit; +} + + + /* * View */ @@ -68,11 +90,43 @@ // Subheader $linkback = '' . $langs->trans('BackToModuleList') . ''; -print load_fiche_titre($title, $linkback, 'tinyurl_color@tinyurl'); +print load_fiche_titre($title, $linkback, 'title_setup'); // Configuration header $head = tinyurl_admin_prepare_head(); print dol_get_fiche_head($head, 'settings', $title, -1, 'tinyurl_color@tinyurl'); +print load_fiche_titre($langs->trans('Config'), '', ''); + +print '
'; +print ''; +print ''; + +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; + +print ''; +print ''; +print ''; +print ''; + +print ''; +print ''; +print ''; +print ''; + +print ''; +print ''; +print ''; +print ''; + +print '
' . $langs->trans('Parameters') . '' . $langs->trans('Description') . '' . $langs->trans('Value') . '
' . $langs->trans('UserNameYourlsAPIDescription') . '
' . $langs->trans('KeyYourlsAPIDescription') . '
' . $langs->trans('SignatureTokenYourlsAPIDescription') . '
'; +print $form->buttonsSaveCancel('Save', '', [], 0, 'butAction'); +print '
'; + $db->close(); llxFooter(); diff --git a/core/triggers/index.php b/core/triggers/index.php new file mode 100644 index 0000000..17927e6 --- /dev/null +++ b/core/triggers/index.php @@ -0,0 +1,2 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file core/triggers/interface_99_modTinyURL_DolisirhTriggers.class.php + * \ingroup tinyurl + * \brief TinyURL trigger + */ + +// Load Dolibarr libraries +require_once DOL_DOCUMENT_ROOT . '/core/triggers/dolibarrtriggers.class.php'; + +/** + * Class of triggers for TinyURL module + */ +class InterfaceTinyURLTriggers extends DolibarrTriggers +{ + /** + * @var DoliDB Database handler + */ + protected $db; + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + public function __construct(DoliDB $db) + { + $this->db = $db; + + $this->name = preg_replace('/^Interface/i', '', get_class($this)); + $this->family = 'demo'; + $this->description = 'TinyURL triggers.'; + $this->version = '1.0.0'; + $this->picto = 'tinyurl@tinyurl'; + } + + /** + * Trigger name + * + * @return string Name of trigger file + */ + public function getName(): string + { + return parent::getName(); + } + + /** + * Trigger description + * + * @return string Description of trigger file + */ + public function getDesc(): string + { + return parent::getDesc(); + } + + /** + * Function called when a Dolibarr business event is done + * All functions "runTrigger" are triggered if file + * is inside directory core/triggers + * + * @param string $action Event action code + * @param CommonObject $object Object + * @param User $user Object user + * @param Translate $langs Object langs + * @param Conf $conf Object conf + * @return int 0 < if KO, 0 if no triggered ran, >0 if OK + * @throws Exception + */ + public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf): int + { + if (!isModEnabled('tinyurl')) { + return 0; // If module is not enabled, we do nothing + } + + saturne_load_langs(); + + // Data and type of action are stored into $object and $action + dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . '. id=' . $object->id); + + switch ($action) { + case 'BILL_VALIDATE': + break; + } + return 0; + } +}