ENHANCEMENT Added SiteTreeDecorator

ENHANCEMENT Addings hooks in SiteTree for onBeforeUnpublish and onAfterUnpublish
ENHANCEMENT Removed GoogleSitemap references in SiteTree (moving to decorator implementation)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@66168 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-11-18 22:31:26 +00:00 committed by Sam Minnee
parent 1cdca8a66a
commit 2153737da7
3 changed files with 92 additions and 14 deletions

View File

@ -555,7 +555,7 @@ class SiteTree extends DataObject {
* - canEdit() is not granted
* - There are no classes defined in {@link $allowed_children}
*
* @uses DataObjectDecorator->canAddChildren()
* @uses SiteTreeDecorator->canAddChildren()
* @uses canEdit()
* @uses $allowed_children
*
@ -644,6 +644,7 @@ class SiteTree extends DataObject {
* - any descendant page returns FALSE for canDelete()
*
* @uses canDelete()
* @uses DataObjectDecorator->canDelete()
* @uses canEdit()
*
* @param Member $member
@ -686,6 +687,7 @@ class SiteTree extends DataObject {
* Use {@link canAddChildren()} to control behaviour of creating children under this page.
*
* @uses $can_create
* @uses DataObjectDecorator->canCreate()
*
* @param Member $member
* @return boolean True if the current user can create pages on this class.
@ -721,6 +723,7 @@ class SiteTree extends DataObject {
*
* @uses canView()
* @uses EditorGroups()
* @uses DataObjectDecorator->canEdit()
*
* @param Member $member
* @return boolean True if the current user can edit this page.
@ -768,7 +771,7 @@ class SiteTree extends DataObject {
* - canPublish() on any decorator returns FALSE
* - canEdit() returns FALSE
*
* @uses DataObjectDecorator->canPublish()
* @uses SiteTreeDecorator->canPublish()
*
* @param Member $member
* @return boolean True if the current user can publish this page.
@ -1361,7 +1364,10 @@ class SiteTree extends DataObject {
}
/**
* Publish this page
* Publish this page.
*
* @uses SiteTreeDecorator->onBeforePublish()
* @uses SiteTreeDecorator->onAfterPublish()
*/
function doPublish() {
$original = Versioned::get_one_by_stage("SiteTree", "Live", "`SiteTree`.`ID` = $this->ID");
@ -1377,8 +1383,6 @@ class SiteTree extends DataObject {
$this->write();
$this->publish("Stage", "Live");
GoogleSitemap::ping();
// Fix the sort order for this page's siblings
DB::query("UPDATE SiteTree_Live
INNER JOIN SiteTree ON SiteTree_Live.ID = SiteTree.ID
@ -1391,8 +1395,13 @@ class SiteTree extends DataObject {
/**
* Unpublish this page - remove it from the live site
*
* @uses SiteTreeDecorator->onBeforeUnpublish()
* @uses SiteTreeDecorator->onAfterUnpublish()
*/
function doUnpublish() {
$this->extend('onBeforeUnpublish');
// Call delete on a cloned object so that this one doesn't lose its ID
$this->flushCache();
$clone = DataObject::get_by_id("SiteTree", $this->ID);
@ -1401,7 +1410,7 @@ class SiteTree extends DataObject {
$this->Status = "Unpublished";
$this->write();
GoogleSitemap::ping();
$this->extend('onAfterUnpublish');
}
/**

View File

@ -0,0 +1,31 @@
<?php
/**
* Plug-ins for additional functionality in your SiteTree classes.
*
* @package sapphire
* @subpackage model
*/
abstract class SiteTreeDecorator extends DataObjectDecorator {
function onBeforePublish(&$original) {
}
function onAfterPublish(&$original) {
}
function onBeforeUnpublish() {
}
function onAfterUnpublish() {
}
function canAddChildren($member) {
}
function canPublish($member) {
}
}
?>

View File

@ -1,9 +1,21 @@
<?php
/**
* Initial implementation of Sitemap support.
* GoogleSitemap should handle requests to 'sitemap.xml'
* the other two classes are used to render the sitemap
* the other two classes are used to render the sitemap.
*
* You can notify ("ping") Google about a changed sitemap
* automatically whenever a new page is published or unpublished.
* By default, Google is not notified, and will pick up your new
* sitemap whenever the GoogleBot visits your website.
*
* Enabling notification of Google after every publish (in your _config.php):
* <example
* GoogleSitemap::enable_google_notificaton();
* </example>
*
* @see http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=34609
*
* @package sapphire
* @subpackage misc
*/
@ -22,7 +34,7 @@ class GoogleSitemap extends Controller {
/**
* @var boolean
*/
protected static $pings = true;
protected static $google_notification_enabled = false;
public function Items() {
$this->Pages = Versioned::get_by_stage('SiteTree', 'Live');
@ -70,13 +82,21 @@ class GoogleSitemap extends Controller {
}
/**
* Notifies Google about changes to your sitemap.
* Triggered automatically on every publish/unpublish of a page.
* This behaviour is disabled by default, enable with:
* GoogleSitemap::enable_google_notificaton();
*
* If the site is in "dev-mode", no ping will be sent regardless wether
* the Google notification is enabled.
*
* @return string Response text
*/
static function ping() {
if(!self::$enabled) return false;
//Don't ping if the site has disabled it, or if the site is in dev mode
if(!GoogleSitemap::$pings || Director::isDev())
if(!GoogleSitemap::$google_notification_enabled || Director::isDev())
return;
$location = urlencode(Director::absoluteBaseURL() . '/sitemap.xml');
@ -87,15 +107,18 @@ class GoogleSitemap extends Controller {
return $response;
}
/**
* Enable pings to google.com whenever sitemap changes.
*/
public static function enable_google_notification() {
self::$pings = true;
self::$google_notification_enabled = true;
}
/**
* Disables pings to google when the sitemap changes.
*/
public static function disable_google_notification() {
self::$pings = false;
self::$google_notification_enabled = false;
}
function index($url) {
@ -120,4 +143,19 @@ class GoogleSitemap extends Controller {
self::$enabled = false;
}
}
/**
* @package sapphire
* @subpackage misc
*/
class GoogleSitemapDecorator extends SiteTreeDecorator {
function onAfterPublish() {
GoogleSiteMap::ping();
}
function onAfterUnpublish() {
GoogleSiteMap::ping();
}
}
Object::add_extension('SiteTree', 'GoogleSitemapDecorator');
?>