silverstripe-googlesitemaps/code/controllers/GoogleSitemapController.php

90 lines
2.5 KiB
PHP
Raw Permalink Normal View History

<?php
2016-11-02 12:39:25 +01:00
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\HTTPResponse;
/**
2016-11-02 12:39:25 +01:00
* 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()) {
$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 {
2016-11-02 12:39:25 +01:00
return new HTTPResponse('Page not found', 404);
2015-12-17 19:09:19 +01:00
}
}
2015-12-17 19:09:19 +01:00
/**
2016-11-02 12:39:25 +01:00
* Specific controller action for displaying a particular list of links
2015-12-17 19:09:19 +01:00
* for a class
2016-11-02 12:39:25 +01:00
*
2015-12-17 19:09:19 +01:00
* @return mixed
*/
public function sitemap()
{
$class = $this->unsanitiseClassName($this->request->param('ID'));
2015-12-17 19:09:19 +01:00
$page = $this->request->param('OtherID');
2016-11-02 12:39:25 +01:00
if (GoogleSitemap::enabled() && $class && $page && ($class == SiteTree::class || $class == 'GoogleSitemapRoute' || GoogleSitemap::is_registered($class))) {
2015-12-17 19:09:19 +01:00
$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 {
2016-11-02 12:39:25 +01:00
return new HTTPResponse('Page not found', 404);
2015-12-17 19:09:19 +01:00
}
}
/**
* Unsanitise a namespaced class' name from a URL param
* @return string
*/
protected function unsanitiseClassName($class)
{
return str_replace('-', '\\', $class);
}
}