Skip to content

Commit

Permalink
Dynamic Checklist Generator dev
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
egbot committed Feb 10, 2021
1 parent 323ec4a commit 809cd1e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
28 changes: 14 additions & 14 deletions checklists/dynamicchecklist.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
//error_reporting(E_ALL);
include_once('../config/symbini.php');
include_once($SERVER_ROOT.'/classes/DynamicChecklistManager.php');
header("Content-Type: text/html; charset=".$CHARSET);
Expand All @@ -9,6 +8,7 @@
$radius = $_POST['radius'];
$radiusUnits = $_POST['radiusunits'];
$dynamicRadius = (isset($DYN_CHECKLIST_RADIUS)?$DYN_CHECKLIST_RADIUS:10);
$taxa = $_POST['taxa'];
$tid = $_POST['tid'];
$interface = $_POST['interface'];

Expand All @@ -18,24 +18,24 @@
if(!is_numeric($radius)) $radius = 0;
if($radiusUnits != 'mi') $radiusUnits == 'km';
if(!is_numeric($dynamicRadius)) $dynamicRadius = 10;
$taxa = filter_var($taxa,FILTER_SANITIZE_STRING);
if(!is_numeric($tid)) $tid = 0;

$dynClManager = new DynamicChecklistManager();

if(is_numeric($radius)){
$dynClid = $dynClManager->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();
?>
60 changes: 34 additions & 26 deletions classes/DynamicChecklistManager.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
<?php
include_once($SERVER_ROOT.'/config/dbconnection.php');
include_once($SERVER_ROOT.'/classes/Manager.php');

class DynamicChecklistManager {

private $conn;
class DynamicChecklistManager extends Manager {

public function __construct(){
$this->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
Expand All @@ -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;
Expand All @@ -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.')';
Expand All @@ -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) '.
Expand All @@ -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.* '.
Expand All @@ -122,6 +130,6 @@ public function removeOldChecklists(){
$this->conn->query($sql1);
$sql2 = 'DELETE FROM fmdynamicchecklists WHERE expiration < NOW()';
$this->conn->query($sql2);
}
}
}
?>

0 comments on commit 809cd1e

Please sign in to comment.