diff --git a/Console/RecoRecoCommand.php b/Console/RecoRecoCommand.php new file mode 100644 index 0000000..e361d4b --- /dev/null +++ b/Console/RecoRecoCommand.php @@ -0,0 +1,32 @@ +setName('recoreco:run') + ->setDescription('Spawn RecoReco calendar-scheduled cards whose occurrence is due'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $model = new RecoRecoModel($this->container); + $count = $model->run(); + + $output->writeln('RecoReco: spawned '.$count.' card(s).'); + + return 0; + } +} diff --git a/Model/OccurrenceCalculator.php b/Model/OccurrenceCalculator.php new file mode 100644 index 0000000..8e8fd65 --- /dev/null +++ b/Model/OccurrenceCalculator.php @@ -0,0 +1,70 @@ +monthlyByDay((int) $anchor, (bool) $lastDay, (int) $reference, (bool) $inclusive); + default: + return null; + } + } + + private function monthlyByDay($anchor, $lastDay, $reference, $inclusive) + { + $anchorDay = (int) date('j', $anchor); + $h = (int) date('G', $anchor); + $i = (int) date('i', $anchor); + $s = (int) date('s', $anchor); + + $y = (int) date('Y', $reference); + $m = (int) date('n', $reference); + + for ($k = 0; $k < self::MAX_MONTHS; $k++) { + $lastDom = (int) date('t', mktime(0, 0, 0, $m, 1, $y)); + $day = $lastDay ? $lastDom : min($anchorDay, $lastDom); + $occurrence = mktime($h, $i, $s, $m, $day, $y); + + if ($inclusive ? $occurrence >= $reference : $occurrence > $reference) { + return $occurrence; + } + + $m++; + if ($m > 12) { + $m = 1; + $y++; + } + } + + return null; + } +} diff --git a/Model/RecoRecoModel.php b/Model/RecoRecoModel.php new file mode 100644 index 0000000..4a60240 --- /dev/null +++ b/Model/RecoRecoModel.php @@ -0,0 +1,170 @@ +db->table('task_has_metadata') + ->eq('name', 'recoreco_enabled') + ->eq('value', '1') + ->findAllByColumn('task_id'); + + foreach ($task_ids as $task_id) { + $spawned += $this->processTemplate((int) $task_id, $now); + } + + return $spawned; + } + + public function processTemplate($task_id, $now) + { + $task = $this->taskFinderModel->getById($task_id); + + if (empty($task)) { + return 0; + } + + // Native wins: if the card became native-recurring, RecoReco yields (decision 13). + if ($task['recurrence_status'] != TaskModel::RECURRING_STATUS_NONE) { + $this->taskMetadataModel->save($task_id, array('recoreco_enabled' => 0)); + $this->logger->info('RecoReco: disabled on task '.$task_id.' (now native-recurring)'); + return 0; + } + + $meta = $this->taskMetadataModel->getAll($task_id); + $frequency = isset($meta['recoreco_frequency']) ? $meta['recoreco_frequency'] : ''; + + // v0.3 handles monthly-by-day only; the rest arrive in v1.1. + if ($frequency !== 'monthly_day') { + return 0; + } + + $anchor = isset($meta['recoreco_anchor']) ? (int) $meta['recoreco_anchor'] : 0; + + if ($anchor <= 0) { + return 0; + } + + $lastDay = isset($meta['recoreco_last_day']) && $meta['recoreco_last_day'] == 1; + $daysBefore = isset($meta['recoreco_days_before']) ? (int) $meta['recoreco_days_before'] : 0; + $targetColumn = isset($meta['recoreco_target_column']) ? (int) $meta['recoreco_target_column'] : (int) $task['column_id']; + + $cursor = (int) $task['date_due']; + $synced = isset($meta['recoreco_synced_due']) ? (int) $meta['recoreco_synced_due'] : null; + + if ($synced === null) { + // First run after enabling: place the cursor on the first occurrence. + $cursor = $this->firstCursor($anchor, $frequency, $lastDay, $now); + $this->setDue($task_id, $cursor); + } elseif ($cursor !== $synced) { + // The user hand-edited the due date -> re-anchor to it (a reschedule). + $anchor = $cursor; + $this->taskMetadataModel->save($task_id, array('recoreco_anchor' => $anchor)); + $this->setDue($task_id, $cursor); + } + + // Fire every occurrence whose fire time has arrived, advancing the cursor each time. + $spawned = 0; + $fireTime = $cursor - $daysBefore * 86400; + + while ($fireTime <= $now && $spawned < self::CATCHUP_CAP) { + $this->spawn($task, $targetColumn); + + $next = $this->calculator()->occurrenceFrom($anchor, $frequency, $lastDay, $cursor, false); + + if ($next === null || $next <= $cursor) { + break; + } + + $cursor = $next; + $this->setDue($task_id, $cursor); + $fireTime = $cursor - $daysBefore * 86400; + $spawned++; + } + + return $spawned; + } + + /** + * The first occurrence: respect an explicitly future anchor (the due date the user set) as the + * first one; otherwise jump to the next occurrence on/after now. + */ + private function firstCursor($anchor, $frequency, $lastDay, $now) + { + if ($anchor >= $now) { + return $anchor; + } + + return $this->calculator()->occurrenceFrom($anchor, $frequency, $lastDay, $now, true); + } + + private function setDue($task_id, $cursor) + { + // Advance the template's due date directly (no task events) + mirror it for edit detection. + $this->db->table(TaskModel::TABLE)->eq('id', $task_id)->update(array('date_due' => $cursor)); + $this->taskMetadataModel->save($task_id, array('recoreco_synced_due' => $cursor)); + } + + private function spawn(array $template, $targetColumn) + { + // The template's date_due is already the occurrence (setDue), so duplicate() copies it onto + // the clone. duplicate() copies fields + tags + links + subtasks, but NO metadata, so the + // clone is not a template. + $newId = $this->taskDuplicationModel->duplicate($template['id']); + + if ($newId === false) { + $this->logger->error('RecoReco: duplicate failed for task '.$template['id']); + return false; + } + + // duplicate() lands the clone in the source column; move it to the chosen target (top). + if ($targetColumn > 0 && $targetColumn != $template['column_id']) { + $this->taskPositionModel->movePosition($template['project_id'], $newId, $targetColumn, 1, $template['swimlane_id']); + } + + // Reset the "[DUPLICATE]" prefix back to the source title (no events). + $this->taskModificationModel->update(array('id' => $newId, 'title' => $template['title']), false); + + // Mark the clone: white icon + cannot itself be made recurring; record its source template. + $this->taskMetadataModel->save($newId, array( + 'recoreco_clone' => 1, + 'recoreco_source' => $template['id'], + )); + + // Signal FinanceBuddy (advance the installment). Harmless no-op if nothing listens. + $payload = array('source_task_id' => (int) $template['id'], 'new_task_id' => (int) $newId); + $this->hook->reference('recoreco:task:spawned', $payload); + + return $newId; + } + + private function calculator() + { + if ($this->calc === null) { + $this->calc = new OccurrenceCalculator(); + } + + return $this->calc; + } +} diff --git a/Plugin.php b/Plugin.php index ff4ecd7..e650928 100644 --- a/Plugin.php +++ b/Plugin.php @@ -11,6 +11,12 @@ class Plugin extends Base // "Recurring schedule" entry in the task Actions sidebar, right after the basic actions // (next to native "Edit recurrence"). Opens the RecoReco config modal. $this->template->hook->attach('template:task:sidebar:after-basic-actions', 'recoReco:task/sidebar_action'); + + // 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') { + $this->container['cli']->add(new \Kanboard\Plugin\RecoReco\Console\RecoRecoCommand($this->container)); + } } public function getPluginName() @@ -30,7 +36,7 @@ class Plugin extends Base public function getPluginVersion() { - return '0.2.1'; + return '0.3.0'; } public function getPluginHomepage() diff --git a/Test/OccurrenceCalculatorTest.php b/Test/OccurrenceCalculatorTest.php new file mode 100644 index 0000000..61e6675 --- /dev/null +++ b/Test/OccurrenceCalculatorTest.php @@ -0,0 +1,103 @@ +occurrenceFrom(ts('2023-01-15 10:30'), 'monthly_day', false, ts('2023-01-20 00:00'), false), + ts('2023-02-15 10:30'), + $failures +); + +// 2. Drift-free clamp: the 30th -> Feb clamps to 28 -> back to 30 in March (NOT 28). +check( + '30th -> Feb (non-leap) clamps to 28', + $calc->occurrenceFrom(ts('2023-01-30 09:00'), 'monthly_day', false, ts('2023-01-30 09:00'), false), + ts('2023-02-28 09:00'), + $failures +); +check( + '30th -> March returns to 30 (no drift)', + $calc->occurrenceFrom(ts('2023-01-30 09:00'), 'monthly_day', false, ts('2023-02-28 09:00'), false), + ts('2023-03-30 09:00'), + $failures +); + +// 3. Last-day rule: last day of each month. +check( + 'last-day: Jan 31 -> Feb 28 (non-leap)', + $calc->occurrenceFrom(ts('2023-01-31 08:00'), 'monthly_day', true, ts('2023-01-31 08:00'), false), + ts('2023-02-28 08:00'), + $failures +); +check( + 'last-day: Feb -> March 31', + $calc->occurrenceFrom(ts('2023-01-31 08:00'), 'monthly_day', true, ts('2023-02-28 08:00'), false), + ts('2023-03-31 08:00'), + $failures +); + +// 4. Leap year: last day of Feb 2024 is the 29th. +check( + 'last-day: Feb 2024 -> 29th (leap)', + $calc->occurrenceFrom(ts('2024-01-31 08:00'), 'monthly_day', true, ts('2024-02-01 00:00'), false), + ts('2024-02-29 08:00'), + $failures +); + +// 5. Inclusive vs exclusive at an exact occurrence. +check( + 'inclusive returns the same instant', + $calc->occurrenceFrom(ts('2023-03-10 12:00'), 'monthly_day', false, ts('2023-03-10 12:00'), true), + ts('2023-03-10 12:00'), + $failures +); +check( + 'exclusive skips to next month', + $calc->occurrenceFrom(ts('2023-03-10 12:00'), 'monthly_day', false, ts('2023-03-10 12:00'), false), + ts('2023-04-10 12:00'), + $failures +); + +// 6. Unsupported frequency (until v1.1) returns null. +check( + 'weekly not supported yet -> null', + $calc->occurrenceFrom(ts('2023-03-10 12:00'), 'weekly', false, ts('2023-03-10 12:00'), false), + null, + $failures +); + +echo "\n".($failures === 0 ? 'ALL PASSED' : $failures.' FAILURE(S)')."\n"; +exit($failures === 0 ? 0 : 1); diff --git a/VERSION b/VERSION index 4c06819..09fa2f0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -RecoReco v0.2.1 +RecoReco v0.3