public static function split_entry($args)
 {
     $hierarchy_entry_id = @$args['hierarchy_entry_id'];
     $bad_match_hierarchy_entry_id = @$args['bad_match_hierarchy_entry_id'];
     $confirmation = @$args['confirmed'];
     if (!$hierarchy_entry_id || !is_numeric($hierarchy_entry_id) || !$bad_match_hierarchy_entry_id || !is_numeric($bad_match_hierarchy_entry_id)) {
         throw new \Exception("split_entry.php [hierarchy_entry_id] [bad_match_hierarchy_entry_id] [confirmed]");
     }
     \CodeBridge::print_message("Splitting HE# {$hierarchy_entry_id} from {$bad_match_hierarchy_entry_id}");
     $he = HierarchyEntry::find($hierarchy_entry_id);
     $bad_he = HierarchyEntry::find($hierarchy_entry_id);
     if (!$he->id || !$bad_he->id) {
         throw new \Exception("Invalid ID");
     }
     if ($he->taxon_concept_id != $bad_he->taxon_concept_id) {
         throw new \Exception("The bad match ID isn't from the same concept");
     }
     if ($confirmation == 'confirmed') {
         $user_id = 13;
         # 13 is Patrick's user ID - TODO - this should be an argument.  :|
         $new_taxon_concept_id = HierarchyEntry::split_from_concept_static($hierarchy_entry_id);
         $GLOBALS['db_connection']->query("INSERT IGNORE INTO curated_hierarchy_entry_relationships (hierarchy_entry_id_1, hierarchy_entry_id_2, user_id, equivalent)\n                VALUES ({$hierarchy_entry_id}, {$bad_match_hierarchy_entry_id}, {$user_id}, 0)");
         \CodeBridge::print_message("Done. HE# {$hierarchy_entry_id} was split into a new concept # {$new_taxon_concept_id}");
     } else {
         echo "\n\nRemoving:\n";
         print_r($he);
         echo "Name: " . $he->name->string . "\n\nFrom:\n";
         print_r($he->taxon_concept);
         $descendant_objects = TaxonConcept::count_descendants_objects($he->taxon_concept_id);
         echo "\n\nDescendant Objects:  {$descendant_objects}\n\n";
     }
 }
 function details_tcs($id)
 {
     $entry = HierarchyEntry::find($id);
     if (@(!$entry->id)) {
         return false;
     }
     $nomenclaturalCode = "Zoological";
     $rankCode = "";
     switch (strtolower($entry->rank->translation->label)) {
         case "kingdom":
             $rankCode = "reg";
             break;
         case "phylum":
             $rankCode = "phyl_div";
             break;
         case "class":
             $rankCode = "cl";
             break;
         case "order":
             $rankCode = "ord";
             break;
         case "family":
             $rankCode = "fam";
             break;
         case "genus":
             $rankCode = "gen";
             break;
         case "species":
             $rankCode = "sp";
             break;
     }
     $return = "";
     $return .= "  <TaxonNames>\n";
     $return .= "    <TaxonName id='n" . $entry->name->id . "' nomenclaturalCode='{$nomenclaturalCode}'>\n";
     $return .= "      <Simple>" . htmlspecialchars($entry->name->string) . "</Simple>\n";
     if ($rankCode) {
         $return .= "      <Rank code='{$rankCode}'>" . ucfirst(strtolower($entry->rank->translation->label)) . "</Rank>\n";
     }
     $return .= "      <CanonicalName>\n";
     $return .= "        <Simple>" . htmlspecialchars($entry->name->canonical_form->string) . "</Simple>\n";
     $return .= "      </CanonicalName>\n";
     if ($ahe = $entry->agents_hierarchy_entries) {
         $return .= "      <ProviderSpecificData>\n";
         $return .= "        <NameSources>\n";
         foreach ($ahe as $k => $v) {
             $return .= "          <NameSource>\n";
             $return .= "            <Simple>" . htmlspecialchars($v->agent->full_name) . "</Simple>\n";
             if ($v->agent_role) {
                 $return .= "            <Role>" . htmlspecialchars($v->agent_role->translation->label) . "</Role>\n";
             }
             $return .= "          </NameSource>\n";
         }
         $return .= "        </NameSources>\n";
         $return .= "      </ProviderSpecificData>\n";
     }
     $return .= "    </TaxonName>\n";
     $return .= "  </TaxonNames>\n";
     $return .= "  <TaxonConcepts>\n";
     $return .= "    <TaxonConcept id='c" . $entry->id . "'>\n";
     $return .= "      <Name scientific='true' ref='n" . $entry->name->id . "'>" . htmlspecialchars($entry->name->string) . "</Name>\n";
     if ($rankCode) {
         $return .= "      <Rank code='{$rankCode}'>" . ucfirst(strtolower($entry->rank->translation->label)) . "</Rank>\n";
     }
     $return .= "      <TaxonRelationships>\n";
     if ($parent = $entry->parent()) {
         $return .= "        <TaxonRelationship type='is child taxon of'>\n";
         $return .= "          <ToTaxonConcept ref='{$this->api_url}?function=details_tcs&amp;id=" . $parent->id . "' linkType='external'/>\n";
         $return .= "        </TaxonRelationship>\n";
     }
     if ($children = $entry->children()) {
         foreach ($children as $k => $v) {
             $return .= "        <TaxonRelationship type='is parent taxon of'>\n";
             $return .= "          <ToTaxonConcept ref='{$this->api_url}?function=details_tcs&amp;id=" . $v->id . "' linkType='external'/>\n";
             $return .= "        </TaxonRelationship>\n";
         }
     }
     if ($synonyms = $entry->synonyms()) {
         foreach ($synonyms as $k => $v) {
             $relationship = "has synonym";
             if (strtolower($v->synonym_relation->translation->label) == "common name") {
                 $relationship = "has vernacular";
             }
             $return .= "        <TaxonRelationship type='{$relationship}'>\n";
             $return .= "          <ToTaxonConcept ref='{$this->api_url}?function=details_tcs&amp;sid=" . $v->id . "' linkType='external'/>\n";
             $return .= "        </TaxonRelationship>\n";
         }
     }
     $return .= "      </TaxonRelationships>\n";
     $return .= "    </TaxonConcept>\n";
     $return .= "  </TaxonConcepts>\n";
     return $return;
 }
 public static function move_entry($args)
 {
     $taxon_concept_id_from = @$args['taxon_concept_id_from'];
     $hierarchy_entry_id = @$args['hierarchy_entry_id'];
     $taxon_concept_id_to = @$args['taxon_concept_id_to'];
     $bad_match_hierarchy_entry_id = @$args['bad_match_hierarchy_entry_id'];
     $confirmation = @$args['confirmed'];
     if (!$taxon_concept_id_from || !is_numeric($taxon_concept_id_from) || !$hierarchy_entry_id || !is_numeric($hierarchy_entry_id) || !$taxon_concept_id_to || !is_numeric($taxon_concept_id_to) || !$bad_match_hierarchy_entry_id || !is_numeric($bad_match_hierarchy_entry_id)) {
         throw new \Exception("split_concept.php [taxon_concept_id_from] [hierarchy_entry_id] [taxon_concept_id_to] [bad_match_hierarchy_entry_id] [confirmed] [reindex?]");
     }
     \CodeBridge::print_message("Moving HE# {$hierarchy_entry_id} from TC# {$taxon_concept_id_from} to TC# " . "{$taxon_concept_id_to} avoiding HE# {$bad_match_hierarchy_entry_id}");
     $tc_from = TaxonConcept::find($taxon_concept_id_from);
     $tc_to = TaxonConcept::find($taxon_concept_id_to);
     $he = HierarchyEntry::find($hierarchy_entry_id);
     $bad_he = HierarchyEntry::find($bad_match_hierarchy_entry_id);
     if (!$he->id || !$tc_from->id || !$tc_to->id || !$bad_he->id) {
         throw new \Exception("Invalid ID");
     }
     if ($he->taxon_concept_id != $tc_from->id) {
         throw new \Exception("This entry is not in the source concept");
     }
     if ($he->taxon_concept_id != $bad_he->taxon_concept_id) {
         throw new \Exception("The bad match ID isn't from the same concept");
     }
     if ($confirmation == 'confirmed' || $confirmation == 'force') {
         if ($confirmation == 'force') {
             $force_move_if_disallowed = true;
         } else {
             $force_move_if_disallowed = false;
         }
         $user_id = 13;
         # 13 is Patrick's user ID
         // TODO Need to look through all the HEs in the TC we're moving *to* and cycle through them to make sure none of
         // them are blocking the move:
         foreach ($tc_to->hierarchy_entries as $tc_he) {
             $GLOBALS['db_connection']->query("DELETE FROM curated_hierarchy_entry_relationships\n                    WHERE hierarchy_entry_id_1={$hierarchy_entry_id} AND hierarchy_entry_id_2=" . $tc_he->id . " AND equivalent=0");
         }
         $moved = HierarchyEntry::move_to_concept_static($hierarchy_entry_id, $taxon_concept_id_to, $force_move_if_disallowed, true);
         if (!$moved) {
             \CodeBridge::print_message("NOT ALLOWED: throwing exception");
             throw new \Exception("This move is not allowed; it would affect other hierarchies");
         }
         $GLOBALS['db_connection']->query("INSERT IGNORE INTO curated_hierarchy_entry_relationships VALUES ({$hierarchy_entry_id}, {$bad_match_hierarchy_entry_id}, {$user_id}, 0)");
         \CodeBridge::print_message("Done. Moved {$hierarchy_entry_id} to {$taxon_concept_id_to}");
     } else {
         echo "\n\nRemoving:\n";
         print_r($he);
         echo "Name: " . $he->name->string . "\n\nFrom:\n";
         print_r($tc_from);
         echo "To:\n";
         print_r($tc_to);
         $descendant_objects = TaxonConcept::count_descendants_objects($tc_from->id);
         $descendants = TaxonConcept::count_descendants($tc_from->id);
         echo "\n\nTaxonConcept1: {$tc_from->id}\n";
         echo "Descendant Objects:  {$descendant_objects}\n";
         echo "Descendant Concepts: {$descendants}\n";
         $descendant_objects = TaxonConcept::count_descendants_objects($tc_to->id);
         $descendants = TaxonConcept::count_descendants($tc_to->id);
         echo "\n\nTaxonConcept1: {$tc_to->id}\n";
         echo "Descendant Objects:  {$descendant_objects}\n";
         echo "Descendant Concepts: {$descendants}\n";
         echo "\n\nDon't forget to solr_update_concept.php\n\n";
     }
 }