Skip to content

Commit

Permalink
Merge pull request #75 from ammopt/ammo_sortablelist_24.07.00
Browse files Browse the repository at this point in the history
Preferred Mail Sender
  • Loading branch information
AlexanderBlanchardAC authored Jun 12, 2024
2 parents b90171a + a4019b9 commit bd715ff
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,8 @@
{/if}
{/foreach}
</div>
{elseif $property.type == 'sortableList'}
{include file="DataObjectUtil/sortableList.tpl"}
{/if}
</div>
Expand Down
111 changes: 111 additions & 0 deletions code/web/interface/themes/responsive/DataObjectUtil/sortableList.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{strip}
<ol class="list-unstyled" id="sortable">
{foreach from=$property.values item=propertyName key=propertyValue}
<li draggable="true" style="cursor:grab" data-value='{$propertyValue}'><i class="fas fa-grip-vertical"></i> {$propertyName|escape}</li>
{/foreach}
</ol>
<input type='hidden' name='{$propName}' id='{$propName}' value='{$propValue|escape}'>

<script type="text/javascript">
$(document).ready(function(){
refreshListOptions();
const sortableList =
document.getElementById("sortable");
let draggedItem = null;
function refreshListValue(){
let listOptions = [];
$( "#sortable li" ).each(function( index ) {
listOptions.push($(this).text().trim().replace('|',''));
});
let text = listOptions.join('|');
$("#{$propName}").val(text.trim());
}
function refreshListOptions(drag){
let value = $("#{$propName}").val();
let optionsFromText = value.split("|");
optionsFromText.forEach(textOption => {
$('#sortable').children().each(function () {
if(textOption.trim() == $(this).data().value){
$('#sortable').append($(this));
}
});
});
}
sortableList.addEventListener(
"dragstart",
(e) => {
draggedItem = e.target;
setTimeout(() => {
e.target.style.display =
"none";
}, 0);
});
sortableList.addEventListener(
"dragend",
(e) => {
setTimeout(() => {
e.target.style.display = "";
draggedItem = null;
refreshListValue();
}, 0);
});
sortableList.addEventListener(
"dragover",
(e) => {
e.preventDefault();
const afterElement =
getDragAfterElement(
sortableList,
e.clientY);
const currentElement =
document.querySelector(
".dragging");
if (afterElement == null) {
sortableList.appendChild(
draggedItem
);
} else {
sortableList.insertBefore(
draggedItem,
afterElement
);
}
});
const getDragAfterElement = (
container, y
) => {
const draggableElements = [
...container.querySelectorAll(
"li:not(.dragging)"
),
];
return draggableElements.reduce(
(closest, child) => {
const box =
child.getBoundingClientRect();
const offset =
y - box.top - box.height / 2;
if (
offset < 0 &&
offset > closest.offset) {
return {
offset: offset,
element: child,
};
} else {
return closest;
}
}, {
offset: Number.NEGATIVE_INFINITY,
}
).element;
};
});
</script>
1 change: 1 addition & 0 deletions code/web/release_notes/24.07.00.MD
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

// pedro
- Added a new feature: SMTP setting. This allows for SMTP configuration to be utilized when sending e-mails from Aspen.
- Added a new feature: System variable 'preferredMailSender'. This allows for ordering the different mail senders (SMTP, SendGrid, AmazonSES) to set the priority order in which they should be used.

// other

Expand Down
9 changes: 9 additions & 0 deletions code/web/sys/DBMaintenance/version_updates/24.07.00.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ function getUpdates24_07_00(): array {
],
], //permissions_create_administer_smtp

'systemVariables_preferredMailSender' => [
'title' => 'System Variables - Preferred Mail Sender',
'description' => 'Allow sortable configuration of which mail sender to use by order of priority.',
'sql' => [
"alter table system_variables ADD COLUMN preferredMailSender varchar(128) DEFAULT 'SMTP|AmazonSES|SendGrid'",
],
],
//systemVariables_preferredMailSender

//other


Expand Down
2 changes: 2 additions & 0 deletions code/web/sys/DataObjectUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ static function processProperty(DataObject $object, $property) {
}

$object->$propertyName = $values;
} elseif ($property['type'] == 'sortableList') {
$object->setProperty($propertyName, $_REQUEST[$propertyName], $property);
}
}
}
42 changes: 30 additions & 12 deletions code/web/sys/Email/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,17 @@ public function send($to, $subject, $body, $replyTo = null, $htmlBody = null, $a
require_once ROOT_DIR . '/sys/Email/AmazonSesSetting.php';
require_once ROOT_DIR . '/sys/Email/SMTPSetting.php';
require_once ROOT_DIR . '/sys/CurlWrapper.php';
require_once ROOT_DIR . '/sys/SystemVariables.php';
//TODO: Do validation of the address
$amazonSesSettings = new AmazonSesSetting();
$smtpServerSettings = new SMTPSetting();

if($smtpServerSettings->find(true)) {
$result = $this->sendViaSMTP($smtpServerSettings, $to, $replyTo, $subject, $body, $htmlBody, $attachments);
}elseif ($amazonSesSettings->find(true)) {
$result = $this->sendViaAmazonSes($amazonSesSettings, $to, $replyTo, $subject, $body, $htmlBody, $attachments);
} else {
$sendGridSettings = new SendGridSetting();
if ($sendGridSettings->find(true)) {
$result = $this->sendViaSendGrid($sendGridSettings, $to, $replyTo, $subject, $body, $htmlBody);
} else {
$result = false;
$systemVariables = new SystemVariables();
if ($systemVariables->find(true) && !empty($systemVariables->preferredMailSender)) {
$mailSenders = explode("|", $systemVariables->preferredMailSender);
foreach ($mailSenders as $mailSender){
$result = $this->sendViaAbstractSender($mailSender, $to, $replyTo, $subject, $body, $htmlBody, $attachments);
if ($result) {
break;
}
}
}

Expand Down Expand Up @@ -145,4 +142,25 @@ private function sendViaAmazonSes(AmazonSesSetting $amazonSesSettings, string $t
private function sendViaSMTP(SMTPSetting $smtpSettings, string $to, ?string $replyTo, string $subject, ?string $body, ?string $htmlBody, ?array $attachments): bool {
return $smtpSettings->sendEmail($to, $replyTo, $subject, $body, $htmlBody, $attachments);
}

private function sendViaAbstractSender($mailSender, $to, $replyTo, $subject, $body, $htmlBody, $attachments) {
$result = false;
if($mailSender == 'AmazonSES') {
$amazonSesSettings = new AmazonSesSetting();
if($amazonSesSettings->find(true)){
$result = $this->sendViaAmazonSes($amazonSesSettings, $to, $replyTo, $subject, $body, $htmlBody, $attachments);
}
} else if($mailSender == 'SendGrid') {
$sendGridSettings = new SendGridSetting();
if($sendGridSettings->find(true)){
$result = $this->sendViaSendGrid($sendGridSettings, $to, $replyTo, $subject, $body, $htmlBody);
}
} else if($mailSender == 'SMTP') {
$smtpSettings = new SMTPSetting();
if($smtpSettings->find(true)){
$result = $this->sendViaSMTP($smtpSettings, $to, $replyTo, $subject, $body, $htmlBody, $attachments);
}
}
return $result;
}
}
61 changes: 41 additions & 20 deletions code/web/sys/SystemVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SystemVariables extends DataObject {
public $errorEmail;
public $ticketEmail;
public $searchErrorEmail;
public $preferredMailSender;
public $loadCoversFrom020z;
public $currencyCode;
public $runNightlyFullIndex;
Expand Down Expand Up @@ -59,26 +60,46 @@ static function getObjectStructure($context = ''): array {
'description' => 'URL of the community content server',
'maxLength' => 128,
],
'errorEmail' => [
'property' => 'errorEmail',
'type' => 'text',
'label' => 'Error Email Address',
'description' => 'Email Address to send errors to',
'maxLength' => 128,
],
'ticketEmail' => [
'property' => 'ticketEmail',
'type' => 'text',
'label' => 'Ticket Email Address',
'description' => 'Email Address to send tickets from administrators to',
'maxLength' => 128,
],
'searchErrorEmail' => [
'property' => 'searchErrorEmail',
'type' => 'text',
'label' => 'Search Error Email Address',
'description' => 'Email Address to send errors to',
'maxLength' => 128,
'emailSection' => [
'property' => 'emailSection',
'type' => 'section',
'label' => 'Email Settings',
'hideInLists' => true,
'expandByDefault' => true,
'properties' => [
'errorEmail' => [
'property' => 'errorEmail',
'type' => 'text',
'label' => 'Error Email Address',
'description' => 'Email Address to send errors to',
'maxLength' => 128,
],
'ticketEmail' => [
'property' => 'ticketEmail',
'type' => 'text',
'label' => 'Ticket Email Address',
'description' => 'Email Address to send tickets from administrators to',
'maxLength' => 128,
],
'searchErrorEmail' => [
'property' => 'searchErrorEmail',
'type' => 'text',
'label' => 'Search Error Email Address',
'description' => 'Email Address to send errors to',
'maxLength' => 128,
],
'preferredMailSender' => [
'property' => 'preferredMailSender',
'type' => 'sortableList',
'values' => [
'AmazonSES' => 'AmazonSES',
'SendGrid' => 'SendGrid',
'SMTP' => 'SMTP',
],
'label' => 'Preferred Mail Sender Order',
'description' => 'Sort order of preferred mail sender',
],
]
],
'googleBucket' => [
'property' => 'googleBucket',
Expand Down
1 change: 1 addition & 0 deletions install/aspen.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4949,6 +4949,7 @@ CREATE TABLE `system_variables` (
`errorEmail` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
`ticketEmail` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
`searchErrorEmail` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
`preferredMailSender` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT 'SMTP|AmazonSES|SendGrid',
`loadCoversFrom020z` tinyint(1) DEFAULT '0',
`runNightlyFullIndex` tinyint(1) DEFAULT '0',
`currencyCode` char(3) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT 'USD',
Expand Down

0 comments on commit bd715ff

Please sign in to comment.