1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees-lib: MyArtJaub library for webtrees |
5
|
|
|
* |
6
|
|
|
* @package MyArtJaub\Webtrees |
7
|
|
|
* @subpackage AdminTasks |
8
|
|
|
* @author Jonathan Jaubart <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2012-2022, Jonathan Jaubart |
10
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace MyArtJaub\Webtrees\Module\AdminTasks\Http\RequestHandlers; |
16
|
|
|
|
17
|
|
|
use Carbon\CarbonInterval; |
18
|
|
|
use Fisharebest\Webtrees\I18N; |
19
|
|
|
use Fisharebest\Webtrees\Registry; |
20
|
|
|
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; |
21
|
|
|
use Fisharebest\Webtrees\Services\ModuleService; |
22
|
|
|
use MyArtJaub\Webtrees\Common\Tasks\TaskSchedule; |
23
|
|
|
use MyArtJaub\Webtrees\Module\AdminTasks\AdminTasksModule; |
24
|
|
|
use MyArtJaub\Webtrees\Module\AdminTasks\Services\TaskScheduleService; |
25
|
|
|
use Psr\Http\Message\ResponseInterface; |
26
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
27
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Request handler for listing task schedules |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
class TasksList implements RequestHandlerInterface |
34
|
|
|
{ |
35
|
|
|
private ?AdminTasksModule $module; |
36
|
|
|
private TaskScheduleService $taskschedules_service; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor for TasksList Request Handler |
40
|
|
|
* |
41
|
|
|
* @param ModuleService $module_service |
42
|
|
|
* @param TaskScheduleService $taskschedules_service |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
ModuleService $module_service, |
46
|
|
|
TaskScheduleService $taskschedules_service |
47
|
|
|
) { |
48
|
|
|
$this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
49
|
|
|
$this->taskschedules_service = $taskschedules_service; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
* @see \Psr\Http\Server\RequestHandlerInterface::handle() |
55
|
|
|
*/ |
56
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
57
|
|
|
{ |
58
|
|
|
if ($this->module === null) { |
59
|
|
|
throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$module = $this->module; |
63
|
|
|
$module_name = $this->module->name(); |
64
|
|
|
return Registry::responseFactory()->response(['data' => $this->taskschedules_service->all(true, true) |
65
|
|
|
->map(function (TaskSchedule $schedule) use ($module, $module_name): array { |
66
|
|
|
$task = $this->taskschedules_service->findTask($schedule->taskId()); |
67
|
|
|
$task_name = $task !== null ? $task->name() : I18N::translate('Task not found'); |
68
|
|
|
$last_run_timestamp = Registry::timestampFactory()->make($schedule->lastRunTime()->getTimestamp()); |
69
|
|
|
|
70
|
|
|
return [ |
71
|
|
|
'edit' => view($module_name . '::admin/tasks-table-options', [ |
72
|
|
|
'task_sched_id' => $schedule->id(), |
73
|
|
|
'task_sched_enabled' => $schedule->isEnabled(), |
74
|
|
|
'task_edit_route' => route(TaskEditPage::class, ['task' => $schedule->id()]), |
75
|
|
|
'task_status_route' => route(TaskStatusAction::class, [ |
76
|
|
|
'task' => $schedule->id(), |
77
|
|
|
'enable' => $schedule->isEnabled() ? 0 : 1 |
78
|
|
|
]) |
79
|
|
|
]), |
80
|
|
|
'status' => [ |
81
|
|
|
'display' => view($module_name . '::components/yes-no-icons', [ |
82
|
|
|
'yes' => $schedule->isEnabled() |
83
|
|
|
]), |
84
|
|
|
'raw' => $schedule->isEnabled() ? 1 : 0 |
85
|
|
|
], |
86
|
|
|
'task_name' => [ |
87
|
|
|
'display' => '<bdi>' . e($task_name) . '</bdi>', |
88
|
|
|
'raw' => $task_name |
89
|
|
|
], |
90
|
|
|
'last_run' => [ |
91
|
|
|
'display' => $last_run_timestamp->timestamp() === 0 ? |
92
|
|
|
view('components/datetime', ['timestamp' => $last_run_timestamp]) : |
93
|
|
|
view('components/datetime-diff', ['timestamp' => $last_run_timestamp]), |
94
|
|
|
'raw' => $schedule->lastRunTime()->getTimestamp() |
95
|
|
|
], |
96
|
|
|
'last_result' => [ |
97
|
|
|
'display' => view($module_name . '::components/yes-no-icons', [ |
98
|
|
|
'yes' => $schedule->wasLastRunSuccess() |
99
|
|
|
]), |
100
|
|
|
'raw' => $schedule->wasLastRunSuccess() ? 1 : 0 |
101
|
|
|
], |
102
|
|
|
'frequency' => |
103
|
|
|
'<bdi>' . e(CarbonInterval::minutes($schedule->frequency())->cascade()->forHumans()) . '</bdi>', |
104
|
|
|
'nb_occurrences' => $schedule->remainingOccurrences() > 0 ? |
105
|
|
|
I18N::number($schedule->remainingOccurrences()) : |
106
|
|
|
I18N::translate('Unlimited'), |
107
|
|
|
'running' => view($module_name . '::components/yes-no-icons', [ |
108
|
|
|
'yes' => $schedule->isRunning(), |
109
|
|
|
'text_yes' => I18N::translate('Running'), |
110
|
|
|
'text_no' => I18N::translate('Not running') |
111
|
|
|
]), |
112
|
|
|
'run' => view($module_name . '::admin/tasks-table-run', [ |
113
|
|
|
'task_sched_id' => $schedule->id(), |
114
|
|
|
'run_route' => route(TaskTrigger::class, [ |
115
|
|
|
'task' => $schedule->taskId(), |
116
|
|
|
'force' => $module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN') |
117
|
|
|
]) |
118
|
|
|
]) |
119
|
|
|
]; |
120
|
|
|
}) |
121
|
|
|
]); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|