2011-03-23 00:25:02 +01:00
|
|
|
<?php
|
2013-06-21 00:45:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Cleanup, refactor, test this class
|
|
|
|
*/
|
2011-04-15 11:37:15 +02:00
|
|
|
class SiteTreeFolderExtension extends DataExtension {
|
2012-03-27 06:05:11 +02:00
|
|
|
|
2011-03-23 00:25:02 +01:00
|
|
|
/**
|
2013-06-21 00:45:33 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
2011-03-23 00:25:02 +01:00
|
|
|
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) {
|
2013-01-23 20:38:19 +01:00
|
|
|
$query = new DataQuery($className);
|
2011-03-23 00:25:02 +01:00
|
|
|
$ids = $query->execute()->column();
|
|
|
|
if(!count($ids)) continue;
|
|
|
|
|
|
|
|
foreach(singleton($className)->has_one() as $relName => $joinClass) {
|
|
|
|
if($joinClass == 'Image' || $joinClass == 'File') {
|
|
|
|
$fieldName = $relName .'ID';
|
2013-01-23 20:38:19 +01:00
|
|
|
$query = DataList::create($className)->where("$fieldName > 0");
|
2011-03-23 00:25:02 +01:00
|
|
|
$query->distinct = true;
|
2012-05-01 17:58:14 +02:00
|
|
|
$query->select(array($fieldName));
|
2011-03-23 00:25:02 +01:00
|
|
|
$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')";
|
|
|
|
}
|
2013-06-21 00:45:33 +02:00
|
|
|
return $where; // @todo - How?
|
2011-03-23 00:25:02 +01:00
|
|
|
}
|
2012-03-27 06:05:11 +02:00
|
|
|
}
|