Skip to content

FOUR-15872 Implement task screen endpoint #6913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ProcessMaker/Http/Controllers/Api/V1_1/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Http\Resources\V1_1\TaskScreen;

class TaskController extends Controller
{
Expand All @@ -30,4 +31,18 @@ public function show(ProcessRequestToken $task)
{
return $task;
}

public function showScreen($taskId)
{
$task = ProcessRequestToken::select('id', 'process_request_id', 'element_id', 'process_id')
->findOrFail($taskId);
$response = new TaskScreen($task);
$response = response($response->toArray(request())['screen'], 200);
$now = time();
// screen cache time
$cacheTime = config('screen_task_cache_time', 86400);
$response->headers->set('Cache-Control', 'max-age=' . $cacheTime . ', must-revalidate, public');
$response->headers->set('Expires', gmdate('D, d M Y H:i:s', $now + $cacheTime) . ' GMT');
return $response;
}
}
59 changes: 59 additions & 0 deletions ProcessMaker/Http/Resources/V1_1/TaskScreen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace ProcessMaker\Http\Resources\V1_1;

use ProcessMaker\Http\Resources\ApiResource;
use ProcessMaker\Http\Resources\ScreenVersion as ScreenVersionResource;
use ProcessMaker\ProcessTranslations\ProcessTranslation;
use ProcessMaker\Traits\TaskScreenResourceTrait;

class TaskScreen extends ApiResource
{
use TaskScreenResourceTrait;

/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return $this->includeScreen($request);
}

private function includeScreen($request)
{
$array = ['screen' => null];

$screen = $this->getScreenVersion();

if ($screen) {
if ($screen->type === 'ADVANCED') {
$array['screen'] = $screen;
} else {
$resource = new ScreenVersionResource($screen);
$array['screen'] = $resource->toArray($request);
}
} else {
$array['screen'] = null;
}

if ($array['screen']) {
// Apply translations to screen
$processTranslation = new ProcessTranslation($this->process);
$array['screen']['config'] = $processTranslation->applyTranslations($array['screen']);
$array['screen']['config'] = $this->removeInspectorMetadata($array['screen']['config']);

// Apply translations to nested screens
if (array_key_exists('nested', $array['screen'])) {
foreach ($array['screen']['nested'] as &$nestedScreen) {
$nestedScreen['config'] = $processTranslation->applyTranslations($nestedScreen);
$nestedScreen['config'] = $this->removeInspectorMetadata($nestedScreen['config']);
}
}
}

return $array;
}
}
11 changes: 10 additions & 1 deletion ProcessMaker/Traits/TaskResourceIncludes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

trait TaskResourceIncludes
{
use TaskScreenResourceTrait;

private function includeData()
{
return ['data' => $this->getData()];
Expand All @@ -29,7 +31,7 @@ private function includeRequestor()

private function includeProcessRequest()
{
return ['process_request' => new Users($this->processRequest)];
return ['process_request' => $this->processRequest->attributesToArray()];
}

private function includeDraft()
Expand Down Expand Up @@ -72,11 +74,13 @@ private function includeScreen($request)
// Apply translations to screen
$processTranslation = new ProcessTranslation($this->process);
$array['screen']['config'] = $processTranslation->applyTranslations($array['screen']);
$array['screen']['config'] = $this->removeInspectorMetadata($array['screen']['config']);

// Apply translations to nested screens
if (array_key_exists('nested', $array['screen'])) {
foreach ($array['screen']['nested'] as &$nestedScreen) {
$nestedScreen['config'] = $processTranslation->applyTranslations($nestedScreen);
$nestedScreen['config'] = $this->removeInspectorMetadata($nestedScreen['config']);
}
}
}
Expand Down Expand Up @@ -125,6 +129,11 @@ private function includeInterstitial()
$translatedConf = $processTranslation->applyTranslations($interstitial['interstitial_screen']);
$interstitial['interstitial_screen']['config'] = $translatedConf;

// Remove inspector metadata
$interstitial['interstitial_screen']['config'] = $this->removeInspectorMetadata(
$interstitial['interstitial_screen']['config']
);

return [
'allow_interstitial' => $interstitial['allow_interstitial'],
'interstitial_screen' => $interstitial['interstitial_screen'],
Expand Down
45 changes: 45 additions & 0 deletions ProcessMaker/Traits/TaskScreenResourceTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace ProcessMaker\Traits;

trait TaskScreenResourceTrait
{

/**
* Removes the inspector metadata from the screen configuration
*
* @param array $config
* @return array
*/
private function removeInspectorMetadata(array $config)
{
foreach ($config as $i => $page) {
$config[$i]['items'] = $this->removeInspectorMetadataItems($page['items']);
}
return $config;
}

/**
* Removes the inspector metadata from the screen configuration items
*
* @param array $items
* @return array
*/
private function removeInspectorMetadataItems(array $items)
{
foreach ($items as $i => $item) {
if (isset($item['inspector'])) {
unset($item['inspector']);
}
if (isset($item['component']) && $item['component'] === 'FormMultiColumn') {
foreach ($item['items'] as $c => $col) {
$item['items'][$c] = $this->removeInspectorMetadataItems($col);
}
} elseif (isset($item['items']) && is_array($item['items'])) {
$item['items'] = $this->removeInspectorMetadataItems($item['items']);
}
$items[$i] = $item;
}
return $items;
}
}
13 changes: 9 additions & 4 deletions routes/v1_1/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
->name('api.1.1.')
->group(function () {
// Tasks Endpoints
Route::name('tasks.')->group(function () {
Route::name('tasks.')->prefix('tasks')->group(function () {
// Route to list tasks
Route::get('tasks', [TaskController::class, 'index'])
Route::get('/', [TaskController::class, 'index'])
->name('index');

// Route to show a task
Route::get('tasks/{task}', [TaskController::class, 'show'])
->name('show');
Route::get('/{task}', [TaskController::class, 'show'])
->name('show')
->middleware(['bindings','can:view,task']);

// Route to show the screen of a task
Route::get('/{taskId}/screen', [TaskController::class, 'showScreen'])
->name('show.screen');
});
});
Loading
Loading