2011-03-23 00:25:02 +01:00
|
|
|
<?php
|
2013-06-21 00:45:33 +02:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Model;
|
|
|
|
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
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
|
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
class SiteTreeFolderExtension extends DataExtension
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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('SilverStripe\\CMS\\Model\\SiteTree');
|
|
|
|
|
|
|
|
if ($result->numRecords() > 0) {
|
|
|
|
while ($nextResult = $result->next()) {
|
|
|
|
$where .= $nextResult['FileID'] . ',';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($classes as $className) {
|
|
|
|
$query = new DataQuery($className);
|
|
|
|
$ids = $query->execute()->column();
|
|
|
|
if (!count($ids)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (singleton($className)->hasOne() as $relName => $joinClass) {
|
|
|
|
if ($joinClass == 'SilverStripe\\Assets\\Image' || $joinClass == 'SilverStripe\\Assets\\File') {
|
|
|
|
$fieldName = $relName .'ID';
|
|
|
|
$query = DataList::create($className)->where("$fieldName > 0");
|
|
|
|
$query->distinct = true;
|
|
|
|
$query->select(array($fieldName));
|
|
|
|
$usedFiles = array_merge($usedFiles, $query->execute()->column());
|
|
|
|
} elseif ($joinClass == 'SilverStripe\\Assets\\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; // @todo - How?
|
|
|
|
}
|
2012-03-27 06:05:11 +02:00
|
|
|
}
|