Skip to content
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

Allow disabling task drafts with env var setting #6820

Merged
merged 4 commits into from
May 4, 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
6 changes: 5 additions & 1 deletion ProcessMaker/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ProcessMaker\Managers\ScreenBuilderManager;
use ProcessMaker\Models\Comment;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\TaskDraft;
use ProcessMaker\Models\UserResourceView;
use ProcessMaker\Nayra\Contracts\Bpmn\ScriptTaskInterface;
use ProcessMaker\Package\SavedSearch\Http\Controllers\SavedSearchController;
Expand Down Expand Up @@ -63,7 +64,9 @@ public function index()
$defaultColumns = null;
}

return view('tasks.index', compact('title', 'userFilter', 'defaultColumns'));
$taskDraftsEnabled = TaskDraft::draftsEnabled();

return view('tasks.index', compact('title', 'userFilter', 'defaultColumns', 'taskDraftsEnabled'));
}

public function edit(ProcessRequestToken $task, string $preview = '')
Expand Down Expand Up @@ -155,6 +158,7 @@ public function edit(ProcessRequestToken $task, string $preview = '')
'dataActionsAddons' => $this->getPluginAddons('edit.dataActions', []),
'currentUser' => $currentUser,
'screenFields' => $screenFields,
'taskDraftsEnabled' => TaskDraft::draftsEnabled(),
]);
}
}
Expand Down
150 changes: 49 additions & 101 deletions ProcessMaker/Http/Resources/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,43 @@
namespace ProcessMaker\Http\Resources;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use ProcessMaker\Http\Resources\ApiCollection;
use ProcessMaker\Http\Resources\Screen as ScreenResource;
use ProcessMaker\Http\Resources\ScreenVersion as ScreenVersionResource;
use ProcessMaker\Http\Resources\Users;
use ProcessMaker\Managers\DataManager;
use ProcessMaker\Models\GroupMember;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\User;
use ProcessMaker\ProcessTranslations\Languages;
use ProcessMaker\ProcessTranslations\ProcessTranslation;
use StdClass;
use ProcessMaker\Traits\TaskResourceIncludes;

class Task extends ApiResource
{
use TaskResourceIncludes;

private $loadedData = null;

private static $screenFields = [];
/**
* A list of includes that have methods `include{Name}`
* in the TaskResourceIncludes trait.
*/
private $includeMethods = [
'data',
'user',
'requestor',
'processRequest',
'draft',
'component',
'screen',
'requestData',
'loopContext',
'definition',
'bpmnTagName',
'interstitial',
'userRequestPermission',
];

private $process = null;

/**
* Transform the resource into an array.
Expand All @@ -32,90 +49,41 @@ class Task extends ApiResource
*/
public function toArray($request)
{
$dataManager = new DataManager();
$array = parent::toArray($request);
$include = explode(',', $request->input('include', ''));
if (in_array('data', $include)) {
$array['data'] = $this->getData();
}
if (in_array('user', $include)) {
$array['user'] = new Users($this->user);
}
if (in_array('requestor', $include)) {
$array['requestor'] = new Users($this->processRequest->user);
}
if (in_array('processRequest', $include)) {
$array['process_request'] = new Users($this->processRequest);
}
if (in_array('draft', $include)) {
$array['draft'] = $this->draft;
}

$parentProcessRequest = $this->processRequest->parentRequest;
$array['can_view_parent_request'] = $parentProcessRequest && $request->user()->can('view', $parentProcessRequest);
$process = Process::findOrFail($this->processRequest->process_id);
$this->process = Process::findOrFail($this->processRequest->process_id);

if (in_array('component', $include)) {
$array['component'] = $this->getScreenVersion() ? $this->getScreenVersion()->parent->renderComponent() : null;
}
if (in_array('screen', $include)) {
$screen = $this->getScreenVersion();
if ($screen) {
if ($screen->type === 'ADVANCED') {
$array['screen'] = $screen;
} else {
$resource = new ScreenVersionResource($screen);
$array['screen'] = $resource->toArray($request);
}
foreach ($this->includeMethods as $key) {
if (!in_array($key, $include)) {
continue;
}
$method = 'include' . ucfirst($key);
$result = $this->$method($request);
if (count($result) === 1) {
$resultKey = array_key_first($result);
$array[$resultKey] = $result[$resultKey];
} else {
$array['screen'] = null;
$array = array_merge($array, $result);
}
}

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

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

if (in_array('requestData', $include)) {
$data = new StdClass();
if ($this->processRequest->data) {
$task = $this->resource->loadTokenInstance();
$data = $dataManager->getData($task);
}
$array['request_data'] = $data;
}
if (in_array('loopContext', $include)) {
$array['loop_context'] = $this->getLoopContext();
}
if (in_array('definition', $include)) {
$array['definition'] = $this->getDefinition();
}
if (in_array('bpmnTagName', $include)) {
$array['bpmn_tag_name'] = $this->getBpmnDefinition()->localName;
}
if (in_array('interstitial', $include)) {
$interstitial = $this->getInterstitial();
$array['allow_interstitial'] = $interstitial['allow_interstitial'];
return $array;
}

// Translate interstitials
$processTranslation = new ProcessTranslation($process);
$translatedConf = $processTranslation->applyTranslations($interstitial['interstitial_screen']);
$interstitial['interstitial_screen']['config'] = $translatedConf;
private function addCanViewParentRequest(&$array, $request)
{
$parentProcessRequest = $this->processRequest->parentRequest;
$array['can_view_parent_request'] =
$parentProcessRequest && $request->user()->can('view', $parentProcessRequest);
}

$array['interstitial_screen'] = $interstitial['interstitial_screen'];
}
if (in_array('userRequestPermission', $include)) {
$array['user_request_permission'] = $this->loadUserRequestPermission($this->processRequest, Auth::user(), []);
}
private function addAssignableUsers(&$array, $include)
{
/**
* @deprecated since 4.1 Use instead `/api/1.0/users`
*/
Expand All @@ -124,7 +92,6 @@ public function toArray($request)
$needToRecalculateAssignableUsers = !array_key_exists('assignable_users', $array)
|| count($array['assignable_users']) < 1;
if (in_array('assignableUsers', $include) && $needToRecalculateAssignableUsers) {
$nivel = array_search('request', array_column(debug_backtrace(), 'function'));
$definition = $this->getDefinition();
if (isset($definition['assignment']) && $definition['assignment'] == 'self_service') {
$users = [];
Expand All @@ -143,11 +110,9 @@ public function toArray($request)
$array['assignable_users'] = $users;
}
}

return $array;
}

private function loadUserRequestPermission(ProcessRequest $request, User $user = null, array $permissions)
private function loadUserRequestPermission(ProcessRequest $request, User $user, array $permissions)
{
$permissions[] = [
'process_request_id' => $request->id,
Expand All @@ -161,23 +126,6 @@ private function loadUserRequestPermission(ProcessRequest $request, User $user =
return $permissions;
}

private function addUser($data, $user)
{
if (!$user) {
return $data;
}

$userData = $user->attributesToArray();
unset($userData['remember_token']);

$data = array_merge($data, ['_user' => $userData]);
if (!empty($this->token_properties['data'])) {
$data = array_merge($data, $this->token_properties['data']);
}

return $data;
}

private function getAssignedUsers($users)
{
foreach ($users as $user) {
Expand Down
5 changes: 5 additions & 0 deletions ProcessMaker/Models/TaskDraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,9 @@ private static function deleteFiles($delete)
$existingMediaItem->delete();
}
}

public static function draftsEnabled()
{
return config('app.task_drafts_enabled');
}
}
135 changes: 135 additions & 0 deletions ProcessMaker/Traits/TaskResourceIncludes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace ProcessMaker\Traits;

use Illuminate\Support\Facades\Auth;
use ProcessMaker\Http\Resources\ScreenVersion as ScreenVersionResource;
use ProcessMaker\Http\Resources\Users;
use ProcessMaker\Managers\DataManager;
use ProcessMaker\Models\TaskDraft;
use ProcessMaker\ProcessTranslations\ProcessTranslation;
use StdClass;

trait TaskResourceIncludes
{
private function includeData()
{
return ['data' => $this->getData()];
}

private function includeUser()
{
return ['user' => new Users($this->user)];
}

private function includeRequestor()
{
return ['requestor' => new Users($this->processRequest->user)];
}

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

private function includeDraft()
{
$draft = $this->draft;
if ($draft && !TaskDraft::draftsEnabled()) {
// Drafts are used to get data from quick-fill to the screen,
// but drafts are disabled so we need to delete it now that
// it's been accessed.
$draft->delete();
}

return ['draft' => $draft];
}

private function includeComponent()
{
$component = $this->getScreenVersion() ? $this->getScreenVersion()->parent->renderComponent() : null;

return ['component' => $component];
}

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']);

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

return $array;
}

private function includeRequestData()
{
$dataManager = new DataManager();
$data = new StdClass();
if ($this->processRequest->data) {
$task = $this->resource->loadTokenInstance();
$data = $dataManager->getData($task);
}

return ['request_data' => $data];
}

private function includeLoopContext()
{
return ['loop_context' => $this->getLoopContext()];
}

private function includeDefinition()
{
return ['definition' => $this->getDefinition()];
}

private function includeBpmnTagName()
{
return ['bpmn_tag_name' => $this->getBpmnDefinition()->localName];
}

private function includeInterstitial()
{
$interstitial = $this->getInterstitial();

// Translate interstitials
$processTranslation = new ProcessTranslation($this->process);
$translatedConf = $processTranslation->applyTranslations($interstitial['interstitial_screen']);
$interstitial['interstitial_screen']['config'] = $translatedConf;

return [
'allow_interstitial' => $interstitial['allow_interstitial'],
'interstitial_screen' => $interstitial['interstitial_screen'],
];
}

private function includeUserRequestPermission()
{
$userRequestPermission = $this->loadUserRequestPermission($this->processRequest, Auth::user(), []);

return ['user_request_permission' => $userRequestPermission];
}
}
2 changes: 2 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,6 @@
'queue_imports' => env('QUEUE_IMPORTS', true),

'node_bin_path' => env('NODE_BIN_PATH', '/usr/bin/node'),

'task_drafts_enabled' => env('TASK_DRAFTS_ENABLED', true),
];
Loading
Loading