Skip to content

Commit

Permalink
Merge pull request #26 from LinkNacional/dev
Browse files Browse the repository at this point in the history
3.2.1 - implementation of internationalization
  • Loading branch information
brunoferreiralkn authored Aug 31, 2023
2 parents d1eec07 + 69d180e commit 485fcec
Show file tree
Hide file tree
Showing 66 changed files with 3,195 additions and 1,492 deletions.
18 changes: 4 additions & 14 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'lowercase_static_reference' => true,
'native_function_casing' => true,
'no_blank_lines_before_namespace' => false,
'blank_lines_before_namespace' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => [
Expand Down Expand Up @@ -64,7 +65,7 @@
'phpdoc_types' => true,
'return_type_declaration' => true,
'short_scalar_cast' => true,
'single_blank_line_before_namespace' => true,
'single_blank_line_before_namespace' => false,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline' => ['elements' => [], 'after_heredoc' => false],
'single_trait_insert_per_statement' => true,
Expand All @@ -81,23 +82,12 @@
'identical' => false,
'always_move_variable' => false,
],
'fully_qualified_strict_types' => true,
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],

// Risk
'modernize_types_casting' => true,
'strict_comparison' => true,
'declare_strict_types' => false,
'strict_param' => true,
'modernize_types_casting' => true,
'final_class' => true,
'final_internal_class' => true,
'final_public_method_for_abstract_class' => true,
'void_return' => true,
'logical_operators' => true,
'strict_param' => true
])
->setIndent(' ')
->setLineEnding("\n")
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Code: WhatsAppPrivateNote
*/

namespace Lkn\HookNotification\Notifications\Chatwoot\WhatsAppPrivateNote;

use Exception;
use Lkn\HookNotification\Config\Platforms;
use Lkn\HookNotification\Config\ReportCategory;
use Lkn\HookNotification\Config\Settings;
use Lkn\HookNotification\Domains\Platforms\Chatwoot\AbstractChatwootNotification;
use Lkn\HookNotification\Helpers\Config;
use Lkn\HookNotification\Helpers\Lang;
use Lkn\HookNotification\Helpers\WhmcsApi;

final class WhatsAppPrivateNoteNotification extends AbstractChatwootNotification
{
public string $notificationCode = 'WhatsAppPrivateNote';
public ?string $channel = 'wp';

/**
* For this notification to work properly, the hook params passed must be:
*
* [ instance => AbstractWhatsAppNotifcation|AbstractChatwootNotification ]
*
* @since 1.0.0
* @var array
*/
public array $hookParams;

public function run(): bool
{
return $this->sendMessage();
}

public function sendMessage(): array|bool
{
/**
* @var \Lkn\HookNotification\Domains\Platforms\WhatsApp\AbstractWhatsAppNotifcation|\Lkn\HookNotification\Domains\Platforms\Chatwoot\AbstractChatwootNotification
*/
$notificationInstance = $this->hookParams['instance'];

/**
* @var \Lkn\HookNotification\Config\ReportCategory
*/
$reportCategory = $notificationInstance->reportCategory;
$reportCategoryId = $notificationInstance->reportCategoryId;

$this->setReportCategory($reportCategory);
$this->setReportCategoryId($reportCategoryId);

$clientId = $notificationInstance->clientId;
$searchBy = $this->getClientWhatsAppNumber($clientId);

$this->setClientId($clientId);

$whatsAppInboxId = Config::get(Platforms::CHATWOOT, Settings::CW_WHATSAPP_INBOX_ID);

if ($whatsAppInboxId === null) {
throw new Exception('WhatsApp inbox ID setting is empty.');
}

$categoryUrl = $this->getUrlPathForObject(
$reportCategory,
$reportCategoryId,
$clientId
);

$msg = Lang::text('notification') . ": {$notificationInstance->lang['notification_title']} [#{$reportCategoryId}]({$categoryUrl})";

$response = $this->api->sendMessageToClient(
$clientId,
$whatsAppInboxId,
$searchBy,
$msg,
true
);

$success = $response['success'];

$this->report($success);

return $success;
}

private function getUrlPathForObject(
ReportCategory $object,
string $objectId,
int $clientId
) {
$objectUrlPath = match ($object) {
ReportCategory::TICKET => 'supporttickets.php?action=view&id=:objectId',
ReportCategory::SERVICE => 'clientsservices.php?userid=:clientId&id=:objectId',
ReportCategory::INVOICE => 'invoices.php?action=edit&id=:objectId',
ReportCategory::ORDER => 'orders.php?action=view&id=:objectId',
ReportCategory::DOMAIN => 'clientsdomains.php?userid=:clientId&id=:objectId'
};

$objectUrlPath = str_replace(':objectId', $objectId, $objectUrlPath);
$objectUrlPath = str_replace(':clientId', $clientId, $objectUrlPath);

return WhmcsApi::getAdminRootUrl($objectUrlPath);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$lang['notification_title'] = 'WhatsApp private note';
$lang['notification_description'] = 'This is used by other notifications for sending a private note to a client.';

return $lang;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$lang['notification_title'] = 'Nota privada do WhatsApp';
$lang['notification_description'] = 'É usada por outras notificações para enviar uma nota privada a um cliente.';

return $lang;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$lang['notification_title'] = 'Nota privada do WhatsApp';
$lang['notification_description'] = 'É usada por outras notificações para enviar uma nota privada a um cliente.';

return $lang;
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
<?php

/**
* Name: Serviço suspenso
* Code: AfterModuleSuspend
* Platform: WhatsApp
* Version: 1.0.0
* Author: Link Nacional
*/

namespace Lkn\HookNotification\Notifications\WhatsApp\AfterModuleSuspend;

use Lkn\HookNotification\Config\Hooks;
use Lkn\HookNotification\Config\ReportCategory;
use Lkn\HookNotification\Domains\Platforms\WhatsApp\AbstractWhatsAppNotifcation;

final class AfterModuleSuspendNotification extends AbstractWhatsAppNotifcation
{
public string $notificationCode = 'AfterModuleSuspend';
public Hooks $hook = Hooks::AFTER_MODULE_SUSPEND;
public Hooks|array|null $hook = Hooks::AFTER_MODULE_SUSPEND;

public function run(): void
public function run(): bool
{
// Setup properties for reporting purposes (not required).
$this->setReportCategory(ReportCategory::SERVICE);
$this->setReportCategoryId($this->hookParams['params']['serviceid']);

// Setup client ID for getting its WhatsApp number (required).
$this->setClientId($this->hookParams['params']['userid']);

// Send the message and get the raw response (converted to array) from WhatsApp API.
$response = $this->sendMessage();

// Defines if response tells if the message was sent successfully.
$success = isset($response['messages'][0]['id']);

$this->report($response, 'invoice', $this->hookParams['invoiceId']);

if ($success) {
$this->events->sendMsgToChatwootAsPrivateNote(
$this->clientId,
"Notificação: serviço suspenso #{$this->hookParams['invoiceId']}"
);
}
return $success;
}

public function defineParameters(): void
{
$this->parameters = [
'service_id' => [
'label' => 'ID do serviço',
'label' => $this->lang['service_id'],
'parser' => fn () => $this->hookParams['params']['serviceid']
],
'client_first_name' => [
'label' => 'Primeiro nome do cliente',
'label' => $this->lang['client_first_name'],
'parser' => fn () => $this->getClientFirstNameByClientId($this->clientId)
],
'client_full_name' => [
'label' => 'Nome completo do cliente',
'label' => $this->lang['client_full_name'],
'parser' => fn () => $this->getClientFullNameByClientId($this->clientId)
]
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$lang['notification_title'] = 'Suspended service';
$lang['notification_description'] = 'Runs when a client service is suspended.';

// Parameters labels

$lang['service_id'] = 'Service ID';
$lang['client_first_name'] = 'Client first name';
$lang['client_full_name'] = 'Client full name';

return $lang;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$lang['notification_title'] = 'Serviço suspenso';
$lang['notification_description'] = 'Executada quando o serviço de um cliente é suspenso.';

// Nomes dos parâmetros

$lang['service_id'] = 'ID do serviço';
$lang['client_first_name'] = 'Primeiro nome do cliente';
$lang['client_full_name'] = 'Nome completo do cliente';

return $lang;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$lang['notification_title'] = 'Serviço suspenso';
$lang['notification_description'] = 'Executada quando o serviço de um cliente é suspenso.';

// Nomes dos parâmetros

$lang['service_id'] = 'ID do serviço';
$lang['client_first_name'] = 'Primeiro nome do cliente';
$lang['client_full_name'] = 'Nome completo do cliente';

return $lang;
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* Code: DomainRenewal5DaysBefore
*/

namespace Lkn\HookNotification\Notifications\WhatsApp\DomainRenewal5DaysBefore;

use DateTime;
use Lkn\HookNotification\Config\Hooks;
use Lkn\HookNotification\Config\Platforms;
use Lkn\HookNotification\Config\ReportCategory;
use Lkn\HookNotification\Config\Settings;
use Lkn\HookNotification\Domains\Platforms\WhatsApp\AbstractWhatsAppNotifcation;
use Lkn\HookNotification\Helpers\Config;
use Lkn\HookNotification\Helpers\Logger;
use Lkn\HookNotification\Notifications\Chatwoot\WhatsAppPrivateNote\WhatsAppPrivateNoteNotification;
use Throwable;
use WHMCS\Database\Capsule;

final class DomainRenewal5DaysBeforeNotification extends AbstractWhatsAppNotifcation
{
public string $notificationCode = 'DomainRenewal5DaysBefore';
public Hooks|array|null $hook = Hooks::DAILY_CRON_JOB;

public function run(): bool
{
// Disable the event of sending a private note to Chatwoot, which is by default for registered clients.
$this->events = [];
$this->enableAutoReport = false;

// Setup properties for reporting purposes (not required).
$this->setReportCategory(ReportCategory::DOMAIN);

$threeDaysLater = (new DateTime())->modify('+3 days');

$domainsDueInThreeDays = Capsule::table('tbldomains')
->where('nextduedate', $threeDaysLater->format('Y-m-d'))
->get(['id', 'userid', 'domain'])
->toArray();

foreach ($domainsDueInThreeDays as $domain) {
$domainId = $domain->id;
$clientId = $domain->userid;
$domainUrl = $domain->domain;

$this->setReportCategoryId($domainId);

// Setup client ID for getting its WhatsApp number (required).
$this->setClientId($clientId);

$this->setHookParams([
'domain_id' => $domainId,
'domain_url' => $domainUrl,
'client_id' => $clientId
]);

try {
// Send the message and get the raw response (converted to array) from WhatsApp API.
$response = $this->sendMessage();

// Defines if response tells if the message was sent successfully.
$success = isset($response['messages'][0]['id']);

$this->report($success);

if (
$success
&& class_exists('Lkn\HookNotification\Notifications\Chatwoot\WhatsAppPrivateNote\WhatsAppPrivateNoteNotification')
&& Config::get(Platforms::CHATWOOT, Settings::CW_LISTEN_WHATSAPP)
) {
(new WhatsAppPrivateNoteNotification(['instance' => $this]))->run();
}
} catch (Throwable $th) {
$this->report(false);

Logger::log(
"{$this->getNotificationLogName()} error for domain {$domainId}",
[
'msg' => 'Unable to send notification for this domain.',
'context' => ['order' => $domain]
],
[
'response' => $response,
'error' => $th->__toString()
]
);
}
}

return true;
}

public function defineParameters(): void
{
$this->parameters = [
'domain_id' => [
'label' => $this->lang['domain_id'],
'parser' => fn () => $this->hookParams['domain_id']
],
'domain_url' => [
'label' => $this->lang['domain_url'],
'parser' => fn () => $this->hookParams['domain_url']
],
'client_first_name' => [
'label' => $this->lang['client_first_name'],
'parser' => fn () => $this->getClientFirstNameByClientId($this->clientId),
],
'client_full_name' => [
'label' => $this->lang['client_full_name'],
'parser' => fn () => $this->getClientFullNameByClientId($this->clientId),
]
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use Lkn\HookNotification\Domains\Notifications\Messenger;
use Lkn\HookNotification\Notifications\WhatsApp\DomainRenewal5DaysBefore\DomainRenewal5DaysBeforeNotification;

Messenger::run(DomainRenewal5DaysBeforeNotification::class);
Loading

0 comments on commit 485fcec

Please sign in to comment.