diff options
Diffstat (limited to 'modules-available/news/api.inc.php')
-rw-r--r-- | modules-available/news/api.inc.php | 65 |
1 files changed, 50 insertions, 15 deletions
diff --git a/modules-available/news/api.inc.php b/modules-available/news/api.inc.php index 851f31a8..3b56c70d 100644 --- a/modules-available/news/api.inc.php +++ b/modules-available/news/api.inc.php @@ -4,23 +4,58 @@ header('Content-Type: application/xml; charset=utf-8'); $type = Request::any('type', 'news', 'string'); +if (Module::isAvailable('locations')) { + $locationId = Request::any('location', 0, 'int'); + if ($locationId === 0) { + $locationId = Location::getFromIp($_SERVER['REMOTE_ADDR']); + } + $locations = Location::getLocationRootChain($locationId); + $locations[] = 0; +} else { + $locations = [0]; +} + // Fetch news from DB -$row = Database::queryFirst('SELECT title, content, dateline FROM vmchooser_pages' - . ' WHERE type = :type AND expires > UNIX_TIMESTAMP() ORDER BY dateline DESC LIMIT 1', compact('type')); -if ($row !== false ) { +$res = Database::simpleQuery('SELECT title, locationid, content, dateline FROM vmchooser_pages + WHERE type = :type AND (locationid IS NULL OR locationid IN (:lids)) + AND expires > UNIX_TIMESTAMP() ORDER BY dateline DESC', [ + 'type' => $type, + 'lids' => $locations, + ]); - echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; - echo "<news>" . "\n"; - echo "\t" . '<headline>' . "\n"; - echo "\t\t" . htmlspecialchars($row['title']) . "\n"; - echo "\t" . '</headline>' . "\n"; - echo "\t" . "<info>" . "\n"; - echo "\t\t" . htmlspecialchars($row['content']) . "\n"; - echo "\t" . '</info>' . "\n"; - echo "\t" . "<date>" . "\n"; - echo "\t\t" . $row['dateline'] . "\n"; - echo "\t" . "</date>" . "\n"; - echo "</news>"; +// Get one for each location. As we sort by dateline and check expiry in the query, we only +// need one per location and then pick the first one that is set, as the locations are ordered +// by closest to furthest +$locations = array_flip($locations); +foreach ($res as $row) { + $lid = (int)$row['locationid']; + if (is_array($locations[$lid])) + continue; // Already have one + $locations[$lid] = $row; +} +// Pick first one +foreach ($locations as $row) { + if (is_array($row)) + break; +} + +if (is_array($row)) { + $title = htmlspecialchars($row['title']); + $content = htmlspecialchars($row['content']); + echo <<<EOF +<?xml version="1.0" encoding="UTF-8"?> +<news> + <headline> + $title + </headline> + <info> + $content + </info> + <date> + {$row['dateline']} + </date> +</news> +EOF; } else { // no news in DB, output a 'null' news xml |