2023-02-09 05:55:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories\Db;
|
|
|
|
|
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
|
|
|
trait TimelineWritePlaces
|
|
|
|
{
|
|
|
|
protected IDBConnection $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add places data for a file.
|
2023-03-08 19:32:57 +00:00
|
|
|
*
|
|
|
|
* @param int $fileId The file ID
|
|
|
|
* @param null|float $lat The latitude of the file
|
|
|
|
* @param null|float $lon The longitude of the file
|
2023-02-09 05:55:12 +00:00
|
|
|
*/
|
2023-03-08 19:32:57 +00:00
|
|
|
protected function updatePlacesData(int $fileId, $lat, $lon): void
|
2023-02-09 05:55:12 +00:00
|
|
|
{
|
|
|
|
// Get GIS type
|
|
|
|
$gisType = \OCA\Memories\Util::placesGISType();
|
|
|
|
|
2023-03-08 19:32:57 +00:00
|
|
|
// Check if valid
|
2023-03-08 19:48:36 +00:00
|
|
|
if ($gisType <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-08 19:32:57 +00:00
|
|
|
|
|
|
|
// Delete previous records
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete('memories_places')
|
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
|
|
|
|
;
|
|
|
|
$query->executeStatement();
|
|
|
|
|
|
|
|
// Just remove from if the point is no longer valid
|
|
|
|
if (null === $lat || null === $lon) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-09 05:55:12 +00:00
|
|
|
// Construct WHERE clause depending on GIS type
|
|
|
|
$where = null;
|
|
|
|
if (1 === $gisType) {
|
|
|
|
$where = "ST_Contains(geometry, ST_GeomFromText('POINT({$lon} {$lat})'))";
|
|
|
|
} elseif (2 === $gisType) {
|
|
|
|
$where = "POINT('{$lon},{$lat}') <@ geometry";
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make query to memories_planet table
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select($query->createFunction('DISTINCT(osm_id)'))
|
|
|
|
->from('memories_planet_geometry')
|
|
|
|
->where($query->createFunction($where))
|
|
|
|
;
|
|
|
|
|
|
|
|
// Cancel out inner rings
|
|
|
|
$query->groupBy('poly_id', 'osm_id');
|
|
|
|
$query->having($query->createFunction('SUM(type_id) > 0'));
|
|
|
|
|
|
|
|
// memories_planet_geometry has no *PREFIX*
|
|
|
|
$sql = str_replace('*PREFIX*memories_planet_geometry', 'memories_planet_geometry', $query->getSQL());
|
|
|
|
|
|
|
|
// Run query
|
2023-03-24 01:12:39 +00:00
|
|
|
$rows = $this->connection->executeQuery($sql)->fetchAll();
|
|
|
|
|
|
|
|
// Insert records in transaction
|
|
|
|
$this->connection->beginTransaction();
|
2023-02-09 05:55:12 +00:00
|
|
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->insert('memories_places')
|
|
|
|
->values([
|
|
|
|
'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
|
|
|
|
'osm_id' => $query->createNamedParameter($row['osm_id'], IQueryBuilder::PARAM_INT),
|
|
|
|
])
|
|
|
|
;
|
|
|
|
$query->executeStatement();
|
|
|
|
}
|
2023-03-24 01:12:39 +00:00
|
|
|
|
|
|
|
$this->connection->commit();
|
2023-02-09 05:55:12 +00:00
|
|
|
}
|
|
|
|
}
|