@@ -499,6 +499,26 @@ function apiOccurrences($query_data, &$metadata = []){
499499        $ page  = $ query_data ['@page ' ] ?? null ;
500500        $ order  = $ query_data ['@order ' ] ?? null ;
501501
502+         // Filter by state and municipality 
503+         // Helper function to extract values from IIN(...) 
504+         $ extractIIN  = function ($ val ) {
505+             if  ($ val  === null ) {
506+                 return  [];
507+             }
508+             if  (is_string ($ val ) && preg_match ('/^IIN\((.*)\)$/i ' , $ val , $ matches )) {
509+                 $ list  = trim ($ matches [1 ]);
510+                 if  ($ list  === '' ) {
511+                     return  [];
512+                 }
513+                 return  array_map ('trim ' , explode (', ' , $ list ));
514+             }
515+             return  [];
516+         };
517+ 
518+         // Extract En_Estado and En_Municipio, removing the IIN() prefix and splitting into array if needed 
519+         $ en_estado_filter  = $ extractIIN ($ query_data ['En_Estado ' ] ?? null );
520+         $ en_municipio_filter  = $ extractIIN ($ query_data ['En_Municipio ' ] ?? null );
521+ 
502522        if ($ event_ids ){
503523
504524            $ event_ids  = implode (', ' ,$ event_ids );
@@ -522,6 +542,8 @@ function apiOccurrences($query_data, &$metadata = []){
522542                $ events_by_id [$ event ['id ' ]] = $ event ;
523543            }
524544
545+             $ conn  = $ app ->em ->getConnection ();
546+ 
525547            $ result  = [];
526548
527549            foreach ($ _result  as  $ i  => $ occ ){
@@ -533,8 +555,34 @@ function apiOccurrences($query_data, &$metadata = []){
533555                if (!isset ($ events_by_id [$ event_id ]))
534556                    continue ;
535557
536- 
537558                unset($ occ ->space_id );
559+ 
560+                 // Check if the space matches the event's state and municipality filters 
561+                 if  ($ en_estado_filter  || $ en_municipio_filter ) {
562+                     // Query to get the state and municipality of the event 
563+                     $ filter_space_query  = " 
564+                         SELECT sm.key, sm.value 
565+                         FROM space_meta sm 
566+                         WHERE (sm.key = 'En_Estado' OR sm.key = 'En_Municipio') 
567+                         AND sm.object_id = :space_id 
568+                         ORDER BY sm.key ASC; " ; // Alphabetic sort to ensure order: First En_Estado key and second En_Municipio key 
569+                     
570+                     $ space_state_city  = $ conn ->fetchAll ($ filter_space_query , [
571+                         'space_id '  => $ space_id ,
572+                         ]);
573+                     
574+                     // Extract the state and municipality values from the query result 
575+                     $ space_state  = $ space_state_city [0 ]['value ' ] ?? null ;
576+                     $ space_city  = $ space_state_city [1 ]['value ' ] ?? null ;
577+                     
578+                     // Check if the space's state and municipality match the event's filters, if not, skip the occurrence 
579+                     if  ($ en_estado_filter  && !in_array ($ space_state , $ en_estado_filter )) {
580+                         continue ;
581+                     }
582+                     if  ($ en_municipio_filter  && !in_array ($ space_city , $ en_municipio_filter )) {
583+                         continue ;
584+                     }
585+                 }
538586
539587                if (isset ($ spaces_by_id [$ space_id ]) && isset ($ events_by_id [$ event_id ])){
540588                    unset($ occ ->event );
0 commit comments