additional_plugins/serendipity_event_geo_json/serendipity_event_geo_json.php

116 lines
4 KiB
PHP
Raw Normal View History

2020-01-31 06:12:07 +01:00
<?php
2020-02-04 16:33:58 +01:00
if (IN_serendipity !== true) {
die ("Don't hack!");
}
2020-01-31 06:12:07 +01:00
2020-02-04 16:33:58 +01:00
@serendipity_plugin_api::load_language(dirname(__FILE__));
include dirname(__FILE__) . '/plugin_version.inc.php';
2020-01-31 06:12:07 +01:00
2020-02-04 16:33:58 +01:00
class serendipity_event_geo_json extends serendipity_event
{
function introspect(&$propbag)
{
$propbag->add('name', PLUGIN_EVENT_GEO_JSON_NAME);
$propbag->add('description', PLUGIN_EVENT_GEO_JSON_DESCRIPTION);
$propbag->add('copyright', 'GPL');
$propbag->add('event_hooks', array('frontend_header' => true));
2020-09-16 11:24:03 +02:00
$propbag->add('author', PLUGIN_EVENT_GEO_JSON_AUTHOR);
$propbag->add('version', PLUGIN_EVENT_GEO_JSON_VERSION);
2020-02-04 16:33:58 +01:00
$propbag->add('requirements', array('serendipity' => '2.3'));
$propbag->add('stackable', false);
$propbag->add('groups', array('FRONTEND_FEATURES'));
}
2020-01-31 06:12:07 +01:00
2020-02-04 16:33:58 +01:00
function generate_content(&$title)
{
$title = PLUGIN_EVENT_GEO_JSON_NAME;
}
2020-01-31 06:12:07 +01:00
2020-02-05 06:46:52 +01:00
function simple_query($sql) {
$rows = serendipity_db_query($sql, false, 'assoc');
return is_array($rows) ? $rows : [];
}
2020-01-31 06:12:07 +01:00
2020-02-05 06:46:52 +01:00
function get_entries() {
global $serendipity;
$entries = [];
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
2020-02-04 17:09:05 +01:00
FROM {$serendipity['dbPrefix']}entries e
2020-02-05 06:46:52 +01:00
JOIN {$serendipity['dbPrefix']}entryproperties eplat ON (eplat.entryid = e.id AND eplat.property = 'geo_lat')
2020-03-11 03:49:08 +01:00
JOIN {$serendipity['dbPrefix']}entryproperties eplng ON (eplng.entryid = e.id AND eplng.property = 'geo_long')
2020-02-05 06:46:52 +01:00
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
2020-03-13 01:42:14 +01:00
WHERE e.isdraft = 'false'
2020-03-11 02:04:35 +01:00
ORDER BY e.timestamp",
2020-02-05 06:46:52 +01:00
false, 'assoc'
) as $row) {
$entries[$row['id']] = [
'title' => $row['title'],
2020-03-13 01:42:14 +01:00
'url' => serendipity_db_bool($serendipity['showFutureEntries']) || $row['timestamp'] <= serendipity_db_time()
? $serendipity['serendipityHTTPPath'].$row['permalink']
: null,
2020-09-16 11:24:03 +02:00
'date' => intval($row['timestamp']),
'size' => intval($row['size']),
2020-02-05 06:46:52 +01:00
'author' => $row['realname'],
2020-09-16 11:24:03 +02:00
'pos' => [floatval($row['lat']), floatval($row['lng'])],
2020-02-05 06:46:52 +01:00
'categories' => []
];
2020-02-04 17:09:05 +01:00
}
2020-02-05 06:46:52 +01:00
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')
2020-03-13 01:42:14 +01:00
WHERE e.isdraft = 'false'"
2020-02-05 06:46:52 +01:00
) as $row) {
2020-09-16 11:24:03 +02:00
$entries[$row['entryid']]['categories'][] = intval($row['categoryid']);
2020-02-05 06:46:52 +01:00
}
return array_values($entries);
}
2020-02-27 01:25:13 +01:00
2020-02-05 06:46:52 +01:00
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'],
2020-09-16 11:24:03 +02:00
'date' => intval($row['date']),
'size' => intval($row['size'])
2020-02-05 06:46:52 +01:00
];
}, $this->simple_query(
2020-02-04 17:09:05 +01:00
"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'
2020-02-05 06:46:52 +01:00
ORDER BY i.path, i.realname",
false, 'assoc'
));
}
2020-01-31 06:12:07 +01:00
2020-02-05 06:46:52 +01:00
function get_geo_json()
{
return json_encode([
'entries' => $this->get_entries(),
'uploads' => $this->get_uploads()
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
2020-02-04 17:09:05 +01:00
}
function event_hook($event, &$bag, &$eventData, $addData = null)
{
if ($event == 'frontend_header') {
2020-02-05 06:46:52 +01:00
echo ' <script>const geo = '.$this->get_geo_json().';</script>'.PHP_EOL;
2020-02-04 16:33:58 +01:00
}
}
2020-01-31 06:12:07 +01:00
2020-02-04 16:33:58 +01:00
function introspect_config_item($name, &$propbag)
{
return true;
}
}
2020-01-31 06:12:07 +01:00
?>