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;
|
|
|
|
|
2017-09-20 03:51:07 +02:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\Folder;
|
2018-04-06 05:53:57 +02:00
|
|
|
use SilverStripe\Assets\Shortcodes\FileLink;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
2017-09-20 03:51:07 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Convert;
|
2018-04-06 05:53:57 +02:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
2017-09-20 03:51:07 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\DataExtension;
|
2017-09-20 03:51:07 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
/**
|
|
|
|
* @deprecated 4.2..5.0 Will be removed in cms 5.0
|
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
class SiteTreeFolderExtension extends DataExtension
|
|
|
|
{
|
2018-04-06 05:53:57 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
Deprecation::notice('5.0', 'Will be removed in 5.0');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
/**
|
|
|
|
* Looks for files used in system and create where clause which contains all ID's of files.
|
|
|
|
*
|
2018-04-06 05:53:57 +02:00
|
|
|
* @deprecated 4.2..5.0
|
2017-09-20 03:51:07 +02:00
|
|
|
* @returns string where clause which will work as filter.
|
2017-01-25 21:59:25 +01:00
|
|
|
*/
|
|
|
|
public function getUnusedFilesListFilter()
|
|
|
|
{
|
2018-04-06 05:53:57 +02:00
|
|
|
Deprecation::notice('5.0', 'Will be removed in 5.0');
|
|
|
|
|
2017-09-20 03:51:07 +02:00
|
|
|
// Add all records in link tracking
|
2018-04-06 05:53:57 +02:00
|
|
|
$usedFiles = FileLink::get()->column('LinkedID');
|
2017-09-20 03:51:07 +02:00
|
|
|
|
|
|
|
// Get all classes that aren't folder
|
|
|
|
$fileClasses = array_diff_key(
|
2022-04-13 07:07:59 +02:00
|
|
|
ClassInfo::subclassesFor(File::class) ?? [],
|
2017-09-20 03:51:07 +02:00
|
|
|
ClassInfo::subclassesFor(Folder::class)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Search on a class-by-class basis
|
|
|
|
$classes = ClassInfo::subclassesFor(SiteTree::class);
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2017-09-20 03:51:07 +02:00
|
|
|
$schema = DataObject::getSchema();
|
2017-01-25 21:59:25 +01:00
|
|
|
foreach ($classes as $className) {
|
2017-09-20 03:51:07 +02:00
|
|
|
// Build query based on all direct has_ones on this class
|
|
|
|
$hasOnes = Config::inst()->get($className, 'has_one', Config::UNINHERITED);
|
|
|
|
if (empty($hasOnes)) {
|
2017-01-25 21:59:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
2017-09-20 03:51:07 +02:00
|
|
|
$where = [];
|
|
|
|
$columns = [];
|
|
|
|
foreach ($hasOnes as $relName => $joinClass) {
|
2022-04-13 07:07:59 +02:00
|
|
|
if (in_array($joinClass, $fileClasses ?? [])) {
|
2017-09-20 03:51:07 +02:00
|
|
|
$column = $relName . 'ID';
|
|
|
|
$columns[] = $column;
|
|
|
|
$quotedColumn = $schema->sqlColumnForField($className, $column);
|
|
|
|
$where[] = "{$quotedColumn} > 0";
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-20 03:51:07 +02:00
|
|
|
|
|
|
|
// Get all records with any file ID in the searched columns
|
|
|
|
$recordsArray = DataList::create($className)->whereAny($where)->toArray();
|
|
|
|
$records = ArrayList::create($recordsArray);
|
|
|
|
foreach ($columns as $column) {
|
|
|
|
$usedFiles = array_unique(array_merge($usedFiles, $records->column($column)));
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
2017-09-20 03:51:07 +02:00
|
|
|
// Create filter based on class and id
|
|
|
|
$classFilter = sprintf(
|
|
|
|
"(\"File\".\"ClassName\" IN (%s))",
|
|
|
|
implode(", ", Convert::raw2sql($fileClasses, true))
|
|
|
|
);
|
2017-01-25 21:59:25 +01:00
|
|
|
if ($usedFiles) {
|
2017-09-20 03:51:07 +02:00
|
|
|
return "\"File\".\"ID\" NOT IN (" . implode(', ', $usedFiles) . ") AND $classFilter";
|
2017-01-25 21:59:25 +01:00
|
|
|
} else {
|
2017-09-20 03:51:07 +02:00
|
|
|
return $classFilter;
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 06:05:11 +02:00
|
|
|
}
|