cap days-before to under one period, per frequency (v1.1.1)
This commit is contained in:
50
Asset/js/recoreco-modal.js
Normal file
50
Asset/js/recoreco-modal.js
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* RecoReco -- keep the "days before" field within one period of the selected frequency.
|
||||
*
|
||||
* The Recurring schedule modal is injected by AJAX, so we react to the frequency <select> both when
|
||||
* it appears (MutationObserver) and when it changes (delegated event). Daily -> field disabled (0);
|
||||
* weekly -> max 6; monthly -> max 27; yearly -> max 364. The server clamps too, so this is only the
|
||||
* UI guard.
|
||||
*/
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
var CAPS = { daily: 0, weekly: 6, monthly_day: 27, monthly_dow: 27, yearly: 364 };
|
||||
|
||||
function apply(select) {
|
||||
var input = document.querySelector('input[name="recoreco_days_before"]');
|
||||
if (! input) {
|
||||
return;
|
||||
}
|
||||
|
||||
var max = CAPS.hasOwnProperty(select.value) ? CAPS[select.value] : 27;
|
||||
|
||||
if (max === 0) {
|
||||
input.value = 0;
|
||||
input.setAttribute("max", "0");
|
||||
input.disabled = true;
|
||||
} else {
|
||||
input.disabled = false;
|
||||
input.setAttribute("max", String(max));
|
||||
if (parseInt(input.value, 10) > max) {
|
||||
input.value = max;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("change", function (e) {
|
||||
if (e.target && e.target.name === "recoreco_frequency") {
|
||||
apply(e.target);
|
||||
}
|
||||
});
|
||||
|
||||
if (window.MutationObserver) {
|
||||
new MutationObserver(function () {
|
||||
var select = document.querySelector('select[name="recoreco_frequency"]:not([data-recoreco-init])');
|
||||
if (select) {
|
||||
select.setAttribute("data-recoreco-init", "1");
|
||||
apply(select);
|
||||
}
|
||||
}).observe(document.body, { childList: true, subtree: true });
|
||||
}
|
||||
})();
|
||||
@@ -46,12 +46,16 @@ class RecurrenceController extends BaseController
|
||||
? $input['recoreco_frequency']
|
||||
: 'monthly_day';
|
||||
|
||||
// Keep the lead time under one period (else a copy's fire time crosses the prior occurrence).
|
||||
$daysBefore = isset($input['recoreco_days_before']) && ctype_digit((string) $input['recoreco_days_before']) ? (int) $input['recoreco_days_before'] : 0;
|
||||
$daysBefore = min($daysBefore, $this->maxDaysBefore($frequency));
|
||||
|
||||
$values = array(
|
||||
'recoreco_enabled' => $enabled,
|
||||
'recoreco_target_column' => isset($input['recoreco_target_column']) ? (int) $input['recoreco_target_column'] : (int) $task['column_id'],
|
||||
'recoreco_frequency' => $frequency,
|
||||
'recoreco_last_day' => isset($input['recoreco_last_day']) ? 1 : 0,
|
||||
'recoreco_days_before' => isset($input['recoreco_days_before']) && ctype_digit((string) $input['recoreco_days_before']) ? (int) $input['recoreco_days_before'] : 0,
|
||||
'recoreco_days_before' => $daysBefore,
|
||||
'recoreco_link_copies' => isset($input['recoreco_link_copies']) ? 1 : 0,
|
||||
);
|
||||
|
||||
@@ -97,4 +101,22 @@ class RecurrenceController extends BaseController
|
||||
'daily' => t('Daily'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The largest allowed "days before" for a frequency -- kept strictly under one period so a
|
||||
* copy's fire time never crosses the previous occurrence. Daily = 0 (disabled).
|
||||
*/
|
||||
private function maxDaysBefore($frequency)
|
||||
{
|
||||
switch ($frequency) {
|
||||
case 'daily':
|
||||
return 0;
|
||||
case 'weekly':
|
||||
return 6;
|
||||
case 'yearly':
|
||||
return 364;
|
||||
default: // monthly_day, monthly_dow
|
||||
return 27;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,11 @@ class Plugin extends Base
|
||||
// (next to native "Edit recurrence"). Opens the RecoReco config modal.
|
||||
$this->template->hook->attach('template:task:sidebar:after-basic-actions', 'recoReco:task/sidebar_action');
|
||||
|
||||
// Modal JS: keep "days before" within one period of the selected frequency.
|
||||
$this->hook->on('template:layout:js', array(
|
||||
'template' => 'plugins/RecoReco/Asset/js/recoreco-modal.js',
|
||||
));
|
||||
|
||||
// The scheduling engine runs from the CLI (cron). Registered CLI-only so web requests do
|
||||
// not build the console app.
|
||||
if (php_sapi_name() === 'cli') {
|
||||
@@ -36,7 +41,7 @@ class Plugin extends Base
|
||||
|
||||
public function getPluginVersion()
|
||||
{
|
||||
return '1.1.0';
|
||||
return '1.1.1';
|
||||
}
|
||||
|
||||
public function getPluginHomepage()
|
||||
|
||||
Reference in New Issue
Block a user