@@ -31,7 +31,7 @@ class Events extends Endpoint
3131 public function ics_get (): ICalendar
3232 {
3333 // get events
34- $ query = self ::get_query ();
34+ $ query = self ::get_query (Request:: $ get -> all () );
3535 $ events = Cache::get_events ($ query , fn (string $ q ) => self ::get_events ($ q ));
3636
3737 // create calendar
@@ -49,7 +49,7 @@ public function ics_get(): ICalendar
4949 public function json_get (): Json
5050 {
5151 // get events
52- $ events = Cache::get_events (self ::get_query (), fn (string $ q ) => self ::get_events ($ q ));
52+ $ events = Cache::get_events (self ::get_query (Request:: $ get -> all () ), fn (string $ q ) => self ::get_events ($ q ));
5353
5454 // return JSON action
5555 return new Json ($ events );
@@ -58,16 +58,17 @@ public function json_get(): Json
5858 /**
5959 * Get Church Suite compatible query options.
6060 *
61- * @return string URL-encoded query (using http_build_query()).
61+ * @param array<string, mixed> $values Query values.
62+ * @return string URL-encoded query (using http_build_query()).
6263 */
63- private static function get_query (): string
64+ private static function get_query (array $ values ): string
6465 {
6566 // get query options
6667 $ query = array (
67- "category " => Request:: $ get -> int ( "cat " ),
68- "date_start " => Request:: $ get-> string ( "start " ),
69- "date_end " => Request:: $ get-> string ( "end " ),
70- "q " => Request:: $ get-> string ( "q " ),
68+ "category " => Arr:: get_integer ( $ values , "cat " ),
69+ "date_start " => Arr:: get ( $ values , "start " ),
70+ "date_end " => Arr:: get ( $ values , "end " ),
71+ "q " => Arr:: get ( $ values , "q " ),
7172 );
7273
7374 // return encoded query
@@ -77,13 +78,16 @@ private static function get_query(): string
7778 /**
7879 * Get events from Church Suite matching the query.
7980 *
80- * @param string $query URL-encoded query (e.g. using http_build_query()).
81- * @return Event[] Array of matching events.
81+ * @param array< string, mixed>|string $query Array of query values, or URL-encoded query (e.g. using http_build_query()).
82+ * @return Event[] Array of matching events.
8283 */
83- public static function get_events (string $ query ): array
84+ public static function get_events (array | string $ query ): array
8485 {
86+ // build query string
87+ $ query_values = is_array ($ query ) ? self ::get_query ($ query ) : $ query ;
88+
8589 // create curl request
86- $ url = sprintf (self ::CALENDAR_HREF , C::$ churchsuite ->org , $ query );
90+ $ url = sprintf (self ::CALENDAR_HREF , C::$ churchsuite ->org , $ query_values );
8791 $ handle = curl_init ($ url );
8892 if ($ handle === false ) {
8993 _l ("Unable to create cURL request for %s. " , $ url );
@@ -139,6 +143,9 @@ public static function get_events(string $query): array
139143 );
140144 }
141145
146+ // sort events by start time
147+ usort ($ events , fn ($ a , $ b ) => ($ a ->start < $ b ->start ) ? -1 : 1 );
148+
142149 // return events
143150 return $ events ;
144151 }
0 commit comments