1919
2020trait TaskControllerIndexMethods
2121{
22+ private const SELF_SERVICE_STATUS = 'self service ' ;
23+
2224 private function indexBaseQuery ($ request )
2325 {
2426 // Parse the includes parameter
@@ -291,6 +293,19 @@ private function applyStatusFilter($query, $request)
291293 }
292294 }
293295
296+ private function requestHasStatusFilter ($ request ): bool
297+ {
298+ if ($ request ->filled ('status ' ) || $ request ->filled ('statusfilter ' )) {
299+ return true ;
300+ }
301+
302+ if ($ this ->advancedFilterHasStatus ($ request )) {
303+ return true ;
304+ }
305+
306+ return preg_match ('/(?:^|[\s(])status\s*(?:=|!=|<>)/i ' , $ request ->input ('pmql ' , '' )) === 1 ;
307+ }
308+
294309 private function applyPmql ($ query , $ request , $ user )
295310 {
296311 $ pmql = $ request ->input ('pmql ' , '' );
@@ -327,7 +342,7 @@ private function advancedFilterHasSelfServiceStatus($request): bool
327342 foreach ($ this ->getAdvancedFilterArray ($ request ) as $ filter ) {
328343 $ values = (array ) ($ filter ['value ' ] ?? []);
329344 foreach ($ values as $ v ) {
330- if (mb_strtolower ($ v ) === ' self service ' ) {
345+ if (mb_strtolower ($ v ) === self :: SELF_SERVICE_STATUS ) {
331346 return true ;
332347 }
333348 }
@@ -384,34 +399,62 @@ private function applyAdvancedFilter($query, $request)
384399 // If processesIManage is active, handle "Self Service" status filter specially
385400 if ($ isProcessManager && is_array ($ filterArray )) {
386401 $ hasSelfServiceFilter = false ;
387- $ filteredArray = [];
402+ $ nonStatusFilters = [];
403+ $ statusFilters = [];
388404
389405 foreach ($ filterArray as $ filter ) {
390- // Check if this is a "Self Service" status filter
391- if (isset ($ filter ['subject ' ]['type ' ]) &&
392- $ filter ['subject ' ]['type ' ] === 'Status ' &&
393- isset ($ filter ['value ' ]) &&
394- mb_strtolower ($ filter ['value ' ]) === 'self service ' ) {
395- $ hasSelfServiceFilter = true ;
396- // Don't add this filter to the array - we'll handle it manually
406+ $ isStatusFilter = isset ($ filter ['subject ' ]['type ' ]) && $ filter ['subject ' ]['type ' ] === 'Status ' ;
407+ if (!$ isStatusFilter ) {
408+ $ nonStatusFilters [] = $ filter ;
397409 continue ;
398410 }
399- $ filteredArray [] = $ filter ;
411+
412+ $ values = is_array ($ filter ['value ' ]) ? $ filter ['value ' ] : [$ filter ['value ' ]];
413+ $ hasSelfServiceValue = in_array (
414+ self ::SELF_SERVICE_STATUS ,
415+ array_map (fn ($ value ) => is_string ($ value ) ? mb_strtolower ($ value ) : $ value , $ values ),
416+ true
417+ );
418+
419+ if ($ hasSelfServiceValue ) {
420+ $ hasSelfServiceFilter = true ;
421+ $ values = array_values (array_filter ($ values , function ($ value ) {
422+ return !is_string ($ value ) || mb_strtolower ($ value ) !== self ::SELF_SERVICE_STATUS ;
423+ }));
424+
425+ if (empty ($ values )) {
426+ continue ;
427+ }
428+
429+ $ filter ['value ' ] = is_array ($ filter ['value ' ]) ? $ values : $ values [0 ];
430+ }
431+
432+ $ statusFilters [] = $ filter ;
400433 }
401434
402- // Apply the filtered advanced_filter (without Self Service)
403- if (!empty ($ filteredArray )) {
404- Filter::filter ($ query , $ filteredArray );
435+ if (!$ hasSelfServiceFilter ) {
436+ Filter::filter ($ query , $ filterArray );
437+
438+ return ;
405439 }
406440
407- // Manually apply the Self Service filter for process managers
408- if ($ hasSelfServiceFilter ) {
409- $ selfServiceTaskIds = ProcessRequestToken::select (['id ' ])
410- ->whereIn ('process_id ' , $ processManagerIds )
411- ->where ('is_self_service ' , 1 )
412- ->whereNull ('user_id ' )
413- ->where ('status ' , 'ACTIVE ' );
441+ if (!empty ($ nonStatusFilters )) {
442+ Filter::filter ($ query , $ nonStatusFilters );
443+ }
414444
445+ // Manually apply the Self Service filter for process managers
446+ $ selfServiceTaskIds = ProcessRequestToken::select (['id ' ])
447+ ->whereIn ('process_id ' , $ processManagerIds )
448+ ->where ('is_self_service ' , 1 )
449+ ->whereNull ('user_id ' )
450+ ->where ('status ' , 'ACTIVE ' );
451+
452+ if (!empty ($ statusFilters )) {
453+ $ query ->where (function ($ query ) use ($ statusFilters , $ selfServiceTaskIds ) {
454+ Filter::filter ($ query , $ statusFilters );
455+ $ query ->orWhereIn ('process_request_tokens.id ' , $ selfServiceTaskIds );
456+ });
457+ } else {
415458 $ query ->whereIn ('process_request_tokens.id ' , $ selfServiceTaskIds );
416459 }
417460 } else {
@@ -453,7 +496,7 @@ private function applyForCurrentUser($query, $user)
453496 });
454497 }
455498
456- public function applyProcessManager ($ query , $ user )
499+ public function applyProcessManager ($ query , $ user, $ request )
457500 {
458501 $ ids = Process::select (['id ' ])
459502 ->where (function ($ subQuery ) use ($ user ) {
@@ -472,17 +515,21 @@ public function applyProcessManager($query, $user)
472515 return ;
473516 }
474517
475- // Show tasks from processes the user manages that are ACTIVE
518+ // Show tasks from processes the user manages. Default to ACTIVE unless the request
519+ // already has an explicit status filter that will be applied later.
476520 // OR show self-service tasks from those processes
477521 // Store the process IDs in the query so we can use them later to add self-service tasks
478522 // We'll add self-service tasks after PMQL is applied to avoid the is_self_service = 0 filter
479523 $ query ->getQuery ()->processManagerIds = $ ids ;
480524
481525 // Apply condition for regular tasks from managed processes
482526 // Self-service tasks will be added after PMQL to avoid conflicts
483- $ query ->where (function ($ query ) use ($ ids ) {
484- $ query ->whereIn ('process_request_tokens.process_id ' , $ ids )
485- ->where ('process_request_tokens.status ' , 'ACTIVE ' );
527+ $ query ->where (function ($ query ) use ($ ids , $ request ) {
528+ $ query ->whereIn ('process_request_tokens.process_id ' , $ ids );
529+
530+ if (!$ this ->requestHasStatusFilter ($ request )) {
531+ $ query ->where ('process_request_tokens.status ' , 'ACTIVE ' );
532+ }
486533 });
487534 }
488535
0 commit comments