Skip to content

Commit

Permalink
Added the possibility to change time to send message or send it immed…
Browse files Browse the repository at this point in the history
…iately
  • Loading branch information
josaphatim committed Jun 27, 2024
1 parent 0aef136 commit 36599a8
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 103 deletions.
30 changes: 30 additions & 0 deletions modules/core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,36 @@ function nexter_formats() {
return $values;
}}

if (!hm_exists('schedule_dropdown')) {
function schedule_dropdown($output, $send_now = false) {
$values = nexter_formats();

$txt = '';
if ($send_now) {
$txt .= '<div class="dropdown d-inline-block">
<button type="button" class="btn btn-light btn-sm dropdown-toggle" id="dropdownMenuNexterDate" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="true">'.$output->trans('Schedule sending').'</button>';
}
$txt .= '<ul class="dropdown-menu nexter_dropdown schedule_dropdown" aria-labelledby="dropdownMenuNexterDate">';
foreach ($values as $format) {
$labels = get_nexter_date($format, true);
$txt .= '<li><a href="#" class="nexter_date_helper dropdown-item d-flex justify-content-between gap-5" data-value="'.$format.'"><span>'.$output->trans($labels[0]).'</span> <span class="text-end">'.$labels[1].'</span></a></li>';
}
$txt .= '<li><hr class="dropdown-divider"></li>';
$txt .= '<li><label for="nexter_input_date" class="nexter_date_picker dropdown-item cursor-pointer">'.$output->trans('Pick a date').'</label>';
$txt .= '<input id="nexter_input_date" type="datetime-local" min="'.date('Y-m-d\Th:m').'" class="nexter_input_date" style="visibility: hidden; position: absolute; height: 0;">';
$txt .= '<input class="nexter_input" style="display:none;"></li>';
if ($send_now) {
$txt .= '<li><hr class="dropdown-divider"></li>';
$txt .= '<li><a href="#" data-value="now" class="nexter_date_helper dropdown-item"">'.$output->trans('Send now').'</a></li>';
}
$txt .= '</ul>';
if ($send_now) {
$txt .= '</div>';
}

return $txt;
}}

/**
* @subpackage imap/functions
*/
Expand Down
6 changes: 3 additions & 3 deletions modules/core/message_list_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ function subject_callback($vals, $style, $output_mod) {
*/
if (!hm_exists('date_callback')) {
function date_callback($vals, $style, $output_mod) {
$snooze_class = isset($vals[2]) && $vals[2]? ' snoozed_date': '';
$nexter_class = isset($vals[2]) && $vals[2]? ' nexter_date': '';
if ($style == 'news') {
return sprintf('<div class="msg_date%s">%s<input type="hidden" class="msg_timestamp" value="%s" /></div>', $snooze_class, $output_mod->html_safe($vals[0]), $output_mod->html_safe($vals[1]));
return sprintf('<div class="msg_date%s">%s<input type="hidden" class="msg_timestamp" value="%s" /></div>', $nexter_class, $output_mod->html_safe($vals[0]), $output_mod->html_safe($vals[1]));
}
return sprintf('<td class="msg_date%s" title="%s">%s<input type="hidden" class="msg_timestamp" value="%s" /></td>', $snooze_class, $output_mod->html_safe(date('r', $vals[1])), $output_mod->html_safe($vals[0]), $output_mod->html_safe($vals[1]));
return sprintf('<td class="msg_date%s" title="%s">%s<input type="hidden" class="msg_timestamp" value="%s" /></td>', $nexter_class, $output_mod->html_safe(date('r', $vals[1])), $output_mod->html_safe($vals[0]), $output_mod->html_safe($vals[1]));
}}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/core/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ div.unseen,
.mobile .imap_sort {
width: 100%;
}
.snoozed_date {
.nexter_date {
color: var(--bs-primary) !important;
}

Expand Down
12 changes: 8 additions & 4 deletions modules/core/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -2707,26 +2707,30 @@ const observeMessageTextMutationAndHandleExternalResources = (inline) => {
};

var setup_nexter_date = function(callback) {
var scopedElement = function (el, selector) {
return $(el).parent().parent().find(selector);
}
$(document).on('click', '.nexter_date_picker', function(e) {
document.querySelector('.nexter_input_date').showPicker();
});
$(document).on('click', '.nexter_date_helper', function(e) {
e.preventDefault();
$('.nexter_input').val($(this).attr('data-value')).trigger('change');
scopedElement(this, '.nexter_input').val($(this).attr('data-value')).trigger('change');
});
$(document).on('input', '.nexter_input_date', function(e) {
var now = new Date();
now.setMinutes(now.getMinutes() + 1);
$(this).attr('min', now.toJSON().slice(0, 16));
var date_picker = scopedElement(this, '.nexter_date_picker');
if (new Date($(this).val()).getTime() <= now.getTime()) {
$('.nexter_date_picker').css('border', '1px solid red');
date_picker.css('border', '1px solid red');
} else {
$('.nexter_date_picker').css({'border': 'unset', 'border-top': '1px solid #ddd'});
date_picker.css({'border': 'unset', 'border-top': '1px solid #ddd'});
}
});
$(document).on('change', '.nexter_input_date', function(e) {
if ($(this).val() && new Date().getTime() < new Date($(this).val()).getTime()) {
$('.nexter_input').val($(this).val()).trigger('change');
scopedElement(this, '.nexter_input').val($(this).val()).trigger('change');
}
});
$(document).on('change', '.nexter_input', callback);
Expand Down
10 changes: 7 additions & 3 deletions modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,14 @@ function format_imap_message_list($msg_list, $output_module, $parent_list=false,
$nofrom = ' nofrom';
}
$is_snoozed = !empty($msg['x_snoozed']) && hex2bin($msg['folder']) == 'Snoozed';
$is_scheduled = !empty($msg['x_schedule']) && hex2bin($msg['folder']) == 'Scheduled';
if ($is_snoozed) {
$snooze_header = parse_nexter_header('X-Snoozed: '.$msg['x_snoozed'], 'X-Snoozed');
$date = $snooze_header['until'];
$timestamp = strtotime($date);
} elseif ($is_scheduled) {
$date = $msg['x_schedule'];
$timestamp = strtotime($date);
} else {
if ($list_sort == 'date') {
$date_field = 'date';
Expand Down Expand Up @@ -303,7 +307,7 @@ function format_imap_message_list($msg_list, $output_module, $parent_list=false,
array('safe_output_callback', 'source', $source, $icon),
array('safe_output_callback', 'from'.$nofrom, $from, null, str_replace(array($from, '<', '>'), '', $msg['from'])),
array('subject_callback', $subject, $url, $flags),
array('date_callback', $date, $timestamp, $is_snoozed),
array('date_callback', $date, $timestamp, $is_snoozed || $is_scheduled),
array('icon_callback', $flags)
),
$id,
Expand Down Expand Up @@ -1325,8 +1329,8 @@ function snooze_dropdown($output, $unsnooze = false) {
$values = nexter_formats();

$txt = '<div class="dropdown d-inline-block">
<button type="button" class="btn btn-outline-success btn-sm dropdown-toggle" id="dropdownMenuNexterDate" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="true">'.$output->trans('Snooze').'</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuNexterDate">';
<button type="button" class="btn btn-light btn-sm dropdown-toggle" id="dropdownMenuNexterDate" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="true">'.$output->trans('Snooze').'</button>
<ul class="dropdown-menu nexter_dropdown snooze_dropdown" aria-labelledby="dropdownMenuNexterDate">';
foreach ($values as $format) {
$labels = get_nexter_date($format, true);
$txt .= '<li><a href="#" class="nexter_date_helper dropdown-item d-flex justify-content-between gap-5" data-value="'.$format.'"><span>'.$output->trans($labels[0]).'</span> <span class="text-end">'.$labels[1].'</span></a></li>';
Expand Down
5 changes: 4 additions & 1 deletion modules/imap/output_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,12 @@ protected function output() {
$txt .= ' | <a class="hlink" id="copy_message" href="#">'.$this->trans('Copy').'</a>';
$txt .= ' | <a class="hlink" id="move_message" href="#">'.$this->trans('Move').'</a>';
$txt .= ' | <a class="archive_link hlink" id="archive_message" href="#">'.$this->trans('Archive').'</a>';
if (isset($headers['X-Schedule'])) {
$txt .= ' | ' . schedule_dropdown($this, true);
}

$is_draft = isset($headers['Flags']) && stristr($headers['Flags'], 'draft');
if ($this->get('sieve_filters_enabled') && !$is_draft) {
if ($this->get('sieve_filters_enabled') && !$is_draft && isset($headers['X-Snoozed'])) {
$txt .= ' | ' . snooze_dropdown($this, isset($headers['X-Snoozed']));
$server_id = $this->get('msg_server_id');
$imap_server = $this->get('imap_accounts')[$server_id];
Expand Down
40 changes: 28 additions & 12 deletions modules/imap/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ $(function() {

if (hm_page_name() === 'message_list' || hm_page_name() === 'message') {
setup_nexter_date(function(e) {
$('.snooze_dropdown').hide();
$('.nexter_dropdown').hide();
var ids = [];
if (hm_page_name() == 'message') {
var list_path = hm_list_path().split('_');
Expand All @@ -1146,18 +1146,34 @@ $(function() {
return;
};
}
Hm_Ajax.request(
[{'name': 'hm_ajax_hook', 'value': 'ajax_imap_snooze'},
{'name': 'imap_snooze_ids', 'value': ids},
{'name': 'imap_snooze_until', 'value': $(this).val()}],
function(res) {
if (res.snoozed_messages > 0) {
Hm_Folders.reload_folders(true);
var path = hm_list_parent()? hm_list_parent(): hm_list_path();
window.location.replace('?page=message_list&list_path='+path);
var reload_and_redirect = function() {
Hm_Folders.reload_folders(true);
var path = hm_list_parent()? hm_list_parent(): hm_list_path();
window.location.replace('?page=message_list&list_path='+path);
}
if ($(this).parent().parent().is('.snooze_dropdown')) {
Hm_Ajax.request(
[{'name': 'hm_ajax_hook', 'value': 'ajax_imap_snooze'},
{'name': 'imap_snooze_ids', 'value': ids},
{'name': 'imap_snooze_until', 'value': $(this).val()}],
function(res) {
if (res.snoozed_messages > 0) {
reload_and_redirect();
}
}
}
);
);
} else {
Hm_Ajax.request(
[{'name': 'hm_ajax_hook', 'value': 'ajax_re_schedule_message_sending'},
{'name': 'scheduled_msg_ids', 'value': ids},
{'name': 'schedule_date', 'value': $(this).val()}],
function(res) {
if (res.scheduled_msg_count > 0) {
reload_and_redirect();
}
}
);
}
});
}

Expand Down
19 changes: 0 additions & 19 deletions modules/smtp/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,6 @@ function connect_to_smtp_server($address, $name, $port, $user, $pass, $tls, $ser
}
}

if (!hm_exists('schedule_dropdown')) {
function schedule_dropdown($output) {
$values = nexter_formats();

$txt = '<ul class="dropdown-menu" aria-labelledby="dropdownMenuNexterDate">';
foreach ($values as $format) {
$labels = get_nexter_date($format, true);
$txt .= '<li><a href="#" class="nexter_date_helper dropdown-item d-flex justify-content-between gap-5" data-value="'.$format.'"><span>'.$output->trans($labels[0]).'</span> <span class="text-end">'.$labels[1].'</span></a></li>';
}
$txt .= '<li><hr class="dropdown-divider"></li>';
$txt .= '<li><label for="nexter_input_date" class="nexter_date_picker dropdown-item cursor-pointer">'.$output->trans('Pick a date').'</label>';
$txt .= '<input id="nexter_input_date" type="datetime-local" min="'.date('Y-m-d\Th:m').'" class="nexter_input_date" style="visibility: hidden; position: absolute; height: 0;">';
$txt .= '<input class="nexter_input" style="display:none;"></li>';
$txt .= '</ul>';

return $txt;
}}


if (!hm_exists('delete_smtp_server')) {
function delete_smtp_server($smtp_server_id) {
Hm_SMTP_List::del($smtp_server_id);
Expand Down
Loading

0 comments on commit 36599a8

Please sign in to comment.