Skip to content

Commit

Permalink
Merge pull request #76 from devgeniem/TMS-911
Browse files Browse the repository at this point in the history
TMS-911-2: Group artist archive results by category
  • Loading branch information
Viljami Hirvonen authored Nov 2, 2022
2 parents 7948239 + c0a3d0f commit 5f7af64
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- TMS-911-2: Group artist archive results by category #76

### Added

- TMS-911: Add page template Orchester
Expand Down
12 changes: 12 additions & 0 deletions lib/ACF/ArtistGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ protected function get_details_tab( string $key ) : Field\Tab {
'title' => 'Äänenjohtaja',
'instructions' => '',
],
'menu_order' => [
'title' => 'Järjestysnumero',
'instructions' => 'Orkesterisivun tulokset järjestetään pienimmästä suurimpaan. Oletusarvo: 100.',
],
];

$tab = ( new Field\Tab( $strings['tab'] ) )
Expand Down Expand Up @@ -205,6 +209,13 @@ protected function get_details_tab( string $key ) : Field\Tab {
->set_wrapper_width( 50 )
->set_instructions( $strings['is_principal']['instructions'] );

$menu_order_field = ( new Field\Number( $strings['menu_order']['title'] ) )
->set_key( "${key}_menu_order" )
->set_name( 'menu_order' )
->set_wrapper_width( 50 )
->set_default_value( 100 )
->set_instructions( $strings['menu_order']['instructions'] );

$tab->add_fields( [
$first_name_field,
$last_name_field,
Expand All @@ -214,6 +225,7 @@ protected function get_details_tab( string $key ) : Field\Tab {
$additional_info_repeater,
$is_concertmaster_field,
$is_principal_field,
$menu_order_field,
] );

return $tab;
Expand Down
99 changes: 94 additions & 5 deletions models/archive-artist.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,24 @@ public static function modify_query( WP_Query $wp_query ) : void {
}

$wp_query->set( 'posts_per_page', - 1 );
$wp_query->set( 'orderby', [ 'last_name' => 'ASC' ] );
$wp_query->set( 'meta_key', 'last_name' );
$wp_query->set( 'orderby',
[
'menu_order_clause' => 'ASC',
'last_name_clause' => 'ASC',
]
);
$wp_query->set(
'meta_query',
[
'menu_order_clause' => [
'key' => 'menu_order',
'type' => 'NUMERIC',
],
'last_name_clause' => [
'key' => 'last_name',
],
]
);

$artist_category = self::get_filter_query_var();

Expand Down Expand Up @@ -152,6 +168,8 @@ public function filters() {
return [];
}

$categories = $this->sort_terms( $categories );

$base_url = get_post_type_archive_link( Artist::SLUG );
$categories = array_map( function ( $item ) use ( $base_url ) {
return [
Expand Down Expand Up @@ -186,7 +204,58 @@ public function filters() {
public function results() {
global $wp_query;

return $this->format_posts( $wp_query->posts );
$posts = $this->format_posts( $wp_query->posts );

if ( $this->is_filtered() ) {
return [
[
'posts' => $posts,
],
];
}

$results = [];

foreach ( $posts as $post_item ) {
if ( empty( $post_item->categories ) ) {
continue;
}

foreach ( $post_item->categories as $term ) {
if ( empty( $results[ $term->term_id ] ) ) {
$results[ $term->term_id ] = $term;
}

if ( empty( $results[ $term->term_id ]->posts ) ) {
$results[ $term->term_id ]->posts = [ $post_item ];
}
else {
$results[ $term->term_id ]->posts[] = $post_item;
}
}
}

return $this->sort_terms( $results );
}

/**
* Do we have posts?
*
* @return bool
*/
public function have_posts() {
global $wp_query;

return $wp_query->have_posts();
}

/**
* Is category filter active?
*
* @return bool
*/
public function is_filtered() {
return ! empty( $this->get_filter_query_var() );
}

/**
Expand All @@ -202,8 +271,9 @@ public static function format_posts( array $posts ) : array {
$item->image = get_post_thumbnail_id( $item->ID );
}

$item->permalink = get_the_permalink( $item->ID );
$item->fields = get_fields( $item->ID );
$item->permalink = get_the_permalink( $item->ID );
$item->fields = get_fields( $item->ID );
$item->categories = wp_get_post_terms( $item->ID, ArtistCategory::SLUG );

if ( ! empty( $item->fields['description'] ) ) {
$item->fields['description'] = wp_trim_words( $item->fields['description'], 18 );
Expand All @@ -219,4 +289,23 @@ public static function format_posts( array $posts ) : array {
return $item;
}, $posts );
}

/**
* Sort terms by menu_order.
*
* @param array $terms Array of WP_Terms.
*
* @return array
*/
protected function sort_terms( $terms ) {
$terms = array_map( function ( $term ) {
$term->menu_order = \get_field( 'menu_order', $term );

return $term;
}, $terms );

uasort( $terms, fn( $a, $b ) => (int) $a->menu_order > (int) $b->menu_order );

return $terms;
}
}
38 changes: 29 additions & 9 deletions partials/archive-artist.dust
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,35 @@
{>"shared/archive-view-title" /}
{>"shared/archive-filters" /}

<div class="columns is-multiline artist-list">
{#results}
{>"ui/artist-item" artist=. /}
{:else}
<div class="column pt-8 pb-8 is-text-centered is-size-5">
{Strings.s.home.no_results|s}
</div>
{/results}
</div>
{^ArchiveArtist.is_filtered}
{#.results}
{?.posts}
<h2 class="mb-3">
{.name|html}
</h2>

<div class="mt-3 artist-list columns is-multiline">
{#.posts}
{>"ui/artist-item" artist=. /}
{/.posts}
</div>
{/.posts}
{/.results}
{:else}
<div class="mt-3 artist-list columns is-multiline">
{#.results}
{#.posts}
{>"ui/artist-item" artist=. /}
{/.posts}
{/.results}
</div>
{/ArchiveArtist.is_filtered}

{^ArchiveArtist.have_posts}
<div class="column pt-8 pb-8 is-text-centered is-size-5">
{Strings.s.home.no_results|s}
</div>
{/ArchiveArtist.have_posts}
</div>
</main>
{/ArchiveArtist}
Expand Down

0 comments on commit 5f7af64

Please sign in to comment.