Skip to content

Commit

Permalink
terms generation update
Browse files Browse the repository at this point in the history
  • Loading branch information
kasprzyk-sz committed Oct 30, 2024
1 parent 7ae49db commit 8868e02
Showing 1 changed file with 40 additions and 44 deletions.
84 changes: 40 additions & 44 deletions wp-content/plugins/embl-taxonomy/includes/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,66 +533,60 @@ static private function decode_terms($data) {
static private function generate_terms(& $api_terms, & $terms) {
// Iterate over each base term
foreach ($api_terms as $uuid => $term) {
// Initialize variables to hold the prefixed name and IDs
// Initialize prefix IDs with the term's own IDs
$prefix_ids = $term[EMBL_Taxonomy::META_IDS];

// Check the 'primary' term and add specific IDs to META_IDS
if (isset($term['primary'])) {
switch ($term['primary']) {
case 'what':
array_unshift($prefix_ids, '302cfdf7-365b-462a-be65-82c7b783ebf7');
break;
case 'who':
array_unshift($prefix_ids, '4428d1fd-441a-4d6d-a1c5-5dcf5665f213');
break;
case 'where':
array_unshift($prefix_ids, 'b14d3f13-5670-44fb-8970-e54dfd9c921a');
break;
}
// Initialize the prefix name with the primary term
$primary_term_name = ucfirst($term['primary']);
$prefix_name = $primary_term_name . EMBL_Taxonomy::TAXONOMY_SEPARATOR;

// Add primary term ID to prefix IDs
switch ($term['primary']) {
case 'what':
array_unshift($prefix_ids, '302cfdf7-365b-462a-be65-82c7b783ebf7');
break;
case 'who':
array_unshift($prefix_ids, '4428d1fd-441a-4d6d-a1c5-5dcf5665f213');
break;
case 'where':
array_unshift($prefix_ids, 'b14d3f13-5670-44fb-8970-e54dfd9c921a');
break;
}

// Initialize the name with the primary term
$prefix_name = ucfirst($term['primary']) . EMBL_Taxonomy::TAXONOMY_SEPARATOR;

// Check if the term has parents and only get the one that corresponds to the primary term
if (is_array($term['parents']) && count($term['parents'])) {
// Get the first parent (as per your requirement)
$primary_parent_id = $term['parents'][0]; // Take only the first parent

// Log the identified primary parent ID for debugging
error_log("Identified primary parent ID: $primary_parent_id");
// Check if the term has parents and retrieve the correct one based on the primary term
if (is_array($term['parents']) && !empty($term['parents'])) {
// Initialize a variable to track if a parent was found
$found_parent = false;

// If a primary parent ID exists, construct the name accordingly
if ($primary_parent_id && array_key_exists($primary_parent_id, $api_terms)) {
$parent_term = $api_terms[$primary_parent_id];

// Check if the parent term has a name
if (isset($parent_term['name']) && !empty($parent_term['name'])) {
// Append the parent's name to the prefix name
$prefix_name .= $parent_term['name'] . EMBL_Taxonomy::TAXONOMY_SEPARATOR;
// Loop through parents to find the one that corresponds to the primary term
foreach ($term['parents'] as $parent_id) {
if (array_key_exists($parent_id, $api_terms)) {
$parent_term = $api_terms[$parent_id];

// Add the parent term's ID to the prefix_ids
$parent_meta_id = $parent_term[EMBL_Taxonomy::META_IDS];
if (!is_array($parent_meta_id)) {
error_log("Parent term ID is not an array: $parent_meta_id");
$parent_meta_id = [$parent_meta_id];
// Check if the parent term is of the same type as the primary
if ($parent_term['primary'] === $term['primary']) {
// Append the parent's name to the prefix name
$prefix_name .= $parent_term['name'] . EMBL_Taxonomy::TAXONOMY_SEPARATOR;
// Add the parent term's IDs to prefix_ids
$prefix_ids = array_merge((array)$parent_term[EMBL_Taxonomy::META_IDS], $prefix_ids);
$found_parent = true;
break; // Stop after finding the first matching parent
}
$prefix_ids = array_merge($parent_meta_id, $prefix_ids);
} else {
// Debugging statement if the parent's name is empty
error_log("Parent term ID '$primary_parent_id' does not have a valid name.");
error_log("Parent ID '$parent_id' not found in api_terms.");
}
} else {
// Debugging statement if the primary parent ID is not found in api_terms
error_log("Primary parent ID '$primary_parent_id' not found in api_terms.");
}

if (!$found_parent) {
error_log("No valid parent found for term '{$term['name']}' with primary '{$term['primary']}'.");
}
}

// Append the current term's name
$prefix_name .= $term['name'];

// Update the term with the prefixed name and IDs
$term['name'] = $prefix_name;
$term['name'] = rtrim($prefix_name, EMBL_Taxonomy::TAXONOMY_SEPARATOR); // Remove trailing separator
$term[EMBL_Taxonomy::META_IDS] = $prefix_ids;

// Add the modified term to the terms array
Expand All @@ -606,6 +600,8 @@ static private function generate_terms(& $api_terms, & $terms) {
}




/**
* Sort taxonomy terms
* only matters when displaying in Admin UI ?
Expand Down

0 comments on commit 8868e02

Please sign in to comment.