Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from Marketo/get-child-mod
Browse files Browse the repository at this point in the history
Getting to child pages from Sitetree for sitemap
  • Loading branch information
solo800 authored Nov 8, 2017
2 parents 1716d73 + 8d6dd55 commit 9c13ef7
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 77 deletions.
82 changes: 41 additions & 41 deletions code/Pages/SiteMapPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,63 @@ class SiteMapPage_Controller extends Page_Controller
*/
public function index()
{
$results = array();
$results = [];

$page = $this->data();
$sitemap = $this->getHierarchicalSitemapHTML();

return array('Content' => str_replace("<?xml version=\"1.0\"?>\n", '', $sitemap));
return ['Content' => str_replace("<?xml version=\"1.0\"?>\n", '', $sitemap)];

}

public function getHierarchicalSitemapHTML() {
$all = array();
$allids = array();
$final = array();

$page = $this->data();
public function getHierarchicalSitemapHTML()
{
$all = [];
$allids = [];
$final = [];

$page = $this->data();
$siteData = $page->siteData ? $page->siteData : singleton('SiteDataService');
$items = $siteData->getItems();
// get just the top level ones; ie parentID = 0;
$topLevel = array();
foreach ($items as $item) {
if ($item->ParentID == 0 && $item->ShowInMenus) {
$topLevel[] = $item;
}
}
$sitemapXML = new SimpleXMLElement('<div></div>');

$items = $siteData->getItems();

// get just the top level ones; ie parentID = 0;

$topLevel = [];
foreach ($items as $item) {
if ($item->ParentID == 0 && $item->ShowInMenus) {
$topLevel[] = $item;
}
}

$sitemapXML = new SimpleXMLElement('<div></div>');
$sitemapXML->addAttribute('id', 'SitemapList');
foreach ($topLevel as $item) {
$sitemapXML = $this->processPageToHierarchicalHTML($sitemapXML, $item);
}
return $sitemapXML->asXML();

foreach ($topLevel as $item) {
$sitemapXML = $this->processPageToHierarchicalHTML($sitemapXML, $item);
}

return $sitemapXML->asXML();
}

protected $processed = array();
protected $processed = [];

protected function processPageToHierarchicalHTML($xml, $page) {
protected function processPageToHierarchicalHTML($xml, $page)
{
if ($page->ID && !isset($this->processed[$page->ID])) {
// add stuff
// add html
$ul = $xml->addChild('ul');
$li = $ul->addChild('li');
$li->a = $page->Title;
$li->a->addAttribute('href', $page->Link);
// check if they have children
$kids = $page->Children();

if (count($kids)) {
foreach ($kids as $subPage) {
$this->processPageToHierarchicalHTML(
$li,
$subPage
);
}

// check if the page has children and if so output them
$kids = SiteTree::get()->filter('ParentID', $page->ID);
foreach ($kids as $subPage) {
$this->processPageToHierarchicalHTML(
$li,
$subPage
);
}
// add page id to processed
$this->processed[$page->ID] = true;
Expand Down
34 changes: 17 additions & 17 deletions code/controllers/SiteMapXMLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@

/**
* Redirect xml sitemap requests
*
*
* @author Kirk Mayo <[email protected]>
*/
class SiteMapXMLController extends Controller
{
private static $allowed_actions = array(
'index'
);
private static $base_url = '';
private static $allowed_actions = [
'index',
];

private static $base_url = '';

private static $match_host = '';

private static $allowed_sub_hosts = array();
private static $allowed_sub_hosts = [];

private static $sitemap_name = 'sitemap.xml';

private static $protocol = 'https';

public function index(SS_HTTPRequest $request)
{
$sitemap = ASSETS_PATH . '/' . $this->config()->sitemap_name;
$subhosts = $this->config()->allowed_sub_hosts;
$matchHost = $this->config()->match_host;

$generateAsHost = null;

if (is_array($subhosts) && in_array($_SERVER['HTTP_HOST'], $subhosts) ||
$matchHost && substr($_SERVER['HTTP_HOST'], -strlen($matchHost)) == $matchHost) {
$generateAsHost = $_SERVER['HTTP_HOST'];
$sitemap = ASSETS_PATH .'/'. $generateAsHost . '.' . $this->config()->sitemap_name;
$sitemap = ASSETS_PATH . '/' . $generateAsHost . '.' . $this->config()->sitemap_name;
}

if (file_exists($sitemap) && !isset($_GET['flush'])) {
Expand Down Expand Up @@ -63,20 +63,20 @@ public function generateSiteMap($sitemap = 'sitemap.xml', $siteURL = null)
'xsi:schemaLocation',
'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'
);

if (!$siteURL) {
$siteURL = self::config()->get('base_url');
if (!$siteURL) {
$siteURL = Director::absoluteBaseURL();
}
}

$siteURL = rtrim($siteURL, '/') . '/';

if (!strpos($siteURL, ':/')) {
$siteURL = $this->config()->protocol . '://' . $siteURL;
}

foreach ($pages as $page) {
$url = $xml->addChild('url');
$url->addChild('loc', $siteURL . $page->Link);
Expand Down
10 changes: 5 additions & 5 deletions code/extensions/SiteMapExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

class SiteMapExtension extends DataExtension
{
private static $db = array(
private static $db = [
'ChangeFreq' => "Enum(array('daily', 'weekly'), 'daily')",
'Priority' => "Enum('0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0', 0.0)"
);
'Priority' => "Enum('0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0', 0.0)",
];

public function updateSettingsFields(FieldList $fields)
{
$changeFreq = Singleton('Page')->dbObject('ChangeFreq')->enumValues();
$fields->addFieldToTab(
'Root.Settings',
'Root.Settings',
DropDownField::create('ChangeFreq', 'Change Frequency', $changeFreq)
);
$priority = Singleton('Page')->dbObject('Priority')->enumValues();
$fields->addFieldToTab(
'Root.Settings',
'Root.Settings',
DropDownField::create('Priority', 'Priority', $priority)
);
}
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"sitemap"
],
"require": {
"php": ">=5.3.3",
"silverstripe/queuedjobs": "~2.3",
"silverstripe/cms": "3.*",
"nyeholt/silverstripe-performant": "~1.0"
Expand Down
23 changes: 14 additions & 9 deletions tests/SiteMapControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
* Some controller testing
*/

class SiteMapControllerTest extends FunctionalTest {
class SiteMapControllerTest extends FunctionalTest
{

private $sitemap = 'sitemap-test.xml';

public function setUp() {
public function setUp()
{
parent::setUp();
for ($i=0; $i<100; $i++) {
for ($i = 0; $i < 100; $i++) {
$page = new Page(
array(
'title' => "Page $i"
)
[
'title' => "Page $i",
]
);
$page->write();
$page->publish('Stage', 'Live');
Expand All @@ -26,7 +28,8 @@ public function setUp() {
$sitemap->write();
}

public function tearDown() {
public function tearDown()
{
parent::tearDown();
if (file_exists(ASSETS_PATH . "/{$this->sitemap}")) {
unlink(ASSETS_PATH . "/{$this->sitemap}");
Expand All @@ -42,7 +45,8 @@ public function tearDown() {
}
}

public function testSiteMapGeneration() {
public function testSiteMapGeneration()
{
$sitemap = ASSETS_PATH . "/{$this->sitemap}";
$controller = new SiteMapXMLController();
$controller->generateSiteMap($sitemap);
Expand All @@ -57,7 +61,8 @@ public function testSiteMapGeneration() {
$this->assertTrue(file_exists($sitemap));
}

public function testHTMLGeneration() {
public function testHTMLGeneration()
{
$controller = new SiteMapPage_Controller();
$html = $controller->getHierarchicalSitemapHTML();

Expand Down
13 changes: 8 additions & 5 deletions tests/SiteMapPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
* Some basic testing for the SiteMapPage
*/

class SiteMapPageTest extends SapphireTest {
class SiteMapPageTest extends SapphireTest
{

protected static $fixture_file = 'SiteMapPageTest.yml';
protected static $fixture_file = 'SiteMapPageTest.yml';

public function setup() {
public function setup()
{
parent::setup();
}

public function testPage() {
$page = $this->objFromFixture('SiteMapPage', 'Page');
public function testPage()
{
$page = $this->objFromFixture('SiteMapPage', 'Page');

$this->assertEquals($page->Title, 'SiteMap');
}
Expand Down

0 comments on commit 9c13ef7

Please sign in to comment.