2011-03-23 00:25:02 +01:00
|
|
|
<?php
|
2013-06-21 00:45:33 +02:00
|
|
|
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\DataQuery;
|
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
|
2013-06-21 00:45:33 +02:00
|
|
|
/**
|
|
|
|
* @todo Cleanup, refactor, test this class
|
2015-08-24 06:14:33 +02:00
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage model
|
2013-06-21 00:45:33 +02:00
|
|
|
*/
|
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.
|
2016-01-06 00:42:07 +01:00
|
|
|
*
|
2013-06-21 00:45:33 +02:00
|
|
|
* @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');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-23 00:25:02 +01:00
|
|
|
if($result->numRecords() > 0) {
|
|
|
|
while($nextResult = $result->next()) {
|
2016-01-06 00:42:07 +01:00
|
|
|
$where .= $nextResult['FileID'] . ',';
|
2011-03-23 00:25:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2015-03-31 08:54:43 +02:00
|
|
|
foreach(singleton($className)->hasOne() as $relName => $joinClass) {
|
2011-03-23 00:25:02 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-23 00:25:02 +01:00
|
|
|
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
|
|
|
}
|