2011-03-22 22:40:09 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package cms
|
|
|
|
* @subpackage model
|
|
|
|
*/
|
2011-04-15 11:37:15 +02:00
|
|
|
class SiteTreeFileExtension extends DataExtension {
|
2011-03-22 22:40:09 +01:00
|
|
|
|
|
|
|
function extraStatics() {
|
|
|
|
return array(
|
|
|
|
'belongs_many_many' => array(
|
|
|
|
"BackLinkTracking" => "SiteTree",
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-06 16:13:16 +02:00
|
|
|
/**
|
|
|
|
* Extend through {@link updateBackLinkTracking()} in your own {@link Extension}.
|
|
|
|
*
|
|
|
|
* @return ComponentSet
|
|
|
|
*/
|
|
|
|
function BackLinkTracking($filter = "", $sort = "", $join = "", $limit = "") {
|
|
|
|
if(class_exists("Subsite")){
|
|
|
|
$rememberSubsiteFilter = Subsite::$disable_subsite_filter;
|
|
|
|
Subsite::disable_subsite_filter(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$links = $this->owner->getManyManyComponents('BackLinkTracking', $filter, $sort, $join, $limit);
|
|
|
|
$this->owner->extend('updateBackLinkTracking', $links);
|
|
|
|
|
|
|
|
if(class_exists("Subsite")){
|
|
|
|
Subsite::disable_subsite_filter($rememberSubsiteFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $links;
|
|
|
|
}
|
|
|
|
|
2011-03-22 22:40:09 +01:00
|
|
|
/**
|
|
|
|
* @todo Unnecessary shortcut for AssetTableField, coupled with cms module.
|
|
|
|
*
|
|
|
|
* @return Integer
|
|
|
|
*/
|
|
|
|
function BackLinkTrackingCount() {
|
|
|
|
$pages = $this->owner->BackLinkTracking();
|
|
|
|
if($pages) {
|
|
|
|
return $pages->Count();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates link tracking.
|
|
|
|
*/
|
|
|
|
function onAfterDelete() {
|
2011-03-30 07:57:50 +02:00
|
|
|
// We query the explicit ID list, because BackLinkTracking will get modified after the stage
|
|
|
|
// site does its thing
|
|
|
|
$brokenPageIDs = $this->owner->BackLinkTracking()->column("ID");
|
|
|
|
if($brokenPageIDs) {
|
2011-03-22 22:40:09 +01:00
|
|
|
$origStage = Versioned::current_stage();
|
|
|
|
|
|
|
|
// This will syncLinkTracking on draft
|
|
|
|
Versioned::reading_stage('Stage');
|
2011-03-30 07:57:50 +02:00
|
|
|
$brokenPages = DataObject::get('SiteTree')->byIDs($brokenPageIDs);
|
2011-03-22 22:40:09 +01:00
|
|
|
foreach($brokenPages as $brokenPage) $brokenPage->write();
|
|
|
|
|
|
|
|
// This will syncLinkTracking on published
|
|
|
|
Versioned::reading_stage('Live');
|
2011-03-30 07:57:50 +02:00
|
|
|
$liveBrokenPages = DataObject::get('SiteTree')->byIDs($brokenPageIDs);
|
|
|
|
foreach($liveBrokenPages as $brokenPage) {
|
|
|
|
$brokenPage->write();
|
|
|
|
}
|
2011-03-22 22:40:09 +01:00
|
|
|
|
|
|
|
Versioned::reading_stage($origStage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rewrite links to the $old file to now point to the $new file.
|
|
|
|
*
|
|
|
|
* @uses SiteTree->rewriteFileURL()
|
|
|
|
*
|
|
|
|
* @param String $old File path relative to the webroot
|
|
|
|
* @param String $new File path relative to the webroot
|
|
|
|
*/
|
|
|
|
function updateLinks($old, $new) {
|
|
|
|
if(class_exists('Subsite')) Subsite::disable_subsite_filter(true);
|
|
|
|
|
|
|
|
$pages = $this->owner->BackLinkTracking();
|
|
|
|
|
|
|
|
$summary = "";
|
|
|
|
if($pages) {
|
|
|
|
foreach($pages as $page) $page->rewriteFileURL($old,$new);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(class_exists('Subsite')) Subsite::disable_subsite_filter(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|