@@ -30,75 +30,75 @@ |
||
30 | 30 | class TaskStatusAction implements RequestHandlerInterface |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * @var AdminTasksModule|null $module |
|
35 | - */ |
|
36 | - private $module; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var TaskScheduleService $taskschedules_service |
|
40 | - */ |
|
41 | - private $taskschedules_service; |
|
42 | - |
|
43 | - /** |
|
44 | - * Constructor for TaskStatusAction Request Handler |
|
45 | - * |
|
46 | - * @param ModuleService $module_service |
|
47 | - * @param TaskScheduleService $taskschedules_service |
|
48 | - */ |
|
49 | - public function __construct(ModuleService $module_service, TaskScheduleService $taskschedules_service) |
|
50 | - { |
|
51 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
52 | - $this->taskschedules_service = $taskschedules_service; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * {@inheritDoc} |
|
57 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
58 | - */ |
|
59 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
60 | - { |
|
61 | - $admin_config_route = route(AdminConfigPage::class); |
|
62 | - |
|
63 | - if ($this->module === null) { |
|
64 | - FlashMessages::addMessage( |
|
65 | - I18N::translate('The attached module could not be found.'), |
|
66 | - 'danger' |
|
67 | - ); |
|
68 | - return redirect($admin_config_route); |
|
69 | - } |
|
70 | - |
|
71 | - $task_sched_id = (int) $request->getAttribute('task'); |
|
72 | - $task_schedule = $this->taskschedules_service->find($task_sched_id); |
|
73 | - |
|
74 | - $admin_config_route = route(AdminConfigPage::class); |
|
75 | - |
|
76 | - if ($task_schedule === null) { |
|
77 | - FlashMessages::addMessage( |
|
78 | - I18N::translate('The task shedule with ID “%d” does not exist.', I18N::number($task_sched_id)), |
|
79 | - 'danger' |
|
80 | - ); |
|
81 | - return redirect($admin_config_route); |
|
82 | - } |
|
83 | - |
|
84 | - ((bool) $request->getAttribute('enable', false)) ? $task_schedule->enable() : $task_schedule->disable(); |
|
85 | - |
|
86 | - if ($this->taskschedules_service->update($task_schedule) > 0) { |
|
87 | - FlashMessages::addMessage( |
|
88 | - I18N::translate('The scheduled task has been successfully updated'), |
|
89 | - 'success' |
|
90 | - ); |
|
91 | - //phpcs:ignore Generic.Files.LineLength.TooLong |
|
92 | - Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” has been updated.'); |
|
93 | - } else { |
|
94 | - FlashMessages::addMessage( |
|
95 | - I18N::translate('An error occured while updating the scheduled task'), |
|
96 | - 'danger' |
|
97 | - ); |
|
98 | - //phpcs:ignore Generic.Files.LineLength.TooLong |
|
99 | - Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” could not be updated. See error log.'); |
|
100 | - } |
|
101 | - |
|
102 | - return redirect($admin_config_route); |
|
103 | - } |
|
33 | + /** |
|
34 | + * @var AdminTasksModule|null $module |
|
35 | + */ |
|
36 | + private $module; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var TaskScheduleService $taskschedules_service |
|
40 | + */ |
|
41 | + private $taskschedules_service; |
|
42 | + |
|
43 | + /** |
|
44 | + * Constructor for TaskStatusAction Request Handler |
|
45 | + * |
|
46 | + * @param ModuleService $module_service |
|
47 | + * @param TaskScheduleService $taskschedules_service |
|
48 | + */ |
|
49 | + public function __construct(ModuleService $module_service, TaskScheduleService $taskschedules_service) |
|
50 | + { |
|
51 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
52 | + $this->taskschedules_service = $taskschedules_service; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * {@inheritDoc} |
|
57 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
58 | + */ |
|
59 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
60 | + { |
|
61 | + $admin_config_route = route(AdminConfigPage::class); |
|
62 | + |
|
63 | + if ($this->module === null) { |
|
64 | + FlashMessages::addMessage( |
|
65 | + I18N::translate('The attached module could not be found.'), |
|
66 | + 'danger' |
|
67 | + ); |
|
68 | + return redirect($admin_config_route); |
|
69 | + } |
|
70 | + |
|
71 | + $task_sched_id = (int) $request->getAttribute('task'); |
|
72 | + $task_schedule = $this->taskschedules_service->find($task_sched_id); |
|
73 | + |
|
74 | + $admin_config_route = route(AdminConfigPage::class); |
|
75 | + |
|
76 | + if ($task_schedule === null) { |
|
77 | + FlashMessages::addMessage( |
|
78 | + I18N::translate('The task shedule with ID “%d” does not exist.', I18N::number($task_sched_id)), |
|
79 | + 'danger' |
|
80 | + ); |
|
81 | + return redirect($admin_config_route); |
|
82 | + } |
|
83 | + |
|
84 | + ((bool) $request->getAttribute('enable', false)) ? $task_schedule->enable() : $task_schedule->disable(); |
|
85 | + |
|
86 | + if ($this->taskschedules_service->update($task_schedule) > 0) { |
|
87 | + FlashMessages::addMessage( |
|
88 | + I18N::translate('The scheduled task has been successfully updated'), |
|
89 | + 'success' |
|
90 | + ); |
|
91 | + //phpcs:ignore Generic.Files.LineLength.TooLong |
|
92 | + Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” has been updated.'); |
|
93 | + } else { |
|
94 | + FlashMessages::addMessage( |
|
95 | + I18N::translate('An error occured while updating the scheduled task'), |
|
96 | + 'danger' |
|
97 | + ); |
|
98 | + //phpcs:ignore Generic.Files.LineLength.TooLong |
|
99 | + Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” could not be updated. See error log.'); |
|
100 | + } |
|
101 | + |
|
102 | + return redirect($admin_config_route); |
|
103 | + } |
|
104 | 104 | } |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | return redirect($admin_config_route); |
69 | 69 | } |
70 | 70 | |
71 | - $task_sched_id = (int) $request->getAttribute('task'); |
|
71 | + $task_sched_id = (int)$request->getAttribute('task'); |
|
72 | 72 | $task_schedule = $this->taskschedules_service->find($task_sched_id); |
73 | 73 | |
74 | 74 | $admin_config_route = route(AdminConfigPage::class); |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | return redirect($admin_config_route); |
82 | 82 | } |
83 | 83 | |
84 | - ((bool) $request->getAttribute('enable', false)) ? $task_schedule->enable() : $task_schedule->disable(); |
|
84 | + ((bool)$request->getAttribute('enable', false)) ? $task_schedule->enable() : $task_schedule->disable(); |
|
85 | 85 | |
86 | 86 | if ($this->taskschedules_service->update($task_schedule) > 0) { |
87 | 87 | FlashMessages::addMessage( |
@@ -89,14 +89,14 @@ discard block |
||
89 | 89 | 'success' |
90 | 90 | ); |
91 | 91 | //phpcs:ignore Generic.Files.LineLength.TooLong |
92 | - Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” has been updated.'); |
|
92 | + Log::addConfigurationLog('Module '.$this->module->title().' : Task Schedule “'.$task_schedule->id().'” has been updated.'); |
|
93 | 93 | } else { |
94 | 94 | FlashMessages::addMessage( |
95 | 95 | I18N::translate('An error occured while updating the scheduled task'), |
96 | 96 | 'danger' |
97 | 97 | ); |
98 | 98 | //phpcs:ignore Generic.Files.LineLength.TooLong |
99 | - Log::addConfigurationLog('Module ' . $this->module->title() . ' : Task Schedule “' . $task_schedule->id() . '” could not be updated. See error log.'); |
|
99 | + Log::addConfigurationLog('Module '.$this->module->title().' : Task Schedule “'.$task_schedule->id().'” could not be updated. See error log.'); |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | return redirect($admin_config_route); |
@@ -39,188 +39,188 @@ |
||
39 | 39 | */ |
40 | 40 | class HealthCheckEmailTask implements TaskInterface, ConfigurableTaskInterface |
41 | 41 | { |
42 | - /** |
|
43 | - * Name of the Tree preference to check if the task is enabled for that tree |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - public const TREE_PREFERENCE_NAME = 'MAJ_AT_HEALTHCHECK_ENABLED'; |
|
47 | - |
|
48 | - /** |
|
49 | - * @var AdminTasksModule|null $module |
|
50 | - */ |
|
51 | - private $module; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var HealthCheckService $healthcheck_service; |
|
55 | - */ |
|
56 | - private $healthcheck_service; |
|
57 | - |
|
58 | - /** |
|
59 | - * @var EmailService $email_service; |
|
60 | - */ |
|
61 | - private $email_service; |
|
62 | - |
|
63 | - /** |
|
64 | - * @var UserService $user_service |
|
65 | - */ |
|
66 | - private $user_service; |
|
67 | - |
|
68 | - /** |
|
69 | - * @var TreeService $tree_service |
|
70 | - */ |
|
71 | - private $tree_service; |
|
72 | - |
|
73 | - /** |
|
74 | - * @var UpgradeService $upgrade_service |
|
75 | - */ |
|
76 | - private $upgrade_service; |
|
77 | - |
|
78 | - /** |
|
79 | - * Constructor for HealthCheckTask |
|
80 | - * |
|
81 | - * @param ModuleService $module_service |
|
82 | - * @param HealthCheckService $healthcheck_service |
|
83 | - * @param EmailService $email_service |
|
84 | - * @param UserService $user_service |
|
85 | - * @param TreeService $tree_service |
|
86 | - * @param UpgradeService $upgrade_service |
|
87 | - */ |
|
88 | - public function __construct( |
|
89 | - ModuleService $module_service, |
|
90 | - HealthCheckService $healthcheck_service, |
|
91 | - EmailService $email_service, |
|
92 | - UserService $user_service, |
|
93 | - TreeService $tree_service, |
|
94 | - UpgradeService $upgrade_service |
|
95 | - ) { |
|
96 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
97 | - $this->healthcheck_service = $healthcheck_service; |
|
98 | - $this->email_service = $email_service; |
|
99 | - $this->user_service = $user_service; |
|
100 | - $this->tree_service = $tree_service; |
|
101 | - $this->upgrade_service = $upgrade_service; |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * {@inheritDoc} |
|
107 | - * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\TaskInterface::name() |
|
108 | - */ |
|
109 | - public function name(): string |
|
110 | - { |
|
111 | - return I18N::translate('Healthcheck Email'); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * {@inheritDoc} |
|
116 | - * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\TaskInterface::defaultFrequency() |
|
117 | - */ |
|
118 | - public function defaultFrequency(): int |
|
119 | - { |
|
120 | - return 10080; // = 1 week = 7 * 24 * 60 min |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * {@inheritDoc} |
|
125 | - * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\TaskInterface::run() |
|
126 | - */ |
|
127 | - public function run(TaskSchedule $task_schedule): bool |
|
128 | - { |
|
129 | - if ($this->module === null) { |
|
130 | - return false; |
|
131 | - } |
|
132 | - |
|
133 | - $res = true; |
|
134 | - |
|
135 | - // Compute the number of days to compute |
|
136 | - $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(Carbon::now()); |
|
137 | - //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
138 | - $interval = $interval_lastrun->greaterThan($task_schedule->frequency()) ? $interval_lastrun : $task_schedule->frequency(); |
|
139 | - $nb_days = (int) $interval->ceilDay()->totalDays; |
|
140 | - |
|
141 | - $view_params_site = [ |
|
142 | - 'nb_days' => $nb_days, |
|
143 | - 'upgrade_available' => $this->upgrade_service->isUpgradeAvailable(), |
|
144 | - 'latest_version' => $this->upgrade_service->latestVersion(), |
|
145 | - 'download_url' => $this->upgrade_service->downloadUrl(), |
|
146 | - 'all_users' => $this->user_service->all(), |
|
147 | - 'unapproved' => $this->user_service->unapproved(), |
|
148 | - 'unverified' => $this->user_service->unverified(), |
|
149 | - ]; |
|
150 | - |
|
151 | - foreach ($this->tree_service->all() as $tree) { |
|
152 | - /** @var Tree $tree */ |
|
153 | - |
|
154 | - if ($tree->getPreference(self::TREE_PREFERENCE_NAME) !== '1') { |
|
155 | - continue; |
|
156 | - } |
|
157 | - |
|
158 | - $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
159 | - if ($webmaster === null) { |
|
160 | - continue; |
|
161 | - } |
|
162 | - I18N::init($webmaster->getPreference('language')); |
|
163 | - |
|
164 | - $error_logs = $this->healthcheck_service->errorLogs($tree, $nb_days); |
|
165 | - $nb_errors = $error_logs->sum('nblogs'); |
|
166 | - |
|
167 | - $view_params = array_merge($view_params_site, [ |
|
168 | - 'tree' => $tree, |
|
169 | - 'total_by_type' => $this->healthcheck_service->countByRecordType($tree), |
|
170 | - 'change_by_type' => $this->healthcheck_service->changesByRecordType($tree, $nb_days), |
|
171 | - 'error_logs' => $error_logs, |
|
172 | - 'nb_errors' => $nb_errors |
|
173 | - ]); |
|
174 | - |
|
175 | - $res = $res && $this->email_service->send( |
|
176 | - new TreeUser($tree), |
|
177 | - $webmaster, |
|
178 | - new NoReplyUser(), |
|
179 | - I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
180 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
181 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
182 | - ); |
|
183 | - } |
|
184 | - |
|
185 | - return $res; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * {@inheritDoc} |
|
190 | - * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\ConfigurableTaskInterface::configView() |
|
191 | - */ |
|
192 | - public function configView(ServerRequestInterface $request): string |
|
193 | - { |
|
194 | - return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
195 | - 'all_trees' => $this->tree_service->all() |
|
196 | - ]); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * {@inheritDoc} |
|
201 | - * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\ConfigurableTaskInterface::updateConfig() |
|
202 | - */ |
|
203 | - public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
|
204 | - { |
|
205 | - try { |
|
206 | - $params = (array) $request->getParsedBody(); |
|
207 | - |
|
208 | - foreach ($this->tree_service->all() as $tree) { |
|
209 | - if (Auth::isManager($tree)) { |
|
210 | - $tree_enabled = (bool) ($params['HEALTHCHECK_ENABLED_' . $tree->id()] ?? false); |
|
211 | - $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
|
212 | - } |
|
213 | - } |
|
214 | - return true; |
|
215 | - } catch (Throwable $ex) { |
|
216 | - Log::addErrorLog( |
|
217 | - sprintf( |
|
218 | - 'Error while updating the Task schedule "%s". Exception: %s', |
|
219 | - $task_schedule->id(), |
|
220 | - $ex->getMessage() |
|
221 | - ) |
|
222 | - ); |
|
223 | - } |
|
224 | - return false; |
|
225 | - } |
|
42 | + /** |
|
43 | + * Name of the Tree preference to check if the task is enabled for that tree |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + public const TREE_PREFERENCE_NAME = 'MAJ_AT_HEALTHCHECK_ENABLED'; |
|
47 | + |
|
48 | + /** |
|
49 | + * @var AdminTasksModule|null $module |
|
50 | + */ |
|
51 | + private $module; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var HealthCheckService $healthcheck_service; |
|
55 | + */ |
|
56 | + private $healthcheck_service; |
|
57 | + |
|
58 | + /** |
|
59 | + * @var EmailService $email_service; |
|
60 | + */ |
|
61 | + private $email_service; |
|
62 | + |
|
63 | + /** |
|
64 | + * @var UserService $user_service |
|
65 | + */ |
|
66 | + private $user_service; |
|
67 | + |
|
68 | + /** |
|
69 | + * @var TreeService $tree_service |
|
70 | + */ |
|
71 | + private $tree_service; |
|
72 | + |
|
73 | + /** |
|
74 | + * @var UpgradeService $upgrade_service |
|
75 | + */ |
|
76 | + private $upgrade_service; |
|
77 | + |
|
78 | + /** |
|
79 | + * Constructor for HealthCheckTask |
|
80 | + * |
|
81 | + * @param ModuleService $module_service |
|
82 | + * @param HealthCheckService $healthcheck_service |
|
83 | + * @param EmailService $email_service |
|
84 | + * @param UserService $user_service |
|
85 | + * @param TreeService $tree_service |
|
86 | + * @param UpgradeService $upgrade_service |
|
87 | + */ |
|
88 | + public function __construct( |
|
89 | + ModuleService $module_service, |
|
90 | + HealthCheckService $healthcheck_service, |
|
91 | + EmailService $email_service, |
|
92 | + UserService $user_service, |
|
93 | + TreeService $tree_service, |
|
94 | + UpgradeService $upgrade_service |
|
95 | + ) { |
|
96 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
97 | + $this->healthcheck_service = $healthcheck_service; |
|
98 | + $this->email_service = $email_service; |
|
99 | + $this->user_service = $user_service; |
|
100 | + $this->tree_service = $tree_service; |
|
101 | + $this->upgrade_service = $upgrade_service; |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * {@inheritDoc} |
|
107 | + * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\TaskInterface::name() |
|
108 | + */ |
|
109 | + public function name(): string |
|
110 | + { |
|
111 | + return I18N::translate('Healthcheck Email'); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * {@inheritDoc} |
|
116 | + * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\TaskInterface::defaultFrequency() |
|
117 | + */ |
|
118 | + public function defaultFrequency(): int |
|
119 | + { |
|
120 | + return 10080; // = 1 week = 7 * 24 * 60 min |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * {@inheritDoc} |
|
125 | + * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\TaskInterface::run() |
|
126 | + */ |
|
127 | + public function run(TaskSchedule $task_schedule): bool |
|
128 | + { |
|
129 | + if ($this->module === null) { |
|
130 | + return false; |
|
131 | + } |
|
132 | + |
|
133 | + $res = true; |
|
134 | + |
|
135 | + // Compute the number of days to compute |
|
136 | + $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(Carbon::now()); |
|
137 | + //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
138 | + $interval = $interval_lastrun->greaterThan($task_schedule->frequency()) ? $interval_lastrun : $task_schedule->frequency(); |
|
139 | + $nb_days = (int) $interval->ceilDay()->totalDays; |
|
140 | + |
|
141 | + $view_params_site = [ |
|
142 | + 'nb_days' => $nb_days, |
|
143 | + 'upgrade_available' => $this->upgrade_service->isUpgradeAvailable(), |
|
144 | + 'latest_version' => $this->upgrade_service->latestVersion(), |
|
145 | + 'download_url' => $this->upgrade_service->downloadUrl(), |
|
146 | + 'all_users' => $this->user_service->all(), |
|
147 | + 'unapproved' => $this->user_service->unapproved(), |
|
148 | + 'unverified' => $this->user_service->unverified(), |
|
149 | + ]; |
|
150 | + |
|
151 | + foreach ($this->tree_service->all() as $tree) { |
|
152 | + /** @var Tree $tree */ |
|
153 | + |
|
154 | + if ($tree->getPreference(self::TREE_PREFERENCE_NAME) !== '1') { |
|
155 | + continue; |
|
156 | + } |
|
157 | + |
|
158 | + $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
159 | + if ($webmaster === null) { |
|
160 | + continue; |
|
161 | + } |
|
162 | + I18N::init($webmaster->getPreference('language')); |
|
163 | + |
|
164 | + $error_logs = $this->healthcheck_service->errorLogs($tree, $nb_days); |
|
165 | + $nb_errors = $error_logs->sum('nblogs'); |
|
166 | + |
|
167 | + $view_params = array_merge($view_params_site, [ |
|
168 | + 'tree' => $tree, |
|
169 | + 'total_by_type' => $this->healthcheck_service->countByRecordType($tree), |
|
170 | + 'change_by_type' => $this->healthcheck_service->changesByRecordType($tree, $nb_days), |
|
171 | + 'error_logs' => $error_logs, |
|
172 | + 'nb_errors' => $nb_errors |
|
173 | + ]); |
|
174 | + |
|
175 | + $res = $res && $this->email_service->send( |
|
176 | + new TreeUser($tree), |
|
177 | + $webmaster, |
|
178 | + new NoReplyUser(), |
|
179 | + I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
180 | + view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
181 | + view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
182 | + ); |
|
183 | + } |
|
184 | + |
|
185 | + return $res; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * {@inheritDoc} |
|
190 | + * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\ConfigurableTaskInterface::configView() |
|
191 | + */ |
|
192 | + public function configView(ServerRequestInterface $request): string |
|
193 | + { |
|
194 | + return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
195 | + 'all_trees' => $this->tree_service->all() |
|
196 | + ]); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * {@inheritDoc} |
|
201 | + * @see \MyArtJaub\Webtrees\Module\AdminTasks\Model\ConfigurableTaskInterface::updateConfig() |
|
202 | + */ |
|
203 | + public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
|
204 | + { |
|
205 | + try { |
|
206 | + $params = (array) $request->getParsedBody(); |
|
207 | + |
|
208 | + foreach ($this->tree_service->all() as $tree) { |
|
209 | + if (Auth::isManager($tree)) { |
|
210 | + $tree_enabled = (bool) ($params['HEALTHCHECK_ENABLED_' . $tree->id()] ?? false); |
|
211 | + $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
|
212 | + } |
|
213 | + } |
|
214 | + return true; |
|
215 | + } catch (Throwable $ex) { |
|
216 | + Log::addErrorLog( |
|
217 | + sprintf( |
|
218 | + 'Error while updating the Task schedule "%s". Exception: %s', |
|
219 | + $task_schedule->id(), |
|
220 | + $ex->getMessage() |
|
221 | + ) |
|
222 | + ); |
|
223 | + } |
|
224 | + return false; |
|
225 | + } |
|
226 | 226 | } |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | $interval_lastrun = $task_schedule->lastRunTime()->diffAsCarbonInterval(Carbon::now()); |
137 | 137 | //@phpcs:ignore Generic.Files.LineLength.TooLong |
138 | 138 | $interval = $interval_lastrun->greaterThan($task_schedule->frequency()) ? $interval_lastrun : $task_schedule->frequency(); |
139 | - $nb_days = (int) $interval->ceilDay()->totalDays; |
|
139 | + $nb_days = (int)$interval->ceilDay()->totalDays; |
|
140 | 140 | |
141 | 141 | $view_params_site = [ |
142 | 142 | 'nb_days' => $nb_days, |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | continue; |
156 | 156 | } |
157 | 157 | |
158 | - $webmaster = $this->user_service->find((int) $tree->getPreference('WEBMASTER_USER_ID')); |
|
158 | + $webmaster = $this->user_service->find((int)$tree->getPreference('WEBMASTER_USER_ID')); |
|
159 | 159 | if ($webmaster === null) { |
160 | 160 | continue; |
161 | 161 | } |
@@ -176,9 +176,9 @@ discard block |
||
176 | 176 | new TreeUser($tree), |
177 | 177 | $webmaster, |
178 | 178 | new NoReplyUser(), |
179 | - I18N::translate('Health Check Report') . ' - ' . I18N::translate('Tree %s', $tree->name()), |
|
180 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
181 | - view($this->module->name() . '::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
179 | + I18N::translate('Health Check Report').' - '.I18N::translate('Tree %s', $tree->name()), |
|
180 | + view($this->module->name().'::tasks/healthcheck/email-healthcheck-text', $view_params), |
|
181 | + view($this->module->name().'::tasks/healthcheck/email-healthcheck-html', $view_params) |
|
182 | 182 | ); |
183 | 183 | } |
184 | 184 | |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | */ |
192 | 192 | public function configView(ServerRequestInterface $request): string |
193 | 193 | { |
194 | - return $this->module === null ? '' : view($this->module->name() . '::tasks/healthcheck/config', [ |
|
194 | + return $this->module === null ? '' : view($this->module->name().'::tasks/healthcheck/config', [ |
|
195 | 195 | 'all_trees' => $this->tree_service->all() |
196 | 196 | ]); |
197 | 197 | } |
@@ -203,11 +203,11 @@ discard block |
||
203 | 203 | public function updateConfig(ServerRequestInterface $request, TaskSchedule $task_schedule): bool |
204 | 204 | { |
205 | 205 | try { |
206 | - $params = (array) $request->getParsedBody(); |
|
206 | + $params = (array)$request->getParsedBody(); |
|
207 | 207 | |
208 | 208 | foreach ($this->tree_service->all() as $tree) { |
209 | 209 | if (Auth::isManager($tree)) { |
210 | - $tree_enabled = (bool) ($params['HEALTHCHECK_ENABLED_' . $tree->id()] ?? false); |
|
210 | + $tree_enabled = (bool)($params['HEALTHCHECK_ENABLED_'.$tree->id()] ?? false); |
|
211 | 211 | $tree->setPreference(self::TREE_PREFERENCE_NAME, $tree_enabled ? '1' : '0'); |
212 | 212 | } |
213 | 213 | } |
@@ -35,259 +35,259 @@ |
||
35 | 35 | */ |
36 | 36 | class TaskScheduleService |
37 | 37 | { |
38 | - /** |
|
39 | - * Time-out after which the task will be considered not running any more. |
|
40 | - * In seconds, default 5 mins. |
|
41 | - * @var integer |
|
42 | - */ |
|
43 | - public const TASK_TIME_OUT = 600; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var Collection|null $available_tasks |
|
47 | - */ |
|
48 | - private $available_tasks; |
|
49 | - |
|
50 | - /** |
|
51 | - * Returns all Tasks schedules in database. |
|
52 | - * Stored records can be synchronised with the tasks actually available to the system. |
|
53 | - * |
|
54 | - * @param bool $sync_available Should tasks synchronised with available ones |
|
55 | - * @param bool $include_disabled Should disabled tasks be returned |
|
56 | - * @return Collection Collection of TaskSchedule |
|
57 | - */ |
|
58 | - public function all(bool $sync_available = false, bool $include_disabled = true): Collection |
|
59 | - { |
|
60 | - $tasks_schedules = DB::table('maj_admintasks') |
|
61 | - ->select() |
|
62 | - ->get() |
|
63 | - ->map(self::rowMapper()); |
|
64 | - |
|
65 | - if ($sync_available) { |
|
66 | - $available_tasks = clone $this->available(); |
|
67 | - foreach ($tasks_schedules as $task_schedule) { |
|
68 | - /** @var TaskSchedule $task_schedule */ |
|
69 | - if ($available_tasks->has($task_schedule->taskId())) { |
|
70 | - $available_tasks->forget($task_schedule->taskId()); |
|
71 | - } else { |
|
72 | - $this->delete($task_schedule); |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - foreach ($available_tasks as $task_name => $task) { |
|
77 | - /** @var TaskInterface $task */ |
|
78 | - $this->insertTask($task_name, $task->defaultFrequency()); |
|
79 | - } |
|
80 | - |
|
81 | - return $this->all(false, $include_disabled); |
|
82 | - } |
|
83 | - |
|
84 | - return $tasks_schedules; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Returns tasks exposed through modules implementing ModuleTasksProviderInterface. |
|
89 | - * |
|
90 | - * @return Collection |
|
91 | - */ |
|
92 | - public function available(): Collection |
|
93 | - { |
|
94 | - if ($this->available_tasks === null) { |
|
95 | - $tasks_providers = app(ModuleService::class)->findByInterface(ModuleTasksProviderInterface::class); |
|
96 | - |
|
97 | - $this->available_tasks = new Collection(); |
|
98 | - foreach ($tasks_providers as $task_provider) { |
|
99 | - $this->available_tasks = $this->available_tasks->merge($task_provider->listTasks()); |
|
100 | - } |
|
101 | - } |
|
102 | - return $this->available_tasks; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Find a task schedule by its ID. |
|
107 | - * |
|
108 | - * @param int $task_schedule_id |
|
109 | - * @return TaskSchedule|NULL |
|
110 | - */ |
|
111 | - public function find(int $task_schedule_id): ?TaskSchedule |
|
112 | - { |
|
113 | - return DB::table('maj_admintasks') |
|
114 | - ->select() |
|
115 | - ->where('majat_id', '=', $task_schedule_id) |
|
116 | - ->get() |
|
117 | - ->map(self::rowMapper()) |
|
118 | - ->first(); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Add a new task schedule with the specified task ID, and frequency if defined. |
|
123 | - * Uses default for other settings. |
|
124 | - * |
|
125 | - * @param string $task_id |
|
126 | - * @param int $frequency |
|
127 | - * @return bool |
|
128 | - */ |
|
129 | - public function insertTask(string $task_id, int $frequency = 0): bool |
|
130 | - { |
|
131 | - $values = ['majat_task_id' => $task_id]; |
|
132 | - if ($frequency > 0) { |
|
133 | - $values['majat_frequency'] = $frequency; |
|
134 | - } |
|
135 | - |
|
136 | - return DB::table('maj_admintasks') |
|
137 | - ->insert($values); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Update a task schedule. |
|
142 | - * Returns the number of tasks schedules updated. |
|
143 | - * |
|
144 | - * @param TaskSchedule $task_schedule |
|
145 | - * @return int |
|
146 | - */ |
|
147 | - public function update(TaskSchedule $task_schedule): int |
|
148 | - { |
|
149 | - return DB::table('maj_admintasks') |
|
150 | - ->where('majat_id', '=', $task_schedule->id()) |
|
151 | - ->update([ |
|
152 | - 'majat_status' => $task_schedule->isEnabled() ? 'enabled' : 'disabled', |
|
153 | - 'majat_last_run' => $task_schedule->lastRunTime(), |
|
154 | - 'majat_last_result' => $task_schedule->wasLastRunSuccess(), |
|
155 | - 'majat_frequency' => $task_schedule->frequency()->totalMinutes, |
|
156 | - 'majat_nb_occur' => $task_schedule->remainingOccurences(), |
|
157 | - 'majat_running' => $task_schedule->isRunning() |
|
158 | - ]); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Delete a task schedule. |
|
163 | - * |
|
164 | - * @param TaskSchedule $task_schedule |
|
165 | - * @return int |
|
166 | - */ |
|
167 | - public function delete(TaskSchedule $task_schedule): int |
|
168 | - { |
|
169 | - return DB::table('maj_admintasks') |
|
170 | - ->where('majat_id', '=', $task_schedule->id()) |
|
171 | - ->delete(); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Find a task by its name |
|
176 | - * |
|
177 | - * @param string $task_id |
|
178 | - * @return TaskInterface|NULL |
|
179 | - */ |
|
180 | - public function findTask(string $task_id): ?TaskInterface |
|
181 | - { |
|
182 | - if ($this->available()->has($task_id)) { |
|
183 | - return app($this->available()->get($task_id)); |
|
184 | - } |
|
185 | - return null; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Retrieve all tasks that are candidates to be run. |
|
190 | - * |
|
191 | - * @param bool $force Should the run be forced |
|
192 | - * @param string $task_id Specific task ID to be run |
|
193 | - * @return Collection |
|
194 | - */ |
|
195 | - public function findTasksToRun(bool $force, string $task_id = null): Collection |
|
196 | - { |
|
197 | - $query = DB::table('maj_admintasks') |
|
198 | - ->select() |
|
199 | - ->where('majat_status', '=', 'enabled') |
|
200 | - ->where(function (Builder $query): void { |
|
201 | - |
|
202 | - $query->where('majat_running', '=', 0) |
|
203 | - ->orWhere('majat_last_run', '<=', Carbon::now()->subSeconds(self::TASK_TIME_OUT)); |
|
204 | - }); |
|
205 | - |
|
206 | - if (!$force) { |
|
207 | - $query->where(function (Builder $query): void { |
|
208 | - |
|
209 | - $query->where('majat_running', '=', 0) |
|
210 | - ->orWhereRaw('DATE_ADD(majat_last_run, INTERVAL majat_frequency MINUTE) <= NOW()'); |
|
211 | - }); |
|
212 | - } |
|
213 | - |
|
214 | - if ($task_id !== null) { |
|
215 | - $query->where('majat_task_id', '=', $task_id); |
|
216 | - } |
|
217 | - |
|
218 | - return $query->get()->map(self::rowMapper()); |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Run the task associated with the schedule. |
|
223 | - * The task will run if either forced to, or its next scheduled run time has been exceeded. |
|
224 | - * The last run time is recorded only if the task is successful. |
|
225 | - * |
|
226 | - * @param TaskSchedule $task_schedule |
|
227 | - * @param boolean $force |
|
228 | - */ |
|
229 | - public function run(TaskSchedule $task_schedule, $force = false): void |
|
230 | - { |
|
231 | - /** @var TaskSchedule $task_schedule */ |
|
232 | - $task_schedule = DB::table('maj_admintasks') |
|
233 | - ->select() |
|
234 | - ->where('majat_id', '=', $task_schedule->id()) |
|
235 | - ->lockForUpdate() |
|
236 | - ->get() |
|
237 | - ->map(self::rowMapper()) |
|
238 | - ->first(); |
|
239 | - |
|
240 | - if ( |
|
241 | - !$task_schedule->isRunning() && |
|
242 | - ($force || $task_schedule->lastRunTime()->add($task_schedule->frequency())->lessThan(Carbon::now())) |
|
243 | - ) { |
|
244 | - $task_schedule->setLastResult(false); |
|
245 | - |
|
246 | - $task = $this->findTask($task_schedule->taskId()); |
|
247 | - if ($task !== null) { |
|
248 | - $task_schedule->startRunning(); |
|
249 | - $this->update($task_schedule); |
|
250 | - |
|
251 | - $first_error = $task_schedule->wasLastRunSuccess(); |
|
252 | - try { |
|
253 | - $task_schedule->setLastResult($task->run($task_schedule)); |
|
254 | - } catch (Throwable $ex) { |
|
255 | - if ($first_error) { // Only record the first error, as this could fill the log. |
|
256 | - Log::addErrorLog(I18N::translate('Error while running task %s:', $task->name()) . ' ' . |
|
257 | - '[' . get_class($ex) . '] ' . $ex->getMessage() . ' ' . $ex->getFile() . ':' |
|
258 | - . $ex->getLine() . PHP_EOL . $ex->getTraceAsString()); |
|
259 | - } |
|
260 | - } |
|
261 | - |
|
262 | - if ($task_schedule->wasLastRunSuccess()) { |
|
263 | - $task_schedule->setLastRunTime(Carbon::now()); |
|
264 | - $task_schedule->decrementRemainingOccurences(); |
|
265 | - } |
|
266 | - $task_schedule->stopRunning(); |
|
267 | - } |
|
268 | - $this->update($task_schedule); |
|
269 | - } |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Mapper to return a TaskSchedule object from an object. |
|
274 | - * |
|
275 | - * @return Closure |
|
276 | - */ |
|
277 | - public static function rowMapper(): Closure |
|
278 | - { |
|
279 | - return static function (stdClass $row): TaskSchedule { |
|
280 | - |
|
281 | - return new TaskSchedule( |
|
282 | - (int) $row->majat_id, |
|
283 | - $row->majat_task_id, |
|
284 | - $row->majat_status === 'enabled', |
|
285 | - Carbon::parse($row->majat_last_run), |
|
286 | - (bool) $row->majat_last_result, |
|
287 | - CarbonInterval::minutes($row->majat_frequency), |
|
288 | - (int) $row->majat_nb_occur, |
|
289 | - (bool) $row->majat_running |
|
290 | - ); |
|
291 | - }; |
|
292 | - } |
|
38 | + /** |
|
39 | + * Time-out after which the task will be considered not running any more. |
|
40 | + * In seconds, default 5 mins. |
|
41 | + * @var integer |
|
42 | + */ |
|
43 | + public const TASK_TIME_OUT = 600; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var Collection|null $available_tasks |
|
47 | + */ |
|
48 | + private $available_tasks; |
|
49 | + |
|
50 | + /** |
|
51 | + * Returns all Tasks schedules in database. |
|
52 | + * Stored records can be synchronised with the tasks actually available to the system. |
|
53 | + * |
|
54 | + * @param bool $sync_available Should tasks synchronised with available ones |
|
55 | + * @param bool $include_disabled Should disabled tasks be returned |
|
56 | + * @return Collection Collection of TaskSchedule |
|
57 | + */ |
|
58 | + public function all(bool $sync_available = false, bool $include_disabled = true): Collection |
|
59 | + { |
|
60 | + $tasks_schedules = DB::table('maj_admintasks') |
|
61 | + ->select() |
|
62 | + ->get() |
|
63 | + ->map(self::rowMapper()); |
|
64 | + |
|
65 | + if ($sync_available) { |
|
66 | + $available_tasks = clone $this->available(); |
|
67 | + foreach ($tasks_schedules as $task_schedule) { |
|
68 | + /** @var TaskSchedule $task_schedule */ |
|
69 | + if ($available_tasks->has($task_schedule->taskId())) { |
|
70 | + $available_tasks->forget($task_schedule->taskId()); |
|
71 | + } else { |
|
72 | + $this->delete($task_schedule); |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + foreach ($available_tasks as $task_name => $task) { |
|
77 | + /** @var TaskInterface $task */ |
|
78 | + $this->insertTask($task_name, $task->defaultFrequency()); |
|
79 | + } |
|
80 | + |
|
81 | + return $this->all(false, $include_disabled); |
|
82 | + } |
|
83 | + |
|
84 | + return $tasks_schedules; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Returns tasks exposed through modules implementing ModuleTasksProviderInterface. |
|
89 | + * |
|
90 | + * @return Collection |
|
91 | + */ |
|
92 | + public function available(): Collection |
|
93 | + { |
|
94 | + if ($this->available_tasks === null) { |
|
95 | + $tasks_providers = app(ModuleService::class)->findByInterface(ModuleTasksProviderInterface::class); |
|
96 | + |
|
97 | + $this->available_tasks = new Collection(); |
|
98 | + foreach ($tasks_providers as $task_provider) { |
|
99 | + $this->available_tasks = $this->available_tasks->merge($task_provider->listTasks()); |
|
100 | + } |
|
101 | + } |
|
102 | + return $this->available_tasks; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Find a task schedule by its ID. |
|
107 | + * |
|
108 | + * @param int $task_schedule_id |
|
109 | + * @return TaskSchedule|NULL |
|
110 | + */ |
|
111 | + public function find(int $task_schedule_id): ?TaskSchedule |
|
112 | + { |
|
113 | + return DB::table('maj_admintasks') |
|
114 | + ->select() |
|
115 | + ->where('majat_id', '=', $task_schedule_id) |
|
116 | + ->get() |
|
117 | + ->map(self::rowMapper()) |
|
118 | + ->first(); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Add a new task schedule with the specified task ID, and frequency if defined. |
|
123 | + * Uses default for other settings. |
|
124 | + * |
|
125 | + * @param string $task_id |
|
126 | + * @param int $frequency |
|
127 | + * @return bool |
|
128 | + */ |
|
129 | + public function insertTask(string $task_id, int $frequency = 0): bool |
|
130 | + { |
|
131 | + $values = ['majat_task_id' => $task_id]; |
|
132 | + if ($frequency > 0) { |
|
133 | + $values['majat_frequency'] = $frequency; |
|
134 | + } |
|
135 | + |
|
136 | + return DB::table('maj_admintasks') |
|
137 | + ->insert($values); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Update a task schedule. |
|
142 | + * Returns the number of tasks schedules updated. |
|
143 | + * |
|
144 | + * @param TaskSchedule $task_schedule |
|
145 | + * @return int |
|
146 | + */ |
|
147 | + public function update(TaskSchedule $task_schedule): int |
|
148 | + { |
|
149 | + return DB::table('maj_admintasks') |
|
150 | + ->where('majat_id', '=', $task_schedule->id()) |
|
151 | + ->update([ |
|
152 | + 'majat_status' => $task_schedule->isEnabled() ? 'enabled' : 'disabled', |
|
153 | + 'majat_last_run' => $task_schedule->lastRunTime(), |
|
154 | + 'majat_last_result' => $task_schedule->wasLastRunSuccess(), |
|
155 | + 'majat_frequency' => $task_schedule->frequency()->totalMinutes, |
|
156 | + 'majat_nb_occur' => $task_schedule->remainingOccurences(), |
|
157 | + 'majat_running' => $task_schedule->isRunning() |
|
158 | + ]); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Delete a task schedule. |
|
163 | + * |
|
164 | + * @param TaskSchedule $task_schedule |
|
165 | + * @return int |
|
166 | + */ |
|
167 | + public function delete(TaskSchedule $task_schedule): int |
|
168 | + { |
|
169 | + return DB::table('maj_admintasks') |
|
170 | + ->where('majat_id', '=', $task_schedule->id()) |
|
171 | + ->delete(); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Find a task by its name |
|
176 | + * |
|
177 | + * @param string $task_id |
|
178 | + * @return TaskInterface|NULL |
|
179 | + */ |
|
180 | + public function findTask(string $task_id): ?TaskInterface |
|
181 | + { |
|
182 | + if ($this->available()->has($task_id)) { |
|
183 | + return app($this->available()->get($task_id)); |
|
184 | + } |
|
185 | + return null; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Retrieve all tasks that are candidates to be run. |
|
190 | + * |
|
191 | + * @param bool $force Should the run be forced |
|
192 | + * @param string $task_id Specific task ID to be run |
|
193 | + * @return Collection |
|
194 | + */ |
|
195 | + public function findTasksToRun(bool $force, string $task_id = null): Collection |
|
196 | + { |
|
197 | + $query = DB::table('maj_admintasks') |
|
198 | + ->select() |
|
199 | + ->where('majat_status', '=', 'enabled') |
|
200 | + ->where(function (Builder $query): void { |
|
201 | + |
|
202 | + $query->where('majat_running', '=', 0) |
|
203 | + ->orWhere('majat_last_run', '<=', Carbon::now()->subSeconds(self::TASK_TIME_OUT)); |
|
204 | + }); |
|
205 | + |
|
206 | + if (!$force) { |
|
207 | + $query->where(function (Builder $query): void { |
|
208 | + |
|
209 | + $query->where('majat_running', '=', 0) |
|
210 | + ->orWhereRaw('DATE_ADD(majat_last_run, INTERVAL majat_frequency MINUTE) <= NOW()'); |
|
211 | + }); |
|
212 | + } |
|
213 | + |
|
214 | + if ($task_id !== null) { |
|
215 | + $query->where('majat_task_id', '=', $task_id); |
|
216 | + } |
|
217 | + |
|
218 | + return $query->get()->map(self::rowMapper()); |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Run the task associated with the schedule. |
|
223 | + * The task will run if either forced to, or its next scheduled run time has been exceeded. |
|
224 | + * The last run time is recorded only if the task is successful. |
|
225 | + * |
|
226 | + * @param TaskSchedule $task_schedule |
|
227 | + * @param boolean $force |
|
228 | + */ |
|
229 | + public function run(TaskSchedule $task_schedule, $force = false): void |
|
230 | + { |
|
231 | + /** @var TaskSchedule $task_schedule */ |
|
232 | + $task_schedule = DB::table('maj_admintasks') |
|
233 | + ->select() |
|
234 | + ->where('majat_id', '=', $task_schedule->id()) |
|
235 | + ->lockForUpdate() |
|
236 | + ->get() |
|
237 | + ->map(self::rowMapper()) |
|
238 | + ->first(); |
|
239 | + |
|
240 | + if ( |
|
241 | + !$task_schedule->isRunning() && |
|
242 | + ($force || $task_schedule->lastRunTime()->add($task_schedule->frequency())->lessThan(Carbon::now())) |
|
243 | + ) { |
|
244 | + $task_schedule->setLastResult(false); |
|
245 | + |
|
246 | + $task = $this->findTask($task_schedule->taskId()); |
|
247 | + if ($task !== null) { |
|
248 | + $task_schedule->startRunning(); |
|
249 | + $this->update($task_schedule); |
|
250 | + |
|
251 | + $first_error = $task_schedule->wasLastRunSuccess(); |
|
252 | + try { |
|
253 | + $task_schedule->setLastResult($task->run($task_schedule)); |
|
254 | + } catch (Throwable $ex) { |
|
255 | + if ($first_error) { // Only record the first error, as this could fill the log. |
|
256 | + Log::addErrorLog(I18N::translate('Error while running task %s:', $task->name()) . ' ' . |
|
257 | + '[' . get_class($ex) . '] ' . $ex->getMessage() . ' ' . $ex->getFile() . ':' |
|
258 | + . $ex->getLine() . PHP_EOL . $ex->getTraceAsString()); |
|
259 | + } |
|
260 | + } |
|
261 | + |
|
262 | + if ($task_schedule->wasLastRunSuccess()) { |
|
263 | + $task_schedule->setLastRunTime(Carbon::now()); |
|
264 | + $task_schedule->decrementRemainingOccurences(); |
|
265 | + } |
|
266 | + $task_schedule->stopRunning(); |
|
267 | + } |
|
268 | + $this->update($task_schedule); |
|
269 | + } |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Mapper to return a TaskSchedule object from an object. |
|
274 | + * |
|
275 | + * @return Closure |
|
276 | + */ |
|
277 | + public static function rowMapper(): Closure |
|
278 | + { |
|
279 | + return static function (stdClass $row): TaskSchedule { |
|
280 | + |
|
281 | + return new TaskSchedule( |
|
282 | + (int) $row->majat_id, |
|
283 | + $row->majat_task_id, |
|
284 | + $row->majat_status === 'enabled', |
|
285 | + Carbon::parse($row->majat_last_run), |
|
286 | + (bool) $row->majat_last_result, |
|
287 | + CarbonInterval::minutes($row->majat_frequency), |
|
288 | + (int) $row->majat_nb_occur, |
|
289 | + (bool) $row->majat_running |
|
290 | + ); |
|
291 | + }; |
|
292 | + } |
|
293 | 293 | } |
@@ -197,14 +197,14 @@ discard block |
||
197 | 197 | $query = DB::table('maj_admintasks') |
198 | 198 | ->select() |
199 | 199 | ->where('majat_status', '=', 'enabled') |
200 | - ->where(function (Builder $query): void { |
|
200 | + ->where(function(Builder $query): void { |
|
201 | 201 | |
202 | 202 | $query->where('majat_running', '=', 0) |
203 | 203 | ->orWhere('majat_last_run', '<=', Carbon::now()->subSeconds(self::TASK_TIME_OUT)); |
204 | 204 | }); |
205 | 205 | |
206 | 206 | if (!$force) { |
207 | - $query->where(function (Builder $query): void { |
|
207 | + $query->where(function(Builder $query): void { |
|
208 | 208 | |
209 | 209 | $query->where('majat_running', '=', 0) |
210 | 210 | ->orWhereRaw('DATE_ADD(majat_last_run, INTERVAL majat_frequency MINUTE) <= NOW()'); |
@@ -253,9 +253,9 @@ discard block |
||
253 | 253 | $task_schedule->setLastResult($task->run($task_schedule)); |
254 | 254 | } catch (Throwable $ex) { |
255 | 255 | if ($first_error) { // Only record the first error, as this could fill the log. |
256 | - Log::addErrorLog(I18N::translate('Error while running task %s:', $task->name()) . ' ' . |
|
257 | - '[' . get_class($ex) . '] ' . $ex->getMessage() . ' ' . $ex->getFile() . ':' |
|
258 | - . $ex->getLine() . PHP_EOL . $ex->getTraceAsString()); |
|
256 | + Log::addErrorLog(I18N::translate('Error while running task %s:', $task->name()).' '. |
|
257 | + '['.get_class($ex).'] '.$ex->getMessage().' '.$ex->getFile().':' |
|
258 | + . $ex->getLine().PHP_EOL.$ex->getTraceAsString()); |
|
259 | 259 | } |
260 | 260 | } |
261 | 261 | |
@@ -276,17 +276,17 @@ discard block |
||
276 | 276 | */ |
277 | 277 | public static function rowMapper(): Closure |
278 | 278 | { |
279 | - return static function (stdClass $row): TaskSchedule { |
|
279 | + return static function(stdClass $row): TaskSchedule { |
|
280 | 280 | |
281 | 281 | return new TaskSchedule( |
282 | - (int) $row->majat_id, |
|
282 | + (int)$row->majat_id, |
|
283 | 283 | $row->majat_task_id, |
284 | 284 | $row->majat_status === 'enabled', |
285 | 285 | Carbon::parse($row->majat_last_run), |
286 | - (bool) $row->majat_last_result, |
|
286 | + (bool)$row->majat_last_result, |
|
287 | 287 | CarbonInterval::minutes($row->majat_frequency), |
288 | - (int) $row->majat_nb_occur, |
|
289 | - (bool) $row->majat_running |
|
288 | + (int)$row->majat_nb_occur, |
|
289 | + (bool)$row->majat_running |
|
290 | 290 | ); |
291 | 291 | }; |
292 | 292 | } |
@@ -33,59 +33,59 @@ |
||
33 | 33 | */ |
34 | 34 | class AncestorsList implements RequestHandlerInterface |
35 | 35 | { |
36 | - use ViewResponseTrait; |
|
36 | + use ViewResponseTrait; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @var SosaModule|null $module |
|
40 | - */ |
|
41 | - private $module; |
|
38 | + /** |
|
39 | + * @var SosaModule|null $module |
|
40 | + */ |
|
41 | + private $module; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @var SosaRecordsService $sosa_record_service |
|
45 | - */ |
|
46 | - private $sosa_record_service; |
|
43 | + /** |
|
44 | + * @var SosaRecordsService $sosa_record_service |
|
45 | + */ |
|
46 | + private $sosa_record_service; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Constructor for AncestorsList Request Handler |
|
50 | - * |
|
51 | - * @param ModuleService $module_service |
|
52 | - * @param SosaRecordsService $sosa_record_service |
|
53 | - */ |
|
54 | - public function __construct( |
|
55 | - ModuleService $module_service, |
|
56 | - SosaRecordsService $sosa_record_service |
|
57 | - ) { |
|
58 | - $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
59 | - $this->sosa_record_service = $sosa_record_service; |
|
60 | - } |
|
48 | + /** |
|
49 | + * Constructor for AncestorsList Request Handler |
|
50 | + * |
|
51 | + * @param ModuleService $module_service |
|
52 | + * @param SosaRecordsService $sosa_record_service |
|
53 | + */ |
|
54 | + public function __construct( |
|
55 | + ModuleService $module_service, |
|
56 | + SosaRecordsService $sosa_record_service |
|
57 | + ) { |
|
58 | + $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
59 | + $this->sosa_record_service = $sosa_record_service; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * {@inheritDoc} |
|
64 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
65 | - */ |
|
66 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
67 | - { |
|
68 | - if ($this->module === null) { |
|
69 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
70 | - } |
|
62 | + /** |
|
63 | + * {@inheritDoc} |
|
64 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
65 | + */ |
|
66 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
67 | + { |
|
68 | + if ($this->module === null) { |
|
69 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
70 | + } |
|
71 | 71 | |
72 | - $tree = $request->getAttribute('tree'); |
|
73 | - assert($tree instanceof Tree); |
|
72 | + $tree = $request->getAttribute('tree'); |
|
73 | + assert($tree instanceof Tree); |
|
74 | 74 | |
75 | - $user = Auth::check() ? $request->getAttribute('user') : new DefaultUser(); |
|
75 | + $user = Auth::check() ? $request->getAttribute('user') : new DefaultUser(); |
|
76 | 76 | |
77 | - /** @var SosaStatisticsService $sosa_stats_service */ |
|
78 | - $sosa_stats_service = app()->makeWith(SosaStatisticsService::class, ['tree' => $tree, 'user' => $user]); |
|
77 | + /** @var SosaStatisticsService $sosa_stats_service */ |
|
78 | + $sosa_stats_service = app()->makeWith(SosaStatisticsService::class, ['tree' => $tree, 'user' => $user]); |
|
79 | 79 | |
80 | - $current_gen = (int) ($request->getQueryParams()['gen'] ?? $request->getAttribute('gen') ?? 0); |
|
80 | + $current_gen = (int) ($request->getQueryParams()['gen'] ?? $request->getAttribute('gen') ?? 0); |
|
81 | 81 | |
82 | - return $this->viewResponse($this->module->name() . '::list-ancestors-page', [ |
|
83 | - 'module_name' => $this->module->name(), |
|
84 | - 'title' => I18N::translate('Sosa Ancestors'), |
|
85 | - 'tree' => $tree, |
|
86 | - 'root_indi' => $sosa_stats_service->rootIndividual(), |
|
87 | - 'max_gen' => $sosa_stats_service->maxGeneration(), |
|
88 | - 'current_gen' => $current_gen |
|
89 | - ]); |
|
90 | - } |
|
82 | + return $this->viewResponse($this->module->name() . '::list-ancestors-page', [ |
|
83 | + 'module_name' => $this->module->name(), |
|
84 | + 'title' => I18N::translate('Sosa Ancestors'), |
|
85 | + 'tree' => $tree, |
|
86 | + 'root_indi' => $sosa_stats_service->rootIndividual(), |
|
87 | + 'max_gen' => $sosa_stats_service->maxGeneration(), |
|
88 | + 'current_gen' => $current_gen |
|
89 | + ]); |
|
90 | + } |
|
91 | 91 | } |
@@ -77,9 +77,9 @@ |
||
77 | 77 | /** @var SosaStatisticsService $sosa_stats_service */ |
78 | 78 | $sosa_stats_service = app()->makeWith(SosaStatisticsService::class, ['tree' => $tree, 'user' => $user]); |
79 | 79 | |
80 | - $current_gen = (int) ($request->getQueryParams()['gen'] ?? $request->getAttribute('gen') ?? 0); |
|
80 | + $current_gen = (int)($request->getQueryParams()['gen'] ?? $request->getAttribute('gen') ?? 0); |
|
81 | 81 | |
82 | - return $this->viewResponse($this->module->name() . '::list-ancestors-page', [ |
|
82 | + return $this->viewResponse($this->module->name().'::list-ancestors-page', [ |
|
83 | 83 | 'module_name' => $this->module->name(), |
84 | 84 | 'title' => I18N::translate('Sosa Ancestors'), |
85 | 85 | 'tree' => $tree, |
@@ -36,144 +36,144 @@ |
||
36 | 36 | */ |
37 | 37 | class SosaStatistics implements RequestHandlerInterface |
38 | 38 | { |
39 | - use ViewResponseTrait; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var SosaModule|null $module |
|
43 | - */ |
|
44 | - private $module; |
|
45 | - |
|
46 | - /** |
|
47 | - * Constructor for AncestorsList Request Handler |
|
48 | - * |
|
49 | - * @param ModuleService $module_service |
|
50 | - */ |
|
51 | - public function __construct(ModuleService $module_service) |
|
52 | - { |
|
53 | - $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * {@inheritDoc} |
|
58 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
59 | - */ |
|
60 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
61 | - { |
|
62 | - if ($this->module === null) { |
|
63 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
64 | - } |
|
65 | - |
|
66 | - $tree = $request->getAttribute('tree'); |
|
67 | - assert($tree instanceof Tree); |
|
68 | - |
|
69 | - $user = Auth::check() ? $request->getAttribute('user') : new DefaultUser(); |
|
70 | - |
|
71 | - /** @var SosaStatisticsService $sosa_stats_service */ |
|
72 | - $sosa_stats_service = app()->makeWith(SosaStatisticsService::class, ['tree' => $tree, 'user' => $user]); |
|
73 | - |
|
74 | - return $this->viewResponse($this->module->name() . '::statistics-page', [ |
|
75 | - 'module_name' => $this->module->name(), |
|
76 | - 'title' => I18N::translate('Sosa Statistics'), |
|
77 | - 'tree' => $tree, |
|
78 | - 'theme' => app(ModuleThemeInterface::class), |
|
79 | - 'root_indi' => $sosa_stats_service->rootIndividual(), |
|
80 | - 'general_stats' => $this->statisticsGeneral($sosa_stats_service), |
|
81 | - 'generation_stats' => $this->statisticsByGenerations($sosa_stats_service), |
|
82 | - 'generation_depth' => $sosa_stats_service->generationDepthStatsAtGeneration(1)->first(), |
|
83 | - 'multiple_sosas' => $sosa_stats_service->topMultipleAncestorsWithNoTies(10)->groupBy('sosa_count'), |
|
84 | - 'sosa_dispersion_g2' => $sosa_stats_service->ancestorsDispersionForGeneration(2), |
|
85 | - 'sosa_dispersion_g3' => $sosa_stats_service->ancestorsDispersionForGeneration(3), |
|
86 | - 'gen_depth_g3' => $sosa_stats_service->generationDepthStatsAtGeneration(3) |
|
87 | - ]); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Retrieve and compute the global statistics of ancestors for the tree. |
|
92 | - * Statistics include the number of ancestors, the number of different ancestors, pedigree collapse... |
|
93 | - * |
|
94 | - * @param SosaStatisticsService $sosa_stats_service |
|
95 | - * @return array<string, int|float> |
|
96 | - */ |
|
97 | - private function statisticsGeneral(SosaStatisticsService $sosa_stats_service): array |
|
98 | - { |
|
99 | - $ancestors_count = $sosa_stats_service->totalAncestors(); |
|
100 | - $ancestors_distinct_count = $sosa_stats_service->totalDistinctAncestors(); |
|
101 | - $individual_count = $sosa_stats_service->totalIndividuals(); |
|
102 | - |
|
103 | - return [ |
|
104 | - 'sosa_count' => $ancestors_count, |
|
105 | - 'distinct_count' => $ancestors_distinct_count, |
|
106 | - 'sosa_rate' => $this->safeDivision( |
|
107 | - BigInteger::of($ancestors_distinct_count), |
|
108 | - BigInteger::of($individual_count) |
|
109 | - ), |
|
110 | - 'mean_gen_time' => $sosa_stats_service->meanGenerationTime() |
|
111 | - ]; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Retrieve and compute the statistics of ancestors by generations. |
|
116 | - * Statistics include the number of ancestors, the number of different ancestors, cumulative statistics... |
|
117 | - * |
|
118 | - * @param SosaStatisticsService $sosa_stats_service |
|
119 | - * @return array<int, array<string, int|float>> |
|
120 | - */ |
|
121 | - private function statisticsByGenerations(SosaStatisticsService $sosa_stats_service): array |
|
122 | - { |
|
123 | - $stats_by_gen = $sosa_stats_service->statisticsByGenerations(); |
|
124 | - |
|
125 | - $generation_stats = array(); |
|
126 | - |
|
127 | - foreach ($stats_by_gen as $gen => $stats_gen) { |
|
128 | - $gen_diff = $gen > 1 ? |
|
129 | - (int) $stats_gen['diffSosaTotalCount'] - (int) $stats_by_gen[$gen - 1]['diffSosaTotalCount'] : |
|
130 | - 1; |
|
131 | - $generation_stats[$gen] = array( |
|
132 | - 'gen_min_birth' => $stats_gen['firstBirth'] ?? (int) $stats_gen['firstEstimatedBirth'], |
|
133 | - 'gen_max_birth' => $stats_gen['lastBirth'] ?? (int) $stats_gen['lastEstimatedBirth'], |
|
134 | - 'theoretical' => BigInteger::of(2)->power($gen - 1)->toInt(), |
|
135 | - 'known' => (int) $stats_gen['sosaCount'], |
|
136 | - 'perc_known' => $this->safeDivision( |
|
137 | - BigInteger::of((int) $stats_gen['sosaCount']), |
|
138 | - BigInteger::of(2)->power($gen - 1) |
|
139 | - ), |
|
140 | - 'missing' => $gen > 1 ? |
|
141 | - 2 * (int) $stats_by_gen[$gen - 1]['sosaCount'] - (int) $stats_gen['sosaCount'] : |
|
142 | - 0, |
|
143 | - 'perc_missing' => $gen > 1 ? |
|
144 | - 1 - $this->safeDivision( |
|
145 | - BigInteger::of((int) $stats_gen['sosaCount']), |
|
146 | - BigInteger::of(2 * (int) $stats_by_gen[$gen - 1]['sosaCount']) |
|
147 | - ) : |
|
148 | - 0, |
|
149 | - 'total_known' => (int) $stats_gen['sosaTotalCount'], |
|
150 | - 'perc_total_known' => $this->safeDivision( |
|
151 | - BigInteger::of((int) $stats_gen['sosaTotalCount']), |
|
152 | - BigInteger::of(2)->power($gen)->minus(1) |
|
153 | - ), |
|
154 | - 'different' => $gen_diff, |
|
155 | - 'perc_different' => $this->safeDivision( |
|
156 | - BigInteger::of($gen_diff), |
|
157 | - BigInteger::of((int) $stats_gen['sosaCount']) |
|
158 | - ), |
|
159 | - 'total_different' => (int) $stats_gen['diffSosaTotalCount'] |
|
160 | - ); |
|
161 | - } |
|
162 | - |
|
163 | - return $generation_stats; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Return the result of a division, and a default value if denominator is 0 |
|
168 | - * |
|
169 | - * @param BigInteger $p Numerator |
|
170 | - * @param BigInteger $q Denominator |
|
171 | - * @param int $scale Rounding scale |
|
172 | - * @param float $default Value if denominator is 0 |
|
173 | - * @return float |
|
174 | - */ |
|
175 | - private function safeDivision(BigInteger $p, BigInteger $q, int $scale = 10, float $default = 0): float |
|
176 | - { |
|
177 | - return $q->isZero() ? $default : $p->toBigDecimal()->dividedBy($q, $scale, RoundingMode::HALF_DOWN)->toFloat(); |
|
178 | - } |
|
39 | + use ViewResponseTrait; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var SosaModule|null $module |
|
43 | + */ |
|
44 | + private $module; |
|
45 | + |
|
46 | + /** |
|
47 | + * Constructor for AncestorsList Request Handler |
|
48 | + * |
|
49 | + * @param ModuleService $module_service |
|
50 | + */ |
|
51 | + public function __construct(ModuleService $module_service) |
|
52 | + { |
|
53 | + $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * {@inheritDoc} |
|
58 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
59 | + */ |
|
60 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
61 | + { |
|
62 | + if ($this->module === null) { |
|
63 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
64 | + } |
|
65 | + |
|
66 | + $tree = $request->getAttribute('tree'); |
|
67 | + assert($tree instanceof Tree); |
|
68 | + |
|
69 | + $user = Auth::check() ? $request->getAttribute('user') : new DefaultUser(); |
|
70 | + |
|
71 | + /** @var SosaStatisticsService $sosa_stats_service */ |
|
72 | + $sosa_stats_service = app()->makeWith(SosaStatisticsService::class, ['tree' => $tree, 'user' => $user]); |
|
73 | + |
|
74 | + return $this->viewResponse($this->module->name() . '::statistics-page', [ |
|
75 | + 'module_name' => $this->module->name(), |
|
76 | + 'title' => I18N::translate('Sosa Statistics'), |
|
77 | + 'tree' => $tree, |
|
78 | + 'theme' => app(ModuleThemeInterface::class), |
|
79 | + 'root_indi' => $sosa_stats_service->rootIndividual(), |
|
80 | + 'general_stats' => $this->statisticsGeneral($sosa_stats_service), |
|
81 | + 'generation_stats' => $this->statisticsByGenerations($sosa_stats_service), |
|
82 | + 'generation_depth' => $sosa_stats_service->generationDepthStatsAtGeneration(1)->first(), |
|
83 | + 'multiple_sosas' => $sosa_stats_service->topMultipleAncestorsWithNoTies(10)->groupBy('sosa_count'), |
|
84 | + 'sosa_dispersion_g2' => $sosa_stats_service->ancestorsDispersionForGeneration(2), |
|
85 | + 'sosa_dispersion_g3' => $sosa_stats_service->ancestorsDispersionForGeneration(3), |
|
86 | + 'gen_depth_g3' => $sosa_stats_service->generationDepthStatsAtGeneration(3) |
|
87 | + ]); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Retrieve and compute the global statistics of ancestors for the tree. |
|
92 | + * Statistics include the number of ancestors, the number of different ancestors, pedigree collapse... |
|
93 | + * |
|
94 | + * @param SosaStatisticsService $sosa_stats_service |
|
95 | + * @return array<string, int|float> |
|
96 | + */ |
|
97 | + private function statisticsGeneral(SosaStatisticsService $sosa_stats_service): array |
|
98 | + { |
|
99 | + $ancestors_count = $sosa_stats_service->totalAncestors(); |
|
100 | + $ancestors_distinct_count = $sosa_stats_service->totalDistinctAncestors(); |
|
101 | + $individual_count = $sosa_stats_service->totalIndividuals(); |
|
102 | + |
|
103 | + return [ |
|
104 | + 'sosa_count' => $ancestors_count, |
|
105 | + 'distinct_count' => $ancestors_distinct_count, |
|
106 | + 'sosa_rate' => $this->safeDivision( |
|
107 | + BigInteger::of($ancestors_distinct_count), |
|
108 | + BigInteger::of($individual_count) |
|
109 | + ), |
|
110 | + 'mean_gen_time' => $sosa_stats_service->meanGenerationTime() |
|
111 | + ]; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Retrieve and compute the statistics of ancestors by generations. |
|
116 | + * Statistics include the number of ancestors, the number of different ancestors, cumulative statistics... |
|
117 | + * |
|
118 | + * @param SosaStatisticsService $sosa_stats_service |
|
119 | + * @return array<int, array<string, int|float>> |
|
120 | + */ |
|
121 | + private function statisticsByGenerations(SosaStatisticsService $sosa_stats_service): array |
|
122 | + { |
|
123 | + $stats_by_gen = $sosa_stats_service->statisticsByGenerations(); |
|
124 | + |
|
125 | + $generation_stats = array(); |
|
126 | + |
|
127 | + foreach ($stats_by_gen as $gen => $stats_gen) { |
|
128 | + $gen_diff = $gen > 1 ? |
|
129 | + (int) $stats_gen['diffSosaTotalCount'] - (int) $stats_by_gen[$gen - 1]['diffSosaTotalCount'] : |
|
130 | + 1; |
|
131 | + $generation_stats[$gen] = array( |
|
132 | + 'gen_min_birth' => $stats_gen['firstBirth'] ?? (int) $stats_gen['firstEstimatedBirth'], |
|
133 | + 'gen_max_birth' => $stats_gen['lastBirth'] ?? (int) $stats_gen['lastEstimatedBirth'], |
|
134 | + 'theoretical' => BigInteger::of(2)->power($gen - 1)->toInt(), |
|
135 | + 'known' => (int) $stats_gen['sosaCount'], |
|
136 | + 'perc_known' => $this->safeDivision( |
|
137 | + BigInteger::of((int) $stats_gen['sosaCount']), |
|
138 | + BigInteger::of(2)->power($gen - 1) |
|
139 | + ), |
|
140 | + 'missing' => $gen > 1 ? |
|
141 | + 2 * (int) $stats_by_gen[$gen - 1]['sosaCount'] - (int) $stats_gen['sosaCount'] : |
|
142 | + 0, |
|
143 | + 'perc_missing' => $gen > 1 ? |
|
144 | + 1 - $this->safeDivision( |
|
145 | + BigInteger::of((int) $stats_gen['sosaCount']), |
|
146 | + BigInteger::of(2 * (int) $stats_by_gen[$gen - 1]['sosaCount']) |
|
147 | + ) : |
|
148 | + 0, |
|
149 | + 'total_known' => (int) $stats_gen['sosaTotalCount'], |
|
150 | + 'perc_total_known' => $this->safeDivision( |
|
151 | + BigInteger::of((int) $stats_gen['sosaTotalCount']), |
|
152 | + BigInteger::of(2)->power($gen)->minus(1) |
|
153 | + ), |
|
154 | + 'different' => $gen_diff, |
|
155 | + 'perc_different' => $this->safeDivision( |
|
156 | + BigInteger::of($gen_diff), |
|
157 | + BigInteger::of((int) $stats_gen['sosaCount']) |
|
158 | + ), |
|
159 | + 'total_different' => (int) $stats_gen['diffSosaTotalCount'] |
|
160 | + ); |
|
161 | + } |
|
162 | + |
|
163 | + return $generation_stats; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Return the result of a division, and a default value if denominator is 0 |
|
168 | + * |
|
169 | + * @param BigInteger $p Numerator |
|
170 | + * @param BigInteger $q Denominator |
|
171 | + * @param int $scale Rounding scale |
|
172 | + * @param float $default Value if denominator is 0 |
|
173 | + * @return float |
|
174 | + */ |
|
175 | + private function safeDivision(BigInteger $p, BigInteger $q, int $scale = 10, float $default = 0): float |
|
176 | + { |
|
177 | + return $q->isZero() ? $default : $p->toBigDecimal()->dividedBy($q, $scale, RoundingMode::HALF_DOWN)->toFloat(); |
|
178 | + } |
|
179 | 179 | } |
@@ -35,74 +35,74 @@ |
||
35 | 35 | */ |
36 | 36 | class AncestorsListIndividual implements RequestHandlerInterface |
37 | 37 | { |
38 | - use ViewResponseTrait; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var SosaModule|null $module |
|
42 | - */ |
|
43 | - private $module; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var SosaRecordsService $sosa_record_service |
|
47 | - */ |
|
48 | - private $sosa_record_service; |
|
49 | - |
|
50 | - /** |
|
51 | - * Constructor for AncestorsListIndividual Request Handler |
|
52 | - * |
|
53 | - * @param ModuleService $module_service |
|
54 | - * @param SosaRecordsService $sosa_record_service |
|
55 | - */ |
|
56 | - public function __construct( |
|
57 | - ModuleService $module_service, |
|
58 | - SosaRecordsService $sosa_record_service |
|
59 | - ) { |
|
60 | - $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
61 | - $this->sosa_record_service = $sosa_record_service; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * {@inheritDoc} |
|
66 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
67 | - */ |
|
68 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
69 | - { |
|
70 | - $this->layout = 'layouts/ajax'; |
|
71 | - |
|
72 | - if ($this->module === null) { |
|
73 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
74 | - } |
|
75 | - |
|
76 | - $tree = $request->getAttribute('tree'); |
|
77 | - assert($tree instanceof Tree); |
|
78 | - |
|
79 | - $user = Auth::check() ? $request->getAttribute('user') : new DefaultUser(); |
|
80 | - |
|
81 | - $current_gen = (int) ($request->getAttribute('gen') ?? 0); |
|
82 | - |
|
83 | - if ($current_gen <= 0) { |
|
84 | - return response('Invalid generation', StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY); |
|
85 | - } |
|
86 | - |
|
87 | - $list_ancestors = $this->sosa_record_service->listAncestorsAtGeneration($tree, $user, $current_gen); |
|
88 | - $nb_ancestors_all = $list_ancestors->count(); |
|
89 | - |
|
90 | - /** @var \Illuminate\Support\Collection<int, \Fisharebest\Webtrees\Individual> $list_ancestors */ |
|
91 | - $list_ancestors = $list_ancestors->mapWithKeys(function (stdClass $value) use ($tree): ?array { |
|
92 | - $indi = Registry::individualFactory()->make($value->majs_i_id, $tree); |
|
93 | - return ($indi !== null && $indi->canShowName()) ? [(int) $value->majs_sosa => $indi] : null; |
|
94 | - })->filter(); |
|
95 | - |
|
96 | - $nb_ancestors_shown = $list_ancestors->count(); |
|
97 | - |
|
98 | - return $this->viewResponse($this->module->name() . '::list-ancestors-indi-tab', [ |
|
99 | - 'module_name' => $this->module->name(), |
|
100 | - 'title' => I18N::translate('Sosa Ancestors'), |
|
101 | - 'tree' => $tree, |
|
102 | - 'list_ancestors' => $list_ancestors, |
|
103 | - 'nb_ancestors_all' => $nb_ancestors_all, |
|
104 | - 'nb_ancestors_theor' => pow(2, $current_gen - 1), |
|
105 | - 'nb_ancestors_shown' => $nb_ancestors_shown |
|
106 | - ]); |
|
107 | - } |
|
38 | + use ViewResponseTrait; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var SosaModule|null $module |
|
42 | + */ |
|
43 | + private $module; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var SosaRecordsService $sosa_record_service |
|
47 | + */ |
|
48 | + private $sosa_record_service; |
|
49 | + |
|
50 | + /** |
|
51 | + * Constructor for AncestorsListIndividual Request Handler |
|
52 | + * |
|
53 | + * @param ModuleService $module_service |
|
54 | + * @param SosaRecordsService $sosa_record_service |
|
55 | + */ |
|
56 | + public function __construct( |
|
57 | + ModuleService $module_service, |
|
58 | + SosaRecordsService $sosa_record_service |
|
59 | + ) { |
|
60 | + $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
61 | + $this->sosa_record_service = $sosa_record_service; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * {@inheritDoc} |
|
66 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
67 | + */ |
|
68 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
69 | + { |
|
70 | + $this->layout = 'layouts/ajax'; |
|
71 | + |
|
72 | + if ($this->module === null) { |
|
73 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
74 | + } |
|
75 | + |
|
76 | + $tree = $request->getAttribute('tree'); |
|
77 | + assert($tree instanceof Tree); |
|
78 | + |
|
79 | + $user = Auth::check() ? $request->getAttribute('user') : new DefaultUser(); |
|
80 | + |
|
81 | + $current_gen = (int) ($request->getAttribute('gen') ?? 0); |
|
82 | + |
|
83 | + if ($current_gen <= 0) { |
|
84 | + return response('Invalid generation', StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY); |
|
85 | + } |
|
86 | + |
|
87 | + $list_ancestors = $this->sosa_record_service->listAncestorsAtGeneration($tree, $user, $current_gen); |
|
88 | + $nb_ancestors_all = $list_ancestors->count(); |
|
89 | + |
|
90 | + /** @var \Illuminate\Support\Collection<int, \Fisharebest\Webtrees\Individual> $list_ancestors */ |
|
91 | + $list_ancestors = $list_ancestors->mapWithKeys(function (stdClass $value) use ($tree): ?array { |
|
92 | + $indi = Registry::individualFactory()->make($value->majs_i_id, $tree); |
|
93 | + return ($indi !== null && $indi->canShowName()) ? [(int) $value->majs_sosa => $indi] : null; |
|
94 | + })->filter(); |
|
95 | + |
|
96 | + $nb_ancestors_shown = $list_ancestors->count(); |
|
97 | + |
|
98 | + return $this->viewResponse($this->module->name() . '::list-ancestors-indi-tab', [ |
|
99 | + 'module_name' => $this->module->name(), |
|
100 | + 'title' => I18N::translate('Sosa Ancestors'), |
|
101 | + 'tree' => $tree, |
|
102 | + 'list_ancestors' => $list_ancestors, |
|
103 | + 'nb_ancestors_all' => $nb_ancestors_all, |
|
104 | + 'nb_ancestors_theor' => pow(2, $current_gen - 1), |
|
105 | + 'nb_ancestors_shown' => $nb_ancestors_shown |
|
106 | + ]); |
|
107 | + } |
|
108 | 108 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | |
79 | 79 | $user = Auth::check() ? $request->getAttribute('user') : new DefaultUser(); |
80 | 80 | |
81 | - $current_gen = (int) ($request->getAttribute('gen') ?? 0); |
|
81 | + $current_gen = (int)($request->getAttribute('gen') ?? 0); |
|
82 | 82 | |
83 | 83 | if ($current_gen <= 0) { |
84 | 84 | return response('Invalid generation', StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY); |
@@ -88,14 +88,14 @@ discard block |
||
88 | 88 | $nb_ancestors_all = $list_ancestors->count(); |
89 | 89 | |
90 | 90 | /** @var \Illuminate\Support\Collection<int, \Fisharebest\Webtrees\Individual> $list_ancestors */ |
91 | - $list_ancestors = $list_ancestors->mapWithKeys(function (stdClass $value) use ($tree): ?array { |
|
91 | + $list_ancestors = $list_ancestors->mapWithKeys(function(stdClass $value) use ($tree): ?array { |
|
92 | 92 | $indi = Registry::individualFactory()->make($value->majs_i_id, $tree); |
93 | - return ($indi !== null && $indi->canShowName()) ? [(int) $value->majs_sosa => $indi] : null; |
|
93 | + return ($indi !== null && $indi->canShowName()) ? [(int)$value->majs_sosa => $indi] : null; |
|
94 | 94 | })->filter(); |
95 | 95 | |
96 | 96 | $nb_ancestors_shown = $list_ancestors->count(); |
97 | 97 | |
98 | - return $this->viewResponse($this->module->name() . '::list-ancestors-indi-tab', [ |
|
98 | + return $this->viewResponse($this->module->name().'::list-ancestors-indi-tab', [ |
|
99 | 99 | 'module_name' => $this->module->name(), |
100 | 100 | 'title' => I18N::translate('Sosa Ancestors'), |
101 | 101 | 'tree' => $tree, |
@@ -28,40 +28,40 @@ |
||
28 | 28 | */ |
29 | 29 | class SosaComputeModal implements RequestHandlerInterface |
30 | 30 | { |
31 | - /** |
|
32 | - * @var SosaModule|null $module |
|
33 | - */ |
|
34 | - private $module; |
|
31 | + /** |
|
32 | + * @var SosaModule|null $module |
|
33 | + */ |
|
34 | + private $module; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Constructor for SosaComputeModal Request Handler |
|
38 | - * |
|
39 | - * @param ModuleService $module_service |
|
40 | - */ |
|
41 | - public function __construct(ModuleService $module_service) |
|
42 | - { |
|
43 | - $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
44 | - } |
|
36 | + /** |
|
37 | + * Constructor for SosaComputeModal Request Handler |
|
38 | + * |
|
39 | + * @param ModuleService $module_service |
|
40 | + */ |
|
41 | + public function __construct(ModuleService $module_service) |
|
42 | + { |
|
43 | + $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * {@inheritDoc} |
|
48 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
49 | - */ |
|
50 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
51 | - { |
|
52 | - if ($this->module === null) { |
|
53 | - return response(view('modals/error', [ |
|
54 | - 'title' => I18N::translate('Error'), |
|
55 | - 'error' => I18N::translate('The attached module could not be found.') |
|
56 | - ])); |
|
57 | - } |
|
46 | + /** |
|
47 | + * {@inheritDoc} |
|
48 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
49 | + */ |
|
50 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
51 | + { |
|
52 | + if ($this->module === null) { |
|
53 | + return response(view('modals/error', [ |
|
54 | + 'title' => I18N::translate('Error'), |
|
55 | + 'error' => I18N::translate('The attached module could not be found.') |
|
56 | + ])); |
|
57 | + } |
|
58 | 58 | |
59 | - $tree = $request->getAttribute('tree'); |
|
60 | - assert($tree instanceof Tree); |
|
59 | + $tree = $request->getAttribute('tree'); |
|
60 | + assert($tree instanceof Tree); |
|
61 | 61 | |
62 | - return response(view($this->module->name() . '::modals/sosa-compute', [ |
|
63 | - 'tree' => $tree, |
|
64 | - 'xref' => $request->getAttribute('xref') |
|
65 | - ])); |
|
66 | - } |
|
62 | + return response(view($this->module->name() . '::modals/sosa-compute', [ |
|
63 | + 'tree' => $tree, |
|
64 | + 'xref' => $request->getAttribute('xref') |
|
65 | + ])); |
|
66 | + } |
|
67 | 67 | } |
@@ -32,72 +32,72 @@ |
||
32 | 32 | */ |
33 | 33 | class SosaConfig implements RequestHandlerInterface |
34 | 34 | { |
35 | - use ViewResponseTrait; |
|
35 | + use ViewResponseTrait; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @var SosaModule|null $module |
|
39 | - */ |
|
40 | - private $module; |
|
37 | + /** |
|
38 | + * @var SosaModule|null $module |
|
39 | + */ |
|
40 | + private $module; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Constructor for SosaConfig Request Handler |
|
44 | - * |
|
45 | - * @param ModuleService $module_service |
|
46 | - */ |
|
47 | - public function __construct(ModuleService $module_service) |
|
48 | - { |
|
49 | - $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
50 | - } |
|
42 | + /** |
|
43 | + * Constructor for SosaConfig Request Handler |
|
44 | + * |
|
45 | + * @param ModuleService $module_service |
|
46 | + */ |
|
47 | + public function __construct(ModuleService $module_service) |
|
48 | + { |
|
49 | + $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
50 | + } |
|
51 | 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 | - } |
|
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 | 61 | |
62 | - $tree = $request->getAttribute('tree'); |
|
63 | - assert($tree instanceof Tree); |
|
62 | + $tree = $request->getAttribute('tree'); |
|
63 | + assert($tree instanceof Tree); |
|
64 | 64 | |
65 | - $users_root = array(); |
|
66 | - if (Auth::check()) { |
|
67 | - /** @var \Fisharebest\Webtrees\User $user */ |
|
68 | - $user = Auth::user(); |
|
69 | - $users_root[] = [ |
|
70 | - 'user' => $user, |
|
71 | - 'root_id' => $tree->getUserPreference($user, 'MAJ_SOSA_ROOT_ID'), |
|
72 | - 'max_gen' => $tree->getUserPreference($user, 'MAJ_SOSA_MAX_GEN') |
|
73 | - ]; |
|
65 | + $users_root = array(); |
|
66 | + if (Auth::check()) { |
|
67 | + /** @var \Fisharebest\Webtrees\User $user */ |
|
68 | + $user = Auth::user(); |
|
69 | + $users_root[] = [ |
|
70 | + 'user' => $user, |
|
71 | + 'root_id' => $tree->getUserPreference($user, 'MAJ_SOSA_ROOT_ID'), |
|
72 | + 'max_gen' => $tree->getUserPreference($user, 'MAJ_SOSA_MAX_GEN') |
|
73 | + ]; |
|
74 | 74 | |
75 | - if (Auth::isManager($tree)) { |
|
76 | - $default_user = new DefaultUser(); |
|
77 | - $users_root[] = [ |
|
78 | - 'user' => $default_user, |
|
79 | - 'root_id' => $tree->getUserPreference($default_user, 'MAJ_SOSA_ROOT_ID'), |
|
80 | - 'max_gen' => $tree->getUserPreference($default_user, 'MAJ_SOSA_MAX_GEN') |
|
81 | - ]; |
|
82 | - } |
|
83 | - } |
|
75 | + if (Auth::isManager($tree)) { |
|
76 | + $default_user = new DefaultUser(); |
|
77 | + $users_root[] = [ |
|
78 | + 'user' => $default_user, |
|
79 | + 'root_id' => $tree->getUserPreference($default_user, 'MAJ_SOSA_ROOT_ID'), |
|
80 | + 'max_gen' => $tree->getUserPreference($default_user, 'MAJ_SOSA_MAX_GEN') |
|
81 | + ]; |
|
82 | + } |
|
83 | + } |
|
84 | 84 | |
85 | - // Use the system max generations if not set |
|
86 | - $max_gen_system = app(SosaRecordsService::class)->maxSystemGenerations(); |
|
87 | - foreach ($users_root as $key => $user_root) { |
|
88 | - $users_root[$key]['max_gen'] = is_numeric($user_root['max_gen']) ? |
|
89 | - (int) $user_root['max_gen'] : |
|
90 | - $max_gen_system; |
|
91 | - }; |
|
85 | + // Use the system max generations if not set |
|
86 | + $max_gen_system = app(SosaRecordsService::class)->maxSystemGenerations(); |
|
87 | + foreach ($users_root as $key => $user_root) { |
|
88 | + $users_root[$key]['max_gen'] = is_numeric($user_root['max_gen']) ? |
|
89 | + (int) $user_root['max_gen'] : |
|
90 | + $max_gen_system; |
|
91 | + }; |
|
92 | 92 | |
93 | - return $this->viewResponse($this->module->name() . '::config-page', [ |
|
94 | - 'module_name' => $this->module->name(), |
|
95 | - 'title' => I18N::translate('Sosa Configuration'), |
|
96 | - 'tree' => $tree, |
|
97 | - 'user_id' => $request->getAttribute('user'), |
|
98 | - 'selected_user_id' => (int) ($request->getQueryParams()['user_id'] ?? 0), |
|
99 | - 'immediate_compute' => ($request->getQueryParams()['compute'] ?? '') == 'yes', |
|
100 | - 'users_root' => $users_root |
|
101 | - ]); |
|
102 | - } |
|
93 | + return $this->viewResponse($this->module->name() . '::config-page', [ |
|
94 | + 'module_name' => $this->module->name(), |
|
95 | + 'title' => I18N::translate('Sosa Configuration'), |
|
96 | + 'tree' => $tree, |
|
97 | + 'user_id' => $request->getAttribute('user'), |
|
98 | + 'selected_user_id' => (int) ($request->getQueryParams()['user_id'] ?? 0), |
|
99 | + 'immediate_compute' => ($request->getQueryParams()['compute'] ?? '') == 'yes', |
|
100 | + 'users_root' => $users_root |
|
101 | + ]); |
|
102 | + } |
|
103 | 103 | } |
@@ -35,73 +35,73 @@ |
||
35 | 35 | */ |
36 | 36 | class AncestorsListFamily implements RequestHandlerInterface |
37 | 37 | { |
38 | - use ViewResponseTrait; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var SosaModule|null $module |
|
42 | - */ |
|
43 | - private $module; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var SosaRecordsService $sosa_record_service |
|
47 | - */ |
|
48 | - private $sosa_record_service; |
|
49 | - |
|
50 | - /** |
|
51 | - * Constructor for AncestorsListFamily Request Handler |
|
52 | - * |
|
53 | - * @param ModuleService $module_service |
|
54 | - * @param SosaRecordsService $sosa_record_service |
|
55 | - */ |
|
56 | - public function __construct( |
|
57 | - ModuleService $module_service, |
|
58 | - SosaRecordsService $sosa_record_service |
|
59 | - ) { |
|
60 | - $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
61 | - $this->sosa_record_service = $sosa_record_service; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * {@inheritDoc} |
|
66 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
67 | - */ |
|
68 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
69 | - { |
|
70 | - $this->layout = 'layouts/ajax'; |
|
71 | - |
|
72 | - if ($this->module === null) { |
|
73 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
74 | - } |
|
75 | - |
|
76 | - $tree = $request->getAttribute('tree'); |
|
77 | - assert($tree instanceof Tree); |
|
78 | - |
|
79 | - $user = Auth::check() ? $request->getAttribute('user') : new DefaultUser(); |
|
80 | - |
|
81 | - $current_gen = (int) ($request->getAttribute('gen') ?? 0); |
|
82 | - |
|
83 | - if ($current_gen <= 0) { |
|
84 | - return response('Invalid generation', StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY); |
|
85 | - } |
|
86 | - |
|
87 | - $list_families = $this->sosa_record_service->listAncestorFamiliesAtGeneration($tree, $user, $current_gen); |
|
88 | - $nb_families_all = $list_families->count(); |
|
89 | - |
|
90 | - /** @var \Illuminate\Support\Collection<int, \Fisharebest\Webtrees\Family> $list_families */ |
|
91 | - $list_families = $list_families->mapWithKeys(function (stdClass $value) use ($tree): ?array { |
|
92 | - $fam = Registry::familyFactory()->make($value->f_id, $tree); |
|
93 | - return ($fam !== null && $fam->canShow()) ? [(int) $value->majs_sosa => $fam] : null; |
|
94 | - })->filter(); |
|
95 | - |
|
96 | - $nb_families_shown = $list_families->count(); |
|
97 | - |
|
98 | - return $this->viewResponse($this->module->name() . '::list-ancestors-fam-tab', [ |
|
99 | - 'module_name' => $this->module->name(), |
|
100 | - 'title' => I18N::translate('Sosa Ancestors'), |
|
101 | - 'tree' => $tree, |
|
102 | - 'list_families' => $list_families, |
|
103 | - 'nb_families_all' => $nb_families_all, |
|
104 | - 'nb_families_shown' => $nb_families_shown |
|
105 | - ]); |
|
106 | - } |
|
38 | + use ViewResponseTrait; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var SosaModule|null $module |
|
42 | + */ |
|
43 | + private $module; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var SosaRecordsService $sosa_record_service |
|
47 | + */ |
|
48 | + private $sosa_record_service; |
|
49 | + |
|
50 | + /** |
|
51 | + * Constructor for AncestorsListFamily Request Handler |
|
52 | + * |
|
53 | + * @param ModuleService $module_service |
|
54 | + * @param SosaRecordsService $sosa_record_service |
|
55 | + */ |
|
56 | + public function __construct( |
|
57 | + ModuleService $module_service, |
|
58 | + SosaRecordsService $sosa_record_service |
|
59 | + ) { |
|
60 | + $this->module = $module_service->findByInterface(SosaModule::class)->first(); |
|
61 | + $this->sosa_record_service = $sosa_record_service; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * {@inheritDoc} |
|
66 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
67 | + */ |
|
68 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
69 | + { |
|
70 | + $this->layout = 'layouts/ajax'; |
|
71 | + |
|
72 | + if ($this->module === null) { |
|
73 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
74 | + } |
|
75 | + |
|
76 | + $tree = $request->getAttribute('tree'); |
|
77 | + assert($tree instanceof Tree); |
|
78 | + |
|
79 | + $user = Auth::check() ? $request->getAttribute('user') : new DefaultUser(); |
|
80 | + |
|
81 | + $current_gen = (int) ($request->getAttribute('gen') ?? 0); |
|
82 | + |
|
83 | + if ($current_gen <= 0) { |
|
84 | + return response('Invalid generation', StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY); |
|
85 | + } |
|
86 | + |
|
87 | + $list_families = $this->sosa_record_service->listAncestorFamiliesAtGeneration($tree, $user, $current_gen); |
|
88 | + $nb_families_all = $list_families->count(); |
|
89 | + |
|
90 | + /** @var \Illuminate\Support\Collection<int, \Fisharebest\Webtrees\Family> $list_families */ |
|
91 | + $list_families = $list_families->mapWithKeys(function (stdClass $value) use ($tree): ?array { |
|
92 | + $fam = Registry::familyFactory()->make($value->f_id, $tree); |
|
93 | + return ($fam !== null && $fam->canShow()) ? [(int) $value->majs_sosa => $fam] : null; |
|
94 | + })->filter(); |
|
95 | + |
|
96 | + $nb_families_shown = $list_families->count(); |
|
97 | + |
|
98 | + return $this->viewResponse($this->module->name() . '::list-ancestors-fam-tab', [ |
|
99 | + 'module_name' => $this->module->name(), |
|
100 | + 'title' => I18N::translate('Sosa Ancestors'), |
|
101 | + 'tree' => $tree, |
|
102 | + 'list_families' => $list_families, |
|
103 | + 'nb_families_all' => $nb_families_all, |
|
104 | + 'nb_families_shown' => $nb_families_shown |
|
105 | + ]); |
|
106 | + } |
|
107 | 107 | } |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | |
79 | 79 | $user = Auth::check() ? $request->getAttribute('user') : new DefaultUser(); |
80 | 80 | |
81 | - $current_gen = (int) ($request->getAttribute('gen') ?? 0); |
|
81 | + $current_gen = (int)($request->getAttribute('gen') ?? 0); |
|
82 | 82 | |
83 | 83 | if ($current_gen <= 0) { |
84 | 84 | return response('Invalid generation', StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY); |
@@ -88,14 +88,14 @@ discard block |
||
88 | 88 | $nb_families_all = $list_families->count(); |
89 | 89 | |
90 | 90 | /** @var \Illuminate\Support\Collection<int, \Fisharebest\Webtrees\Family> $list_families */ |
91 | - $list_families = $list_families->mapWithKeys(function (stdClass $value) use ($tree): ?array { |
|
91 | + $list_families = $list_families->mapWithKeys(function(stdClass $value) use ($tree): ?array { |
|
92 | 92 | $fam = Registry::familyFactory()->make($value->f_id, $tree); |
93 | - return ($fam !== null && $fam->canShow()) ? [(int) $value->majs_sosa => $fam] : null; |
|
93 | + return ($fam !== null && $fam->canShow()) ? [(int)$value->majs_sosa => $fam] : null; |
|
94 | 94 | })->filter(); |
95 | 95 | |
96 | 96 | $nb_families_shown = $list_families->count(); |
97 | 97 | |
98 | - return $this->viewResponse($this->module->name() . '::list-ancestors-fam-tab', [ |
|
98 | + return $this->viewResponse($this->module->name().'::list-ancestors-fam-tab', [ |
|
99 | 99 | 'module_name' => $this->module->name(), |
100 | 100 | 'title' => I18N::translate('Sosa Ancestors'), |
101 | 101 | 'tree' => $tree, |