added category filter

This commit is contained in:
surrim 2020-02-05 06:46:52 +01:00
parent 1162e6b913
commit 6543378307
3 changed files with 83 additions and 68 deletions

View file

@ -25,68 +25,82 @@
$title = PLUGIN_EVENT_GEO_JSON_NAME;
}
function get_geo_json()
{
function simple_query($sql) {
$rows = serendipity_db_query($sql, false, 'assoc');
return is_array($rows) ? $rows : [];
}
function get_entries() {
global $serendipity;
$tt = serendipity_db_query(
"SELECT e.title, p.permalink, e.timestamp, LENGTH(e.body) AS size, a.realname, e2.value AS lat, e1.value AS lng
FROM {$serendipity['dbPrefix']}entries e
JOIN {$serendipity['dbPrefix']}entryproperties e1 ON (e1.entryid = e.id AND e1.property='geo_long')
JOIN {$serendipity['dbPrefix']}entryproperties e2 ON (e2.entryid = e.id AND e2.property='geo_lat')
LEFT JOIN {$serendipity['dbPrefix']}permalinks p ON (p.entry_id = e.id AND p.type='entry')
LEFT JOIN {$serendipity['dbPrefix']}authors a ON (a.authorid = e.authorid)
WHERE e.isdraft = 'false'
ORDER BY p.permalink"
);
$entries = [];
if (is_array($tt)) {
foreach ($tt as $t) {
$entries[] = [
'title' => $t['title'],
'url' => $serendipity['serendipityHTTPPath'].$t['permalink'],
'date' => (int)$t['timestamp'],
'size' => (int)$t['size'],
'author' => $t['realname'],
'pos' => [(double)$t['lat'], (double)$t['lng']]
];
}
foreach ($this->simple_query(
"SELECT e.id, e.title, p.permalink, e.timestamp, LENGTH(e.body) AS size, a.realname, eplat.value AS lat, eplng.value AS lng
FROM {$serendipity['dbPrefix']}entries e
JOIN {$serendipity['dbPrefix']}entryproperties eplng ON (eplng.entryid = e.id AND eplng.property = 'geo_long')
JOIN {$serendipity['dbPrefix']}entryproperties eplat ON (eplat.entryid = e.id AND eplat.property = 'geo_lat')
JOIN {$serendipity['dbPrefix']}permalinks p ON (p.entry_id = e.id AND p.type = 'entry')
JOIN {$serendipity['dbPrefix']}authors a ON a.authorid = e.authorid
WHERE e.isdraft = 'false'
ORDER BY p.permalink",
false, 'assoc'
) as $row) {
$entries[$row['id']] = [
'title' => $row['title'],
'url' => $serendipity['serendipityHTTPPath'].$row['permalink'],
'date' => $row['timestamp'],
'size' => $row['size'],
'author' => $row['realname'],
'pos' => [(double)$row['lat'], (double)$row['lng']],
'categories' => []
];
}
$tt = serendipity_db_query(
foreach ($this->simple_query(
"SELECT ec.entryid, ec.categoryid
FROM {$serendipity['dbPrefix']}entrycat ec
JOIN {$serendipity['dbPrefix']}entries e ON e.id = ec.entryid
JOIN {$serendipity['dbPrefix']}entryproperties eplat ON (eplat.entryid = ec.entryid AND eplat.property = 'geo_lat')
JOIN {$serendipity['dbPrefix']}entryproperties eplng ON (eplng.entryid = ec.entryid AND eplng.property = 'geo_long')
WHERE e.isdraft = 'false'"
) as $row) {
$entries[$row['entryid']]['categories'][] = $row['categoryid'];
}
return array_values($entries);
}
function get_uploads() {
global $serendipity;
return array_map(function($row) {
global $serendipity;
return [
'title' => $row['realname'],
'url' => $serendipity['serendipityHTTPPath'].$serendipity['uploadPath'].$row['path'].$row['realname'],
'date' => $row['date'],
'size' => $row['size']
];
}, $this->simple_query(
"SELECT i.realname, i.path, IFNULL(m.value, i.date) AS date, i.size
FROM {$serendipity['dbPrefix']}images i
LEFT JOIN {$serendipity['dbPrefix']}mediaproperties m ON (
m.mediaid = i.id AND m.property='DATE' AND m.property_group = 'base_property' AND property_subgroup = ''
)
WHERE i.extension = 'gpx'
ORDER BY i.path, i.realname"
);
ORDER BY i.path, i.realname",
false, 'assoc'
));
}
$uploads = [];
if (is_array($tt)) {
foreach ($tt as $t) {
$uploads[] = [
'title' => $t['realname'],
'url' => $serendipity['serendipityHTTPPath'].$serendipity['uploadPath'].$t['path'].$t['realname'],
'date' => (int)$t['date'],
'size' => (int)$t['size']
];
}
}
$object = [
'entries' => $entries,
'uploads' => $uploads
];
return json_encode($object, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
function get_geo_json()
{
return json_encode([
'entries' => $this->get_entries(),
'uploads' => $this->get_uploads()
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
function event_hook($event, &$bag, &$eventData, $addData = null)
{
if ($event == 'frontend_header') {
echo ' <script>const geo = '.$this->get_geo_json().';</script>'.PHP_EOL;
echo ' <script>const geo = '.$this->get_geo_json().';</script>'.PHP_EOL;
}
}

View file

@ -47,7 +47,7 @@
{
if ($event == 'entries_header') {
if (in_array($this->get_config('category_id'), $this->get_page_categories())) {
echo ' <div id="map" data-path="'.$this->get_config('path', '').'" data-latitude="'.$this->get_config('latitude', 51.48165).'" data-longitude="'.$this->get_config('longitude', 7.21648).'" data-zoom="'.$this->get_config('zoom', 15).'" style="height: '.$this->get_config('height', '463px').'"></div>'.PHP_EOL;
echo ' <div id="map" data-category="'.$this->get_config('category_id', '').'" data-path="'.$this->get_config('path', '').'" data-latitude="'.$this->get_config('latitude', 51.48165).'" data-longitude="'.$this->get_config('longitude', 7.21648).'" data-zoom="'.$this->get_config('zoom', 15).'" style="height: '.$this->get_config('height', '463px').'"></div>'.PHP_EOL;
echo ' <div id="popup" class="ol-popup"></div>'.PHP_EOL;
}
}

View file

@ -14,12 +14,14 @@ window.onload = () => {
element: popup
});
const features = [];
for (const [id, entry] of Object.entries(geo.entries)) {
const data = document.getElementById("map").dataset;
const entries = geo.entries.filter(x => x.categories.includes(data.category));
const uploads = geo.uploads.filter(x => x.url.startsWith(data.path));
const features = entries.map((entry, id) => {
const feature = new ol.Feature(new ol.geom.Point(ol.proj.fromLonLat(entry.pos.reverse())));
feature.setId(id);
features.push(feature);
}
return feature;
});
const osmSource = new ol.source.OSM();
const layers = [
@ -28,7 +30,7 @@ window.onload = () => {
source: new ol.source.Vector({features: features}),
style: feature => {
const id = feature.getId();
const entry = geo.entries[id];
const entry = entries[id];
const date = new Date(entry.date * 1000);
return new ol.style.Style({
@ -41,8 +43,7 @@ window.onload = () => {
zIndex: Infinity
})
];
const data = document.getElementById("map").dataset;
for (const upload of geo.uploads.filter(x => x.url.startsWith(data.path))) {
for (const upload of uploads) {
const layer = new ol.layer.Vector({
source: new ol.source.Vector({
url: upload.url,
@ -95,32 +96,32 @@ window.onload = () => {
return li;
};
const entries = [];
const uploads = [];
const foundEntries = [];
const foundUploads = [];
map.forEachFeatureAtPixel(event.pixel, (feature, layer) => {
const id = feature.getId();
if (id !== undefined) {
entries.push(id);
foundEntries.push(id);
} else {
const url = layer.getSource().getUrl();
const id = geo.uploads.findIndex(upload => upload.url === url);
uploads.push(id);
const id = uploads.findIndex(upload => upload.url === url);
foundUploads.push(id);
}
}, {hitTolerance: 10});
entries.sort((x, y) => x - y);
uploads.sort((x, y) => x - y);
foundEntries.sort((x, y) => x - y);
foundUploads.sort((x, y) => x - y);
if (entries.length || uploads.length) {
if (foundEntries.length || foundUploads.length) {
const initUl = title => {
const ul = document.createElement("ul");
ul.setAttribute("data-title", title);
return ul;
};
const ulEntries = entries.map(x => makeItem(geo.entries[x])).reduce((x, y) => {x.appendChild(y); return x;}, initUl("Blogs"));
const ulUploads = uploads.map(x => makeItem(geo.uploads[x])).reduce((x, y) => {x.appendChild(y); return x;}, initUl("Downloads"));
popup.innerHTML = (entries.length ? ulEntries.outerHTML : '') + (uploads.length ? ulUploads.outerHTML : '');
overlay.setPosition(entries.length ? ol.proj.fromLonLat(
[0, 1].map(latLon => entries.map(x => geo.entries[x].pos[latLon]).reduce((x, y) => x + y, 0) / entries.length)
const ulEntries = foundEntries.map(x => makeItem(entries[x])).reduce((x, y) => {x.appendChild(y); return x;}, initUl("Blogs"));
const ulUploads = foundUploads.map(x => makeItem(uploads[x])).reduce((x, y) => {x.appendChild(y); return x;}, initUl("Downloads"));
popup.innerHTML = (foundEntries.length ? ulEntries.outerHTML : '') + (foundUploads.length ? ulUploads.outerHTML : '');
overlay.setPosition(foundEntries.length ? ol.proj.fromLonLat(
[0, 1].map(latLon => foundEntries.map(x => entries[x].pos[latLon]).reduce((x, y) => x + y, 0) / foundEntries.length)
) : event.coordinate);
} else {
overlay.setPosition(undefined);