public static function getInstance()
 {
     if (self::$instance == null) {
         self::$instance = new TaxonMySQL();
     }
     return self::$instance;
 }
 public function getTaxonDAO()
 {
     return TaxonMySQL::getInstance();
 }
 public function getMainConcept($taxonID)
 {
     global $db;
     $query = "SELECT c.Concept_Type,c.Concept_Level, s.Valid_Taxon_ID, s.Scientific_Name\r\n                FROM species s\r\n                JOIN concepts c\r\n                WHERE s.Taxon_ID = c.Crop_ID\r\n                AND c.Taxon_ID = {$taxonID}\r\n                AND c.Concept_Level NOT \r\n                IN (\r\n                'Crop taxa'\r\n                )\r\n               AND s.Main_Crop = 1 ORDER BY s.Scientific_Name";
     $result = $db->getAll($query);
     $taxa = array();
     // Entre estas dos condiciones, se debe poner lo de los otros tipos potenciales de uso para el enlace al gene pool
     $hasConInfo = false;
     $hasGraInfo = false;
     $ids = array();
     if (!empty($result)) {
         // Tiene informacion de concepto
         $hasConInfo = true;
         foreach ($result as $r) {
             $taxon = new stdClass();
             $taxon->taxon = TaxonMySQL::getInstance()->getTaxon($r["Valid_Taxon_ID"]);
             $taxon->Concept_Type = $r["Concept_Type"];
             $taxon->Concept_Level = $r["Concept_Level"];
             $taxon->Scientific_Name = $r["Scientific_Name"];
             array_push($taxa, $taxon);
             array_push($ids, $r["Valid_Taxon_ID"]);
             // Ingresando en el arreglo de arrays
         }
     }
     $query = "SELECT b.Pot_Conf, b.Description, b.Crop_ID, s.Scientific_Name FROM Breeding_data b JOIN species s ON s.Taxon_ID = b.Crop_ID WHERE b.Taxon_ID = {$taxonID} AND b.Description = 'Graftstock' ORDER BY s.Scientific_Name";
     $result = $db->getAll($query);
     if (!empty($result)) {
         // Tiene informacion de graftstock
         $hasGraInfo = true;
         foreach ($result as $r) {
             if (!in_array($r["Crop_ID"], $ids, true)) {
                 // Solo ingresar la informacion en caso de que no haya relacion de concept
                 $taxon = new stdClass();
                 $taxon->taxon = TaxonMySQL::getInstance()->getTaxon($r["Crop_ID"]);
                 $taxon->Concept_Level = $r['Pot_Conf'];
                 $taxon->Concept_Type = strtolower($r['Description']);
                 $taxon->Scientific_Name = $r["Scientific_Name"];
                 array_push($taxa, $taxon);
             }
         }
     }
     if (!$hasConInfo && !$hasGraInfo) {
         // No tiene informacion asociada ni a graftstock ni a concept
         $query = "SELECT b.Pot_Conf, b.Description, b.Crop_ID, s.Scientific_Name FROM Breeding_data b JOIN species s ON s.Taxon_ID = b.Crop_ID WHERE b.Taxon_ID = {$taxonID} ORDER BY s.Scientific_Name";
         $result = $db->getAll($query);
         $last_id = null;
         if (!empty($result)) {
             foreach ($result as $r) {
                 if ($r["Crop_ID"] != $last_id) {
                     $taxon = new stdClass();
                     $taxon->taxon = TaxonMySQL::getInstance()->getTaxon($r["Crop_ID"]);
                     $taxon->Concept_Level = $r['Pot_Conf'];
                     $taxon->Concept_Type = $r['Description'] . "[PT]";
                     // Incluir para filtrar por el uso potencial en el enlace de retorno al gene pool
                     $taxon->Scientific_Name = $r["Scientific_Name"];
                     array_push($taxa, $taxon);
                     $last_id = $r["Crop_ID"];
                 }
             }
         }
     }
     return $taxa;
 }
function generateJSONConceptInformation($taxonName)
{
    $cwrConceptDAO = DAOFactory::getDAOFactory()->getCWRConceptDAO();
    $cwrConcept = $cwrConceptDAO->getCWRConceptbyTaxonName($taxonName);
    $mainTaxon = $cwrConceptDAO->getMainConcept($cwrConcept->getTaxon()->getId());
    if ($cwrConcept != null && $mainTaxon != null) {
        $r = new stdClass();
        $r->specieName = utf8_encode($cwrConcept->getTaxon()->generateScientificName(true, true));
        $validName = $cwrConcept->getTaxon()->getValidName();
        if (!empty($validName)) {
            $r->validName = utf8_encode($validName);
        }
        $commonName = $cwrConcept->getTaxon()->getCommonName();
        if (!empty($commonName)) {
            $r->commonName = utf8_encode($commonName);
        }
        $taxonID = $cwrConcept->getTaxon()->getId();
        if (!empty($taxonID)) {
            $r->taxonID = $taxonID;
        }
        $mainCropList = array();
        if ($mainTaxon != null) {
            foreach ($mainTaxon as $crop) {
                $mainCrop = new stdClass();
                $mainCrop->mainCropID = $crop->taxon->getId();
                $mainCrop->type = $crop->Concept_Type;
                $mainCrop->level = $crop->Concept_Level;
                $mainCrop->mainCropName = utf8_encode($crop->taxon->generateScientificName(true, false));
                array_push($mainCropList, $mainCrop);
            }
        }
        $r->mainCropList = $mainCropList;
    } else {
        // No tiene un concept asociado en la base de datos
        $taxonDAO = TaxonMySQL::getInstance();
        $taxon = $taxonDAO->getTaxonbyName($taxonName);
        $r = new stdClass();
        $r->taxonID = $taxon->getId();
        $r->specieName = utf8_encode($taxon->generateScientificName(true, false));
    }
    return json_encode($r);
}