silverstripe-cms/code/reports/BrokenLinksReport.php

158 lines
5.3 KiB
PHP
Raw Normal View History

<?php
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ArrayList;
/**
* Content side-report listing pages with broken links
* @package cms
* @subpackage content
*/
class BrokenLinksReport extends SS_Report {
public function title() {
return _t('BrokenLinksReport.BROKENLINKS',"Broken links report");
}
2016-03-08 21:50:55 +01:00
public function sourceRecords($params, $sort, $limit) {
$join = '';
$sortBrokenReason = false;
if($sort) {
$parts = explode(' ', $sort);
$field = $parts[0];
$direction = $parts[1];
2016-03-08 21:50:55 +01:00
if($field == 'AbsoluteLink') {
$sort = 'URLSegment ' . $direction;
} elseif($field == 'Subsite.Title') {
$join = 'LEFT JOIN "Subsite" ON "Subsite"."ID" = "SiteTree"."SubsiteID"';
} elseif($field == 'BrokenReason') {
$sortBrokenReason = true;
$sort = '';
}
}
2015-11-10 03:23:58 +01:00
$brokenFilter = array(
'"SiteTree"."HasBrokenLink" = ? OR "SiteTree"."HasBrokenFile" = ?' => array(true, true)
);
$isLive = !isset($params['CheckSite']) || $params['CheckSite'] == 'Published';
if ($isLive) {
$ret = Versioned::get_by_stage('SiteTree', 'Live', $brokenFilter, $sort, $join, $limit);
} else {
2015-11-10 03:23:58 +01:00
$ret = DataObject::get('SiteTree', $brokenFilter, $sort, $join, $limit);
}
2016-03-08 21:50:55 +01:00
$returnSet = new ArrayList();
if ($ret) foreach($ret as $record) {
$reason = false;
$isRedirectorPage = in_array($record->ClassName, ClassInfo::subclassesFor('RedirectorPage'));
$isVirtualPage = in_array($record->ClassName, ClassInfo::subclassesFor('VirtualPage'));
2016-03-08 21:50:55 +01:00
if ($isVirtualPage) {
if ($record->HasBrokenLink) {
$reason = _t('BrokenLinksReport.VirtualPageNonExistent', "virtual page pointing to non-existent page");
$reasonCodes = array("VPBROKENLINK");
}
} else if ($isRedirectorPage) {
if ($record->HasBrokenLink) {
$reason = _t('BrokenLinksReport.RedirectorNonExistent', "redirector page pointing to non-existent page");
$reasonCodes = array("RPBROKENLINK");
}
} else {
if ($record->HasBrokenLink && $record->HasBrokenFile) {
$reason = _t('BrokenLinksReport.HasBrokenLinkAndFile', "has broken link and file");
$reasonCodes = array("BROKENFILE", "BROKENLINK");
} else if ($record->HasBrokenLink && !$record->HasBrokenFile) {
$reason = _t('BrokenLinksReport.HasBrokenLink', "has broken link");
$reasonCodes = array("BROKENLINK");
} else if (!$record->HasBrokenLink && $record->HasBrokenFile) {
$reason = _t('BrokenLinksReport.HasBrokenFile', "has broken file");
$reasonCodes = array("BROKENFILE");
}
}
2016-03-08 21:50:55 +01:00
if ($reason) {
if (isset($params['Reason']) && $params['Reason'] && !in_array($params['Reason'], $reasonCodes)) continue;
$record->BrokenReason = $reason;
$returnSet->push($record);
}
}
2016-03-08 21:50:55 +01:00
if($sortBrokenReason) $returnSet = $returnSet->sort('BrokenReason', $direction);
2016-03-08 21:50:55 +01:00
return $returnSet;
}
public function columns() {
if(isset($_REQUEST['filters']['CheckSite']) && $_REQUEST['filters']['CheckSite'] == 'Draft') {
$dateTitle = _t('BrokenLinksReport.ColumnDateLastModified', 'Date last modified');
} else {
$dateTitle = _t('BrokenLinksReport.ColumnDateLastPublished', 'Date last published');
}
2016-03-08 21:50:55 +01:00
$linkBase = singleton('CMSPageEditController')->Link('show');
$fields = array(
"Title" => array(
"title" => _t('BrokenLinksReport.PageName', 'Page name'),
'formatting' => function($value, $item) use ($linkBase) {
return sprintf('<a href="%s" title="%s">%s</a>',
Controller::join_links($linkBase, $item->ID),
_t('BrokenLinksReport.HoverTitleEditPage', 'Edit page'),
$value
);
}
),
"LastEdited" => array(
"title" => $dateTitle,
'casting' => 'DBDatetime->Full'
),
"BrokenReason" => array(
"title" => _t('BrokenLinksReport.ColumnProblemType', "Problem type")
),
'AbsoluteLink' => array(
'title' => _t('BrokenLinksReport.ColumnURL', 'URL'),
'formatting' => function($value, $item) {
$liveLink = $item->AbsoluteLiveLink;
$stageLink = $item->AbsoluteLink();
return sprintf('%s <a href="%s">%s</a>',
$stageLink,
$liveLink ? $liveLink : Controller::join_links($stageLink, '?stage=Stage'),
$liveLink ? '(live)' : '(draft)'
);
}
)
);
2016-03-08 21:50:55 +01:00
return $fields;
}
public function parameterFields() {
return new FieldList(
new DropdownField('CheckSite', _t('BrokenLinksReport.CheckSite','Check site'), array(
'Published' => _t('BrokenLinksReport.CheckSiteDropdownPublished', 'Published Site'),
'Draft' => _t('BrokenLinksReport.CheckSiteDropdownDraft', 'Draft Site')
)),
new DropdownField(
'Reason',
_t('BrokenLinksReport.ReasonDropdown', 'Problem to check'),
array(
'' => _t('BrokenLinksReport.Any', 'Any'),
'BROKENFILE' => _t('BrokenLinksReport.ReasonDropdownBROKENFILE', 'Broken file'),
'BROKENLINK' => _t('BrokenLinksReport.ReasonDropdownBROKENLINK', 'Broken link'),
'VPBROKENLINK' => _t('BrokenLinksReport.ReasonDropdownVPBROKENLINK', 'Virtual page pointing to non-existent page'),
'RPBROKENLINK' => _t('BrokenLinksReport.ReasonDropdownRPBROKENLINK', 'Redirector page pointing to non-existent page'),
)
)
);
}
}
2015-11-10 03:23:58 +01:00
/**
* @deprecated 3.2..4.0
*/
class SideReport_BrokenLinks extends BrokenLinksReport {
public function __construct() {
Deprecation::notice('4.0', 'Use BrokenLinksReport instead');
parent::__construct();
}
}