diff --git a/filesystem/Folder.php b/filesystem/Folder.php index 8c4145a4a..f18daa34b 100755 --- a/filesystem/Folder.php +++ b/filesystem/Folder.php @@ -391,8 +391,7 @@ class Folder extends File { new LiteralField("UploadIframe", $this->getUploadIframe() ) - )/* This has been disabled for now because of it's mass memory consumption - , + ), new Tab(_t('AssetAdmin.UNUSEDFILESTAB', "Unused files"), new LiteralField("UnusedAssets", "

"._t('AssetAdmin.UNUSEDFILESTITLE', 'Unused files')."

" @@ -405,7 +404,7 @@ class Folder extends File {
" ) - )*/ + ) ), new HiddenField("ID") ); @@ -414,6 +413,69 @@ class Folder extends File { return $fields; } + + /** + * Creates table for displaying unused files. + * + * @returns AssetTableField + */ + protected function getAssetList() { + $where = $this->getUsedFilesList(); + $assetList = new AssetTableField( + $this, + "AssetList", + "File", + array("Title" => _t('AssetAdmin.TITLE', "Title"), "LinkedURL" => _t('AssetAdmin.FILENAME', "Filename")), + "", + $where + ); + $assetList->setPopupCaption(_t('AssetAdmin.VIEWASSET', "View Asset")); + $assetList->setPermissions(array("show","delete")); + $assetList->Markable = false; + return $assetList; + + } + + /** + * 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. + */ + protected function getUsedFilesList() { + $result = DB::query("SELECT DISTINCT FileID FROM SiteTree_ImageTracking"); + $usedFiles = array(); + $where = ""; + if($result->numRecords() > 0) { + while($nextResult = $result->next()){ + $where .= $nextResult['FileID'] . ','; + } + } + $classes = ClassInfo::subclassesFor('SiteTree'); + foreach($classes as $className) { + $query = singleton($className)->extendedSQL(); + $ids = $query->execute()->column(); + if(!count($ids)) continue; + + foreach(singleton($className)->has_one() as $fieldName => $joinClass) { + if($joinClass == 'Image' || $joinClass == 'File') { + foreach($ids as $id) { + $object = DataObject::get_by_id($className, $id); + if($object->$fieldName != NULL) $usedFiles[] = $object->$fieldName; + unset($object); + } + } elseif($joinClass == 'Folder') { + // @todo + } + } + } + foreach($usedFiles as $file) { + $where .= $file->ID . ','; + } + if($where == "") return "(ClassName = 'File' OR ClassName = 'Image')"; + $where = substr($where,0,strlen($where)-1); + $where = "`File`.ID NOT IN (" . $where . ") AND (ClassName = 'File' OR ClassName = 'Image')"; + return $where; + } /** * Display the upload form. Returns an iframe tag that will show admin/assets/uploadiframe.