ENHANCEMENT Moving GoogleSitemap functionality (priority dropdown and database fields) from SiteTree to GoogleSitemapDecorator

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@66173 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-11-18 23:00:05 +00:00 committed by Sam Minnee
parent 2153737da7
commit c6bf307b94
2 changed files with 75 additions and 66 deletions

View File

@ -78,8 +78,6 @@ class SiteTree extends DataObject {
"HasBrokenLink" => "Boolean",
"Status" => "Varchar",
"ReportClass" => "Varchar",
"Priority" => "Float",
"CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')",
"CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')",
@ -87,10 +85,10 @@ class SiteTree extends DataObject {
"ToDo" => "Text",
);
static $indexes = array(
"SearchFields" => "fulltext (Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords)",
"TitleSearchFields" => "fulltext (Title)",
"URLSegment" => true,
static $indexes = array(
"SearchFields" => "fulltext (Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords)",
"TitleSearchFields" => "fulltext (Title)",
"URLSegment" => true,
);
static $has_many = array(
@ -992,12 +990,6 @@ class SiteTree extends DataObject {
}
}
// If priority is empty or invalid, set it to the default value
if(!is_numeric($this->Priority) ||
(($this->Priority < 0) || ($this->Priority > 1)))
$this->Priority = self::$defaults['Priority'];
parent::onBeforeWrite();
}
@ -1049,22 +1041,6 @@ class SiteTree extends DataObject {
}
}
/**
* The default value of the priority field depends on the depth of the page in
* the site tree, so it must be calculated dynamically.
*/
function getPriority() {
if(!$this->getField('Priority')) {
$parentStack = $this->parentStack();
$numParents = is_array($parentStack) ? count($parentStack) - 1: 0;
return max(0.1, 1.0 - ($numParents / 10));
} else if($this->getField('Priority') == -1) {
return 0;
} else {
return $this->getField('Priority');
}
}
/**
* Returns a FieldSet with which to create the CMS editing form.
*
@ -1141,22 +1117,6 @@ class SiteTree extends DataObject {
if(isset($statusMessage)) {
$message .= "NOTE: " . implode("<br />", $statusMessage);
}
$pagePriorities = array(
'' => _t('SiteTree.PRIORITYAUTOSET','Auto-set based on page depth'),
'-1' => _t('SiteTree.PRIORITYNOTINDEXED', "Not indexed"), // We set this to -ve one because a blank value implies auto-generation of Priority
'1.0' => '1 - ' . _t('SiteTree.PRIORITYMOSTIMPORTANT', "Most important"),
'0.9' => '2',
'0.8' => '3',
'0.7' => '4',
'0.6' => '5',
'0.5' => '6',
'0.4' => '7',
'0.3' => '8',
'0.2' => '9',
'0.1' => '10 - ' . _t('SiteTree.PRIORITYLEASTIMPORTANT', "Least important")
);
// Lay out the fields
$fields = new FieldSet(
@ -1194,27 +1154,7 @@ class SiteTree extends DataObject {
new TextField("MetaTitle", $this->fieldLabel('MetaTitle')),
new TextareaField("MetaDescription", $this->fieldLabel('MetaDescription')),
new TextareaField("MetaKeywords", $this->fieldLabel('MetaKeywords')),
new ToggleCompositeField(
'AdvancedOptions',
_t('SiteTree.METAADVANCEDHEADER', "Advanced Options..."),
array(
new TextareaField("ExtraMeta",$this->fieldLabel('ExtraMeta')),
new LiteralField(
"",
"<p>" .
sprintf(
_t(
'SiteTree.METANOTEPRIORITY',
"Manually specify a Google Sitemaps priority for this page (%s)"
),
'<a href="https://www.google.com/webmasters/tools/docs/en/protocol.html#prioritydef">?</a>'
) .
"</p>"
),
new DropdownField("Priority", $this->fieldLabel('Priority'), $pagePriorities)
),
true
)
new TextareaField("ExtraMeta",$this->fieldLabel('ExtraMeta'))
)
),
$tabBehaviour = new Tab('Behaviour',
@ -1305,7 +1245,6 @@ class SiteTree extends DataObject {
$labels['MetaDescription'] = _t('SiteTree.METADESC', "Description");
$labels['MetaKeywords'] = _t('SiteTree.METAKEYWORDS', "Keywords");
$labels['ExtraMeta'] = _t('SiteTree.METAEXTRA', "Custom Meta Tags");
$labels['Priority'] = _t('SiteTree.METAPAGEPRIO', "Page Priority");
$labels['ClassName'] = _t('SiteTree.PAGETYPE', "Page type", PR_MEDIUM, 'Classname of a page object');
$labels['ShowInMenus'] =_t('SiteTree.SHOWINMENUS', "Show in menus?");
$labels['ShowInSearch'] = _t('SiteTree.SHOWINSEARCH', "Show in search?");

View File

@ -149,12 +149,82 @@ class GoogleSitemap extends Controller {
* @subpackage misc
*/
class GoogleSitemapDecorator extends SiteTreeDecorator {
function extraStatics() {
return array(
'db' => array(
"Priority" => "Float",
)
);
}
function updateCMSFields(&$fields) {
$pagePriorities = array(
'' => _t('SiteTree.PRIORITYAUTOSET','Auto-set based on page depth'),
'-1' => _t('SiteTree.PRIORITYNOTINDEXED', "Not indexed"), // We set this to -ve one because a blank value implies auto-generation of Priority
'1.0' => '1 - ' . _t('SiteTree.PRIORITYMOSTIMPORTANT', "Most important"),
'0.9' => '2',
'0.8' => '3',
'0.7' => '4',
'0.6' => '5',
'0.5' => '6',
'0.4' => '7',
'0.3' => '8',
'0.2' => '9',
'0.1' => '10 - ' . _t('SiteTree.PRIORITYLEASTIMPORTANT', "Least important")
);
$tabset = $fields->findOrMakeTab('Root.Content');
$tabset->push(
$addTab = new Tab(
'GoogleSitemap',
_t('SiteTree.TABGOOGLESITEMAP', 'Google Sitemap'),
new LiteralField(
"GoogleSitemapIntro",
"<p>" .
sprintf(
_t(
'SiteTree.METANOTEPRIORITY',
"Manually specify a Google Sitemaps priority for this page (%s)"
),
'<a href="https://www.google.com/webmasters/tools/docs/en/protocol.html#prioritydef">?</a>'
) .
"</p>"
),
new DropdownField("Priority", $this->owner->fieldLabel('Priority'), $pagePriorities)
)
);
}
function updateFieldLabels(&$labels) {
parent::updateFieldLabels($labels);
$labels['Priority'] = _t('SiteTree.METAPAGEPRIO', "Page Priority");
}
function onAfterPublish() {
GoogleSiteMap::ping();
}
function onAfterUnpublish() {
GoogleSiteMap::ping();
}
/**
* The default value of the priority field depends on the depth of the page in
* the site tree, so it must be calculated dynamically.
*/
function getPriority() {
if(!$this->owner->getField('Priority')) {
$parentStack = $this->owner->parentStack();
$numParents = is_array($parentStack) ? count($parentStack) - 1: 0;
return max(0.1, 1.0 - ($numParents / 10));
} else if($this->owner->getField('Priority') == -1) {
return 0;
} else {
return $this->owner->getField('Priority');
}
}
}
Object::add_extension('SiteTree', 'GoogleSitemapDecorator');