$lon = (double) $wpt['lon'];
$type = (string) $cache->type;
$typeId = DB::queryFirstField("SELECT id FROM type WHERE type=%s LIMIT 1", $type);
$difficulty = (int) $cache->difficulty;
$terrain = (int) $cache->terrain;
$country = (string) $cache->country;
$url = (string) $wpt->url;
foreach ($cache->logs->log as $log) {
$gc = (string) $wpt->name;
$name = (string) $cache->name;
$username = (string) $log->finder;
$logtext = (string) $log->text;
$logType = (string) $log->type;
$logDate = substr((string) $log->date, 0, 10);
if ($inserted == 0) {
DB::insertIgnore('user', array('username' => $username));
$inserted = 1;
$userId = DB::queryFirstField("SELECT id FROM user WHERE username=%s LIMIT 1", $username);
$logIds = DB::queryFirstColumn("SELECT log.id FROM image, log WHERE image.log = log.id AND log.user = %i", $userId);
foreach ($logIds as $logId) {
DB::delete('image', "log=%i", $logId);
}
DB::delete('log', "user=%i", $userId);
}
DB::insertUpdate('geocache', array('gc' => $gc, 'name' => $name, 'type' => $typeId, 'lat' => $lat, 'lon' => $lon, 'difficulty' => $difficulty, 'terrain' => $terrain, 'country' => $country, 'url' => $url));
$geocacheId = DB::queryFirstField("SELECT id FROM geocache WHERE gc = %s LIMIT 1", $gc);
$logTypeId = DB::queryFirstField("SELECT id FROM logtype WHERE type = %s LIMIT 1", $logType);
DB::insert('log', array('user' => $userId, 'geocache' => $geocacheId, 'created' => $logDate, 'type' => $logTypeId, 'log' => $logtext));
}
$finds++;
}