Skip to content

Commit

Permalink
#2 [Triggers] add: config yourls api and triggers bill validate
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-eoxia committed Aug 2, 2023
1 parent 123e5d2 commit 64f58f3
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 1 deletion.
56 changes: 55 additions & 1 deletion admin/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -68,11 +90,43 @@

// Subheader
$linkback = '<a href="' . ($backtopage ?: DOL_URL_ROOT . '/admin/modules.php?restore_lastsearch_values=1') . '">' . $langs->trans('BackToModuleList') . '</a>';
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 '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="set_config">';

print '<table class="noborder centpercent">';
print '<tr class="liste_titre">';
print '<td>' . $langs->trans('Parameters') . '</td>';
print '<td>' . $langs->trans('Description') . '</td>';
print '<td>' . $langs->trans('Value') . '</td>';
print '</tr>';

print '<tr class="oddeven"><td><label for="username_yourls_api">' . $langs->trans('UserNameYourlsAPI') . '</label></td>';
print '<td>' . $langs->trans('UserNameYourlsAPIDescription') . '</td>';
print '<td><input type="text" name="username_yourls_api" value="' . $conf->global->TINYURL_USERNAME_YOURLS_API . '"></td>';
print '</td></tr>';

print '<tr class="oddeven"><td><label for="key_yourls_api">' . $langs->trans('KeyYourlsAPI') . '</label></td>';
print '<td>' . $langs->trans('KeyYourlsAPIDescription') . '</td>';
print '<td><input type="text" name="key_yourls_api" value="' . $conf->global->TINYURL_KEY_YOURLS_API . '"></td>';
print '</td></tr>';

print '<tr class="oddeven"><td><label for="signature_token_yourls_api">' . $langs->trans('SignatureTokenYourlsAPI') . '</label></td>';
print '<td>' . $langs->trans('SignatureTokenYourlsAPIDescription') . '</td>';
print '<td><input type="text" name="signature_token_yourls_api" value="' . $conf->global->TINYURL_SIGNATURE_TOKEN_YOURLS_API . '"></td>';
print '</td></tr>';

print '</table>';
print $form->buttonsSaveCancel('Save', '', [], 0, 'butAction');
print '</form>';

$db->close();
llxFooter();
2 changes: 2 additions & 0 deletions core/triggers/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
//Silence is golden apple
103 changes: 103 additions & 0 deletions core/triggers/interface_99_modTinyurl_TinyurlTriggers.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/* Copyright (C) 2023 EVARISK <technique@evarisk.com>
*
* 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 <https://www.gnu.org/licenses/>.
*/

/**
* \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;
}
}

0 comments on commit 64f58f3

Please sign in to comment.