|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\PerfectCmsImages\Tasks; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Director; |
|
6
|
|
|
use SilverStripe\Control\Controller; |
|
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
8
|
|
|
use SilverStripe\Dev\BuildTask; |
|
9
|
|
|
use SilverStripe\ORM\DB; |
|
10
|
|
|
|
|
11
|
|
|
use SilverStripe\Assets\Image; |
|
12
|
|
|
use SilverStripe\Assets\Folder; |
|
13
|
|
|
|
|
14
|
|
|
use Sunnysideup\PerfectCmsImages\Api\PerfectCMSImages; |
|
15
|
|
|
|
|
16
|
|
|
class MoveUnexpectedImages extends BuildTask |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Folder |
|
21
|
|
|
*/ |
|
22
|
|
|
public $unusedImagesFolder = null; |
|
23
|
|
|
|
|
24
|
|
|
private static $unused_images_folder_name = 'unusedimages'; |
|
25
|
|
|
|
|
26
|
|
|
public function getTitle(): string |
|
27
|
|
|
{ |
|
28
|
|
|
return 'Careful: experimental - DELETE ALL IMAGES THAT ARE IN A FOLDER AND SHOULD NOT BE THERE'; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getDescription(): string |
|
32
|
|
|
{ |
|
33
|
|
|
return 'Goes through all the perfect cms images, checks what folder they write to and moves any images that should not be there.'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create test jobs for the purposes of testing. |
|
38
|
|
|
* |
|
39
|
|
|
* @param HTTPRequest $request |
|
40
|
|
|
*/ |
|
41
|
|
|
public function run($request) // phpcs:ignore |
|
42
|
|
|
{ |
|
43
|
|
|
$this->unusedImagesFolder = Folder::find_or_make($this->config()->get('unused_images_folder_name')); |
|
44
|
|
|
$data = PerfectCMSImages::get_all_values_for_images(); |
|
45
|
|
|
|
|
46
|
|
|
// check folders |
|
47
|
|
|
$folderArray = []; |
|
48
|
|
|
foreach($data as $name => $dataInner) { |
|
49
|
|
|
$folder = $dataInner['folder'] ?? ''; |
|
50
|
|
|
if($folder) { |
|
51
|
|
|
$folderArray[$folder] = []; |
|
52
|
|
|
$classes = $dataInner['used_by'] ?? []; |
|
53
|
|
|
foreach($classes as $class) { |
|
54
|
|
|
$folderArray[$folder][] = $class; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$folderArray = []; |
|
60
|
|
|
$listOfImageIds = []; |
|
61
|
|
|
foreach($folderArray as $folderName => $classAndMethodList) { |
|
62
|
|
|
|
|
63
|
|
|
// find all images that should be there... |
|
64
|
|
|
$listOfIds = []; |
|
65
|
|
|
foreach($classAndMethodList as $classAndMethod) { |
|
66
|
|
|
list($class, $method) = explode('.', $classAndMethod); |
|
67
|
|
|
$listOfImageIds = array_merge( |
|
68
|
|
|
$listOfImageIds, |
|
69
|
|
|
$class::get()->columnUnique($method.'ID') |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
// remove files |
|
73
|
|
|
if (count($listOfIds)) { |
|
74
|
|
|
$this->removeUnusedFiles($folderName, $listOfImageIds); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
protected function removeUnusedFiles(string $folderName, array $listOfImageIds) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
$folder = Folder::find_or_make($this->config()->get('unused_images_folder_name')); |
|
83
|
|
|
$unusedFolderName = $this->unusedImagesFolder->Name; |
|
84
|
|
|
$where = " ParentID = " . $folder->ID. ' AND File.ID NOT IN('.implode('.$listOfImageIds.').')'; |
|
|
|
|
|
|
85
|
|
|
$unused = Image::get()->where($where); |
|
86
|
|
|
if ($unused->exists()) { |
|
87
|
|
|
foreach ($unused as $file) { |
|
88
|
|
|
$oldName = $file->getFullPath(); |
|
89
|
|
|
$file->ParentID = $this->unusedImagesFolder->ID; |
|
90
|
|
|
$file->write(); |
|
91
|
|
|
$file->doPublish(); |
|
92
|
|
|
$newName = str_replace($folder->Name, $unusedFolderName, $oldName); |
|
93
|
|
|
$oldNameFull = Controller::join_links(ASSETS_PATH, $oldName); |
|
94
|
|
|
$newNameFull = Controller::join_links(ASSETS_PATH, $newName); |
|
95
|
|
|
if (file_exists($oldNameFull) && $newNameFull !== $oldNameFull) { |
|
96
|
|
|
if(file_exists($newNameFull)) { |
|
97
|
|
|
unlink($newNameFull); |
|
98
|
|
|
} |
|
99
|
|
|
rename($oldNameFull, $newNameFull); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.