<?php

include_once '../../config/symbini.php';
include_once $serverRoot . '/classes/ImageCleaner.php';
header("Content-Type: text/html; charset=" . $charset);
if (!$SYMB_UID) {
    header('Location: ../../profile/index.php?refurl=../imagelib/admin/thumbnailbuilder.php?' . $_SERVER['QUERY_STRING']);
}
$action = array_key_exists("action", $_REQUEST) ? $_REQUEST["action"] : "";
$collid = array_key_exists("collid", $_REQUEST) ? $_REQUEST["collid"] : "";
$isEditor = false;
if ($IS_ADMIN) {
    $isEditor = true;
}
$imgManager = new ImageCleaner();
?>
<html>
<head>
<title><?php 
echo $defaultTitle;
?>
 Thumbnail Builder</title>
	<link href="../../css/base.css?<?php 
echo $CSS_VERSION;
?>
" type="text/css" rel="stylesheet" />
	<link href="../../css/main.css?<?php 
echo $CSS_VERSION;
?>
" type="text/css" rel="stylesheet" />
</head>
include_once $SERVER_ROOT . '/classes/ImageCleaner.php';
header("Content-Type: text/html; charset=" . $CHARSET);
if (!$SYMB_UID) {
    header('Location: ../../profile/index.php?refurl=../imagelib/admin/thumbnailbuilder.php?' . $_SERVER['QUERY_STRING']);
}
$action = array_key_exists("action", $_REQUEST) ? $_REQUEST["action"] : "";
$collid = array_key_exists("collid", $_REQUEST) ? $_REQUEST["collid"] : "";
$isEditor = false;
if ($IS_ADMIN) {
    $isEditor = true;
} elseif ($collid) {
    if (array_key_exists("CollAdmin", $USER_RIGHTS) && in_array($collid, $USER_RIGHTS["CollAdmin"])) {
        $isEditor = true;
    }
}
$imgManager = new ImageCleaner();
?>
<html>
<head>
<title><?php 
echo $DEFAULT_TITLE;
?>
 Thumbnail Builder</title>
	<link href="../../css/base.css?<?php 
echo $CSS_VERSION;
?>
" type="text/css" rel="stylesheet" />
	<link href="../../css/main.css?<?php 
echo $CSS_VERSION;
?>
" type="text/css" rel="stylesheet" />
 protected function finalCleanup()
 {
     $this->outputMsg('<li>Transfer process complete</li>');
     //Update uploaddate
     $sql = 'UPDATE omcollectionstats SET uploaddate = CURDATE() WHERE collid = ' . $this->collId;
     $this->conn->query($sql);
     //Remove records from occurrence temp table (uploadspectemp)
     $sql = 'DELETE FROM uploadspectemp WHERE (collid = ' . $this->collId . ') OR (initialtimestamp < DATE_SUB(CURDATE(),INTERVAL 3 DAY))';
     $this->conn->query($sql);
     //Optimize table to reset indexes
     $this->conn->query('OPTIMIZE TABLE uploadspectemp');
     //Remove records from determination temp table (uploaddetermtemp)
     $sql = 'DELETE FROM uploaddetermtemp WHERE (collid = ' . $this->collId . ') OR (initialtimestamp < DATE_SUB(CURDATE(),INTERVAL 3 DAY))';
     $this->conn->query($sql);
     //Optimize table to reset indexes
     $this->conn->query('OPTIMIZE TABLE uploaddetermtemp');
     //Remove records from image temp table (uploadimagetemp)
     $sql = 'DELETE FROM uploadimagetemp WHERE (collid = ' . $this->collId . ') OR (initialtimestamp < DATE_SUB(CURDATE(),INTERVAL 3 DAY))';
     $this->conn->query($sql);
     //Optimize table to reset indexes
     $this->conn->query('OPTIMIZE TABLE uploadimagetemp');
     //Do some more cleaning of the data after it haas been indexed in the omoccurrences table
     $occurUtil = new OccurrenceUtilities();
     $this->outputMsg('<li>Cleaning house</li>');
     ob_flush();
     flush();
     if (!$occurUtil->generalOccurrenceCleaning($this->collId)) {
         $errorArr = $occurUtil->getErrorArr();
         foreach ($errorArr as $errorStr) {
             echo '<li style="margin-left:20px;">' . $errorStr . '</li>';
         }
     }
     $this->outputMsg('<li style="margin-left:10px;">Protecting sensitive species...</li>');
     ob_flush();
     flush();
     if (!$occurUtil->protectRareSpecies($this->collId)) {
         $errorArr = $occurUtil->getErrorArr();
         foreach ($errorArr as $errorStr) {
             echo '<li style="margin-left:20px;">' . $errorStr . '</li>';
         }
     }
     $this->outputMsg('<li style="margin-left:10px;">Updating statistics...</li>');
     ob_flush();
     flush();
     if (!$occurUtil->updateCollectionStats($this->collId)) {
         $errorArr = $occurUtil->getErrorArr();
         foreach ($errorArr as $errorStr) {
             echo '<li style="margin-left:20px;">' . $errorStr . '</li>';
         }
     }
     /*
     $this->outputMsg('<li style="margin-left:10px;">Searching for duplicate Catalog Numbers... ');
     ob_flush();
     flush();
     $sql = 'SELECT catalognumber FROM omoccurrences GROUP BY catalognumber, collid '.
     	'HAVING Count(*)>1 AND collid = '.$this->collId.' AND catalognumber IS NOT NULL';
     $rs = $this->conn->query($sql);
     if($rs->num_rows){
     	$this->outputMsg('<span style="color:red;">Duplicate Catalog Numbers exist</span></li>');
     	$this->outputMsg('<li style="margin-left:10px;">');
     	$this->outputMsg('Open <a href="../cleaning/occurrencecleaner.php?collid='.$this->collId.'&action=listdupscatalog" target="_blank">Occurrence Cleaner</a> to resolve this issue');
     	$this->outputMsg('</li>');
     }
     else{
     	$this->outputMsg('All good!</li>');
     }
     $rs->free();
     */
     $this->outputMsg('<li style="margin-left:10px;">Populating global unique identifiers (GUIDs) for all records... </li>');
     ob_flush();
     flush();
     $uuidManager = new UuidFactory();
     $uuidManager->setSilent(1);
     $uuidManager->populateGuids();
     if ($this->imageTransferCount) {
         $this->outputMsg('<li style="margin-left:10px;">Building thumbnails for ' . $this->imageTransferCount . ' specimen images... </li>');
         ob_flush();
         flush();
         //Clean and populate null basic url and thumbnailurl fields
         $imgManager = new ImageCleaner();
         $imgManager->setVerbose(0);
         $imgManager->buildThumbnailImages($this->collId);
     }
 }