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

add filtering for age groups listing #94

Open
wants to merge 1 commit into
base: PO-237
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion content/themes/custom/functions/class-apitranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function get_translations() {
// Get translations from the api and transform them into an easily searchable format
$kaannos_json = get_field( 'kaannos-json', 'option' );
$translations = \POF\Api::get( $kaannos_json, true );
$locale = substr( get_locale(), 0, 2 );
$locale = get_short_locale();
foreach ( $translations as &$group ) {
foreach ( $group as $lang ) {
if ( $lang['lang'] === $locale ) {
Expand Down
50 changes: 47 additions & 3 deletions content/themes/custom/functions/function-api_data_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,19 +322,63 @@ function sort_by_order( $a, $b ) {
/**
* Get age groups from the api
*
* @param bool $filter Whether we should filter the results to the current language.
* @return array
*/
function get_age_groups() {
function get_age_groups( bool $filter = false ) : array {
$ohjelma_json = get_field( 'ohjelma-json', 'option' );
$program = \POF\Api::get( $ohjelma_json, true );
$age_groups = $program['program'][0]['agegroups'];
$program = \POF\Api::get( $ohjelma_json, true )['program'];
$program = $filter ? array_reduce( $program, 'filter_api_items', [] ) : $program;
$age_groups = $program[0]['agegroups'];

usort( $age_groups, 'sort_by_order' );
sort_results( $age_groups );

return $age_groups;
}

/**
* Recursively filter api items and its child items to current language
* Should only be called via array_reduce
*
* @param array $items Final result.
* @param array $item Api item to filter.
* @return array Modified $items.
*/
function filter_api_items( array $items, array $item ) : array {
if ( array_key_exists( 'languages', $item ) ) {
$lang_exists = array_reduce( $item['languages'], 'languages_has_current', false );

if ( $lang_exists ) {
$keys_to_traverse = [
'agegroups',
'taskgroups',
'tasks',
];
foreach ( $keys_to_traverse as $key ) {
if ( ! empty( $item[ $key ] ) ) {
$item[ $key ] = array_reduce( $item[ $key ], 'filter_api_items', [] );
}
}
$items[] = $item;
}
}

return $items;
}

/**
* Check api items languages if it contains current language
* Should only be called via array_reduce
*
* @param bool $result True or false depending on if current language exists.
* @param array $lang Language to check.
* @return bool Whether language exists.
*/
function languages_has_current( bool $result, array $lang ) {
return $result ? $result : $lang['lang'] === get_short_locale();
}

/**
* Flatten api program tree into a single array
*
Expand Down
10 changes: 10 additions & 0 deletions content/themes/custom/functions/function-lang_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,13 @@ function generate_search_url( $search = '', $lang = null ) {

return $url;
}

/**
* Get the short version of the locale
*
* @return string
*/
function get_short_locale() : string {
$locale = reset( str_word_count( get_locale(), 1 ) );
return $locale;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ApiTitle extends Helper {
* @return string
*/
public function output() {
$locale = $this->params->locale ?? $this->context->get( 'locale' ) ?? explode( '_', get_locale() )[0];
$locale = $this->params->locale ?? $this->context->get( 'locale' ) ?? get_short_locale();
$title = $this->params->title ?? $this->context->get( 'title' );
$languages = $this->params->languages ?? $this->context->get( 'languages' );

Expand Down
14 changes: 2 additions & 12 deletions content/themes/custom/models/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ public function Content() {
return true;
}

/**
* Get short locale
*
* @return string First part of locale.
*/
public static function get_locale() {
$locale = explode( '_', get_locale() )[0];
return $locale;
}

/**
* Get search terms and translations from api
*/
Expand All @@ -57,7 +47,7 @@ public static function ApiSearchTerms() {
// Get remote data
$haku_json = get_field( 'haku-json', 'option' );
$search_terms = \POF\Api::get( $haku_json, true );
$age_groups = get_age_groups();
$age_groups = get_age_groups( true ); // Get age groups filtered to current language

// Create pseudo filtering field for api_type
$search_terms['api_type'] = [
Expand Down Expand Up @@ -366,7 +356,7 @@ protected function get_post_data( stdClass $result, stdClass $params ) {
'guid' => $post->fields['api_guid'],
'languages' => [
[
'lang' => static::get_locale(),
'lang' => get_short_locale(),
'title' => $parent_post->post_title,
],
],
Expand Down