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 ) {
2015-04-28 02:03:07 +02:00
$fields -> insertAfter (
ReadonlyField :: create (
2016-01-06 00:42:07 +01:00
'BackLinkCount' ,
_t ( 'AssetTableField.BACKLINKCOUNT' , 'Used on:' ),
2015-04-28 02:03:07 +02:00
$this -> BackLinkTracking () -> Count () . ' ' . _t ( 'AssetTableField.PAGES' , 'page(s)' ))
-> 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 () {
$html = '<em>' . _t ( 'SiteTreeFileExtension.BACKLINK_LIST_DESCRIPTION' , 'This list shows all pages where the file has been added through a WYSIWYG editor.' ) . '</em>' ;
$html .= '<ul>' ;
foreach ( $this -> BackLinkTracking () as $backLink ) {
$listItem = '<li>' ;
// Add the page link
$listItem .= '<a href="' . $backLink -> Link () . '" target="_blank">' . Convert :: raw2xml ( $backLink -> MenuTitle ) . '</a> – ' ;
// Add the CMS link
$listItem .= '<a href="' . $backLink -> CMSEditLink () . '">' . _t ( 'SiteTreeFileExtension.EDIT' , 'Edit' ) . '</a>' ;
$html .= $listItem . '</li>' ;
}
return $html .= '</ul>' ;
}
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
*/
2015-03-31 08:54:43 +02:00
public function BackLinkTracking ( $filter = null , $sort = null , $join = null , $limit = null ) {
if ( $filter !== null || $sort !== null || $join !== null || $limit !== null ) {
2015-06-19 02:00:42 +02:00
Deprecation :: notice ( '4.0' , ' The $filter , $sort , $join and $limit parameters for
2015-03-31 08:54:43 +02:00
SiteTreeFileExtension :: BackLinkTracking () have been deprecated .
Please manipluate the returned list directly . ' , Deprecation :: SCOPE_GLOBAL );
}
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
if ( $filter || $sort || $join || $limit ) {
2016-01-06 00:42:07 +01:00
Deprecation :: notice ( '4.0' , ' The $filter , $sort , $join and $limit parameters for
SiteTreeFileExtension :: BackLinkTracking () have been deprecated .
2015-03-13 18:16:25 +01:00
Please manipluate the returned list directly . ' , Deprecation :: SCOPE_GLOBAL );
}
2011-10-06 16:13:16 +02:00
2015-04-28 13:34:26 +02:00
$links = $this -> owner -> getManyManyComponents ( 'BackLinkTracking' );
if ( $this -> owner -> ID ) {
$links = $links
-> where ( $filter )
-> sort ( $sort )
-> limit ( $limit );
}
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 .
2016-01-06 00:42:07 +01:00
*
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 ;
}
}
/**
* 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 .
2016-01-06 00:42:07 +01:00
*
2011-03-22 22:40:09 +01:00
* @ uses SiteTree -> rewriteFileURL ()
2016-01-06 00:42:07 +01:00
*
2015-09-28 11:31:31 +02:00
* @ param string $old File path relative to the webroot
* @ param string $new File path relative to the webroot
2011-03-22 22:40:09 +01:00
*/
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
}