public function analyzeTaxa($startIndex = 0, $limit = 50)
 {
     $status = true;
     $this->logOrEcho("Starting taxa check ");
     $sql = 'SELECT DISTINCT sciname, family ' . 'FROM omoccurrences ' . 'WHERE (collid = ' . $this->collid . ') AND (tidinterpreted IS NULL) AND (sciname IS NOT NULL) AND (sciname != "") ' . 'ORDER BY sciname ' . 'LIMIT ' . $startIndex . ',' . $limit;
     if ($rs = $this->conn->query($sql)) {
         //Check name through taxonomic resources
         $taxUtil = new TaxonomyUtilities();
         $taxUtil->setVerboseMode(2);
         $this->setVerboseMode(2);
         $nameCnt = 0;
         while ($r = $rs->fetch_object()) {
             $this->logOrEcho('Resolving ' . $r->sciname . ($r->family ? ' (' . $r->family . ')' : '') . '...');
             $newTid = $taxUtil->addSciname($r->sciname, $r->family);
             if (!$newTid) {
                 //Check for near match using SoundEx
                 $this->logOrEcho('Checking close matches in thesaurus...', 1);
                 $closeArr = $taxUtil->getSoundexMatch($r->sciname);
                 if (!$closeArr) {
                     $closeArr = $taxUtil->getCloseMatchEpithet($r->sciname);
                 }
                 if ($closeArr) {
                     $cnt = 0;
                     foreach ($closeArr as $tid => $sciname) {
                         $echoStr = '<i>' . $sciname . '</i> =&gt;<a href="#" onclick="remappTaxon(\'' . $r->sciname . '\',' . $tid . ',\'' . $sciname . '\')"> remap to this taxon</a>';
                         $this->logOrEcho($echoStr, 2);
                         $cnt++;
                     }
                 } else {
                     $this->logOrEcho('No close matches found', 1);
                 }
             }
             $nameCnt++;
         }
         $rs->free();
     }
     $this->linkNewTaxa();
     $this->logOrEcho("Done with taxa check ");
     return $status;
 }
 public function uploadCsvList($hasHeader, $thesId)
 {
     set_time_limit(300);
     ini_set("max_input_time", 300);
     ini_set('auto_detect_line_endings', true);
     $successCnt = 0;
     $fh = fopen($_FILES['uploadfile']['tmp_name'], 'r') or die("Can't open file. File may be too large. Try uploading file in sections.");
     $headerArr = array();
     if ($hasHeader) {
         $headerData = fgetcsv($fh);
         foreach ($headerData as $k => $v) {
             $vStr = strtolower($v);
             $vStr = str_replace(array(" ", ".", "_"), "", $vStr);
             $vStr = str_replace(array("scientificnamewithauthor", "scientificname", "taxa", "species", "taxon"), "sciname", $vStr);
             $headerArr[$vStr] = $k;
         }
     } else {
         $headerArr["sciname"] = 0;
     }
     if (array_key_exists("sciname", $headerArr)) {
         $cnt = 0;
         ob_flush();
         flush();
         $taxUtil = new TaxonomyUtilities();
         while ($valueArr = fgetcsv($fh)) {
             $tid = 0;
             $rankId = 0;
             $sciName = "";
             $family = "";
             $sciNameStr = $this->cleanInStr($valueArr[$headerArr["sciname"]]);
             $noteStr = '';
             if ($sciNameStr) {
                 $sciNameArr = $taxUtil->parseSciName($sciNameStr);
                 //Check name is in taxa table and grab tid if it is
                 $sql = "";
                 if ($thesId && is_numeric($thesId)) {
                     $sql = 'SELECT t2.tid, ts.family, t2.rankid ' . 'FROM (taxa t INNER JOIN taxstatus ts ON t.tid = ts.tid) ' . 'INNER JOIN taxa t2 ON ts.tidaccepted = t2.tid ' . 'WHERE (ts.taxauthid = ' . $thesId . ') ';
                 } else {
                     $sql = 'SELECT t.tid, ts.family, t.rankid ' . 'FROM taxa t INNER JOIN taxstatus ts ON t.tid = ts.tid ' . 'WHERE ts.taxauthid = 1 ';
                 }
                 $cleanSciName = $this->encodeString($sciNameArr['sciname']);
                 $sql .= 'AND (t.sciname IN("' . $sciNameStr . '"' . ($cleanSciName ? ',"' . $cleanSciName . '"' : '') . '))';
                 $rs = $this->conn->query($sql);
                 if ($rs) {
                     if ($row = $rs->fetch_object()) {
                         $tid = $row->tid;
                         $family = $row->family;
                         $rankId = $row->rankid;
                     }
                     $rs->free();
                 }
                 //Load taxon into checklist
                 if ($tid) {
                     if ($rankId >= 180) {
                         $sqlInsert = '';
                         $sqlValues = '';
                         if (array_key_exists('family', $headerArr) && ($valueArr[$headerArr['family']] && strtolower($family) != strtolower($valueArr[$headerArr['family']]))) {
                             $sqlInsert .= ',familyoverride';
                             $sqlValues .= ',"' . $this->cleanInStr($valueArr[$headerArr['family']]) . '"';
                         }
                         if (array_key_exists('habitat', $headerArr) && $valueArr[$headerArr['habitat']]) {
                             $sqlInsert .= ',habitat';
                             $sqlValues .= ',"' . $this->cleanInStr($valueArr[$headerArr['habitat']]) . '"';
                         }
                         if (array_key_exists('abundance', $headerArr) && $valueArr[$headerArr['abundance']]) {
                             $sqlInsert .= ',abundance';
                             $sqlValues .= ',"' . $this->cleanInStr($valueArr[$headerArr['abundance']]) . '"';
                         }
                         if ($noteStr || array_key_exists('notes', $headerArr) && $valueArr[$headerArr['notes']]) {
                             if (array_key_exists('notes', $headerArr) && $valueArr[$headerArr['notes']]) {
                                 if ($noteStr) {
                                     $noteStr .= '; ';
                                 }
                                 $noteStr .= $valueArr[$headerArr['notes']];
                             }
                             $sqlInsert .= ',notes';
                             $sqlValues .= ',"' . $this->cleanInStr($noteStr) . '"';
                         }
                         $sql = 'INSERT INTO fmchklsttaxalink (tid,clid' . $sqlInsert . ') VALUES (' . $tid . ', ' . $this->clid . $sqlValues . ')';
                         //echo $sql;
                         if ($this->conn->query($sql)) {
                             $successCnt++;
                         } else {
                             $this->errorArr[] = $sciNameStr . " (TID = {$tid}) failed to load<br />Error msg: " . $this->conn->error;
                             //echo $sql."<br />";
                         }
                     } else {
                         $this->errorArr[] = $sciNameStr . " failed to load (taxon must be of genus, species, or infraspecific ranking)";
                     }
                 } else {
                     $this->problemTaxa[] = $cleanSciName;
                     //$statusStr = $sciNameStr." failed to load (misspelled or not yet in taxonomic thesaurus)";
                     //$failCnt++;
                 }
                 $cnt++;
                 if ($cnt % 500 == 0) {
                     echo '<li style="margin-left:10px;">' . $cnt . ' taxa loaded</li>';
                     ob_flush();
                     flush();
                 }
             }
         }
         fclose($fh);
         if ($cnt && $this->clMeta['type'] == 'rarespp') {
             $occurMain = new OccurrenceMaintenance($this->conn);
             $occurMain->protectStateRareSpecies();
         }
     } else {
         $this->errorStr = 'ERROR: unable to locate scientific name column';
     }
     return $successCnt;
 }
<?php

include_once '../../config/symbini.php';
include_once $SERVER_ROOT . '/classes/TaxonomyUtilities.php';
if (!$SYMB_UID) {
    header('Location: ../../profile/index.php?refurl=../taxa/admin/taxonomymaintenance.php?' . $_SERVER['QUERY_STRING']);
}
$action = array_key_exists("action", $_REQUEST) ? $_REQUEST["action"] : "";
$taxonManager = new TaxonomyUtilities();
$isEditor = false;
if ($isAdmin || array_key_exists("Taxonomy", $userRights)) {
    $isEditor = true;
}
if ($isEditor) {
    if ($action == 'buildenumtree') {
        if ($taxonManager->buildHierarchyEnumTree()) {
            $statusStr = 'SUCCESS building Taxonomic Index';
        } else {
            $statusStr = 'ERROR building Taxonomic Index: ' . $taxonManager->getErrorMessage();
        }
    } elseif ($action == 'rebuildenumtree') {
        if ($taxonManager->rebuildHierarchyEnumTree()) {
            $statusStr = 'SUCCESS building Taxonomic Index';
        } else {
            $statusStr = 'ERROR building Taxonomic Index: ' . $taxonManager->getErrorMessage();
        }
    }
}
?>
<html>
<head>
 public function getDynamicTreePath()
 {
     $retArr = array();
     $tid = '';
     $acceptedTid = '';
     //Temporary code: check to make sure taxaenumtree is populated
     //This code can be removed somewhere down the line
     $sqlTest = 'SELECT tid FROM taxaenumtree LIMIT 1';
     $rsTest = $this->conn->query($sqlTest);
     if (!$rsTest->num_rows) {
         echo '<div style="color:red;margin:30px;">';
         echo 'NOTICE: Building new taxonomic hierarchy table (taxaenumtree).<br/>';
         echo 'This may take a few minutes, but only needs to be done once.<br/>';
         echo 'Do not terminate this process early.';
         echo '</div>';
         ob_flush();
         flush();
         $taxMainObj = new TaxonomyUtilities();
         $taxMainObj->buildHierarchyEnumTree();
     }
     $rsTest->free();
     //Get target taxa (we don't want children and parents of non-accepted taxa, so we'll get those later)
     if ($this->targetStr) {
         $sql1 = 'SELECT DISTINCT t.tid, ts.tidaccepted ' . 'FROM taxa t LEFT JOIN taxstatus ts ON t.tid = ts.tid ' . 'LEFT JOIN taxstatus ts1 ON t.tid = ts1.tidaccepted ' . 'LEFT JOIN taxa t1 ON ts1.tid = t1.tid ' . 'WHERE (ts.taxauthid = 1 OR ts.taxauthid IS NULL) AND (ts1.taxauthid = 1 OR ts1.taxauthid IS NULL) ';
         if (is_numeric($this->targetStr)) {
             $sql1 .= 'AND (t.tid IN(' . implode(',', $this->targetStr) . ') OR (ts1.tid = ' . $this->targetStr . '))';
         } else {
             $sql1 .= 'AND ((t.sciname = "' . $this->targetStr . '") OR (t1.sciname = "' . $this->targetStr . '"))';
         }
         //echo "<div>".$sql1."</div>";
         $rs1 = $this->conn->query($sql1);
         while ($row1 = $rs1->fetch_object()) {
             if ($rs1->num_rows == 1) {
                 $tid = $row1->tid;
             } elseif ($row1->tid != $row1->tidaccepted) {
                 $tid = $row1->tid;
                 $acceptedTid = $row1->tidaccepted;
             }
         }
         $rs1->free();
         if ($tid) {
             $i = 1;
             $prevTid = '';
             $retArr[0] = 'root';
             $sql2 = 'SELECT t.RankId, te.parenttid, ts.tidaccepted, ts.parenttid AS par2 ' . 'FROM (taxa t LEFT JOIN taxaenumtree te ON t.TID = te.parenttid) ' . 'LEFT JOIN taxstatus ts ON te.parenttid = ts.tid ' . 'WHERE te.TID = ' . ($acceptedTid ? $acceptedTid : $tid) . ' AND te.taxauthid = 1 AND ts.taxauthid = 1 ' . 'ORDER BY t.RankId ';
             //echo "<div>".$sql2."</div>";
             $rs2 = $this->conn->query($sql2);
             while ($row2 = $rs2->fetch_object()) {
                 if (!$prevTid || $row2->par2 == $prevTid) {
                     $retArr[$i] = $row2->tidaccepted;
                     $prevTid = $row2->tidaccepted;
                 } else {
                     $sql3 = 'SELECT tid ' . 'FROM taxstatus ' . 'WHERE parenttid = ' . $prevTid . ' AND taxauthid = 1 ' . 'AND tid IN(SELECT parenttid FROM taxaenumtree WHERE tid = ' . $tid . ' AND taxauthid = 1) ';
                     //echo "<div>".$sql3."</div>";
                     $rs3 = $this->conn->query($sql3);
                     while ($row3 = $rs3->fetch_object()) {
                         $retArr[$i] = $row3->tid;
                         $prevTid = $row3->tid;
                     }
                     $rs3->free();
                 }
                 $i++;
             }
             if ($acceptedTid) {
                 $retArr[$i] = $acceptedTid;
                 $i++;
             }
             $retArr[$i] = $tid;
             $rs2->free();
         }
     }
     return $retArr;
 }