2011-03-22 22:40:09 +01:00
|
|
|
<?php
|
2016-01-26 06:38:42 +01:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Model;
|
|
|
|
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\ReadonlyField;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\ManyManyList;
|
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
2016-09-13 00:47:02 +02:00
|
|
|
use SilverStripe\View\SSViewer;
|
2016-07-22 01:32:32 +02:00
|
|
|
use Subsite;
|
2016-06-16 06:57:19 +02: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
|
|
|
*/
|
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-08-10 06:08:39 +02:00
|
|
|
'BackLinkTracking' => 'SilverStripe\\CMS\\Model\\SiteTree.ImageTracking' // {@see SiteTreeLinkTracking}
|
2012-05-23 12:13:27 +02:00
|
|
|
);
|
|
|
|
|
2016-02-21 23:44:24 +01:00
|
|
|
/**
|
|
|
|
* Images tracked by pages are owned by those pages
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $owned_by = array(
|
|
|
|
'BackLinkTracking'
|
|
|
|
);
|
|
|
|
|
2016-09-20 02:08:20 +02:00
|
|
|
private static $casting = array(
|
|
|
|
'BackLinkHTMLList' => 'HTMLFragment'
|
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function updateCMSFields(FieldList $fields) {
|
2015-04-28 02:03:07 +02:00
|
|
|
$fields->insertAfter(
|
2016-08-10 06:08:39 +02:00
|
|
|
'LastEdited',
|
2015-04-28 02:03:07 +02:00
|
|
|
ReadonlyField::create(
|
2016-02-21 23:44:24 +01:00
|
|
|
'BackLinkCount',
|
|
|
|
_t('AssetTableField.BACKLINKCOUNT', 'Used on:'),
|
2016-08-10 06:08:39 +02:00
|
|
|
$this->BackLinkTracking()->count() . ' ' . _t('AssetTableField.PAGES', 'page(s)')
|
2015-10-15 00:08:52 +02:00
|
|
|
)
|
2016-01-26 06:38:42 +01:00
|
|
|
->addExtraClass('cms-description-toggle')
|
2016-08-10 06:08:39 +02:00
|
|
|
->setDescription($this->BackLinkHTMLList())
|
2012-06-19 12:56:42 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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() {
|
2016-09-13 00:47:02 +02:00
|
|
|
$viewer = new SSViewer(["type" => "Includes", __CLASS__ . "_description"]);
|
2015-04-28 02:03:07 +02:00
|
|
|
|
2016-09-20 02:08:20 +02:00
|
|
|
return $viewer->process($this->owner);
|
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);
|
2016-02-21 23:44:24 +01:00
|
|
|
|
2011-10-06 16:13:16 +02:00
|
|
|
if(class_exists("Subsite")){
|
|
|
|
Subsite::disable_subsite_filter($rememberSubsiteFilter);
|
|
|
|
}
|
2016-02-21 23:44:24 +01:00
|
|
|
|
2011-10-06 16:13:16 +02:00
|
|
|
return $links;
|
|
|
|
}
|
2016-02-21 23:44:24 +01:00
|
|
|
|
2011-03-22 22:40:09 +01:00
|
|
|
/**
|
|
|
|
* @todo Unnecessary shortcut for AssetTableField, coupled with cms module.
|
2016-02-21 23:44:24 +01:00
|
|
|
*
|
2016-08-10 06:08:39 +02:00
|
|
|
* @return int
|
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) {
|
2016-08-10 06:08:39 +02:00
|
|
|
return $pages->count();
|
2011-03-22 22:40:09 +01:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-02-21 23:44:24 +01:00
|
|
|
|
2011-03-22 22:40:09 +01:00
|
|
|
/**
|
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
|
2016-06-16 06:57:19 +02:00
|
|
|
if(Versioned::get_stage() === Versioned::LIVE) {
|
2016-01-26 06:38:42 +01:00
|
|
|
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
|
2016-07-22 01:32:32 +02:00
|
|
|
$brokenPages = DataObject::get('SilverStripe\\CMS\\Model\\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
|
|
|
}
|
|
|
|
}
|
2012-03-27 06:05:11 +02:00
|
|
|
}
|