@@ -25,290 +25,290 @@ |
||
| 25 | 25 | class TaskSchedule |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Task Schedule ID |
|
| 30 | - * @var int $id |
|
| 31 | - */ |
|
| 32 | - private $id; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Task schedule status |
|
| 36 | - * @var bool $enabled |
|
| 37 | - */ |
|
| 38 | - private $enabled; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * ID of the task attached to schedule |
|
| 42 | - * @var string $task_id |
|
| 43 | - */ |
|
| 44 | - private $task_id; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Last updated date |
|
| 48 | - * @var Carbon $last_run |
|
| 49 | - */ |
|
| 50 | - private $last_run; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Last run result |
|
| 54 | - * @var bool $last_result |
|
| 55 | - */ |
|
| 56 | - private $last_result; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Task run frequency |
|
| 60 | - * @var CarbonInterval $frequency |
|
| 61 | - */ |
|
| 62 | - private $frequency; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Task remaining runs |
|
| 66 | - * @var int $nb_occurrences |
|
| 67 | - */ |
|
| 68 | - private $nb_occurrences; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Current running status of the task |
|
| 72 | - * @var bool $is_running |
|
| 73 | - */ |
|
| 74 | - private $is_running; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Constructor for TaskSchedule |
|
| 78 | - * |
|
| 79 | - * @param int $id Schedule ID |
|
| 80 | - * @param string $task_id Task ID |
|
| 81 | - * @param bool $enabled Is the schedule enabled |
|
| 82 | - * @param Carbon $last_run Last successful run date/time |
|
| 83 | - * @param bool $last_result Result of the last run |
|
| 84 | - * @param CarbonInterval $frequency Schedule frequency |
|
| 85 | - * @param int $nb_occurrences Number of remaining occurrences to be run |
|
| 86 | - * @param bool $is_running Is the task currently running |
|
| 87 | - */ |
|
| 88 | - public function __construct( |
|
| 89 | - int $id, |
|
| 90 | - string $task_id, |
|
| 91 | - bool $enabled, |
|
| 92 | - Carbon $last_run, |
|
| 93 | - bool $last_result, |
|
| 94 | - CarbonInterval $frequency, |
|
| 95 | - int $nb_occurrences, |
|
| 96 | - bool $is_running |
|
| 97 | - ) { |
|
| 98 | - $this->id = $id; |
|
| 99 | - $this->task_id = $task_id; |
|
| 100 | - $this->enabled = $enabled; |
|
| 101 | - $this->last_run = $last_run; |
|
| 102 | - $this->last_result = $last_result; |
|
| 103 | - $this->frequency = $frequency; |
|
| 104 | - $this->nb_occurrences = $nb_occurrences; |
|
| 105 | - $this->is_running = $is_running; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get the schedule ID. |
|
| 110 | - * |
|
| 111 | - * @return int |
|
| 112 | - */ |
|
| 113 | - public function id(): int |
|
| 114 | - { |
|
| 115 | - return $this->id; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Get the task ID. |
|
| 120 | - * |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - public function taskId(): string |
|
| 124 | - { |
|
| 125 | - return $this->task_id; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Returns whether the schedule is enabled |
|
| 130 | - * |
|
| 131 | - * @return bool |
|
| 132 | - */ |
|
| 133 | - public function isEnabled(): bool |
|
| 134 | - { |
|
| 135 | - return $this->enabled; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Enable the schedule |
|
| 140 | - * |
|
| 141 | - * @return self |
|
| 142 | - */ |
|
| 143 | - public function enable(): self |
|
| 144 | - { |
|
| 145 | - $this->enabled = true; |
|
| 146 | - return $this; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Disable the schedule |
|
| 151 | - * |
|
| 152 | - * @return self |
|
| 153 | - */ |
|
| 154 | - public function disable(): self |
|
| 155 | - { |
|
| 156 | - $this->enabled = false; |
|
| 157 | - return $this; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Get the frequency of the schedule |
|
| 162 | - * |
|
| 163 | - * @return CarbonInterval |
|
| 164 | - */ |
|
| 165 | - public function frequency(): CarbonInterval |
|
| 166 | - { |
|
| 167 | - return $this->frequency; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Set the frequency of the schedule |
|
| 172 | - * |
|
| 173 | - * @param CarbonInterval $frequency |
|
| 174 | - * @return self |
|
| 175 | - */ |
|
| 176 | - public function setFrequency(CarbonInterval $frequency): self |
|
| 177 | - { |
|
| 178 | - $this->frequency = $frequency; |
|
| 179 | - return $this; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Get the date/time of the last successful run. |
|
| 184 | - * |
|
| 185 | - * @return Carbon |
|
| 186 | - */ |
|
| 187 | - public function lastRunTime(): Carbon |
|
| 188 | - { |
|
| 189 | - return $this->last_run; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Set the last successful run date/time |
|
| 194 | - * |
|
| 195 | - * @param Carbon $last_run |
|
| 196 | - * @return self |
|
| 197 | - */ |
|
| 198 | - public function setLastRunTime(Carbon $last_run): self |
|
| 199 | - { |
|
| 200 | - $this->last_run = $last_run; |
|
| 201 | - return $this; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Returns whether the last run was successful |
|
| 206 | - * |
|
| 207 | - * @return bool |
|
| 208 | - */ |
|
| 209 | - public function wasLastRunSuccess(): bool |
|
| 210 | - { |
|
| 211 | - return $this->last_result; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Set the last run result |
|
| 216 | - * |
|
| 217 | - * @param bool $last_result |
|
| 218 | - * @return self |
|
| 219 | - */ |
|
| 220 | - public function setLastResult(bool $last_result): self |
|
| 221 | - { |
|
| 222 | - $this->last_result = $last_result; |
|
| 223 | - return $this; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Get the number of remaining of occurrences of task runs. |
|
| 228 | - * Returns 0 if the tasks must be run indefinitely. |
|
| 229 | - * |
|
| 230 | - * @return int |
|
| 231 | - */ |
|
| 232 | - public function remainingOccurences(): int |
|
| 233 | - { |
|
| 234 | - return $this->nb_occurrences; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Decrements the number of remaining occurences by 1. |
|
| 239 | - * The task will be disabled when the number reaches 0. |
|
| 240 | - * |
|
| 241 | - * @return self |
|
| 242 | - */ |
|
| 243 | - public function decrementRemainingOccurences(): self |
|
| 244 | - { |
|
| 245 | - if ($this->nb_occurrences > 0) { |
|
| 246 | - $this->nb_occurrences--; |
|
| 247 | - if ($this->nb_occurrences == 0) { |
|
| 248 | - $this->disable(); |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - return $this; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Set the number of remaining occurences of task runs. |
|
| 256 | - * |
|
| 257 | - * @param int $nb_occurrences |
|
| 258 | - * @return self |
|
| 259 | - */ |
|
| 260 | - public function setRemainingOccurences(int $nb_occurrences): self |
|
| 261 | - { |
|
| 262 | - $this->nb_occurrences = $nb_occurrences; |
|
| 263 | - return $this; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Returns whether the task is running |
|
| 268 | - * @return bool |
|
| 269 | - */ |
|
| 270 | - public function isRunning(): bool |
|
| 271 | - { |
|
| 272 | - return $this->is_running; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Informs the schedule that the task is going to run |
|
| 277 | - * |
|
| 278 | - * @return self |
|
| 279 | - */ |
|
| 280 | - public function startRunning(): self |
|
| 281 | - { |
|
| 282 | - $this->is_running = true; |
|
| 283 | - return $this; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Informs the schedule that the task has stopped running. |
|
| 288 | - * @return self |
|
| 289 | - */ |
|
| 290 | - public function stopRunning(): self |
|
| 291 | - { |
|
| 292 | - $this->is_running = false; |
|
| 293 | - return $this; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Returns the schedule details as an associate array |
|
| 298 | - * |
|
| 299 | - * @return array |
|
| 300 | - */ |
|
| 301 | - public function toArray(): array |
|
| 302 | - { |
|
| 303 | - return [ |
|
| 304 | - 'id' => $this->id, |
|
| 305 | - 'task_id' => $this->task_id, |
|
| 306 | - 'enabled' => $this->enabled, |
|
| 307 | - 'last_run' => $this->last_run, |
|
| 308 | - 'last_result' => $this->last_result, |
|
| 309 | - 'frequency' => $this->frequency, |
|
| 310 | - 'nb_occurrences' => $this->nb_occurrences, |
|
| 311 | - 'is_running' => $this->is_running |
|
| 312 | - ]; |
|
| 313 | - } |
|
| 28 | + /** |
|
| 29 | + * Task Schedule ID |
|
| 30 | + * @var int $id |
|
| 31 | + */ |
|
| 32 | + private $id; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Task schedule status |
|
| 36 | + * @var bool $enabled |
|
| 37 | + */ |
|
| 38 | + private $enabled; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * ID of the task attached to schedule |
|
| 42 | + * @var string $task_id |
|
| 43 | + */ |
|
| 44 | + private $task_id; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Last updated date |
|
| 48 | + * @var Carbon $last_run |
|
| 49 | + */ |
|
| 50 | + private $last_run; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Last run result |
|
| 54 | + * @var bool $last_result |
|
| 55 | + */ |
|
| 56 | + private $last_result; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Task run frequency |
|
| 60 | + * @var CarbonInterval $frequency |
|
| 61 | + */ |
|
| 62 | + private $frequency; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Task remaining runs |
|
| 66 | + * @var int $nb_occurrences |
|
| 67 | + */ |
|
| 68 | + private $nb_occurrences; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Current running status of the task |
|
| 72 | + * @var bool $is_running |
|
| 73 | + */ |
|
| 74 | + private $is_running; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Constructor for TaskSchedule |
|
| 78 | + * |
|
| 79 | + * @param int $id Schedule ID |
|
| 80 | + * @param string $task_id Task ID |
|
| 81 | + * @param bool $enabled Is the schedule enabled |
|
| 82 | + * @param Carbon $last_run Last successful run date/time |
|
| 83 | + * @param bool $last_result Result of the last run |
|
| 84 | + * @param CarbonInterval $frequency Schedule frequency |
|
| 85 | + * @param int $nb_occurrences Number of remaining occurrences to be run |
|
| 86 | + * @param bool $is_running Is the task currently running |
|
| 87 | + */ |
|
| 88 | + public function __construct( |
|
| 89 | + int $id, |
|
| 90 | + string $task_id, |
|
| 91 | + bool $enabled, |
|
| 92 | + Carbon $last_run, |
|
| 93 | + bool $last_result, |
|
| 94 | + CarbonInterval $frequency, |
|
| 95 | + int $nb_occurrences, |
|
| 96 | + bool $is_running |
|
| 97 | + ) { |
|
| 98 | + $this->id = $id; |
|
| 99 | + $this->task_id = $task_id; |
|
| 100 | + $this->enabled = $enabled; |
|
| 101 | + $this->last_run = $last_run; |
|
| 102 | + $this->last_result = $last_result; |
|
| 103 | + $this->frequency = $frequency; |
|
| 104 | + $this->nb_occurrences = $nb_occurrences; |
|
| 105 | + $this->is_running = $is_running; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get the schedule ID. |
|
| 110 | + * |
|
| 111 | + * @return int |
|
| 112 | + */ |
|
| 113 | + public function id(): int |
|
| 114 | + { |
|
| 115 | + return $this->id; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Get the task ID. |
|
| 120 | + * |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + public function taskId(): string |
|
| 124 | + { |
|
| 125 | + return $this->task_id; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Returns whether the schedule is enabled |
|
| 130 | + * |
|
| 131 | + * @return bool |
|
| 132 | + */ |
|
| 133 | + public function isEnabled(): bool |
|
| 134 | + { |
|
| 135 | + return $this->enabled; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Enable the schedule |
|
| 140 | + * |
|
| 141 | + * @return self |
|
| 142 | + */ |
|
| 143 | + public function enable(): self |
|
| 144 | + { |
|
| 145 | + $this->enabled = true; |
|
| 146 | + return $this; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Disable the schedule |
|
| 151 | + * |
|
| 152 | + * @return self |
|
| 153 | + */ |
|
| 154 | + public function disable(): self |
|
| 155 | + { |
|
| 156 | + $this->enabled = false; |
|
| 157 | + return $this; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Get the frequency of the schedule |
|
| 162 | + * |
|
| 163 | + * @return CarbonInterval |
|
| 164 | + */ |
|
| 165 | + public function frequency(): CarbonInterval |
|
| 166 | + { |
|
| 167 | + return $this->frequency; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Set the frequency of the schedule |
|
| 172 | + * |
|
| 173 | + * @param CarbonInterval $frequency |
|
| 174 | + * @return self |
|
| 175 | + */ |
|
| 176 | + public function setFrequency(CarbonInterval $frequency): self |
|
| 177 | + { |
|
| 178 | + $this->frequency = $frequency; |
|
| 179 | + return $this; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Get the date/time of the last successful run. |
|
| 184 | + * |
|
| 185 | + * @return Carbon |
|
| 186 | + */ |
|
| 187 | + public function lastRunTime(): Carbon |
|
| 188 | + { |
|
| 189 | + return $this->last_run; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Set the last successful run date/time |
|
| 194 | + * |
|
| 195 | + * @param Carbon $last_run |
|
| 196 | + * @return self |
|
| 197 | + */ |
|
| 198 | + public function setLastRunTime(Carbon $last_run): self |
|
| 199 | + { |
|
| 200 | + $this->last_run = $last_run; |
|
| 201 | + return $this; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Returns whether the last run was successful |
|
| 206 | + * |
|
| 207 | + * @return bool |
|
| 208 | + */ |
|
| 209 | + public function wasLastRunSuccess(): bool |
|
| 210 | + { |
|
| 211 | + return $this->last_result; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Set the last run result |
|
| 216 | + * |
|
| 217 | + * @param bool $last_result |
|
| 218 | + * @return self |
|
| 219 | + */ |
|
| 220 | + public function setLastResult(bool $last_result): self |
|
| 221 | + { |
|
| 222 | + $this->last_result = $last_result; |
|
| 223 | + return $this; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Get the number of remaining of occurrences of task runs. |
|
| 228 | + * Returns 0 if the tasks must be run indefinitely. |
|
| 229 | + * |
|
| 230 | + * @return int |
|
| 231 | + */ |
|
| 232 | + public function remainingOccurences(): int |
|
| 233 | + { |
|
| 234 | + return $this->nb_occurrences; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Decrements the number of remaining occurences by 1. |
|
| 239 | + * The task will be disabled when the number reaches 0. |
|
| 240 | + * |
|
| 241 | + * @return self |
|
| 242 | + */ |
|
| 243 | + public function decrementRemainingOccurences(): self |
|
| 244 | + { |
|
| 245 | + if ($this->nb_occurrences > 0) { |
|
| 246 | + $this->nb_occurrences--; |
|
| 247 | + if ($this->nb_occurrences == 0) { |
|
| 248 | + $this->disable(); |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + return $this; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Set the number of remaining occurences of task runs. |
|
| 256 | + * |
|
| 257 | + * @param int $nb_occurrences |
|
| 258 | + * @return self |
|
| 259 | + */ |
|
| 260 | + public function setRemainingOccurences(int $nb_occurrences): self |
|
| 261 | + { |
|
| 262 | + $this->nb_occurrences = $nb_occurrences; |
|
| 263 | + return $this; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Returns whether the task is running |
|
| 268 | + * @return bool |
|
| 269 | + */ |
|
| 270 | + public function isRunning(): bool |
|
| 271 | + { |
|
| 272 | + return $this->is_running; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Informs the schedule that the task is going to run |
|
| 277 | + * |
|
| 278 | + * @return self |
|
| 279 | + */ |
|
| 280 | + public function startRunning(): self |
|
| 281 | + { |
|
| 282 | + $this->is_running = true; |
|
| 283 | + return $this; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Informs the schedule that the task has stopped running. |
|
| 288 | + * @return self |
|
| 289 | + */ |
|
| 290 | + public function stopRunning(): self |
|
| 291 | + { |
|
| 292 | + $this->is_running = false; |
|
| 293 | + return $this; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Returns the schedule details as an associate array |
|
| 298 | + * |
|
| 299 | + * @return array |
|
| 300 | + */ |
|
| 301 | + public function toArray(): array |
|
| 302 | + { |
|
| 303 | + return [ |
|
| 304 | + 'id' => $this->id, |
|
| 305 | + 'task_id' => $this->task_id, |
|
| 306 | + 'enabled' => $this->enabled, |
|
| 307 | + 'last_run' => $this->last_run, |
|
| 308 | + 'last_result' => $this->last_result, |
|
| 309 | + 'frequency' => $this->frequency, |
|
| 310 | + 'nb_occurrences' => $this->nb_occurrences, |
|
| 311 | + 'is_running' => $this->is_running |
|
| 312 | + ]; |
|
| 313 | + } |
|
| 314 | 314 | } |
@@ -31,110 +31,110 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class TasksList implements RequestHandlerInterface |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * @var AdminTasksModule $module |
|
| 36 | - */ |
|
| 37 | - private $module; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var TaskScheduleService $taskschedules_service |
|
| 41 | - */ |
|
| 42 | - private $taskschedules_service; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @var DatatablesService $datatables_service |
|
| 46 | - */ |
|
| 47 | - private $datatables_service; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Constructor for TasksList Request Handler |
|
| 51 | - * |
|
| 52 | - * @param ModuleService $module_service |
|
| 53 | - * @param TaskScheduleService $taskschedules_service |
|
| 54 | - * @param DatatablesService $datatables_service |
|
| 55 | - */ |
|
| 56 | - public function __construct( |
|
| 57 | - ModuleService $module_service, |
|
| 58 | - TaskScheduleService $taskschedules_service, |
|
| 59 | - DatatablesService $datatables_service |
|
| 60 | - ) { |
|
| 61 | - $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
| 62 | - $this->taskschedules_service = $taskschedules_service; |
|
| 63 | - $this->datatables_service = $datatables_service; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * {@inheritDoc} |
|
| 68 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
| 69 | - */ |
|
| 70 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
| 71 | - { |
|
| 72 | - if ($this->module === null) { |
|
| 73 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $task_schedules = $this->taskschedules_service->all(true, true) |
|
| 77 | - ->map(function (TaskSchedule $value): array { |
|
| 78 | - |
|
| 79 | - $row = $value->toArray(); |
|
| 80 | - $task = $this->taskschedules_service->findTask($row['task_id']); |
|
| 81 | - $row['task_name'] = $task !== null ? $task->name() : I18N::translate('Task not found'); |
|
| 82 | - return $row; |
|
| 83 | - }); |
|
| 84 | - |
|
| 85 | - $search_columns = ['task_name']; |
|
| 86 | - $sort_columns = ['task_name', 'enabled', 'last_run']; |
|
| 87 | - $module_name = $this->module->name(); |
|
| 88 | - |
|
| 89 | - $callback = function (array $row) use ($module_name): array { |
|
| 90 | - |
|
| 91 | - $row['frequency']->setLocale(I18N::locale()->code()); |
|
| 92 | - |
|
| 93 | - $task_options_params = [ |
|
| 94 | - 'task_sched_id' => $row['id'], |
|
| 95 | - 'task_sched_enabled' => $row['enabled'], |
|
| 96 | - 'task_edit_route' => route(TaskEditPage::class, ['task' => $row['id']]), |
|
| 97 | - 'task_status_route' => route(TaskStatusAction::class, [ |
|
| 98 | - 'task' => $row['id'], |
|
| 99 | - 'enable' => $row['enabled'] ? 0 : 1 |
|
| 100 | - ]) |
|
| 101 | - ]; |
|
| 102 | - |
|
| 103 | - $task_run_params = [ |
|
| 104 | - 'task_sched_id' => $row['id'], |
|
| 105 | - 'run_route' => route(TaskTrigger::class, [ |
|
| 106 | - 'task' => $row['task_id'], |
|
| 107 | - 'force' => $this->module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN') |
|
| 108 | - ]) |
|
| 109 | - ]; |
|
| 110 | - |
|
| 111 | - $datum = [ |
|
| 112 | - view($module_name . '::admin/tasks-table-options', $task_options_params), |
|
| 113 | - view($module_name . '::components/yes-no-icons', ['yes' => $row['enabled']]), |
|
| 114 | - '<span dir="auto">' . e($row['task_name']) . '</span>', |
|
| 115 | - $row['last_run']->unix() === 0 ? |
|
| 116 | - view('components/datetime', ['timestamp' => $row['last_run']]) : |
|
| 117 | - view('components/datetime-diff', ['timestamp' => $row['last_run']]), |
|
| 118 | - view($module_name . '::components/yes-no-icons', ['yes' => $row['last_result']]), |
|
| 119 | - '<span dir="auto">' . e($row['frequency']->cascade()->forHumans()) . '</span>', |
|
| 120 | - $row['nb_occurrences'] > 0 ? I18N::number($row['nb_occurrences']) : I18N::translate('Unlimited'), |
|
| 121 | - view($module_name . '::components/yes-no-icons', [ |
|
| 122 | - 'yes' => $row['is_running'], |
|
| 123 | - 'text_yes' => I18N::translate('Running'), |
|
| 124 | - 'text_no' => I18N::translate('Not running') |
|
| 125 | - ]), |
|
| 126 | - view($module_name . '::admin/tasks-table-run', $task_run_params) |
|
| 127 | - ]; |
|
| 128 | - |
|
| 129 | - return $datum; |
|
| 130 | - }; |
|
| 131 | - |
|
| 132 | - return $this->datatables_service->handleCollection( |
|
| 133 | - $request, |
|
| 134 | - $task_schedules, |
|
| 135 | - $search_columns, |
|
| 136 | - $sort_columns, |
|
| 137 | - $callback |
|
| 138 | - ); |
|
| 139 | - } |
|
| 34 | + /** |
|
| 35 | + * @var AdminTasksModule $module |
|
| 36 | + */ |
|
| 37 | + private $module; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var TaskScheduleService $taskschedules_service |
|
| 41 | + */ |
|
| 42 | + private $taskschedules_service; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @var DatatablesService $datatables_service |
|
| 46 | + */ |
|
| 47 | + private $datatables_service; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Constructor for TasksList Request Handler |
|
| 51 | + * |
|
| 52 | + * @param ModuleService $module_service |
|
| 53 | + * @param TaskScheduleService $taskschedules_service |
|
| 54 | + * @param DatatablesService $datatables_service |
|
| 55 | + */ |
|
| 56 | + public function __construct( |
|
| 57 | + ModuleService $module_service, |
|
| 58 | + TaskScheduleService $taskschedules_service, |
|
| 59 | + DatatablesService $datatables_service |
|
| 60 | + ) { |
|
| 61 | + $this->module = $module_service->findByInterface(AdminTasksModule::class)->first(); |
|
| 62 | + $this->taskschedules_service = $taskschedules_service; |
|
| 63 | + $this->datatables_service = $datatables_service; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * {@inheritDoc} |
|
| 68 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
| 69 | + */ |
|
| 70 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
| 71 | + { |
|
| 72 | + if ($this->module === null) { |
|
| 73 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $task_schedules = $this->taskschedules_service->all(true, true) |
|
| 77 | + ->map(function (TaskSchedule $value): array { |
|
| 78 | + |
|
| 79 | + $row = $value->toArray(); |
|
| 80 | + $task = $this->taskschedules_service->findTask($row['task_id']); |
|
| 81 | + $row['task_name'] = $task !== null ? $task->name() : I18N::translate('Task not found'); |
|
| 82 | + return $row; |
|
| 83 | + }); |
|
| 84 | + |
|
| 85 | + $search_columns = ['task_name']; |
|
| 86 | + $sort_columns = ['task_name', 'enabled', 'last_run']; |
|
| 87 | + $module_name = $this->module->name(); |
|
| 88 | + |
|
| 89 | + $callback = function (array $row) use ($module_name): array { |
|
| 90 | + |
|
| 91 | + $row['frequency']->setLocale(I18N::locale()->code()); |
|
| 92 | + |
|
| 93 | + $task_options_params = [ |
|
| 94 | + 'task_sched_id' => $row['id'], |
|
| 95 | + 'task_sched_enabled' => $row['enabled'], |
|
| 96 | + 'task_edit_route' => route(TaskEditPage::class, ['task' => $row['id']]), |
|
| 97 | + 'task_status_route' => route(TaskStatusAction::class, [ |
|
| 98 | + 'task' => $row['id'], |
|
| 99 | + 'enable' => $row['enabled'] ? 0 : 1 |
|
| 100 | + ]) |
|
| 101 | + ]; |
|
| 102 | + |
|
| 103 | + $task_run_params = [ |
|
| 104 | + 'task_sched_id' => $row['id'], |
|
| 105 | + 'run_route' => route(TaskTrigger::class, [ |
|
| 106 | + 'task' => $row['task_id'], |
|
| 107 | + 'force' => $this->module->getPreference('MAJ_AT_FORCE_EXEC_TOKEN') |
|
| 108 | + ]) |
|
| 109 | + ]; |
|
| 110 | + |
|
| 111 | + $datum = [ |
|
| 112 | + view($module_name . '::admin/tasks-table-options', $task_options_params), |
|
| 113 | + view($module_name . '::components/yes-no-icons', ['yes' => $row['enabled']]), |
|
| 114 | + '<span dir="auto">' . e($row['task_name']) . '</span>', |
|
| 115 | + $row['last_run']->unix() === 0 ? |
|
| 116 | + view('components/datetime', ['timestamp' => $row['last_run']]) : |
|
| 117 | + view('components/datetime-diff', ['timestamp' => $row['last_run']]), |
|
| 118 | + view($module_name . '::components/yes-no-icons', ['yes' => $row['last_result']]), |
|
| 119 | + '<span dir="auto">' . e($row['frequency']->cascade()->forHumans()) . '</span>', |
|
| 120 | + $row['nb_occurrences'] > 0 ? I18N::number($row['nb_occurrences']) : I18N::translate('Unlimited'), |
|
| 121 | + view($module_name . '::components/yes-no-icons', [ |
|
| 122 | + 'yes' => $row['is_running'], |
|
| 123 | + 'text_yes' => I18N::translate('Running'), |
|
| 124 | + 'text_no' => I18N::translate('Not running') |
|
| 125 | + ]), |
|
| 126 | + view($module_name . '::admin/tasks-table-run', $task_run_params) |
|
| 127 | + ]; |
|
| 128 | + |
|
| 129 | + return $datum; |
|
| 130 | + }; |
|
| 131 | + |
|
| 132 | + return $this->datatables_service->handleCollection( |
|
| 133 | + $request, |
|
| 134 | + $task_schedules, |
|
| 135 | + $search_columns, |
|
| 136 | + $sort_columns, |
|
| 137 | + $callback |
|
| 138 | + ); |
|
| 139 | + } |
|
| 140 | 140 | } |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | $task_schedules = $this->taskschedules_service->all(true, true) |
| 77 | - ->map(function (TaskSchedule $value): array { |
|
| 77 | + ->map(function(TaskSchedule $value): array { |
|
| 78 | 78 | |
| 79 | 79 | $row = $value->toArray(); |
| 80 | 80 | $task = $this->taskschedules_service->findTask($row['task_id']); |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | $sort_columns = ['task_name', 'enabled', 'last_run']; |
| 87 | 87 | $module_name = $this->module->name(); |
| 88 | 88 | |
| 89 | - $callback = function (array $row) use ($module_name): array { |
|
| 89 | + $callback = function(array $row) use ($module_name): array { |
|
| 90 | 90 | |
| 91 | 91 | $row['frequency']->setLocale(I18N::locale()->code()); |
| 92 | 92 | |
@@ -109,21 +109,20 @@ discard block |
||
| 109 | 109 | ]; |
| 110 | 110 | |
| 111 | 111 | $datum = [ |
| 112 | - view($module_name . '::admin/tasks-table-options', $task_options_params), |
|
| 113 | - view($module_name . '::components/yes-no-icons', ['yes' => $row['enabled']]), |
|
| 114 | - '<span dir="auto">' . e($row['task_name']) . '</span>', |
|
| 112 | + view($module_name.'::admin/tasks-table-options', $task_options_params), |
|
| 113 | + view($module_name.'::components/yes-no-icons', ['yes' => $row['enabled']]), |
|
| 114 | + '<span dir="auto">'.e($row['task_name']).'</span>', |
|
| 115 | 115 | $row['last_run']->unix() === 0 ? |
| 116 | - view('components/datetime', ['timestamp' => $row['last_run']]) : |
|
| 117 | - view('components/datetime-diff', ['timestamp' => $row['last_run']]), |
|
| 118 | - view($module_name . '::components/yes-no-icons', ['yes' => $row['last_result']]), |
|
| 119 | - '<span dir="auto">' . e($row['frequency']->cascade()->forHumans()) . '</span>', |
|
| 116 | + view('components/datetime', ['timestamp' => $row['last_run']]) : view('components/datetime-diff', ['timestamp' => $row['last_run']]), |
|
| 117 | + view($module_name.'::components/yes-no-icons', ['yes' => $row['last_result']]), |
|
| 118 | + '<span dir="auto">'.e($row['frequency']->cascade()->forHumans()).'</span>', |
|
| 120 | 119 | $row['nb_occurrences'] > 0 ? I18N::number($row['nb_occurrences']) : I18N::translate('Unlimited'), |
| 121 | - view($module_name . '::components/yes-no-icons', [ |
|
| 120 | + view($module_name.'::components/yes-no-icons', [ |
|
| 122 | 121 | 'yes' => $row['is_running'], |
| 123 | 122 | 'text_yes' => I18N::translate('Running'), |
| 124 | 123 | 'text_no' => I18N::translate('Not running') |
| 125 | 124 | ]), |
| 126 | - view($module_name . '::admin/tasks-table-run', $task_run_params) |
|
| 125 | + view($module_name.'::admin/tasks-table-run', $task_run_params) |
|
| 127 | 126 | ]; |
| 128 | 127 | |
| 129 | 128 | return $datum; |
@@ -25,25 +25,25 @@ |
||
| 25 | 25 | class Migration1 implements MigrationInterface |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * {@inheritDoc} |
|
| 30 | - * @see \Fisharebest\Webtrees\Schema\MigrationInterface::upgrade() |
|
| 31 | - */ |
|
| 32 | - public function upgrade(): void |
|
| 33 | - { |
|
| 34 | - // Clean up previous admin tasks table if it exists |
|
| 35 | - DB::schema()->dropIfExists('maj_admintasks'); |
|
| 36 | - |
|
| 37 | - DB::schema()->create('maj_admintasks', static function (Blueprint $table): void { |
|
| 38 | - |
|
| 39 | - $table->increments('majat_id'); |
|
| 40 | - $table->string('majat_task_id', 32)->unique(); |
|
| 41 | - $table->enum('majat_status', ['enabled', 'disabled'])->default('disabled'); |
|
| 42 | - $table->dateTime('majat_last_run')->default(Carbon::createFromTimestampUTC(0)); |
|
| 43 | - $table->boolean('majat_last_result')->default(true); |
|
| 44 | - $table->integer('majat_frequency')->default(10080); |
|
| 45 | - $table->smallInteger('majat_nb_occur')->default(0); |
|
| 46 | - $table->boolean('majat_running')->default(false); |
|
| 47 | - }); |
|
| 48 | - } |
|
| 28 | + /** |
|
| 29 | + * {@inheritDoc} |
|
| 30 | + * @see \Fisharebest\Webtrees\Schema\MigrationInterface::upgrade() |
|
| 31 | + */ |
|
| 32 | + public function upgrade(): void |
|
| 33 | + { |
|
| 34 | + // Clean up previous admin tasks table if it exists |
|
| 35 | + DB::schema()->dropIfExists('maj_admintasks'); |
|
| 36 | + |
|
| 37 | + DB::schema()->create('maj_admintasks', static function (Blueprint $table): void { |
|
| 38 | + |
|
| 39 | + $table->increments('majat_id'); |
|
| 40 | + $table->string('majat_task_id', 32)->unique(); |
|
| 41 | + $table->enum('majat_status', ['enabled', 'disabled'])->default('disabled'); |
|
| 42 | + $table->dateTime('majat_last_run')->default(Carbon::createFromTimestampUTC(0)); |
|
| 43 | + $table->boolean('majat_last_result')->default(true); |
|
| 44 | + $table->integer('majat_frequency')->default(10080); |
|
| 45 | + $table->smallInteger('majat_nb_occur')->default(0); |
|
| 46 | + $table->boolean('majat_running')->default(false); |
|
| 47 | + }); |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -34,7 +34,7 @@ |
||
| 34 | 34 | // Clean up previous admin tasks table if it exists |
| 35 | 35 | DB::schema()->dropIfExists('maj_admintasks'); |
| 36 | 36 | |
| 37 | - DB::schema()->create('maj_admintasks', static function (Blueprint $table): void { |
|
| 37 | + DB::schema()->create('maj_admintasks', static function(Blueprint $table): void { |
|
| 38 | 38 | |
| 39 | 39 | $table->increments('majat_id'); |
| 40 | 40 | $table->string('majat_task_id', 32)->unique(); |
@@ -33,253 +33,253 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class TaskScheduleService |
| 35 | 35 | { |
| 36 | - /** |
|
| 37 | - * Time-out after which the task will be considered not running any more. |
|
| 38 | - * In seconds, default 5 mins. |
|
| 39 | - * @var integer |
|
| 40 | - */ |
|
| 41 | - public const TASK_TIME_OUT = 600; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var Collection $available_tasks |
|
| 45 | - */ |
|
| 46 | - private $available_tasks; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Returns all Tasks schedules in database. |
|
| 50 | - * Stored records can be synchronised with the tasks actually available to the system. |
|
| 51 | - * |
|
| 52 | - * @param bool $sync_available Should tasks synchronised with available ones |
|
| 53 | - * @param bool $include_disabled Should disabled tasks be returned |
|
| 54 | - * @return Collection Collection of TaskSchedule |
|
| 55 | - */ |
|
| 56 | - public function all(bool $sync_available = false, bool $include_disabled = true): Collection |
|
| 57 | - { |
|
| 58 | - $tasks_schedules = DB::table('maj_admintasks') |
|
| 59 | - ->select() |
|
| 60 | - ->get() |
|
| 61 | - ->map(self::rowMapper()); |
|
| 62 | - |
|
| 63 | - if ($sync_available) { |
|
| 64 | - $available_tasks = clone $this->available(); |
|
| 65 | - foreach ($tasks_schedules as $task_schedule) { |
|
| 66 | - /** @var TaskSchedule $task_schedule */ |
|
| 67 | - if ($available_tasks->has($task_schedule->taskId())) { |
|
| 68 | - $available_tasks->forget($task_schedule->taskId()); |
|
| 69 | - } else { |
|
| 70 | - $this->delete($task_schedule); |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - foreach ($available_tasks as $task_name => $task) { |
|
| 75 | - /** @var TaskInterface $task */ |
|
| 76 | - $this->insertTask($task_name, $task->defaultFrequency()); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return $this->all(false, $include_disabled); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return $tasks_schedules; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Returns tasks exposed through modules implementing ModuleTasksProviderInterface. |
|
| 87 | - * |
|
| 88 | - * @return Collection |
|
| 89 | - */ |
|
| 90 | - public function available(): Collection |
|
| 91 | - { |
|
| 92 | - if ($this->available_tasks === null) { |
|
| 93 | - $tasks_providers = app(ModuleService::class)->findByInterface(ModuleTasksProviderInterface::class); |
|
| 94 | - |
|
| 95 | - $this->available_tasks = new Collection(); |
|
| 96 | - foreach ($tasks_providers as $task_provider) { |
|
| 97 | - $this->available_tasks = $this->available_tasks->merge($task_provider->listTasks()); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - return $this->available_tasks; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Find a task schedule by its ID. |
|
| 105 | - * |
|
| 106 | - * @param int $task_schedule_id |
|
| 107 | - * @return TaskSchedule|NULL |
|
| 108 | - */ |
|
| 109 | - public function find(int $task_schedule_id): ?TaskSchedule |
|
| 110 | - { |
|
| 111 | - return DB::table('maj_admintasks') |
|
| 112 | - ->select() |
|
| 113 | - ->where('majat_id', '=', $task_schedule_id) |
|
| 114 | - ->get() |
|
| 115 | - ->map(self::rowMapper()) |
|
| 116 | - ->first(); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Add a new task schedule with the specified task ID, and frequency if defined. |
|
| 121 | - * Uses default for other settings. |
|
| 122 | - * |
|
| 123 | - * @param string $task_id |
|
| 124 | - * @param int $frequency |
|
| 125 | - * @return bool |
|
| 126 | - */ |
|
| 127 | - public function insertTask(string $task_id, int $frequency = 0): bool |
|
| 128 | - { |
|
| 129 | - $values = ['majat_task_id' => $task_id]; |
|
| 130 | - if ($frequency > 0) { |
|
| 131 | - $values['majat_frequency'] = $frequency; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return DB::table('maj_admintasks') |
|
| 135 | - ->insert($values); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Update a task schedule. |
|
| 140 | - * Returns the number of tasks schedules updated. |
|
| 141 | - * |
|
| 142 | - * @param TaskSchedule $task_schedule |
|
| 143 | - * @return int |
|
| 144 | - */ |
|
| 145 | - public function update(TaskSchedule $task_schedule): int |
|
| 146 | - { |
|
| 147 | - return DB::table('maj_admintasks') |
|
| 148 | - ->where('majat_id', '=', $task_schedule->id()) |
|
| 149 | - ->update([ |
|
| 150 | - 'majat_status' => $task_schedule->isEnabled() ? 'enabled' : 'disabled', |
|
| 151 | - 'majat_last_run' => $task_schedule->lastRunTime(), |
|
| 152 | - 'majat_last_result' => $task_schedule->wasLastRunSuccess(), |
|
| 153 | - 'majat_frequency' => $task_schedule->frequency()->totalMinutes, |
|
| 154 | - 'majat_nb_occur' => $task_schedule->remainingOccurences(), |
|
| 155 | - 'majat_running' => $task_schedule->isRunning() |
|
| 156 | - ]); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Delete a task schedule. |
|
| 161 | - * |
|
| 162 | - * @param TaskSchedule $task_schedule |
|
| 163 | - * @return int |
|
| 164 | - */ |
|
| 165 | - public function delete(TaskSchedule $task_schedule): int |
|
| 166 | - { |
|
| 167 | - return DB::table('maj_admintasks') |
|
| 168 | - ->where('majat_id', '=', $task_schedule->id()) |
|
| 169 | - ->delete(); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Find a task by its name |
|
| 174 | - * |
|
| 175 | - * @param string $task_id |
|
| 176 | - * @return TaskInterface|NULL |
|
| 177 | - */ |
|
| 178 | - public function findTask(string $task_id): ?TaskInterface |
|
| 179 | - { |
|
| 180 | - if ($this->available()->has($task_id)) { |
|
| 181 | - return app($this->available()->get($task_id)); |
|
| 182 | - } |
|
| 183 | - return null; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Retrieve all tasks that are candidates to be run. |
|
| 188 | - * |
|
| 189 | - * @param bool $force Should the run be forced |
|
| 190 | - * @param string $task_id Specific task ID to be run |
|
| 191 | - * @return Collection |
|
| 192 | - */ |
|
| 193 | - public function findTasksToRun(bool $force, string $task_id = null): Collection |
|
| 194 | - { |
|
| 195 | - $query = DB::table('maj_admintasks') |
|
| 196 | - ->select() |
|
| 197 | - ->where('majat_status', '=', 'enabled') |
|
| 198 | - ->where(function (Builder $query): void { |
|
| 199 | - |
|
| 200 | - $query->where('majat_running', '=', 0) |
|
| 201 | - ->orWhere('majat_last_run', '<=', Carbon::now()->subSeconds(self::TASK_TIME_OUT)); |
|
| 202 | - }); |
|
| 203 | - |
|
| 204 | - if (!$force) { |
|
| 205 | - $query->where(function (Builder $query): void { |
|
| 206 | - |
|
| 207 | - $query->where('majat_running', '=', 0) |
|
| 208 | - ->orWhereRaw('DATE_ADD(majat_last_run, INTERVAL majat_frequency MINUTE) <= NOW()'); |
|
| 209 | - }); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - if ($task_id !== null) { |
|
| 213 | - $query->where('majat_task_id', '=', $task_id); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - return $query->get()->map(self::rowMapper()); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Run the task associated with the schedule. |
|
| 221 | - * The task will run if either forced to, or its next scheduled run time has been exceeded. |
|
| 222 | - * The last run time is recorded only if the task is successful. |
|
| 223 | - * |
|
| 224 | - * @param TaskSchedule $task_schedule |
|
| 225 | - * @param boolean $force |
|
| 226 | - */ |
|
| 227 | - public function run(TaskSchedule $task_schedule, $force = false): void |
|
| 228 | - { |
|
| 229 | - /** @var TaskSchedule $task_schedule */ |
|
| 230 | - $task_schedule = DB::table('maj_admintasks') |
|
| 231 | - ->select() |
|
| 232 | - ->where('majat_id', '=', $task_schedule->id()) |
|
| 233 | - ->lockForUpdate() |
|
| 234 | - ->get() |
|
| 235 | - ->map(self::rowMapper()) |
|
| 236 | - ->first(); |
|
| 237 | - |
|
| 238 | - if ( |
|
| 239 | - !$task_schedule->isRunning() && |
|
| 240 | - ($force || $task_schedule->lastRunTime()->add($task_schedule->frequency())->lessThan(Carbon::now())) |
|
| 241 | - ) { |
|
| 242 | - $task_schedule->setLastResult(false); |
|
| 243 | - |
|
| 244 | - $task = $this->findTask($task_schedule->taskId()); |
|
| 245 | - if ($task !== null) { |
|
| 246 | - $task_schedule->startRunning(); |
|
| 247 | - $this->update($task_schedule); |
|
| 248 | - |
|
| 249 | - try { |
|
| 250 | - $task_schedule->setLastResult($task->run($task_schedule)); |
|
| 251 | - } catch (Exception $ex) { |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - if ($task_schedule->wasLastRunSuccess()) { |
|
| 255 | - $task_schedule->setLastRunTime(Carbon::now()); |
|
| 256 | - $task_schedule->decrementRemainingOccurences(); |
|
| 257 | - } |
|
| 258 | - $task_schedule->stopRunning(); |
|
| 259 | - } |
|
| 260 | - $this->update($task_schedule); |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Mapper to return a TaskSchedule object from an object. |
|
| 266 | - * |
|
| 267 | - * @return Closure |
|
| 268 | - */ |
|
| 269 | - public static function rowMapper(): Closure |
|
| 270 | - { |
|
| 271 | - return static function (stdClass $row): TaskSchedule { |
|
| 272 | - |
|
| 273 | - return new TaskSchedule( |
|
| 274 | - (int) $row->majat_id, |
|
| 275 | - $row->majat_task_id, |
|
| 276 | - $row->majat_status === 'enabled', |
|
| 277 | - Carbon::parse($row->majat_last_run), |
|
| 278 | - (bool) $row->majat_last_result, |
|
| 279 | - CarbonInterval::minutes($row->majat_frequency), |
|
| 280 | - (int) $row->majat_nb_occur, |
|
| 281 | - (bool) $row->majat_running |
|
| 282 | - ); |
|
| 283 | - }; |
|
| 284 | - } |
|
| 36 | + /** |
|
| 37 | + * Time-out after which the task will be considered not running any more. |
|
| 38 | + * In seconds, default 5 mins. |
|
| 39 | + * @var integer |
|
| 40 | + */ |
|
| 41 | + public const TASK_TIME_OUT = 600; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var Collection $available_tasks |
|
| 45 | + */ |
|
| 46 | + private $available_tasks; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Returns all Tasks schedules in database. |
|
| 50 | + * Stored records can be synchronised with the tasks actually available to the system. |
|
| 51 | + * |
|
| 52 | + * @param bool $sync_available Should tasks synchronised with available ones |
|
| 53 | + * @param bool $include_disabled Should disabled tasks be returned |
|
| 54 | + * @return Collection Collection of TaskSchedule |
|
| 55 | + */ |
|
| 56 | + public function all(bool $sync_available = false, bool $include_disabled = true): Collection |
|
| 57 | + { |
|
| 58 | + $tasks_schedules = DB::table('maj_admintasks') |
|
| 59 | + ->select() |
|
| 60 | + ->get() |
|
| 61 | + ->map(self::rowMapper()); |
|
| 62 | + |
|
| 63 | + if ($sync_available) { |
|
| 64 | + $available_tasks = clone $this->available(); |
|
| 65 | + foreach ($tasks_schedules as $task_schedule) { |
|
| 66 | + /** @var TaskSchedule $task_schedule */ |
|
| 67 | + if ($available_tasks->has($task_schedule->taskId())) { |
|
| 68 | + $available_tasks->forget($task_schedule->taskId()); |
|
| 69 | + } else { |
|
| 70 | + $this->delete($task_schedule); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + foreach ($available_tasks as $task_name => $task) { |
|
| 75 | + /** @var TaskInterface $task */ |
|
| 76 | + $this->insertTask($task_name, $task->defaultFrequency()); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return $this->all(false, $include_disabled); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return $tasks_schedules; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Returns tasks exposed through modules implementing ModuleTasksProviderInterface. |
|
| 87 | + * |
|
| 88 | + * @return Collection |
|
| 89 | + */ |
|
| 90 | + public function available(): Collection |
|
| 91 | + { |
|
| 92 | + if ($this->available_tasks === null) { |
|
| 93 | + $tasks_providers = app(ModuleService::class)->findByInterface(ModuleTasksProviderInterface::class); |
|
| 94 | + |
|
| 95 | + $this->available_tasks = new Collection(); |
|
| 96 | + foreach ($tasks_providers as $task_provider) { |
|
| 97 | + $this->available_tasks = $this->available_tasks->merge($task_provider->listTasks()); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + return $this->available_tasks; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Find a task schedule by its ID. |
|
| 105 | + * |
|
| 106 | + * @param int $task_schedule_id |
|
| 107 | + * @return TaskSchedule|NULL |
|
| 108 | + */ |
|
| 109 | + public function find(int $task_schedule_id): ?TaskSchedule |
|
| 110 | + { |
|
| 111 | + return DB::table('maj_admintasks') |
|
| 112 | + ->select() |
|
| 113 | + ->where('majat_id', '=', $task_schedule_id) |
|
| 114 | + ->get() |
|
| 115 | + ->map(self::rowMapper()) |
|
| 116 | + ->first(); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Add a new task schedule with the specified task ID, and frequency if defined. |
|
| 121 | + * Uses default for other settings. |
|
| 122 | + * |
|
| 123 | + * @param string $task_id |
|
| 124 | + * @param int $frequency |
|
| 125 | + * @return bool |
|
| 126 | + */ |
|
| 127 | + public function insertTask(string $task_id, int $frequency = 0): bool |
|
| 128 | + { |
|
| 129 | + $values = ['majat_task_id' => $task_id]; |
|
| 130 | + if ($frequency > 0) { |
|
| 131 | + $values['majat_frequency'] = $frequency; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return DB::table('maj_admintasks') |
|
| 135 | + ->insert($values); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Update a task schedule. |
|
| 140 | + * Returns the number of tasks schedules updated. |
|
| 141 | + * |
|
| 142 | + * @param TaskSchedule $task_schedule |
|
| 143 | + * @return int |
|
| 144 | + */ |
|
| 145 | + public function update(TaskSchedule $task_schedule): int |
|
| 146 | + { |
|
| 147 | + return DB::table('maj_admintasks') |
|
| 148 | + ->where('majat_id', '=', $task_schedule->id()) |
|
| 149 | + ->update([ |
|
| 150 | + 'majat_status' => $task_schedule->isEnabled() ? 'enabled' : 'disabled', |
|
| 151 | + 'majat_last_run' => $task_schedule->lastRunTime(), |
|
| 152 | + 'majat_last_result' => $task_schedule->wasLastRunSuccess(), |
|
| 153 | + 'majat_frequency' => $task_schedule->frequency()->totalMinutes, |
|
| 154 | + 'majat_nb_occur' => $task_schedule->remainingOccurences(), |
|
| 155 | + 'majat_running' => $task_schedule->isRunning() |
|
| 156 | + ]); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Delete a task schedule. |
|
| 161 | + * |
|
| 162 | + * @param TaskSchedule $task_schedule |
|
| 163 | + * @return int |
|
| 164 | + */ |
|
| 165 | + public function delete(TaskSchedule $task_schedule): int |
|
| 166 | + { |
|
| 167 | + return DB::table('maj_admintasks') |
|
| 168 | + ->where('majat_id', '=', $task_schedule->id()) |
|
| 169 | + ->delete(); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Find a task by its name |
|
| 174 | + * |
|
| 175 | + * @param string $task_id |
|
| 176 | + * @return TaskInterface|NULL |
|
| 177 | + */ |
|
| 178 | + public function findTask(string $task_id): ?TaskInterface |
|
| 179 | + { |
|
| 180 | + if ($this->available()->has($task_id)) { |
|
| 181 | + return app($this->available()->get($task_id)); |
|
| 182 | + } |
|
| 183 | + return null; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Retrieve all tasks that are candidates to be run. |
|
| 188 | + * |
|
| 189 | + * @param bool $force Should the run be forced |
|
| 190 | + * @param string $task_id Specific task ID to be run |
|
| 191 | + * @return Collection |
|
| 192 | + */ |
|
| 193 | + public function findTasksToRun(bool $force, string $task_id = null): Collection |
|
| 194 | + { |
|
| 195 | + $query = DB::table('maj_admintasks') |
|
| 196 | + ->select() |
|
| 197 | + ->where('majat_status', '=', 'enabled') |
|
| 198 | + ->where(function (Builder $query): void { |
|
| 199 | + |
|
| 200 | + $query->where('majat_running', '=', 0) |
|
| 201 | + ->orWhere('majat_last_run', '<=', Carbon::now()->subSeconds(self::TASK_TIME_OUT)); |
|
| 202 | + }); |
|
| 203 | + |
|
| 204 | + if (!$force) { |
|
| 205 | + $query->where(function (Builder $query): void { |
|
| 206 | + |
|
| 207 | + $query->where('majat_running', '=', 0) |
|
| 208 | + ->orWhereRaw('DATE_ADD(majat_last_run, INTERVAL majat_frequency MINUTE) <= NOW()'); |
|
| 209 | + }); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + if ($task_id !== null) { |
|
| 213 | + $query->where('majat_task_id', '=', $task_id); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + return $query->get()->map(self::rowMapper()); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Run the task associated with the schedule. |
|
| 221 | + * The task will run if either forced to, or its next scheduled run time has been exceeded. |
|
| 222 | + * The last run time is recorded only if the task is successful. |
|
| 223 | + * |
|
| 224 | + * @param TaskSchedule $task_schedule |
|
| 225 | + * @param boolean $force |
|
| 226 | + */ |
|
| 227 | + public function run(TaskSchedule $task_schedule, $force = false): void |
|
| 228 | + { |
|
| 229 | + /** @var TaskSchedule $task_schedule */ |
|
| 230 | + $task_schedule = DB::table('maj_admintasks') |
|
| 231 | + ->select() |
|
| 232 | + ->where('majat_id', '=', $task_schedule->id()) |
|
| 233 | + ->lockForUpdate() |
|
| 234 | + ->get() |
|
| 235 | + ->map(self::rowMapper()) |
|
| 236 | + ->first(); |
|
| 237 | + |
|
| 238 | + if ( |
|
| 239 | + !$task_schedule->isRunning() && |
|
| 240 | + ($force || $task_schedule->lastRunTime()->add($task_schedule->frequency())->lessThan(Carbon::now())) |
|
| 241 | + ) { |
|
| 242 | + $task_schedule->setLastResult(false); |
|
| 243 | + |
|
| 244 | + $task = $this->findTask($task_schedule->taskId()); |
|
| 245 | + if ($task !== null) { |
|
| 246 | + $task_schedule->startRunning(); |
|
| 247 | + $this->update($task_schedule); |
|
| 248 | + |
|
| 249 | + try { |
|
| 250 | + $task_schedule->setLastResult($task->run($task_schedule)); |
|
| 251 | + } catch (Exception $ex) { |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + if ($task_schedule->wasLastRunSuccess()) { |
|
| 255 | + $task_schedule->setLastRunTime(Carbon::now()); |
|
| 256 | + $task_schedule->decrementRemainingOccurences(); |
|
| 257 | + } |
|
| 258 | + $task_schedule->stopRunning(); |
|
| 259 | + } |
|
| 260 | + $this->update($task_schedule); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Mapper to return a TaskSchedule object from an object. |
|
| 266 | + * |
|
| 267 | + * @return Closure |
|
| 268 | + */ |
|
| 269 | + public static function rowMapper(): Closure |
|
| 270 | + { |
|
| 271 | + return static function (stdClass $row): TaskSchedule { |
|
| 272 | + |
|
| 273 | + return new TaskSchedule( |
|
| 274 | + (int) $row->majat_id, |
|
| 275 | + $row->majat_task_id, |
|
| 276 | + $row->majat_status === 'enabled', |
|
| 277 | + Carbon::parse($row->majat_last_run), |
|
| 278 | + (bool) $row->majat_last_result, |
|
| 279 | + CarbonInterval::minutes($row->majat_frequency), |
|
| 280 | + (int) $row->majat_nb_occur, |
|
| 281 | + (bool) $row->majat_running |
|
| 282 | + ); |
|
| 283 | + }; |
|
| 284 | + } |
|
| 285 | 285 | } |
@@ -195,14 +195,14 @@ discard block |
||
| 195 | 195 | $query = DB::table('maj_admintasks') |
| 196 | 196 | ->select() |
| 197 | 197 | ->where('majat_status', '=', 'enabled') |
| 198 | - ->where(function (Builder $query): void { |
|
| 198 | + ->where(function(Builder $query): void { |
|
| 199 | 199 | |
| 200 | 200 | $query->where('majat_running', '=', 0) |
| 201 | 201 | ->orWhere('majat_last_run', '<=', Carbon::now()->subSeconds(self::TASK_TIME_OUT)); |
| 202 | 202 | }); |
| 203 | 203 | |
| 204 | 204 | if (!$force) { |
| 205 | - $query->where(function (Builder $query): void { |
|
| 205 | + $query->where(function(Builder $query): void { |
|
| 206 | 206 | |
| 207 | 207 | $query->where('majat_running', '=', 0) |
| 208 | 208 | ->orWhereRaw('DATE_ADD(majat_last_run, INTERVAL majat_frequency MINUTE) <= NOW()'); |
@@ -268,17 +268,17 @@ discard block |
||
| 268 | 268 | */ |
| 269 | 269 | public static function rowMapper(): Closure |
| 270 | 270 | { |
| 271 | - return static function (stdClass $row): TaskSchedule { |
|
| 271 | + return static function(stdClass $row): TaskSchedule { |
|
| 272 | 272 | |
| 273 | 273 | return new TaskSchedule( |
| 274 | - (int) $row->majat_id, |
|
| 274 | + (int)$row->majat_id, |
|
| 275 | 275 | $row->majat_task_id, |
| 276 | 276 | $row->majat_status === 'enabled', |
| 277 | 277 | Carbon::parse($row->majat_last_run), |
| 278 | - (bool) $row->majat_last_result, |
|
| 278 | + (bool)$row->majat_last_result, |
|
| 279 | 279 | CarbonInterval::minutes($row->majat_frequency), |
| 280 | - (int) $row->majat_nb_occur, |
|
| 281 | - (bool) $row->majat_running |
|
| 280 | + (int)$row->majat_nb_occur, |
|
| 281 | + (bool)$row->majat_running |
|
| 282 | 282 | ); |
| 283 | 283 | }; |
| 284 | 284 | } |
@@ -27,101 +27,101 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class HealthCheckService |
| 29 | 29 | { |
| 30 | - /** |
|
| 31 | - * Returns a query collating all gedcom records, for use in other queries |
|
| 32 | - * |
|
| 33 | - * @param Tree $tree |
|
| 34 | - * @return Builder |
|
| 35 | - */ |
|
| 36 | - private function allGedcomRecords(Tree $tree): Builder |
|
| 37 | - { |
|
| 38 | - return DB::table('individuals') |
|
| 39 | - ->select(DB::raw("'indi' AS ged_type"), 'i_id AS ged_id')->where('i_file', '=', $tree->id()) |
|
| 40 | - ->unionAll(DB::table('families') |
|
| 41 | - ->select(DB::raw("'fam' AS ged_type"), 'f_id AS ged_id')->where('f_file', '=', $tree->id())) |
|
| 42 | - ->unionAll(DB::table('sources') |
|
| 43 | - ->select(DB::raw("'sour' AS ged_type"), 's_id AS ged_id')->where('s_file', '=', $tree->id())) |
|
| 44 | - ->unionAll(DB::table('media') |
|
| 45 | - ->select(DB::raw("'media' AS ged_type"), 'm_id AS ged_id')->where('m_file', '=', $tree->id())) |
|
| 46 | - ->unionAll(DB::table('other') |
|
| 47 | - ->select(DB::raw('LOWER(o_type) AS ged_type'), 'o_id AS ged_id')->where('o_file', '=', $tree->id())); |
|
| 48 | - } |
|
| 30 | + /** |
|
| 31 | + * Returns a query collating all gedcom records, for use in other queries |
|
| 32 | + * |
|
| 33 | + * @param Tree $tree |
|
| 34 | + * @return Builder |
|
| 35 | + */ |
|
| 36 | + private function allGedcomRecords(Tree $tree): Builder |
|
| 37 | + { |
|
| 38 | + return DB::table('individuals') |
|
| 39 | + ->select(DB::raw("'indi' AS ged_type"), 'i_id AS ged_id')->where('i_file', '=', $tree->id()) |
|
| 40 | + ->unionAll(DB::table('families') |
|
| 41 | + ->select(DB::raw("'fam' AS ged_type"), 'f_id AS ged_id')->where('f_file', '=', $tree->id())) |
|
| 42 | + ->unionAll(DB::table('sources') |
|
| 43 | + ->select(DB::raw("'sour' AS ged_type"), 's_id AS ged_id')->where('s_file', '=', $tree->id())) |
|
| 44 | + ->unionAll(DB::table('media') |
|
| 45 | + ->select(DB::raw("'media' AS ged_type"), 'm_id AS ged_id')->where('m_file', '=', $tree->id())) |
|
| 46 | + ->unionAll(DB::table('other') |
|
| 47 | + ->select(DB::raw('LOWER(o_type) AS ged_type'), 'o_id AS ged_id')->where('o_file', '=', $tree->id())); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Returns the count of gedcom records by type in a Tree, as a keyed Collection. |
|
| 52 | - * |
|
| 53 | - * Collection output: |
|
| 54 | - * - Key : gedcom record type |
|
| 55 | - * - Value: count of records |
|
| 56 | - * |
|
| 57 | - * @param Tree $tree |
|
| 58 | - * @return Collection |
|
| 59 | - */ |
|
| 60 | - public function countByRecordType(Tree $tree): Collection |
|
| 61 | - { |
|
| 62 | - return DB::query() |
|
| 63 | - ->fromSub($this->allGedcomRecords($tree), 'gedrecords') |
|
| 64 | - ->select('ged_type', new Expression('COUNT(ged_id) AS total')) |
|
| 65 | - ->groupBy('ged_type') |
|
| 66 | - ->pluck('total', 'ged_type'); |
|
| 67 | - } |
|
| 50 | + /** |
|
| 51 | + * Returns the count of gedcom records by type in a Tree, as a keyed Collection. |
|
| 52 | + * |
|
| 53 | + * Collection output: |
|
| 54 | + * - Key : gedcom record type |
|
| 55 | + * - Value: count of records |
|
| 56 | + * |
|
| 57 | + * @param Tree $tree |
|
| 58 | + * @return Collection |
|
| 59 | + */ |
|
| 60 | + public function countByRecordType(Tree $tree): Collection |
|
| 61 | + { |
|
| 62 | + return DB::query() |
|
| 63 | + ->fromSub($this->allGedcomRecords($tree), 'gedrecords') |
|
| 64 | + ->select('ged_type', new Expression('COUNT(ged_id) AS total')) |
|
| 65 | + ->groupBy('ged_type') |
|
| 66 | + ->pluck('total', 'ged_type'); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Returns the count of gedcom records changes by type in a Tree across a number of days, as a keyed Collection. |
|
| 71 | - * |
|
| 72 | - * Collection output: |
|
| 73 | - * - Key : gedcom record type |
|
| 74 | - * - Value: count of changes |
|
| 75 | - * |
|
| 76 | - * @param Tree $tree |
|
| 77 | - * @return Collection |
|
| 78 | - */ |
|
| 79 | - public function changesByRecordType(Tree $tree, int $nb_days): Collection |
|
| 80 | - { |
|
| 81 | - return DB::table('change') |
|
| 82 | - ->joinSub($this->allGedcomRecords($tree), 'gedrecords', function (JoinClause $join) use ($tree): void { |
|
| 69 | + /** |
|
| 70 | + * Returns the count of gedcom records changes by type in a Tree across a number of days, as a keyed Collection. |
|
| 71 | + * |
|
| 72 | + * Collection output: |
|
| 73 | + * - Key : gedcom record type |
|
| 74 | + * - Value: count of changes |
|
| 75 | + * |
|
| 76 | + * @param Tree $tree |
|
| 77 | + * @return Collection |
|
| 78 | + */ |
|
| 79 | + public function changesByRecordType(Tree $tree, int $nb_days): Collection |
|
| 80 | + { |
|
| 81 | + return DB::table('change') |
|
| 82 | + ->joinSub($this->allGedcomRecords($tree), 'gedrecords', function (JoinClause $join) use ($tree): void { |
|
| 83 | 83 | |
| 84 | - $join->on('change.xref', '=', 'gedrecords.ged_id') |
|
| 85 | - ->where('change.gedcom_id', '=', $tree->id()); |
|
| 86 | - }) |
|
| 87 | - ->select('ged_type AS type', new Expression('COUNT(change_id) AS count')) |
|
| 88 | - ->where('change.status', '', 'accepted') |
|
| 89 | - ->where('change.change_time', '>=', Carbon::now()->subDays($nb_days)) |
|
| 90 | - ->groupBy('ged_type') |
|
| 91 | - ->pluck('total', 'ged_type'); |
|
| 92 | - } |
|
| 84 | + $join->on('change.xref', '=', 'gedrecords.ged_id') |
|
| 85 | + ->where('change.gedcom_id', '=', $tree->id()); |
|
| 86 | + }) |
|
| 87 | + ->select('ged_type AS type', new Expression('COUNT(change_id) AS count')) |
|
| 88 | + ->where('change.status', '', 'accepted') |
|
| 89 | + ->where('change.change_time', '>=', Carbon::now()->subDays($nb_days)) |
|
| 90 | + ->groupBy('ged_type') |
|
| 91 | + ->pluck('total', 'ged_type'); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Return the error logs associated with a tree across a number of days, grouped by error message, as a Collection. |
|
| 96 | - * |
|
| 97 | - * Collection output: |
|
| 98 | - * - Value: stdClass object |
|
| 99 | - * - log message: Error log message |
|
| 100 | - * - type: 'site' if no associated Tree, the Tree ID otherwise |
|
| 101 | - * - nblogs: The number of occurence of the same error message |
|
| 102 | - * - lastoccurred: Date/time of the last occurence of the error message |
|
| 103 | - * |
|
| 104 | - * @param Tree $tree |
|
| 105 | - * @param int $nb_days |
|
| 106 | - * @return Collection |
|
| 107 | - */ |
|
| 108 | - public function errorLogs(Tree $tree, int $nb_days): Collection |
|
| 109 | - { |
|
| 110 | - return DB::table('log') |
|
| 111 | - ->select( |
|
| 112 | - 'log_message', |
|
| 113 | - new Expression("IFNULL(gedcom_id, 'site') as type"), |
|
| 114 | - new Expression('COUNT(log_id) AS nblogs'), |
|
| 115 | - new Expression('MAX(log_time) AS lastoccurred') |
|
| 116 | - ) |
|
| 117 | - ->where('log_type', '=', 'error') |
|
| 118 | - ->where(function (Builder $query) use ($tree): void { |
|
| 119 | - $query->where('gedcom_id', '=', $tree->id()) |
|
| 120 | - ->orWhereNull('gedcom_id'); |
|
| 121 | - }) |
|
| 122 | - ->where('log_time', '>=', Carbon::now()->subDays($nb_days)) |
|
| 123 | - ->groupBy('log_message', 'gedcom_id') |
|
| 124 | - ->orderByDesc('lastoccurred') |
|
| 125 | - ->get(); |
|
| 126 | - } |
|
| 94 | + /** |
|
| 95 | + * Return the error logs associated with a tree across a number of days, grouped by error message, as a Collection. |
|
| 96 | + * |
|
| 97 | + * Collection output: |
|
| 98 | + * - Value: stdClass object |
|
| 99 | + * - log message: Error log message |
|
| 100 | + * - type: 'site' if no associated Tree, the Tree ID otherwise |
|
| 101 | + * - nblogs: The number of occurence of the same error message |
|
| 102 | + * - lastoccurred: Date/time of the last occurence of the error message |
|
| 103 | + * |
|
| 104 | + * @param Tree $tree |
|
| 105 | + * @param int $nb_days |
|
| 106 | + * @return Collection |
|
| 107 | + */ |
|
| 108 | + public function errorLogs(Tree $tree, int $nb_days): Collection |
|
| 109 | + { |
|
| 110 | + return DB::table('log') |
|
| 111 | + ->select( |
|
| 112 | + 'log_message', |
|
| 113 | + new Expression("IFNULL(gedcom_id, 'site') as type"), |
|
| 114 | + new Expression('COUNT(log_id) AS nblogs'), |
|
| 115 | + new Expression('MAX(log_time) AS lastoccurred') |
|
| 116 | + ) |
|
| 117 | + ->where('log_type', '=', 'error') |
|
| 118 | + ->where(function (Builder $query) use ($tree): void { |
|
| 119 | + $query->where('gedcom_id', '=', $tree->id()) |
|
| 120 | + ->orWhereNull('gedcom_id'); |
|
| 121 | + }) |
|
| 122 | + ->where('log_time', '>=', Carbon::now()->subDays($nb_days)) |
|
| 123 | + ->groupBy('log_message', 'gedcom_id') |
|
| 124 | + ->orderByDesc('lastoccurred') |
|
| 125 | + ->get(); |
|
| 126 | + } |
|
| 127 | 127 | } |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | public function changesByRecordType(Tree $tree, int $nb_days): Collection |
| 80 | 80 | { |
| 81 | 81 | return DB::table('change') |
| 82 | - ->joinSub($this->allGedcomRecords($tree), 'gedrecords', function (JoinClause $join) use ($tree): void { |
|
| 82 | + ->joinSub($this->allGedcomRecords($tree), 'gedrecords', function(JoinClause $join) use ($tree): void { |
|
| 83 | 83 | |
| 84 | 84 | $join->on('change.xref', '=', 'gedrecords.ged_id') |
| 85 | 85 | ->where('change.gedcom_id', '=', $tree->id()); |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | new Expression('MAX(log_time) AS lastoccurred') |
| 116 | 116 | ) |
| 117 | 117 | ->where('log_type', '=', 'error') |
| 118 | - ->where(function (Builder $query) use ($tree): void { |
|
| 118 | + ->where(function(Builder $query) use ($tree): void { |
|
| 119 | 119 | $query->where('gedcom_id', '=', $tree->id()) |
| 120 | 120 | ->orWhereNull('gedcom_id'); |
| 121 | 121 | }) |
@@ -22,12 +22,12 @@ |
||
| 22 | 22 | class Migration1 implements MigrationInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * {@inheritDoc} |
|
| 27 | - * @see \Fisharebest\Webtrees\Schema\MigrationInterface::upgrade() |
|
| 28 | - */ |
|
| 29 | - public function upgrade(): void |
|
| 30 | - { |
|
| 31 | - // These migrations have been merged into migration 2. |
|
| 32 | - } |
|
| 25 | + /** |
|
| 26 | + * {@inheritDoc} |
|
| 27 | + * @see \Fisharebest\Webtrees\Schema\MigrationInterface::upgrade() |
|
| 28 | + */ |
|
| 29 | + public function upgrade(): void |
|
| 30 | + { |
|
| 31 | + // These migrations have been merged into migration 2. |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -44,167 +44,167 @@ |
||
| 44 | 44 | */ |
| 45 | 45 | class SosaModule extends AbstractModuleMaj implements ModuleGlobalInterface, ModuleMenuInterface |
| 46 | 46 | { |
| 47 | - use ModuleGlobalTrait; |
|
| 48 | - use ModuleMenuTrait; |
|
| 47 | + use ModuleGlobalTrait; |
|
| 48 | + use ModuleMenuTrait; |
|
| 49 | 49 | |
| 50 | 50 | // How to update the database schema for this module |
| 51 | 51 | |
| 52 | 52 | |
| 53 | - private const SCHEMA_TARGET_VERSION = 3; |
|
| 54 | - private const SCHEMA_SETTING_NAME = 'MAJ_SOSA_SCHEMA_VERSION'; |
|
| 55 | - private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__ . '\Schema'; |
|
| 53 | + private const SCHEMA_TARGET_VERSION = 3; |
|
| 54 | + private const SCHEMA_SETTING_NAME = 'MAJ_SOSA_SCHEMA_VERSION'; |
|
| 55 | + private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__ . '\Schema'; |
|
| 56 | 56 | /** |
| 57 | - * {@inheritDoc} |
|
| 58 | - * @see \Fisharebest\Webtrees\Module\AbstractModule::title() |
|
| 59 | - */ |
|
| 60 | - public function title(): string |
|
| 61 | - { |
|
| 62 | - return /* I18N: Name of the “Sosa” module */ I18N::translate('Sosa'); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * {@inheritDoc} |
|
| 67 | - * @see \Fisharebest\Webtrees\Module\AbstractModule::description() |
|
| 68 | - */ |
|
| 69 | - public function description(): string |
|
| 70 | - { |
|
| 71 | - //phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 72 | - return /* I18N: Description of the “Sosa” module */ I18N::translate('Calculate and display Sosa ancestors of the root person.'); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * {@inheritDoc} |
|
| 77 | - * @see \MyArtJaub\Webtrees\Module\AbstractModuleMaj::boot() |
|
| 78 | - */ |
|
| 79 | - public function boot(): void |
|
| 80 | - { |
|
| 81 | - parent::boot(); |
|
| 82 | - app(MigrationService::class)->updateSchema( |
|
| 83 | - self::SCHEMA_MIGRATION_PREFIX, |
|
| 84 | - self::SCHEMA_SETTING_NAME, |
|
| 85 | - self::SCHEMA_TARGET_VERSION |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * {@inheritDoc} |
|
| 91 | - * @see \MyArtJaub\Webtrees\Module\AbstractModuleMaj::loadRoutes() |
|
| 92 | - */ |
|
| 93 | - public function loadRoutes(Map $router): void |
|
| 94 | - { |
|
| 95 | - $router->attach('', '', static function (Map $router): void { |
|
| 96 | - |
|
| 97 | - $router->attach('', '/module-maj/sosa', static function (Map $router): void { |
|
| 98 | - |
|
| 99 | - $router->attach('', '/list', static function (Map $router): void { |
|
| 100 | - |
|
| 101 | - |
|
| 102 | - $router->get(AncestorsList::class, '/ancestors/{tree}{/gen}', AncestorsList::class); |
|
| 103 | - $router->get(AncestorsListIndividual::class, '/ancestors/{tree}/{gen}/tab/individuals', AncestorsListIndividual::class); //phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 104 | - $router->get(AncestorsListFamily::class, '/ancestors/{tree}/{gen}/tab/families', AncestorsListFamily::class); //phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 105 | - $router->get(MissingAncestorsList::class, '/missing/{tree}{/gen}', MissingAncestorsList::class); |
|
| 106 | - }); |
|
| 107 | - $router->get(SosaStatistics::class, '/statistics/{tree}', SosaStatistics::class); |
|
| 108 | - $router->attach('', '/config/{tree}', static function (Map $router): void { |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - $router->get(SosaConfig::class, '', SosaConfig::class); |
|
| 112 | - $router->post(SosaConfigAction::class, '', SosaConfigAction::class); |
|
| 113 | - $router->get(SosaComputeModal::class, '/compute/{xref}', SosaComputeModal::class); |
|
| 114 | - $router->post(SosaComputeAction::class, '/compute', SosaComputeAction::class); |
|
| 115 | - }); |
|
| 116 | - }); |
|
| 117 | - }); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * {@inheritDoc} |
|
| 122 | - * @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleVersion() |
|
| 123 | - */ |
|
| 124 | - public function customModuleVersion(): string |
|
| 125 | - { |
|
| 126 | - return '2.0.7-v.1'; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * {@inheritDoc} |
|
| 131 | - * @see \Fisharebest\Webtrees\Module\ModuleMenuInterface::defaultMenuOrder() |
|
| 132 | - */ |
|
| 133 | - public function defaultMenuOrder(): int |
|
| 134 | - { |
|
| 135 | - return 7; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * {@inhericDoc} |
|
| 140 | - * @see \Fisharebest\Webtrees\Module\ModuleMenuInterface::getMenu() |
|
| 141 | - */ |
|
| 142 | - public function getMenu(Tree $tree): ?Menu |
|
| 143 | - { |
|
| 144 | - $menu = new Menu(I18N::translate('Sosa Statistics')); |
|
| 145 | - $menu->setClass('menu-maj-sosa'); |
|
| 146 | - $menu->setSubmenus([ |
|
| 147 | - new Menu( |
|
| 148 | - I18N::translate('Sosa Ancestors'), |
|
| 149 | - route(AncestorsList::class, ['tree' => $tree->name()]), |
|
| 150 | - 'menu-maj-sosa-list', |
|
| 151 | - ['rel' => 'nofollow'] |
|
| 152 | - ), |
|
| 153 | - new Menu( |
|
| 154 | - I18N::translate('Missing Ancestors'), |
|
| 155 | - route(MissingAncestorsList::class, ['tree' => $tree->name()]), |
|
| 156 | - 'menu-maj-sosa-missing', |
|
| 157 | - ['rel' => 'nofollow'] |
|
| 158 | - ), |
|
| 159 | - new Menu( |
|
| 160 | - I18N::translate('Sosa Statistics'), |
|
| 161 | - route(SosaStatistics::class, ['tree' => $tree->name()]), |
|
| 162 | - 'menu-maj-sosa-stats' |
|
| 163 | - ) |
|
| 164 | - ]); |
|
| 165 | - |
|
| 166 | - if (Auth::check()) { |
|
| 167 | - $menu->addSubmenu(new Menu( |
|
| 168 | - I18N::translate('Sosa Configuration'), |
|
| 169 | - route(SosaConfig::class, ['tree' => $tree->name()]), |
|
| 170 | - 'menu-maj-sosa-config' |
|
| 171 | - )); |
|
| 172 | - |
|
| 173 | - /** @var ServerRequestInterface $request */ |
|
| 174 | - $request = app(ServerRequestInterface::class); |
|
| 175 | - $route = $request->getAttribute('route'); |
|
| 176 | - assert($route instanceof Route); |
|
| 177 | - |
|
| 178 | - $root_indi_id = $tree->getUserPreference(Auth::user(), 'MAJ_SOSA_ROOT_ID'); |
|
| 179 | - |
|
| 180 | - if ($route->name === IndividualPage::class && mb_strlen($root_indi_id) > 0) { |
|
| 181 | - $xref = $request->getAttribute('xref'); |
|
| 182 | - assert(is_string($xref)); |
|
| 183 | - |
|
| 184 | - $menu->addSubmenu(new Menu( |
|
| 185 | - I18N::translate('Complete Sosas'), |
|
| 186 | - '#', |
|
| 187 | - 'menu-maj-sosa-compute', |
|
| 188 | - [ |
|
| 189 | - 'rel' => 'nofollow', |
|
| 190 | - 'data-href' => route(SosaComputeModal::class, ['tree' => $tree->name(), 'xref' => $xref]), |
|
| 191 | - 'data-target' => '#wt-ajax-modal', |
|
| 192 | - 'data-toggle' => 'modal', |
|
| 193 | - 'data-backdrop' => 'static' |
|
| 194 | - ] |
|
| 195 | - )); |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return $menu; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * {@inheritDoc} |
|
| 204 | - * @see \Fisharebest\Webtrees\Module\ModuleGlobalInterface::headContent() |
|
| 205 | - */ |
|
| 206 | - public function headContent(): string |
|
| 207 | - { |
|
| 208 | - return '<link rel="stylesheet" href="' . e($this->moduleCssUrl()) . '">'; |
|
| 209 | - } |
|
| 57 | + * {@inheritDoc} |
|
| 58 | + * @see \Fisharebest\Webtrees\Module\AbstractModule::title() |
|
| 59 | + */ |
|
| 60 | + public function title(): string |
|
| 61 | + { |
|
| 62 | + return /* I18N: Name of the “Sosa” module */ I18N::translate('Sosa'); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * {@inheritDoc} |
|
| 67 | + * @see \Fisharebest\Webtrees\Module\AbstractModule::description() |
|
| 68 | + */ |
|
| 69 | + public function description(): string |
|
| 70 | + { |
|
| 71 | + //phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 72 | + return /* I18N: Description of the “Sosa” module */ I18N::translate('Calculate and display Sosa ancestors of the root person.'); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * {@inheritDoc} |
|
| 77 | + * @see \MyArtJaub\Webtrees\Module\AbstractModuleMaj::boot() |
|
| 78 | + */ |
|
| 79 | + public function boot(): void |
|
| 80 | + { |
|
| 81 | + parent::boot(); |
|
| 82 | + app(MigrationService::class)->updateSchema( |
|
| 83 | + self::SCHEMA_MIGRATION_PREFIX, |
|
| 84 | + self::SCHEMA_SETTING_NAME, |
|
| 85 | + self::SCHEMA_TARGET_VERSION |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * {@inheritDoc} |
|
| 91 | + * @see \MyArtJaub\Webtrees\Module\AbstractModuleMaj::loadRoutes() |
|
| 92 | + */ |
|
| 93 | + public function loadRoutes(Map $router): void |
|
| 94 | + { |
|
| 95 | + $router->attach('', '', static function (Map $router): void { |
|
| 96 | + |
|
| 97 | + $router->attach('', '/module-maj/sosa', static function (Map $router): void { |
|
| 98 | + |
|
| 99 | + $router->attach('', '/list', static function (Map $router): void { |
|
| 100 | + |
|
| 101 | + |
|
| 102 | + $router->get(AncestorsList::class, '/ancestors/{tree}{/gen}', AncestorsList::class); |
|
| 103 | + $router->get(AncestorsListIndividual::class, '/ancestors/{tree}/{gen}/tab/individuals', AncestorsListIndividual::class); //phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 104 | + $router->get(AncestorsListFamily::class, '/ancestors/{tree}/{gen}/tab/families', AncestorsListFamily::class); //phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 105 | + $router->get(MissingAncestorsList::class, '/missing/{tree}{/gen}', MissingAncestorsList::class); |
|
| 106 | + }); |
|
| 107 | + $router->get(SosaStatistics::class, '/statistics/{tree}', SosaStatistics::class); |
|
| 108 | + $router->attach('', '/config/{tree}', static function (Map $router): void { |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + $router->get(SosaConfig::class, '', SosaConfig::class); |
|
| 112 | + $router->post(SosaConfigAction::class, '', SosaConfigAction::class); |
|
| 113 | + $router->get(SosaComputeModal::class, '/compute/{xref}', SosaComputeModal::class); |
|
| 114 | + $router->post(SosaComputeAction::class, '/compute', SosaComputeAction::class); |
|
| 115 | + }); |
|
| 116 | + }); |
|
| 117 | + }); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * {@inheritDoc} |
|
| 122 | + * @see \Fisharebest\Webtrees\Module\ModuleCustomInterface::customModuleVersion() |
|
| 123 | + */ |
|
| 124 | + public function customModuleVersion(): string |
|
| 125 | + { |
|
| 126 | + return '2.0.7-v.1'; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * {@inheritDoc} |
|
| 131 | + * @see \Fisharebest\Webtrees\Module\ModuleMenuInterface::defaultMenuOrder() |
|
| 132 | + */ |
|
| 133 | + public function defaultMenuOrder(): int |
|
| 134 | + { |
|
| 135 | + return 7; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * {@inhericDoc} |
|
| 140 | + * @see \Fisharebest\Webtrees\Module\ModuleMenuInterface::getMenu() |
|
| 141 | + */ |
|
| 142 | + public function getMenu(Tree $tree): ?Menu |
|
| 143 | + { |
|
| 144 | + $menu = new Menu(I18N::translate('Sosa Statistics')); |
|
| 145 | + $menu->setClass('menu-maj-sosa'); |
|
| 146 | + $menu->setSubmenus([ |
|
| 147 | + new Menu( |
|
| 148 | + I18N::translate('Sosa Ancestors'), |
|
| 149 | + route(AncestorsList::class, ['tree' => $tree->name()]), |
|
| 150 | + 'menu-maj-sosa-list', |
|
| 151 | + ['rel' => 'nofollow'] |
|
| 152 | + ), |
|
| 153 | + new Menu( |
|
| 154 | + I18N::translate('Missing Ancestors'), |
|
| 155 | + route(MissingAncestorsList::class, ['tree' => $tree->name()]), |
|
| 156 | + 'menu-maj-sosa-missing', |
|
| 157 | + ['rel' => 'nofollow'] |
|
| 158 | + ), |
|
| 159 | + new Menu( |
|
| 160 | + I18N::translate('Sosa Statistics'), |
|
| 161 | + route(SosaStatistics::class, ['tree' => $tree->name()]), |
|
| 162 | + 'menu-maj-sosa-stats' |
|
| 163 | + ) |
|
| 164 | + ]); |
|
| 165 | + |
|
| 166 | + if (Auth::check()) { |
|
| 167 | + $menu->addSubmenu(new Menu( |
|
| 168 | + I18N::translate('Sosa Configuration'), |
|
| 169 | + route(SosaConfig::class, ['tree' => $tree->name()]), |
|
| 170 | + 'menu-maj-sosa-config' |
|
| 171 | + )); |
|
| 172 | + |
|
| 173 | + /** @var ServerRequestInterface $request */ |
|
| 174 | + $request = app(ServerRequestInterface::class); |
|
| 175 | + $route = $request->getAttribute('route'); |
|
| 176 | + assert($route instanceof Route); |
|
| 177 | + |
|
| 178 | + $root_indi_id = $tree->getUserPreference(Auth::user(), 'MAJ_SOSA_ROOT_ID'); |
|
| 179 | + |
|
| 180 | + if ($route->name === IndividualPage::class && mb_strlen($root_indi_id) > 0) { |
|
| 181 | + $xref = $request->getAttribute('xref'); |
|
| 182 | + assert(is_string($xref)); |
|
| 183 | + |
|
| 184 | + $menu->addSubmenu(new Menu( |
|
| 185 | + I18N::translate('Complete Sosas'), |
|
| 186 | + '#', |
|
| 187 | + 'menu-maj-sosa-compute', |
|
| 188 | + [ |
|
| 189 | + 'rel' => 'nofollow', |
|
| 190 | + 'data-href' => route(SosaComputeModal::class, ['tree' => $tree->name(), 'xref' => $xref]), |
|
| 191 | + 'data-target' => '#wt-ajax-modal', |
|
| 192 | + 'data-toggle' => 'modal', |
|
| 193 | + 'data-backdrop' => 'static' |
|
| 194 | + ] |
|
| 195 | + )); |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return $menu; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * {@inheritDoc} |
|
| 204 | + * @see \Fisharebest\Webtrees\Module\ModuleGlobalInterface::headContent() |
|
| 205 | + */ |
|
| 206 | + public function headContent(): string |
|
| 207 | + { |
|
| 208 | + return '<link rel="stylesheet" href="' . e($this->moduleCssUrl()) . '">'; |
|
| 209 | + } |
|
| 210 | 210 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | |
| 53 | 53 | private const SCHEMA_TARGET_VERSION = 3; |
| 54 | 54 | private const SCHEMA_SETTING_NAME = 'MAJ_SOSA_SCHEMA_VERSION'; |
| 55 | - private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__ . '\Schema'; |
|
| 55 | + private const SCHEMA_MIGRATION_PREFIX = __NAMESPACE__.'\Schema'; |
|
| 56 | 56 | /** |
| 57 | 57 | * {@inheritDoc} |
| 58 | 58 | * @see \Fisharebest\Webtrees\Module\AbstractModule::title() |
@@ -92,20 +92,20 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | public function loadRoutes(Map $router): void |
| 94 | 94 | { |
| 95 | - $router->attach('', '', static function (Map $router): void { |
|
| 95 | + $router->attach('', '', static function(Map $router): void { |
|
| 96 | 96 | |
| 97 | - $router->attach('', '/module-maj/sosa', static function (Map $router): void { |
|
| 97 | + $router->attach('', '/module-maj/sosa', static function(Map $router): void { |
|
| 98 | 98 | |
| 99 | - $router->attach('', '/list', static function (Map $router): void { |
|
| 99 | + $router->attach('', '/list', static function(Map $router): void { |
|
| 100 | 100 | |
| 101 | 101 | |
| 102 | 102 | $router->get(AncestorsList::class, '/ancestors/{tree}{/gen}', AncestorsList::class); |
| 103 | - $router->get(AncestorsListIndividual::class, '/ancestors/{tree}/{gen}/tab/individuals', AncestorsListIndividual::class); //phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 104 | - $router->get(AncestorsListFamily::class, '/ancestors/{tree}/{gen}/tab/families', AncestorsListFamily::class); //phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 103 | + $router->get(AncestorsListIndividual::class, '/ancestors/{tree}/{gen}/tab/individuals', AncestorsListIndividual::class); //phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 104 | + $router->get(AncestorsListFamily::class, '/ancestors/{tree}/{gen}/tab/families', AncestorsListFamily::class); //phpcs:ignore Generic.Files.LineLength.TooLong |
|
| 105 | 105 | $router->get(MissingAncestorsList::class, '/missing/{tree}{/gen}', MissingAncestorsList::class); |
| 106 | 106 | }); |
| 107 | 107 | $router->get(SosaStatistics::class, '/statistics/{tree}', SosaStatistics::class); |
| 108 | - $router->attach('', '/config/{tree}', static function (Map $router): void { |
|
| 108 | + $router->attach('', '/config/{tree}', static function(Map $router): void { |
|
| 109 | 109 | |
| 110 | 110 | |
| 111 | 111 | $router->get(SosaConfig::class, '', SosaConfig::class); |
@@ -205,6 +205,6 @@ discard block |
||
| 205 | 205 | */ |
| 206 | 206 | public function headContent(): string |
| 207 | 207 | { |
| 208 | - return '<link rel="stylesheet" href="' . e($this->moduleCssUrl()) . '">'; |
|
| 208 | + return '<link rel="stylesheet" href="'.e($this->moduleCssUrl()).'">'; |
|
| 209 | 209 | } |
| 210 | 210 | } |
@@ -30,50 +30,50 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class SosaConfigAction implements RequestHandlerInterface |
| 32 | 32 | { |
| 33 | - /** |
|
| 34 | - * @var UserService $user_service |
|
| 35 | - */ |
|
| 36 | - private $user_service; |
|
| 33 | + /** |
|
| 34 | + * @var UserService $user_service |
|
| 35 | + */ |
|
| 36 | + private $user_service; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Constructor for SosaConfigAction Request Handler |
|
| 40 | - * |
|
| 41 | - * @param UserService $user_service |
|
| 42 | - */ |
|
| 43 | - public function __construct(UserService $user_service) |
|
| 44 | - { |
|
| 45 | - $this->user_service = $user_service; |
|
| 46 | - } |
|
| 38 | + /** |
|
| 39 | + * Constructor for SosaConfigAction Request Handler |
|
| 40 | + * |
|
| 41 | + * @param UserService $user_service |
|
| 42 | + */ |
|
| 43 | + public function __construct(UserService $user_service) |
|
| 44 | + { |
|
| 45 | + $this->user_service = $user_service; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * {@inheritDoc} |
|
| 50 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
| 51 | - */ |
|
| 52 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
| 53 | - { |
|
| 54 | - $tree = $request->getAttribute('tree'); |
|
| 55 | - assert($tree instanceof Tree); |
|
| 48 | + /** |
|
| 49 | + * {@inheritDoc} |
|
| 50 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
| 51 | + */ |
|
| 52 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
| 53 | + { |
|
| 54 | + $tree = $request->getAttribute('tree'); |
|
| 55 | + assert($tree instanceof Tree); |
|
| 56 | 56 | |
| 57 | - $params = $request->getParsedBody(); |
|
| 58 | - assert(is_array($params)); |
|
| 57 | + $params = $request->getParsedBody(); |
|
| 58 | + assert(is_array($params)); |
|
| 59 | 59 | |
| 60 | - $user_id = (int) $params['sosa-userid']; |
|
| 61 | - $root_id = $params['sosa-rootid'] ?? ''; |
|
| 60 | + $user_id = (int) $params['sosa-userid']; |
|
| 61 | + $root_id = $params['sosa-rootid'] ?? ''; |
|
| 62 | 62 | |
| 63 | - if (Auth::id() == $user_id || ($user_id == -1 && Auth::isManager($tree))) { |
|
| 64 | - $user = $user_id == -1 ? new DefaultUser() : $this->user_service->find($user_id); |
|
| 65 | - if ($user !== null && ($root_indi = Registry::individualFactory()->make($root_id, $tree)) !== null) { |
|
| 66 | - $tree->setUserPreference($user, 'MAJ_SOSA_ROOT_ID', $root_indi->xref()); |
|
| 67 | - FlashMessages::addMessage(I18N::translate('The root individual has been updated.')); |
|
| 68 | - return redirect(route(SosaConfig::class, [ |
|
| 69 | - 'tree' => $tree->name(), |
|
| 70 | - 'compute' => 'yes', |
|
| 71 | - 'user_id' => $user_id |
|
| 72 | - ])); |
|
| 73 | - } |
|
| 74 | - } |
|
| 63 | + if (Auth::id() == $user_id || ($user_id == -1 && Auth::isManager($tree))) { |
|
| 64 | + $user = $user_id == -1 ? new DefaultUser() : $this->user_service->find($user_id); |
|
| 65 | + if ($user !== null && ($root_indi = Registry::individualFactory()->make($root_id, $tree)) !== null) { |
|
| 66 | + $tree->setUserPreference($user, 'MAJ_SOSA_ROOT_ID', $root_indi->xref()); |
|
| 67 | + FlashMessages::addMessage(I18N::translate('The root individual has been updated.')); |
|
| 68 | + return redirect(route(SosaConfig::class, [ |
|
| 69 | + 'tree' => $tree->name(), |
|
| 70 | + 'compute' => 'yes', |
|
| 71 | + 'user_id' => $user_id |
|
| 72 | + ])); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - FlashMessages::addMessage(I18N::translate('The root individual could not be updated.'), 'danger'); |
|
| 77 | - return redirect(route(SosaConfig::class, ['tree' => $tree->name()])); |
|
| 78 | - } |
|
| 76 | + FlashMessages::addMessage(I18N::translate('The root individual could not be updated.'), 'danger'); |
|
| 77 | + return redirect(route(SosaConfig::class, ['tree' => $tree->name()])); |
|
| 78 | + } |
|
| 79 | 79 | } |
@@ -57,7 +57,7 @@ |
||
| 57 | 57 | $params = $request->getParsedBody(); |
| 58 | 58 | assert(is_array($params)); |
| 59 | 59 | |
| 60 | - $user_id = (int) $params['sosa-userid']; |
|
| 60 | + $user_id = (int)$params['sosa-userid']; |
|
| 61 | 61 | $root_id = $params['sosa-rootid'] ?? ''; |
| 62 | 62 | |
| 63 | 63 | if (Auth::id() == $user_id || ($user_id == -1 && Auth::isManager($tree))) { |
@@ -35,73 +35,73 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | class AncestorsListFamily implements RequestHandlerInterface |
| 37 | 37 | { |
| 38 | - use ViewResponseTrait; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var SosaModule $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') ?? $request->getQueryParams()['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 $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') ?? $request->getQueryParams()['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') ?? $request->getQueryParams()['gen'] ?? 0); |
|
| 81 | + $current_gen = (int)($request->getAttribute('gen') ?? $request->getQueryParams()['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, |