mirror of
https://github.com/silverstripe/silverstripe-cms
synced 2024-10-22 06:05:56 +00:00
MINOR Added SiteTreeFolderDecorator and FolderUnusedAssetsField (moved functionality from Folder class in 'sapphire' module)
This commit is contained in:
parent
dd6a86c1c1
commit
317ca6f96a
61
code/FolderUnusedAssetsField.php
Normal file
61
code/FolderUnusedAssetsField.php
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @package sapphire
|
||||||
|
* @subpackage filesystem
|
||||||
|
*/
|
||||||
|
class Folder_UnusedAssetsField extends CompositeField {
|
||||||
|
protected $folder;
|
||||||
|
|
||||||
|
public function __construct($folder) {
|
||||||
|
$this->folder = $folder;
|
||||||
|
parent::__construct(new FieldSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getChildren() {
|
||||||
|
if($this->children->Count() == 0) {
|
||||||
|
$inlineFormAction = new InlineFormAction("delete_unused_thumbnails", _t('Folder.DELETEUNUSEDTHUMBNAILS', 'Delete unused thumbnails'));
|
||||||
|
$inlineFormAction->includeDefaultJS(false) ;
|
||||||
|
|
||||||
|
$this->children = new FieldSet(
|
||||||
|
new LiteralField( "UnusedAssets", "<h2>"._t('Folder.UNUSEDFILESTITLE', 'Unused files')."</h2>" ),
|
||||||
|
$this->getAssetList(),
|
||||||
|
new FieldGroup(
|
||||||
|
new LiteralField( "UnusedThumbnails", "<h2>"._t('Folder.UNUSEDTHUMBNAILSTITLE', 'Unused thumbnails')."</h2>"),
|
||||||
|
$inlineFormAction
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$this->children->setForm($this->form);
|
||||||
|
}
|
||||||
|
return $this->children;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function FieldHolder() {
|
||||||
|
$output = "";
|
||||||
|
foreach($this->getChildren() as $child) {
|
||||||
|
$output .= $child->FieldHolder();
|
||||||
|
}
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates table for displaying unused files.
|
||||||
|
*
|
||||||
|
* @returns AssetTableField
|
||||||
|
*/
|
||||||
|
protected function getAssetList() {
|
||||||
|
$where = $this->folder->getUnusedFilesListFilter();
|
||||||
|
$assetList = new AssetTableField(
|
||||||
|
$this->folder,
|
||||||
|
"AssetList",
|
||||||
|
"File",
|
||||||
|
array("Title" => _t('Folder.TITLE', "Title"), "LinkedURL" => _t('Folder.FILENAME', "Filename")),
|
||||||
|
"",
|
||||||
|
$where
|
||||||
|
);
|
||||||
|
$assetList->setPopupCaption(_t('Folder.VIEWASSET', "View Asset"));
|
||||||
|
$assetList->setPermissions(array("show","delete"));
|
||||||
|
$assetList->Markable = false;
|
||||||
|
return $assetList;
|
||||||
|
}
|
||||||
|
}
|
55
code/SiteTreeFolderDecorator.php
Normal file
55
code/SiteTreeFolderDecorator.php
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
class SiteTreeFolderDecorator extends DataObjectDecorator {
|
||||||
|
|
||||||
|
function updateCMSFields(&$fields) {
|
||||||
|
// TODO commenting out unused files tab till bugs are fixed
|
||||||
|
// $fields->push(new Tab("UnusedFiles", _t('Folder.UNUSEDFILESTAB', "Unused files"),
|
||||||
|
// new Folder_UnusedAssetsField($this)
|
||||||
|
// ));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Looks for files used in system and create where clause which contains all ID's of files.
|
||||||
|
*
|
||||||
|
* @returns String where clause which will work as filter.
|
||||||
|
*/
|
||||||
|
public function getUnusedFilesListFilter() {
|
||||||
|
$result = DB::query("SELECT DISTINCT \"FileID\" FROM \"SiteTree_ImageTracking\"");
|
||||||
|
$usedFiles = array();
|
||||||
|
$where = '';
|
||||||
|
$classes = ClassInfo::subclassesFor('SiteTree');
|
||||||
|
|
||||||
|
if($result->numRecords() > 0) {
|
||||||
|
while($nextResult = $result->next()) {
|
||||||
|
$where .= $nextResult['FileID'] . ',';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($classes as $className) {
|
||||||
|
$query = singleton($className)->extendedSQL();
|
||||||
|
$ids = $query->execute()->column();
|
||||||
|
if(!count($ids)) continue;
|
||||||
|
|
||||||
|
foreach(singleton($className)->has_one() as $relName => $joinClass) {
|
||||||
|
if($joinClass == 'Image' || $joinClass == 'File') {
|
||||||
|
$fieldName = $relName .'ID';
|
||||||
|
$query = singleton($className)->extendedSQL("$fieldName > 0");
|
||||||
|
$query->distinct = true;
|
||||||
|
$query->select = array($fieldName);
|
||||||
|
$usedFiles = array_merge($usedFiles, $query->execute()->column());
|
||||||
|
|
||||||
|
} elseif($joinClass == 'Folder') {
|
||||||
|
// @todo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if($usedFiles) {
|
||||||
|
return "\"File\".\"ID\" NOT IN (" . implode(', ', $usedFiles) . ") AND (\"ClassName\" = 'File' OR \"ClassName\" = 'Image')";
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return "(\"ClassName\" = 'File' OR \"ClassName\" = 'Image')";
|
||||||
|
}
|
||||||
|
return $where;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user