Skip to content

Commit

Permalink
Implement proper sorting for the Name field (#2123)
Browse files Browse the repository at this point in the history
rafaehlers authored Aug 24, 2024
2 parents 3455f66 + 8cd5a57 commit fb341b6
Showing 4 changed files with 22 additions and 15 deletions.
4 changes: 1 addition & 3 deletions future/includes/class-gv-template-view-table.php
Original file line number Diff line number Diff line change
@@ -90,15 +90,13 @@ public static function add_columns_sort_links( $column_label, $context = null )

$class = 'gv-sort';

$sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type( $context->field->ID, $context->view->form->ID );

$sort_args = array(
sprintf( 'sort[%s]', $context->field->ID ),
'asc',
);

// If we are already sorting by the current field...
if ( ! empty( $sorting['key'] ) && (string) $sort_field_id === (string) $sorting['key'] ) {
if ( ! empty( $sorting['key'] ) && (string) $context->field->ID === (string) $sorting['key'] ) {

switch ( $sorting['direction'] ) {
// No sort
23 changes: 14 additions & 9 deletions future/includes/class-gv-view.php
Original file line number Diff line number Diff line change
@@ -1077,9 +1077,15 @@ public function get_entries( $request = null ) {
* Apply multisort.
*/
if ( ! empty( $has_multisort ) ) {
// Clear ordering that was set when initializing the query since we're going to set it from scratch.
( function () {
$this->order = [];
} )->bindTo( $query, $query )();

$atts = $this->settings->as_atts();

$view_setting_sort_field_ids = \GV\Utils::get( $atts, 'sort_field', array() );

$view_setting_sort_directions = \GV\Utils::get( $atts, 'sort_direction', array() );

$has_sort_query_param = ! empty( $_GET['sort'] ) && is_array( $_GET['sort'] );
@@ -1096,21 +1102,20 @@ public function get_entries( $request = null ) {
$sort_directions = $view_setting_sort_directions;
}

$skip_first = false;

foreach ( (array) $sort_field_ids as $key => $sort_field_id ) {
$sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type( $sort_field_id, $this->form->ID );
$sort_direction = strtoupper( \GV\Utils::get( $sort_directions, $key, 'ASC' ) );

if ( ! $skip_first && ! $has_sort_query_param ) {
$skip_first = true; // Skip the first one, it's already in the query
if ( empty( $sort_field_id ) ) {
continue;
}

$sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type( $sort_field_id, $this->form->ID );
$sort_direction = strtoupper( \GV\Utils::get( $sort_directions, $key, 'ASC' ) );
$sort_field_id = explode( '|', $sort_field_id );

foreach ( $sort_field_id as $id ) {
$order = new \GF_Query_Column( $id, $this->form->ID );

if ( ! empty( $sort_field_id ) ) {
$order = new \GF_Query_Column( $sort_field_id, $this->form->ID );
if ( 'id' !== $sort_field_id && \GVCommon::is_field_numeric( $this->form->ID, $sort_field_id ) ) {
if ( 'id' !== $id && \GVCommon::is_field_numeric( $this->form->ID, $id ) ) {
$order = \GF_Query_Call::CAST( $order, defined( 'GF_Query::TYPE_DECIMAL' ) ? \GF_Query::TYPE_DECIMAL : \GF_Query::TYPE_SIGNED );
}

9 changes: 6 additions & 3 deletions includes/class-frontend-views.php
Original file line number Diff line number Diff line change
@@ -1416,17 +1416,20 @@ public static function _override_sorting_id_by_field_type( $sort_field_id, $form
* Override how to sort when sorting full name.
*
* @since 1.7.4
* @since TBD Default sorting is set to first and last name.
*
* @param string $name_part Sort by `first` or `last` (default: `first`)
* @param string $name_part Sort by `first`, `last` or `first-last` (default: `first-last`)
* @param string $sort_field_id Field used for sorting
* @param int $form_id GF Form ID
*/
$name_part = apply_filters( 'gravityview/sorting/full-name', 'first', $sort_field_id, $form_id );
$name_part = apply_filters( 'gravityview/sorting/full-name', 'first-last', $sort_field_id, $form_id );

if ( 'last' === strtolower( $name_part ) ) {
$sort_field_id .= '.6';
} else {
} elseif ( 'first' === strtolower( $name_part ) ) {
$sort_field_id .= '.3';
} elseif ( 'first-last' === strtolower( $name_part ) ) {
$sort_field_id = "{$sort_field_id}.3|{$sort_field_id}.6";
}
}
break;
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h
* Fixed: Deprecated filter notice if using GravityView Maps 3.1.0 or newer.
* Fixed: PHP 8.2 deprecation notice due to passing an empty value to `htmlspecialchars()` and creating dynamic class properties.
* Fixed: The maximum number of files allowed in the File Upload field was not respected when editing an entry.
* Fixed: Sorting the View by the Name field would yield incorrect results.

= 2.27.1 on August 14, 2024 =

0 comments on commit fb341b6

Please sign in to comment.