silverstripe-cms/code/Model/SiteTreeFolderExtension.php

89 lines
3.1 KiB
PHP
Raw Normal View History

<?php
2016-07-22 01:32:32 +02:00
namespace SilverStripe\CMS\Model;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Shortcodes\FileLink;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Convert;
use SilverStripe\Dev\Deprecation;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
/**
2022-10-18 04:23:59 +02:00
* @deprecated 4.2.0 Will be removed without equivalent functionality to replace it
*/
2017-01-25 21:59:25 +01:00
class SiteTreeFolderExtension extends DataExtension
{
public function __construct()
{
2022-10-18 04:23:59 +02:00
Deprecation::notice('4.2.0', 'Will be removed without equivalent functionality to replace it', Deprecation::SCOPE_CLASS);
parent::__construct();
}
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.
*
2022-10-18 04:23:59 +02:00
* @deprecated 4.2.0 Will be removed without equivalent functionality to replace it
* @returns string where clause which will work as filter.
2017-01-25 21:59:25 +01:00
*/
public function getUnusedFilesListFilter()
{
2022-10-18 04:23:59 +02:00
Deprecation::notice('4.2.0', 'Will be removed without equivalent functionality to replace it');
// Add all records in link tracking
$usedFiles = FileLink::get()->column('LinkedID');
// Get all classes that aren't folder
$fileClasses = array_diff_key(
2022-04-13 07:07:59 +02:00
ClassInfo::subclassesFor(File::class) ?? [],
ClassInfo::subclassesFor(Folder::class)
);
// Search on a class-by-class basis
$classes = ClassInfo::subclassesFor(SiteTree::class);
2017-01-25 21:59:25 +01:00
$schema = DataObject::getSchema();
2017-01-25 21:59:25 +01:00
foreach ($classes as $className) {
// 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;
}
$where = [];
$columns = [];
foreach ($hasOnes as $relName => $joinClass) {
2022-04-13 07:07:59 +02:00
if (in_array($joinClass, $fileClasses ?? [])) {
$column = $relName . 'ID';
$columns[] = $column;
$quotedColumn = $schema->sqlColumnForField($className, $column);
$where[] = "{$quotedColumn} > 0";
2017-01-25 21:59:25 +01: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
}
// 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) {
return "\"File\".\"ID\" NOT IN (" . implode(', ', $usedFiles) . ") AND $classFilter";
2017-01-25 21:59:25 +01:00
} else {
return $classFilter;
2017-01-25 21:59:25 +01:00
}
}
}