Added a page & table for viewing a list of installed tasks

This commit is contained in:
2023-05-31 16:00:22 +01:00
parent 2bed04cd18
commit e4db24ee8d
11 changed files with 440 additions and 4 deletions

View File

@@ -0,0 +1,110 @@
<?php
/*
* AVSDev UF Scheduler (https://avsdev.uk)
*
* @link https://git.avsdev.uk/avsdev/sprinkle-scheduler
* @license https://git.avsdev.uk/avsdev/sprinkle-scheduler/blob/master/LICENSE.md (LGPL-3.0 License)
*/
namespace UserFrosting\Sprinkle\Scheduler\Controller;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use UserFrosting\Sprinkle\Core\Controller\SimpleController;
use UserFrosting\Support\Exception\ForbiddenException;
use UserFrosting\Support\Exception\NotFoundException;
/**
* Controller class for task-related requests, (listing tasks).
*
* @author Craig Williams (https://avsdev.uk)
*/
class TaskController extends SimpleController
{
/**
* Returns a list of Tasks.
*
* Generates a list of tasks from the scehduler. No sorting or pagination functionality.
* This page requires authentication.
*
* Request type: GET
*
* @param Request $request
* @param Response $response
* @param array $args
*
* @throws ForbiddenException If user is not authorized to access page
*/
public function getList(Request $request, Response $response, $args)
{
// GET parameters
$params = $request->getQueryParams();
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
$authorizer = $this->ci->authorizer;
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
$currentUser = $this->ci->currentUser;
// Access-controlled page
if (!$authorizer->checkAccess($currentUser, 'uri_tasks')) {
throw new ForbiddenException();
}
/** @var \UserFrosting\Sprinkle\Scheduler\Scheduler $scheduler */
$scheduler = $this->ci->scheduler;
$tasks = $scheduler->getTasks();
$tasks = array_map(
function($val) {
$val['schedule_nice'] = CronTranslator::translate($val['schedule']);
return $val;
},
$tasks
);
// Be careful how you consume this data - it has not been escaped and contains untrusted user-supplied content.
// For example, if you plan to insert it into an HTML DOM, you must escape it on the client side (or use client-side templating).
return $response->withJson($tasks, 200, JSON_PRETTY_PRINT);
}
/**
* Renders the task listing page.
*
* This page renders a table of tasks, with dropdown menus for admin actions for each task.
* Actions typically include: run task
* This page requires authentication.
*
* Request type: GET
*
* @param Request $request
* @param Response $response
* @param array $args
*
* @throws ForbiddenException If user is not authorized to access page
*/
public function pageList(Request $request, Response $response, $args)
{
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
$authorizer = $this->ci->authorizer;
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
$currentUser = $this->ci->currentUser;
// Access-controlled page
if (!$authorizer->checkAccess($currentUser, 'uri_tasks')) {
throw new ForbiddenException();
}
/** @var \UserFrosting\Sprinkle\Scheduler\Scheduler $scheduler */
$scheduler = $this->ci->scheduler;
$tasks = $scheduler->getTasks();
return $this->ci->view->render($response, 'pages/tasks.html.twig', [
'tasks' => $tasks
]);
}
}