Skip to content

Commit

Permalink
[ENH] add functionality to send later scheduled sending
Browse files Browse the repository at this point in the history
  • Loading branch information
amaninyumu1 committed Jun 16, 2024
1 parent d7df56d commit 0c58a6a
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 126 deletions.
48 changes: 48 additions & 0 deletions modules/core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,51 @@ function get_special_folders($mod, $id) {
}
return array();
}

/**
* @subpackage core/functions
*/
if (!hm_exists('get_nexter_date')) {
function get_nexter_date($format, $only_label = false) {
if ($format == 'later_in_day') {
$date_string = 'today 18:00';
$label = 'Later in the day';
} elseif ($format == 'tomorrow') {
$date_string = '+1 day 08:00';
$label = 'Tomorrow';
} elseif ($format == 'next_weekend') {
$date_string = 'next Saturday 08:00';
$label = 'Next weekend';
} elseif ($format == 'next_week') {
$date_string = 'next week 08:00';
$label = 'Next week';
} elseif ($format == 'next_month') {
$date_string = 'next month 08:00';
$label = 'Next month';
} else {
$date_string = $format;
$label = 'Certain date';
}
$time = strtotime($date_string);
if ($only_label) {
return [$label, date('D, H:i', $time)];
}
return date('D, d M Y H:i', $time);
}}

/**
* @subpackage imap/functions
*/
if (!hm_exists('nexter_formats')) {
function nexter_formats() {
$values = array(
'tomorrow',
'next_weekend',
'next_week',
'next_month'
);
if (date('H') <= 16) {
array_push($values, 'later_in_day');
}
return $values;
}}
66 changes: 9 additions & 57 deletions modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1338,74 +1338,26 @@ function parse_snooze_header($snooze_header)
return $result;
}}

/**
* @subpackage imap/functions
*/
if (!hm_exists('get_snooze_date')) {
function get_snooze_date($format, $only_label = false) {
if ($format == 'later_in_day') {
$date_string = 'today 18:00';
$label = 'Later in the day';
} elseif ($format == 'tomorrow') {
$date_string = '+1 day 08:00';
$label = 'Tomorrow';
} elseif ($format == 'next_weekend') {
$date_string = 'next Saturday 08:00';
$label = 'Next weekend';
} elseif ($format == 'next_week') {
$date_string = 'next week 08:00';
$label = 'Next week';
} elseif ($format == 'next_month') {
$date_string = 'next month 08:00';
$label = 'Next month';
} else {
$date_string = $format;
$label = 'Certain date';
}
$time = strtotime($date_string);
if ($only_label) {
return [$label, date('D, H:i', $time)];
}
return date('D, d M Y H:i', $time);
}}

/**
* @subpackage imap/functions
*/
if (!hm_exists('snooze_formats')) {
function snooze_formats() {
$values = array(
'tomorrow',
'next_weekend',
'next_week',
'next_month'
);
if (date('H') <= 16) {
array_push($values, 'later_in_day');
}
return $values;
}}

/**
* @subpackage imap/functions
*/
if (!hm_exists('snooze_dropdown')) {
function snooze_dropdown($output, $unsnooze = false) {
$values = snooze_formats();
$values = nexter_formats();

$txt = '<div class="dropdown d-inline-block">
<button type="button" class="btn btn-outline-success btn-sm dropdown-toggle" id="dropdownMenuSnooze" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="true">'.$output->trans('Snooze').'</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuSnooze">';
<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">';
foreach ($values as $format) {
$labels = get_snooze_date($format, true);
$txt .= '<li><a href="#" class="snooze_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>';
$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="snooze_input_date" class="snooze_date_picker dropdown-item cursor-pointer">'.$output->trans('Pick a date').'</label>';
$txt .= '<input id="snooze_input_date" type="datetime-local" min="'.date('Y-m-d\Th:m').'" class="snooze_input_date" style="visibility: hidden; position: absolute; height: 0;">';
$txt .= '<input class="snooze_input" style="display:none;"></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 ($unsnooze) {
$txt .= '<a href="#" data-value="unsnooze" class="unsnooze snooze_helper dropdown-item"">'.$output->trans('Unsnooze').'</a>';
$txt .= '<a href="#" data-value="unsnooze" class="unsnooze nexter_date_helper dropdown-item"">'.$output->trans('Unsnooze').'</a>';
}
$txt .= '</ul></div>';

Expand Down
2 changes: 1 addition & 1 deletion modules/imap/output_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ protected function output() {
elseif ($fld == 'x-snoozed') {
$snooze_header = parse_snooze_header($value);
$txt .= '<tr class="header_'.$fld.'"><th>';
$txt .= $this->trans('Snoozed').'</th><td>'.$this->trans('Until').' '.$this->html_safe($snooze_header['until']).' <a href="#" data-value="unsnooze" class="unsnooze snooze_helper">Unsnooze</a></td></tr>';
$txt .= $this->trans('Snoozed').'</th><td>'.$this->trans('Until').' '.$this->html_safe($snooze_header['until']).' <a href="#" data-value="unsnooze" class="unsnooze nexter_date_helper">Unsnooze</a></td></tr>';
}
elseif ($fld == 'date') {
try {
Expand Down
4 changes: 2 additions & 2 deletions modules/imap/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@
.header_links {
padding-top: 10px !important;
}
.header_links #dropdownMenuSnooze {
.header_links #dropdownMenuNexterDate {
padding: 0;
border: unset;
font-variant: inherit;
text-transform: inherit;
font-size: inherit;
vertical-align: baseline;
}
.header_links #dropdownMenuSnooze:hover {
.header_links #dropdownMenuNexterDate:hover {
background-color: inherit;
color: inherit;
}
Expand Down
86 changes: 30 additions & 56 deletions modules/imap/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -1072,61 +1072,6 @@ var imap_folder_status = function() {
}
};

var imap_setup_snooze = function() {
$(document).on('click', '.snooze_date_picker', function(e) {
document.querySelector('.snooze_input_date').showPicker();
});
$(document).on('click', '.snooze_helper', function(e) {
e.preventDefault();
$('.snooze_input').val($(this).attr('data-value')).trigger('change');
});
$(document).on('input', '.snooze_input_date', function(e) {
var now = new Date();
now.setMinutes(now.getMinutes() + 1);
$(this).attr('min', now.toJSON().slice(0, 16));
if (new Date($(this).val()).getTime() <= now.getTime()) {
$('.snooze_date_picker').css('border', '1px solid red');
} else {
$('.snooze_date_picker').css({'border': 'unset', 'border-top': '1px solid #ddd'});
}
});
$(document).on('change', '.snooze_input_date', function(e) {
if ($(this).val() && new Date().getTime() < new Date($(this).val()).getTime()) {
$('.snooze_input').val($(this).val()).trigger('change');
}
});
$(document).on('change', '.snooze_input', function(e) {
$('.snooze_dropdown').hide();
var ids = [];
if (hm_page_name() == 'message') {
var list_path = hm_list_path().split('_');
ids.push(list_path[1]+'_'+hm_msg_uid()+'_'+list_path[2]);
} else {
$('input[type=checkbox]').each(function() {
if (this.checked && this.id.search('imap') != -1) {
var parts = this.id.split('_');
ids.push(parts[1]+'_'+parts[2]+'_'+parts[3]);
}
});
if (ids.length == 0) {
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 imap_unsnooze_messages = function() {
Hm_Ajax.request(
[{'name': 'hm_ajax_hook', 'value': 'ajax_imap_unsnooze'}],
Expand Down Expand Up @@ -1184,7 +1129,36 @@ $(function() {
}

if (hm_page_name() === 'message_list' || hm_page_name() === 'message') {
imap_setup_snooze();
setup_nexter_date(function(e) {
$('.snooze_dropdown').hide();
var ids = [];
if (hm_page_name() == 'message') {
var list_path = hm_list_path().split('_');
ids.push(list_path[1]+'_'+hm_msg_uid()+'_'+list_path[2]);
} else {
$('input[type=checkbox]').each(function() {
if (this.checked && this.id.search('imap') != -1) {
var parts = this.id.split('_');
ids.push(parts[1]+'_'+parts[2]+'_'+parts[3]);
}
});
if (ids.length == 0) {
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);
}
}
);
});
}

if (hm_is_logged()) {
Expand Down
20 changes: 20 additions & 0 deletions modules/smtp/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ 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 = '<div class="dropup d-inline-block">
<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></div>';

return $txt;
}}


if (!hm_exists('delete_smtp_server')) {
function delete_smtp_server($smtp_server_id) {
Hm_SMTP_List::del($smtp_server_id);
Expand Down
6 changes: 5 additions & 1 deletion modules/smtp/hm-mime-message.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ class Hm_MIME_Msg {
private $final_msg = '';

/* build mime message data */
function __construct($to, $subject, $body, $from, $html=false, $cc='', $bcc='', $in_reply_to_id='', $from_name='', $reply_to='') {
function __construct($to, $subject, $body, $from, $html=false, $cc='', $bcc='', $in_reply_to_id='', $from_name='', $reply_to='', $schedule='') {
if ($cc) {
$this->headers['Cc'] = $cc;
}
if ($schedule) {
$this->headers['X-Schedule'] = $schedule;
}

if ($in_reply_to_id) {
$this->headers['In-Reply-To'] = $in_reply_to_id;
}
Expand Down
Loading

0 comments on commit 0c58a6a

Please sign in to comment.