Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populated individual Date fields #1658

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions classes/OccurrenceEditorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function __construct($conn = null){
$this->fieldArr['omoccurrences'] = array('basisofrecord' => 's', 'catalognumber' => 's', 'othercatalognumbers' => 's', 'occurrenceid' => 's', 'ownerinstitutioncode' => 's',
'institutioncode' => 's', 'collectioncode' => 's', 'eventid' => 's',
'family' => 's', 'sciname' => 's', 'tidinterpreted' => 'n', 'scientificnameauthorship' => 's', 'identifiedby' => 's', 'dateidentified' => 's',
'identificationreferences' => 's', 'identificationremarks' => 's', 'taxonremarks' => 's', 'identificationqualifier' => 's', 'typestatus' => 's',
'recordedby' => 's', 'recordnumber' => 's', 'associatedcollectors' => 's', 'eventdate' => 'd', 'eventdate2' => 'd',
'identificationreferences' => 's', 'identificationremarks' => 's', 'taxonremarks' => 's', 'identificationqualifier' => 's', 'typestatus' => 's', 'recordedby' => 's', 'recordnumber' => 's',
'associatedcollectors' => 's', 'eventdate' => 'd', 'eventdate2' => 'd', 'year' => 'n', 'month' => 'n', 'day' => 'n', 'startdayofyear' => 'n', 'enddayofyear' => 'n',
'verbatimeventdate' => 's', 'habitat' => 's', 'substrate' => 's', 'fieldnumber' => 's', 'occurrenceremarks' => 's', 'datageneralizations' => 's',
'associatedtaxa' => 's', 'verbatimattributes' => 's', 'behavior' => 's', 'vitality' => 's', 'dynamicproperties' => 's', 'reproductivecondition' => 's', 'cultivationstatus' => 's', 'establishmentmeans' => 's',
'lifestage' => 's', 'sex' => 's', 'individualcount' => 's', 'samplingprotocol' => 's', 'preparations' => 's',
Expand Down Expand Up @@ -980,6 +980,7 @@ public function editOccurrence($postArr, $editorStatus){
//Edit record only if user is authorized to autoCommit
if($autoCommit){
$status = $LANG['SUCCESS_EDITS_SUBMITTED'].' ';
$postArr = array_merge($postArr, $this->getDatefields($postArr));
$sql = '';
//Apply autoprocessing status if set
if(array_key_exists('autoprocessingstatus',$postArr) && $postArr['autoprocessingstatus']){
Expand Down Expand Up @@ -1017,7 +1018,7 @@ public function editOccurrence($postArr, $editorStatus){
$this->conn->query($sqlHost);
}
//Update occurrence record
$sql = 'UPDATE omoccurrences SET '.substr($sql,1).' WHERE (occid = '.$this->occid.')';
$sql = 'UPDATE IGNORE omoccurrences SET '.substr($sql,1).' WHERE (occid = '.$this->occid.')';
if($this->conn->query($sql)){
if(strtolower($postArr['processingstatus']) != 'unprocessed'){
//UPDATE uid within omcrowdsourcequeue, only if not yet processed
Expand Down Expand Up @@ -1256,8 +1257,9 @@ public function addOccurrence($postArr){
global $LANG;
$status = $LANG['SUCCESS_NEW_OCC_SUBMITTED'];
if($postArr){
$postArr = array_merge($postArr, $this->getDatefields($postArr));
$guid = UuidFactory::getUuidV4();
$sql = 'INSERT INTO omoccurrences(collid, recordID, '.implode(',',array_keys($this->fieldArr['omoccurrences'])).') VALUES ('.$postArr['collid'].', "'.$guid.'"';
$sql = 'INSERT IGNORE INTO omoccurrences(collid, recordID, '.implode(',',array_keys($this->fieldArr['omoccurrences'])).') VALUES ('.$postArr['collid'].', "'.$guid.'"';
//if(array_key_exists('cultivationstatus',$postArr) && $postArr['cultivationstatus']) $postArr['cultivationstatus'] = $postArr['cultivationstatus'];
//if(array_key_exists('localitysecurity',$postArr) && $postArr['localitysecurity']) $postArr['localitysecurity'] = $postArr['localitysecurity'];
if(!isset($postArr['dateentered']) || !$postArr['dateentered']) $postArr['dateentered'] = date('Y-m-d H:i:s');
Expand Down Expand Up @@ -1393,6 +1395,37 @@ private function updateIdentifiers($identArr, $existingIdentArr = null){
}
}

private function getDatefields($occurrenceArr){
$dateArr = array();
if(isset($occurrenceArr['eventdate'])){
$dateArr['year'] = '';
$dateArr['month'] = '';
$dateArr['day'] = '';
$dateArr['startdayofyear'] = '';
$dateArr['enddayofyear'] = '';
if(preg_match('/(\d{4})-(\d{2})-(\d{2})/', $occurrenceArr['eventdate'], $m)){
if(!empty((int) $m[1])) $dateArr['year'] = (int) $m[1];
if(!empty((int) $m[2])) $dateArr['month'] = (int) $m[2];
if(!empty((int) $m[3])) $dateArr['day'] = (int) $m[3];
}
if(!empty((int)$dateArr['day'])){
if($dayOfYear = date('z', strtotime($occurrenceArr['eventdate']))){
$dayOfYear++;
$dateArr['startdayofyear'] = $dayOfYear;
$endDayOfYear = $dayOfYear;
if(!empty($occurrenceArr['eventdate2'])){
if($dayOfYear = date('z', strtotime($occurrenceArr['eventdate2']))){
$dayOfYear++;
$endDayOfYear = $dayOfYear;
}
}
$dateArr['enddayofyear'] = $endDayOfYear;
}
}
}
return $dateArr;
}

public function deleteOccurrence($delOccid){
global $CHARSET, $USER_DISPLAY_NAME, $LANG;

Expand Down
65 changes: 65 additions & 0 deletions classes/OccurrenceMaintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,52 @@ public function generalOccurrenceCleaning(){
$rs->free();
if(isset($occidArr)) $this->batchUpdateAuthor($occidArr);

//Update date fields - first look for bad year
$occidArr = array();
$this->outputMsg('Updating individual date fields (e.g. day, month, year, startDayOfYear, endDayOfYear)... ',1);
$sql = 'SELECT occid FROM omoccurrences WHERE eventDate BETWEEN "1500-01-01" AND CURDATE() AND (year IS NULL OR year != YEAR(eventDate)) ';
if($this->collidStr) $sql .= 'AND collid IN('.$this->collidStr.')';
$rs = $this->conn->query($sql);
while($r = $rs->fetch_object()){
$occidArr[] = $r->occid;
if(count($occidArr) > 1000){
$this->batchUpdateDateFields($occidArr);
unset($occidArr);
}
}
$rs->free();
if(isset($occidArr)) $this->batchUpdateDateFields($occidArr);

//Then look for records with bad month
$occidArr = array();
$sql = 'SELECT occid FROM omoccurrences WHERE eventDate BETWEEN "1500-01-01" AND CURDATE() AND (month IS NULL OR month != MONTH(eventDate)) ';
if($this->collidStr) $sql .= 'AND collid IN('.$this->collidStr.')';
$rs = $this->conn->query($sql);
while($r = $rs->fetch_object()){
$occidArr[] = $r->occid;
if(count($occidArr) > 1000){
$this->batchUpdateDateFields($occidArr);
unset($occidArr);
}
}
$rs->free();
if(isset($occidArr)) $this->batchUpdateDateFields($occidArr);

//Then look for records with bad day
$occidArr = array();
$sql = 'SELECT occid FROM omoccurrences WHERE eventDate BETWEEN "1500-01-01" AND CURDATE() AND (day IS NULL OR day != DAY(eventDate)) ';
if($this->collidStr) $sql .= 'AND collid IN('.$this->collidStr.')';
$rs = $this->conn->query($sql);
while($r = $rs->fetch_object()){
$occidArr[] = $r->occid;
if(count($occidArr) > 1000){
$this->batchUpdateDateFields($occidArr);
unset($occidArr);
}
}
$rs->free();
if(isset($occidArr)) $this->batchUpdateDateFields($occidArr);

return $status;
}

Expand Down Expand Up @@ -276,6 +322,25 @@ private function batchUpdateAuthor($occidArr){
return $status;
}

private function batchUpdateDateFields($occidArr){
$status = false;
if($occidArr){
//Update all date fields, no matter which date field was tested as bad
$sql = 'UPDATE omoccurrences
SET year = YEAR(eventDate), month = MONTH(eventDate), day = day(eventDate), startDayOfYear = DAYOFYEAR(eventDate), endDayOfYear = DAYOFYEAR(IFNULL(eventDate2,eventDate))
WHERE occid IN(' . implode(',', $occidArr) . ')';
if($this->conn->query($sql)){
$status = true;
}
else{
$this->errorArr[] = 'WARNING: unable to update date fields; '.$this->conn->error;
$this->outputMsg($this->errorArr,2);
$status = false;
}
}
return $status;
}

public function batchUpdateGeoreferenceIndex(){
$status = false;
$this->outputMsg('Updating georeference index... ',1);
Expand Down