Files
breadcrumb-the-shire/pages/admin/scheduled-jobs/edit($id).php

93 lines
3.8 KiB
PHP

<?php
use MintyPHP\Buffer;
use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Router;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;
$session = app(SessionStoreInterface::class)->all();
Guard::requireLogin();
Guard::requireAbility(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_ADMIN_JOBS_VIEW);
$request = requestInput();
$scheduledJobService = app(\MintyPHP\Service\Scheduler\ScheduledJobService::class);
$jobId = (int) ($id ?? 0);
$editTarget = "admin/scheduled-jobs/edit/$jobId";
$job = $jobId > 0 ? $scheduledJobService->find($jobId) : null;
if (!$job) {
Flash::error(t('Scheduled job not found'), 'admin/scheduled-jobs', 'scheduled_job_not_found');
Router::redirect('admin/scheduled-jobs');
}
$isRegistered = $scheduledJobService->isRegistered((string) ($job['job_key'] ?? ''));
$currentUserId = (int) ($session['user']['id'] ?? 0);
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
$canManage = $authorizationService->authorize(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_ADMIN_JOBS_MANAGE, [
'actor_user_id' => $currentUserId,
])->isAllowed();
$canRunNow = $authorizationService->authorize(\MintyPHP\Service\Access\OperationsAuthorizationPolicy::ABILITY_ADMIN_JOBS_RUN_NOW, [
'actor_user_id' => $currentUserId,
])->isAllowed();
$form = [
'enabled' => (int) ($job['enabled'] ?? 0),
'timezone' => (string) ($job['timezone'] ?? ''),
'schedule_type' => (string) ($job['schedule_type'] ?? 'daily'),
'schedule_interval' => (int) ($job['schedule_interval'] ?? 1),
'schedule_time' => (string) ($job['schedule_time'] ?? ''),
'schedule_weekdays' => $scheduledJobService->weekdaysFromCsv((string) ($job['schedule_weekdays_csv'] ?? '')),
'catchup_once' => (int) ($job['catchup_once'] ?? 1),
];
$errorBag = formErrors();
if (!$isRegistered) {
$canManage = false;
$canRunNow = false;
$errorBag->addGlobal(t('This scheduled job is no longer registered and cannot be edited. It has been superseded by a newer version.'));
}
if ($request->isMethod('POST')) {
if (!$canManage) {
Guard::deny();
}
if (!actionRequireCsrf($editTarget, flashOnFailure: false, redirectOnFailure: false)) {
$errorBag->addGlobal(t('Form expired, please try again'));
}
if (!$errorBag->hasAny()) {
$saved = $scheduledJobService->updateFromAdmin($jobId, $request->bodyAll());
if (!($saved['ok'] ?? false)) {
$errorBag->merge((array) ($saved['errors'] ?? [t('Scheduled job could not be saved')]));
$form = array_merge($form, (array) ($saved['form'] ?? []));
} else {
Flash::success(t('Scheduled job saved'), $editTarget, 'scheduled_job_saved');
$job = $saved['job'] ?? $job;
$form = [
'enabled' => (int) ($job['enabled'] ?? 0),
'timezone' => (string) ($job['timezone'] ?? ''),
'schedule_type' => (string) ($job['schedule_type'] ?? 'daily'),
'schedule_interval' => (int) ($job['schedule_interval'] ?? 1),
'schedule_time' => (string) ($job['schedule_time'] ?? ''),
'schedule_weekdays' => $scheduledJobService->weekdaysFromCsv((string) ($job['schedule_weekdays_csv'] ?? '')),
'catchup_once' => (int) ($job['catchup_once'] ?? 1),
];
}
}
}
$scheduleSummary = $scheduledJobService->scheduleSummary($job ?? []);
$validationSummaryErrors = $errorBag->toArray();
$errors = $errorBag->toFlatList();
$titleText = $canManage ? t('Edit scheduled job') : t('View scheduled job');
Buffer::set('title', $titleText);
$breadcrumbs = [
['label' => t('Home'), 'path' => 'admin'],
['label' => t('Scheduled jobs'), 'path' => 'admin/scheduled-jobs'],
['label' => $titleText],
];
Buffer::set('grid_lang', json_encode(gridLang(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));