From 809cd1e4aea033ca6ddbee23675efb6bbaaf37be Mon Sep 17 00:00:00 2001 From: Edward Gilbert Date: Wed, 10 Feb 2021 11:14:25 -0700 Subject: [PATCH] Dynamic Checklist Generator dev - Fixed bug causing tool to fail that was introduced when adding variable sanitation code - Added code to ensure taxon filter being applied during various methods of applying a taxon terms - Extended Manager class into Dynamic Checklist class --- checklists/dynamicchecklist.php | 28 +++++++------- classes/DynamicChecklistManager.php | 60 ++++++++++++++++------------- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/checklists/dynamicchecklist.php b/checklists/dynamicchecklist.php index a52b322d99..a995fa6b34 100644 --- a/checklists/dynamicchecklist.php +++ b/checklists/dynamicchecklist.php @@ -1,5 +1,4 @@ createChecklist($lat, $lng, $radius, $radiusUnits, $tid); -} -else{ - $dynClid = $dynClManager->createDynamicChecklist($lat, $lng, $dynamicRadius, $tid); -} +if($taxa && !$tid) $tid = $dynClManager->getTid($taxa); +$dynClid = 0; +if($radius) $dynClid = $dynClManager->createChecklist($lat, $lng, $radius, $radiusUnits, $tid); +else $dynClid = $dynClManager->createDynamicChecklist($lat, $lng, $dynamicRadius, $tid); -if($interface == "key"){ - header("Location: ".$CLIENT_ROOT."/ident/key.php?dynclid=".$dynClid."&taxon=All Species"); -} -else{ - header("Location: ".$CLIENT_ROOT."/checklists/checklist.php?dynclid=".$dynClid); +if($dynClid){ + if($interface == "key"){ + header("Location: ".$CLIENT_ROOT."/ident/key.php?dynclid=".$dynClid."&taxon=All Species"); + } + else{ + header("Location: ".$CLIENT_ROOT."/checklists/checklist.php?dynclid=".$dynClid); + } } -ob_flush(); -flush(); +else echo 'ERROR generating checklist'; $dynClManager->removeOldChecklists(); ?> \ No newline at end of file diff --git a/classes/DynamicChecklistManager.php b/classes/DynamicChecklistManager.php index 279d32a974..cfe99e4c9b 100644 --- a/classes/DynamicChecklistManager.php +++ b/classes/DynamicChecklistManager.php @@ -1,28 +1,25 @@ conn = MySQLiConnectionFactory::getCon("write"); + parent::__construct(null,'write'); } public function __destruct(){ - if(!($this->conn === null)) $this->conn->close(); + parent::__destruct(); } public function createChecklist($lat, $lng, $radius, $radiusUnits, $tidFilter){ - global $SYMB_UID; - //Radius is a set value + //Radius is a set value if($radiusUnits == "mi") $radius = round($radius*1.6); $dynPk = 0; //Create checklist $sql = 'INSERT INTO fmdynamicchecklists(name,details,expiration,uid) '. 'VALUES ("'.round($lat,5).' '.round($lng,5).' within '.round($radius,1).' km","'.$lat.' '.$lng.' within '.$radius.' km","'. - date('Y-m-d',mktime(0, 0, 0, date('m'), date('d') + 7, date('Y'))).'",'.($SYMB_UID?$SYMB_UID:'NULL').')'; - //echo $sql; + date('Y-m-d',mktime(0, 0, 0, date('m'), date('d') + 7, date('Y'))).'",'.($GLOBALS['SYMB_UID']?$GLOBALS['SYMB_UID']:'NULL').')'; if($this->conn->query($sql)){ $dynPk = $this->conn->insert_id; //Add species to checklist @@ -36,35 +33,32 @@ public function createChecklist($lat, $lng, $radius, $radiusUnits, $tidFilter){ //$sql = 'SELECT count(o.tid) AS speccnt FROM omoccurgeoindex o '. // 'WHERE (o.DecimalLatitude BETWEEN lat1 AND lat2) AND (o.DecimalLongitude BETWEEN lng1 AND lng2)'; //$this->conn->query($sql); - + $sql = 'INSERT INTO fmdyncltaxalink (dynclid, tid) '. 'SELECT DISTINCT '.$dynPk.' AS dynpk, IF(t.rankid=220,t.tid,ts2.parenttid) as tid '. 'FROM omoccurgeoindex o INNER JOIN taxstatus ts ON o.tid = ts.tid '. 'INNER JOIN taxstatus ts2 ON ts.tidaccepted = ts2.tid '. 'INNER JOIN taxa t ON ts2.tid = t.tid '; if($tidFilter){ - $sql .= 'INNER JOIN taxaenumtree e ON ts2.tid = e.tid '; + $sql .= 'INNER JOIN taxaenumtree e ON ts2.tid = e.tid '; } $sql .= 'WHERE (t.rankid IN(220,230,240,260)) AND (ts.taxauthid = 1) AND (ts2.taxauthid = 1) '. 'AND (o.DecimalLatitude BETWEEN '.$lat1.' AND '.$lat2.') AND (o.DecimalLongitude BETWEEN '.$lng1.' AND '.$lng2.') '; if($tidFilter){ $sql .= 'and e.parentTid = '.$tidFilter; } - //echo $sql; exit; $this->conn->query($sql); } return $dynPk; } - + public function createDynamicChecklist($lat, $lng, $radiusUnit, $tidFilter){ - global $SYMB_UID; $dynPK = 0; $specCnt = 0; - $radius; - $latRadius; $lngRadius; - $lat1; $lat2; $lng1; $lng2; + $radius = 10; + $lat1 = 0; $lat2 = 0; $lng1 = 0; $lng2 = 0; $loopCnt = 1; while($specCnt < 2500 && $loopCnt < 10){ $radius = $radiusUnit*$loopCnt; @@ -74,7 +68,7 @@ public function createDynamicChecklist($lat, $lng, $radiusUnit, $tidFilter){ $lat2 = $lat + $latRadius; $lng1 = $lng - $lngRadius; $lng2 = $lng + $lngRadius; - + $sql1 = 'SELECT count(tid) AS speccnt '. 'FROM omoccurgeoindex '. 'WHERE (DecimalLatitude BETWEEN '.$lat1.' AND '.$lat2.') AND (DecimalLongitude BETWEEN '.$lng1.' AND '.$lng2.')'; @@ -85,12 +79,11 @@ public function createDynamicChecklist($lat, $lng, $radiusUnit, $tidFilter){ $rs1->free(); $loopCnt++; } - + $radius = $radius*1.60934; $sql2 = 'INSERT INTO fmdynamicchecklists(name,details,expiration,uid) '. 'VALUES ("'.round($lat,5).' '.round($lng,5).' within '.round($radius,1).' km","'.$lat.' '.$lng.' within '.$radius.' km","'. - date('Y-m-d',mktime(0, 0, 0, date('m'), date('d') + 7, date('Y'))).'",'.($SYMB_UID?$SYMB_UID:'NULL').')'; - //echo $sql; + date('Y-m-d',mktime(0, 0, 0, date('m'), date('d') + 7, date('Y'))).'",'.($GLOBALS['SYMB_UID']?$GLOBALS['SYMB_UID']:'NULL').')'; if($this->conn->query($sql2)){ $dynPK = $this->conn->insert_id; $sql3 = 'INSERT INTO fmdyncltaxalink (dynclid, tid) '. @@ -99,21 +92,36 @@ public function createDynamicChecklist($lat, $lng, $radiusUnit, $tidFilter){ 'INNER JOIN taxstatus ts2 ON ts.tidaccepted = ts2.tid '. 'INNER JOIN taxa t ON ts2.tid = t.tid '; if($tidFilter){ - $sql3 .= 'INNER JOIN taxaenumtree e ON ts2.tid = e.tid '; + $sql3 .= 'INNER JOIN taxaenumtree e ON ts2.tid = e.tid '; } $sql3 .= 'WHERE (t.rankid >= 220) AND (ts.taxauthid = 1) AND (ts2.taxauthid = 1) '. 'AND (o.DecimalLatitude BETWEEN '.$lat1.' AND '.$lat2.') AND (o.DecimalLongitude BETWEEN '.$lng1.' AND '.$lng2.')'; if($tidFilter){ $sql3 .= 'and e.parentTid = '.$tidFilter; } - //echo $sql3; exit; if(!$this->conn->query($sql3)){ - + $this->errorMessage = 'ERROR adding taxa to checklist: '.$this->conn->error; + echo $this->errorMessage; } } + else { + $this->errorMessage = 'ERROR building checklist: '.$this->conn->connect_error; + echo $this->errorMessage; + } return $dynPK; } - + + public function getTid($sciname){ + $tid = 0; + $sql = 'SELECT tid FROM taxa WHERE sciname = "'.$this->cleanInStr($sciname).'"'; + $rs = $this->conn->query($sql); + if($r = $rs->fetch_object()){ + $tid = $r->tid; + } + $rs->free(); + return $tid; + } + public function removeOldChecklists(){ //Remove any old checklists $sql1 = 'DELETE dcl.* '. @@ -122,6 +130,6 @@ public function removeOldChecklists(){ $this->conn->query($sql1); $sql2 = 'DELETE FROM fmdynamicchecklists WHERE expiration < NOW()'; $this->conn->query($sql2); - } + } } ?> \ No newline at end of file