Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement proper sorting for the Name field #2123

Merged
merged 3 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions future/includes/class-gv-template-view-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions future/includes/class-gv-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
Expand All @@ -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 );
}

Expand Down
9 changes: 6 additions & 3 deletions includes/class-frontend-views.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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 =

Expand Down