silverstripe-googlesitemaps/code/controllers/GoogleSitemapController.php

80 lines
2.2 KiB
PHP
Raw Normal View History

<?php
/**
* Controller for displaying the sitemap.xml. The module displays an index
* sitemap at the sitemap.xml level, then outputs the individual objects
* at a second level.
*
* <code>
* http://site.com/sitemap.xml/
* http://site.com/sitemap.xml/sitemap/$ClassName-$Page.xml
* </code>
*
* @package googlesitemaps
*/
2015-12-17 19:09:19 +01:00
class GoogleSitemapController extends Controller
{
2015-12-17 19:09:19 +01:00
/**
* @var array
*/
private static $allowed_actions = array(
'index',
'sitemap'
);
2015-12-17 19:09:19 +01:00
/**
* Default controller action for the sitemap.xml file. Renders a index
* file containing a list of links to sub sitemaps containing the data.
*
* @return mixed
*/
public function index($url)
{
if (GoogleSitemap::enabled()) {
Config::inst()->update('SSViewer', 'set_source_file_comments', false);
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
2015-12-17 19:09:19 +01:00
$sitemaps = GoogleSitemap::inst()->getSitemaps();
$this->extend('updateGoogleSitemaps', $sitemaps);
2013-01-17 22:49:53 +01:00
2015-12-17 19:09:19 +01:00
return array(
'Sitemaps' => $sitemaps
);
} else {
return new SS_HTTPResponse('Page not found', 404);
}
}
2015-12-17 19:09:19 +01:00
/**
* Specific controller action for displaying a particular list of links
* for a class
*
* @return mixed
*/
public function sitemap()
{
$class = $this->request->param('ID');
$page = $this->request->param('OtherID');
2015-12-17 19:09:19 +01:00
if (GoogleSitemap::enabled() && $class && $page) {
Config::inst()->update('SSViewer', 'set_source_file_comments', false);
$this->getResponse()->addHeader('Content-Type', 'application/xml; charset="utf-8"');
$this->getResponse()->addHeader('X-Robots-Tag', 'noindex');
2015-12-17 19:09:19 +01:00
$items = GoogleSitemap::inst()->getItems($class, $page);
$this->extend('updateGoogleSitemapItems', $items, $class, $page);
2013-01-17 22:49:53 +01:00
2015-12-17 19:09:19 +01:00
return array(
'Items' => $items
);
} else {
return new SS_HTTPResponse('Page not found', 404);
}
}
}