silverstripe-googlesitemaps/src/Control/GoogleSitemapController.php

109 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2017-10-19 22:05:18 +02:00
namespace Wilr\GoogleSitemaps\Control;
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;
2017-10-19 22:05:18 +02:00
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use Wilr\GoogleSitemaps\GoogleSitemap;
use SilverStripe\View\ArrayData;
2016-11-02 12:39:25 +01:00
/**
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
*/
2017-10-19 22:05:18 +02:00
private static $allowed_actions = [
2015-12-17 19:09:19 +01:00
'index',
'sitemap'
2017-10-19 22:05:18 +02:00
];
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
2017-10-19 22:05:18 +02:00
return $this->customise(new ArrayData([
2015-12-17 19:09:19 +01:00
'Sitemaps' => $sitemaps
2017-10-19 22:05:18 +02:00
]))->renderWith(__CLASS__);
2015-12-17 19:09:19 +01:00
} 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');
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
);
}
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);
}
2017-10-19 22:05:18 +02:00
public function StylesheetIndexPath()
{
return ModuleResourceLoader::resourceURL('wilr/silverstripe-googlesitemaps:xsl/xml-sitemapindex.xsl');
}
public function StylesheetPath()
{
return ModuleResourceLoader::resourceURL('wilr/silverstripe-googlesitemaps:xsl/xml-sitemap.xsl');
}
}