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 {
|
2012-05-23 12:13:27 +02:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $belongs_many_many = array(
|
2012-05-23 12:13:27 +02:00
|
|
|
'BackLinkTracking' => 'SiteTree'
|
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function updateCMSFields(FieldList $fields) {
|
2012-06-19 12:56:42 +02:00
|
|
|
$fields->insertAfter(new ReadonlyField('BackLinkCount',
|
|
|
|
_t('AssetTableField.BACKLINKCOUNT', 'Used on:'),
|
|
|
|
$this->BackLinkTracking()->Count() . ' ' . _t('AssetTableField.PAGES', 'page(s)')),
|
|
|
|
'LastEdited'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-06 16:13:16 +02:00
|
|
|
/**
|
|
|
|
* Extend through {@link updateBackLinkTracking()} in your own {@link Extension}.
|
2014-02-10 21:35:13 +01:00
|
|
|
*
|
|
|
|
* @param string|array $filter
|
|
|
|
* @param string $sort
|
|
|
|
* @param string $join
|
|
|
|
* @param string $limit
|
|
|
|
* @return ManyManyList
|
2011-10-06 16:13:16 +02:00
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function BackLinkTracking($filter = "", $sort = "", $join = "", $limit = "") {
|
2011-10-06 16:13:16 +02:00
|
|
|
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
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function BackLinkTrackingCount() {
|
2011-03-22 22:40:09 +01:00
|
|
|
$pages = $this->owner->BackLinkTracking();
|
|
|
|
if($pages) {
|
|
|
|
return $pages->Count();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates link tracking.
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function onAfterDelete() {
|
2013-06-21 00:45:33 +02:00
|
|
|
// We query the explicit ID list, because BackLinkTracking will get modified after the stage
|
|
|
|
// site does its thing
|
2011-03-30 07:57:50 +02:00
|
|
|
$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) {
|
2013-06-21 00:45:33 +02:00
|
|
|
$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
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function updateLinks($old, $new) {
|
2011-03-22 22:40:09 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-03-27 06:05:11 +02:00
|
|
|
}
|