2011-03-22 22:40:09 +01:00
|
|
|
<?php
|
2016-01-26 06:38:42 +01:00
|
|
|
|
2011-03-22 22:40:09 +01:00
|
|
|
/**
|
2016-01-26 06:38:42 +01:00
|
|
|
* Extension applied to {@see File} object to track links to {@see SiteTree} records.
|
|
|
|
*
|
|
|
|
* {@see SiteTreeLinkTracking} for the extension applied to {@see SiteTree}
|
|
|
|
*
|
|
|
|
* Note that since both SiteTree and File are versioned, LinkTracking and ImageTracking will
|
|
|
|
* only be enabled for the Stage record.
|
|
|
|
*
|
|
|
|
* @property File $owner
|
|
|
|
*
|
2011-03-22 22:40:09 +01:00
|
|
|
* @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(
|
2016-01-26 06:38:42 +01:00
|
|
|
'BackLinkTracking' => 'SiteTree.ImageTracking' // {@see SiteTreeLinkTracking}
|
2012-05-23 12:13:27 +02:00
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function updateCMSFields(FieldList $fields) {
|
2015-04-28 02:03:07 +02:00
|
|
|
$fields->insertAfter(
|
|
|
|
ReadonlyField::create(
|
|
|
|
'BackLinkCount',
|
|
|
|
_t('AssetTableField.BACKLINKCOUNT', 'Used on:'),
|
2015-10-15 00:08:52 +02:00
|
|
|
$this->BackLinkTracking()->Count() . ' ' . _t('AssetTableField.PAGES', 'page(s)')
|
|
|
|
)
|
2016-01-26 06:38:42 +01:00
|
|
|
->addExtraClass('cms-description-toggle')
|
|
|
|
->setDescription($this->BackLinkHTMLList()),
|
2012-06-19 12:56:42 +02:00
|
|
|
'LastEdited'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-28 02:03:07 +02:00
|
|
|
/**
|
|
|
|
* Generate an HTML list which provides links to where a file is used.
|
|
|
|
*
|
2015-09-28 11:31:31 +02:00
|
|
|
* @return string
|
2015-04-28 02:03:07 +02:00
|
|
|
*/
|
|
|
|
public function BackLinkHTMLList() {
|
2015-10-15 00:08:52 +02:00
|
|
|
$html = '<em>' . _t(
|
|
|
|
'SiteTreeFileExtension.BACKLINK_LIST_DESCRIPTION',
|
|
|
|
'This list shows all pages where the file has been added through a WYSIWYG editor.'
|
|
|
|
) . '</em>';
|
2015-04-28 02:03:07 +02:00
|
|
|
|
2016-01-26 06:38:42 +01:00
|
|
|
$html .= '<ul>';
|
2015-04-28 02:03:07 +02:00
|
|
|
foreach ($this->BackLinkTracking() as $backLink) {
|
2015-10-15 00:08:52 +02:00
|
|
|
// Add the page link and CMS link
|
|
|
|
$html .= sprintf(
|
|
|
|
'<li><a href="%s" target="_blank">%s</a> – <a href="%s">%s</a></li>',
|
|
|
|
Convert::raw2att($backLink->Link()),
|
|
|
|
Convert::raw2xml($backLink->MenuTitle),
|
|
|
|
Convert::raw2att($backLink->CMSEditLink()),
|
|
|
|
_t('SiteTreeFileExtension.EDIT', 'Edit')
|
|
|
|
);
|
2015-04-28 02:03:07 +02:00
|
|
|
}
|
2016-01-26 06:38:42 +01:00
|
|
|
$html .= '</ul>';
|
2015-04-28 02:03:07 +02:00
|
|
|
|
2016-01-26 06:38:42 +01:00
|
|
|
return $html;
|
2015-04-28 02:03:07 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @return ManyManyList
|
2011-10-06 16:13:16 +02:00
|
|
|
*/
|
2015-10-15 00:08:52 +02:00
|
|
|
public function BackLinkTracking() {
|
2011-10-06 16:13:16 +02:00
|
|
|
if(class_exists("Subsite")){
|
|
|
|
$rememberSubsiteFilter = Subsite::$disable_subsite_filter;
|
|
|
|
Subsite::disable_subsite_filter(true);
|
|
|
|
}
|
2015-03-13 18:16:25 +01:00
|
|
|
|
2015-04-28 13:34:26 +02:00
|
|
|
$links = $this->owner->getManyManyComponents('BackLinkTracking');
|
2011-10-06 16:13:16 +02:00
|
|
|
$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.
|
|
|
|
*
|
2015-09-28 11:31:31 +02:00
|
|
|
* @return integer
|
2011-03-22 22:40:09 +01:00
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-26 06:38:42 +01:00
|
|
|
* Updates link tracking in the current stage.
|
2011-03-22 22:40:09 +01:00
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function onAfterDelete() {
|
2016-01-26 06:38:42 +01:00
|
|
|
// Skip live stage
|
|
|
|
if(\Versioned::current_stage() === \Versioned::get_live_stage()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-01-26 06:38:42 +01:00
|
|
|
// This will syncLinkTracking on the same stage as this file
|
2011-03-30 07:57:50 +02:00
|
|
|
$brokenPages = DataObject::get('SiteTree')->byIDs($brokenPageIDs);
|
2015-10-15 00:08:52 +02:00
|
|
|
foreach($brokenPages as $brokenPage) {
|
|
|
|
$brokenPage->write();
|
|
|
|
}
|
2016-01-26 06:38:42 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-22 22:40:09 +01:00
|
|
|
|
2016-01-26 06:38:42 +01:00
|
|
|
public function onAfterWrite() {
|
|
|
|
// Update any database references in the current stage
|
|
|
|
$this->updateLinks();
|
|
|
|
}
|
2011-03-22 22:40:09 +01:00
|
|
|
|
2016-01-26 06:38:42 +01:00
|
|
|
public function onAfterVersionedPublish() {
|
|
|
|
// Ensure that ->updateLinks is invoked on the draft record
|
|
|
|
// after ->doPublish() is invoked.
|
|
|
|
$this->updateLinks();
|
2011-03-22 22:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rewrite links to the $old file to now point to the $new file.
|
|
|
|
*
|
2015-10-15 00:08:52 +02:00
|
|
|
* @uses SiteTree->rewriteFileID()
|
2011-03-22 22:40:09 +01:00
|
|
|
*/
|
2015-10-15 00:08:52 +02:00
|
|
|
public function updateLinks() {
|
2016-01-26 06:38:42 +01:00
|
|
|
// Skip live stage
|
|
|
|
if(\Versioned::current_stage() === \Versioned::get_live_stage()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-15 00:08:52 +02:00
|
|
|
if(class_exists('Subsite')) {
|
|
|
|
Subsite::disable_subsite_filter(true);
|
|
|
|
}
|
2011-03-22 22:40:09 +01:00
|
|
|
|
|
|
|
$pages = $this->owner->BackLinkTracking();
|
|
|
|
if($pages) {
|
2015-10-15 00:08:52 +02:00
|
|
|
foreach($pages as $page) {
|
|
|
|
$page->rewriteFileLinks();
|
|
|
|
}
|
2011-03-22 22:40:09 +01:00
|
|
|
}
|
|
|
|
|
2015-10-15 00:08:52 +02:00
|
|
|
if(class_exists('Subsite')) {
|
|
|
|
Subsite::disable_subsite_filter(false);
|
|
|
|
}
|
2011-03-22 22:40:09 +01:00
|
|
|
}
|
|
|
|
|
2012-03-27 06:05:11 +02:00
|
|
|
}
|