From 6296b27eed5229ba0dbaf76968aa758a7ad52b83 Mon Sep 17 00:00:00 2001 From: Clemen Date: Fri, 10 Jan 2025 18:51:15 +0000 Subject: [PATCH] CRM: Add the order capability to the API events endpoint. (#38838) * Add to the API event the pagination order support. * changelog * Update API pagination processing with new function * Refactor jpcrm_api_process_pagination for cleaner syntax * Add sort order for customer search * Make ASC the default sort order * Add sort order for quotes * Add order for invoices * Add order for transactions * Update code to use DAL call * Update code to use DAL call * Remove unneeded ignoreowner argument * Make DESC the default sort order (better this way) --------- Co-authored-by: gogdzl --- projects/plugins/crm/api/customer_search.php | 8 +++---- projects/plugins/crm/api/events.php | 7 +++--- projects/plugins/crm/api/invoices.php | 14 +++++++---- projects/plugins/crm/api/quotes.php | 8 +++---- projects/plugins/crm/api/transactions.php | 14 +++++++---- ...dd-crm-api-events-pagination-order-support | 4 ++++ .../plugins/crm/includes/ZeroBSCRM.API.php | 23 ++++++++----------- 7 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 projects/plugins/crm/changelog/add-crm-api-events-pagination-order-support diff --git a/projects/plugins/crm/api/customer_search.php b/projects/plugins/crm/api/customer_search.php index b8fa69b2d168d..78840e9c7f54a 100644 --- a/projects/plugins/crm/api/customer_search.php +++ b/projects/plugins/crm/api/customer_search.php @@ -26,15 +26,15 @@ jpcrm_api_check_http_method( array( 'GET' ) ); // Process the pagination parameters from the query -list( $page, $per_page ) = jpcrm_api_process_pagination(); - +$pagination = jpcrm_api_process_pagination(); $search_phrase = jpcrm_api_process_search(); $replace_hyphens_in_response = jpcrm_api_process_replace_hyphens_in_json_keys(); $args = array( 'searchPhrase' => $search_phrase, - 'perPage' => $per_page, - 'page' => $page, + 'page' => $pagination['page'], + 'perPage' => $pagination['per_page'], + 'sortOrder' => $pagination['order'], ); global $zbs; diff --git a/projects/plugins/crm/api/events.php b/projects/plugins/crm/api/events.php index 0bc7ae28bc1f6..99392f9b08dcc 100644 --- a/projects/plugins/crm/api/events.php +++ b/projects/plugins/crm/api/events.php @@ -26,7 +26,7 @@ jpcrm_api_check_http_method( array( 'GET' ) ); // Process the pagination parameters from the query -list( $page, $per_page ) = jpcrm_api_process_pagination(); +$pagination = jpcrm_api_process_pagination(); /** * Allow events to be filtered by owner. Docs are ambiguous about @@ -44,8 +44,9 @@ $args = array( 'withAssigned' => true, - 'page' => $page, - 'perPage' => $per_page, + 'page' => $pagination['page'], + 'perPage' => $pagination['per_page'], + 'sortOrder' => $pagination['order'], 'ownedBy' => $owner, 'ignoreowner' => zeroBSCRM_DAL2_ignoreOwnership( ZBS_TYPE_TASK ), ); diff --git a/projects/plugins/crm/api/invoices.php b/projects/plugins/crm/api/invoices.php index c346a66839e53..528120c906024 100644 --- a/projects/plugins/crm/api/invoices.php +++ b/projects/plugins/crm/api/invoices.php @@ -26,10 +26,16 @@ jpcrm_api_check_http_method( array( 'GET' ) ); // Process the pagination parameters from the query -list( $page, $per_page ) = jpcrm_api_process_pagination(); +$pagination = jpcrm_api_process_pagination(); +$args = array( + 'withAssigned' => false, + 'page' => $pagination['page'], + 'perPage' => $pagination['per_page'], + 'sortOrder' => $pagination['order'], +); + +global $zbs; +$invoices = $zbs->DAL->invoices->getInvoices( $args ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -// needs moving to the $args version -// v3.0 needs these objects refined, including textify for html -$invoices = zeroBS_getInvoices( true, $per_page, $page ); wp_send_json( $invoices ); diff --git a/projects/plugins/crm/api/quotes.php b/projects/plugins/crm/api/quotes.php index 9550e99e4db9a..f47d508d319c4 100644 --- a/projects/plugins/crm/api/quotes.php +++ b/projects/plugins/crm/api/quotes.php @@ -26,7 +26,7 @@ jpcrm_api_check_http_method( array( 'GET' ) ); // Process the pagination parameters from the query -list( $page, $per_page ) = jpcrm_api_process_pagination(); +$pagination = jpcrm_api_process_pagination(); global $zbs; @@ -39,9 +39,9 @@ // 'withAssigned' => $withCustomerDeets, 'suppressContent' => true, // NO HTML! 'sortByField' => 'ID', - 'sortOrder' => 'DESC', - 'page' => $page, - 'perPage' => $per_page, + 'page' => $pagination['page'], + 'perPage' => $pagination['per_page'], + 'sortOrder' => $pagination['order'], 'ignoreowner' => zeroBSCRM_DAL2_ignoreOwnership( ZBS_TYPE_QUOTE ), ); diff --git a/projects/plugins/crm/api/transactions.php b/projects/plugins/crm/api/transactions.php index 729693f47c979..79cb81c3d9e8b 100644 --- a/projects/plugins/crm/api/transactions.php +++ b/projects/plugins/crm/api/transactions.php @@ -26,10 +26,16 @@ jpcrm_api_check_http_method( array( 'GET' ) ); // Process the pagination parameters from the query -list( $page, $per_page ) = jpcrm_api_process_pagination(); +$pagination = jpcrm_api_process_pagination(); +$args = array( + 'withAssigned' => false, + 'withTags' => false, + 'page' => $pagination['page'], + 'perPage' => $pagination['per_page'], + 'sortOrder' => $pagination['order'], +); -// needs moving to the $args version -// v3.0 needs these objects refined, including textify for html -$transactions = zeroBS_getTransactions( true, $per_page, $page ); +global $zbs; +$transactions = $zbs->DAL->transactions->getTransactions( $args ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase wp_send_json( $transactions ); diff --git a/projects/plugins/crm/changelog/add-crm-api-events-pagination-order-support b/projects/plugins/crm/changelog/add-crm-api-events-pagination-order-support new file mode 100644 index 0000000000000..b070a38901938 --- /dev/null +++ b/projects/plugins/crm/changelog/add-crm-api-events-pagination-order-support @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +API-events: Add the pagination result sort/order capability to the endpoint. diff --git a/projects/plugins/crm/includes/ZeroBSCRM.API.php b/projects/plugins/crm/includes/ZeroBSCRM.API.php index 460466f559129..50f397b7d55a8 100644 --- a/projects/plugins/crm/includes/ZeroBSCRM.API.php +++ b/projects/plugins/crm/includes/ZeroBSCRM.API.php @@ -37,20 +37,17 @@ function zeroBS_api_rewrite_endpoint() { * Process the query and get page and items per page */ function jpcrm_api_process_pagination() { + // phpcs:disable WordPress.Security.NonceVerification.Recommended + $page = isset( $_GET['page'] ) ? max( (int) $_GET['page'], 1 ) : 1; + $per_page = isset( $_GET['perpage'] ) ? max( (int) $_GET['perpage'], 1 ) : 10; + $order = strtoupper( $_GET['order'] ?? '' ) === 'ASC' ? 'ASC' : 'DESC'; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + // phpcs:enable WordPress.Security.NonceVerification.Recommended - if ( isset( $_GET['page'] ) && (int) $_GET['page'] >= 0 ) { - $page = (int) $_GET['page']; - } else { - $page = 0; - } - - if ( isset( $_GET['perpage'] ) && (int) $_GET['perpage'] >= 0 ) { - $per_page = (int) $_GET['perpage']; - } else { - $per_page = 10; - } - - return array( $page, $per_page ); + return array( + 'page' => $page, + 'per_page' => $per_page, + 'order' => $order, + ); } /**