Skip to content

Commit

Permalink
MINOR Code formatting for delete unused thumbnails functionality
Browse files Browse the repository at this point in the history
MINOR Use FormResponse instead of echoing strings as JS

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.3@70678 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
Sean Harvey authored and Sam Minnee committed Feb 2, 2011
1 parent b686f45 commit 39b10d2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 37 deletions.
90 changes: 55 additions & 35 deletions code/AssetAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,67 +616,87 @@ public function save($urlParams, $form) {
*/

/**
* Removes all unused thumbnails, and echos status message to user.
* Removes all unused thumbnails from the file store
* and returns the status of the process to the user.
*/
public function deleteUnusedThumbnails() {
foreach($this->getUnusedThumbnailsArray() as $file) {
unlink(ASSETS_PATH . "/" . $file);
}
echo "statusMessage('"._t('AssetAdmin.THUMBSDELETED', 'All unused thumbnails have been deleted')."','good')";
public function deleteunusedthumbnails() {
$count = 0;
$thumbnails = $this->getUnusedThumbnails();

if($thumbnails) {
foreach($thumbnails as $thumbnail) {
unlink(ASSETS_PATH . "/" . $thumbnail);
$count++;
}
}

$message = sprintf(_t('AssetAdmin.THUMBSDELETED', '%s unused thumbnails have been deleted'), $count);
FormResponse::status_message($message, 'good');
echo FormResponse::respond();
}

/**
* Creates array containg all unused thumbnails.
*
* Array is created in three steps:
* 1.Scan assets folder and retrieve all thumbnails
* 2.Scan all HTMLField in system and retrieve thumbnails from them.
* 3.Count difference between two sets (array_diff)
* 1. Scan assets folder and retrieve all thumbnails
* 2. Scan all HTMLField in system and retrieve thumbnails from them.
* 3. Count difference between two sets (array_diff)
*
* @return array
*/
private function getUnusedThumbnailsArray() {
private function getUnusedThumbnails() {
$allThumbnails = array();
$usedThumbnails = array();
$dirIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(ASSETS_PATH));
$classes = ClassInfo::subclassesFor('SiteTree');

foreach($dirIterator as $file) {
if($file->isFile()) {
if(strpos($file->getPathname(),"_resampled") !== false) {
$pathInfo = pathinfo($file->getPathname());
if(in_array(strtolower($pathInfo['extension']), array('jpeg', 'jpg', 'jpe', 'png', 'gif'))) {
$path = str_replace('\\','/', $file->getPathname());
$allThumbnails[] = substr($path, strpos($path, '/assets/') + 8);
if($dirIterator) {
foreach($dirIterator as $file) {
if($file->isFile()) {
if(strpos($file->getPathname(), '_resampled') !== false) {
$pathInfo = pathinfo($file->getPathname());
if(in_array(strtolower($pathInfo['extension']), array('jpeg', 'jpg', 'jpe', 'png', 'gif'))) {
$path = str_replace('\\','/', $file->getPathname());
$allThumbnails[] = substr($path, strpos($path, '/assets/') + 8);
}
}
}
}
}

$classes = ClassInfo::subclassesFor('SiteTree');

foreach($classes as $className) {
$sng = singleton($className);
$objects = DataObject::get($className);
if($objects !== NULL) {
foreach($objects as $object) {
foreach($sng->db() as $fieldName => $fieldType) {
if($fieldType == 'HTMLText') {
$url1 = HTTP::findByTagAndAttribute($object->$fieldName,array("img" => "src"));
if($url1 != NULL) $usedThumbnails[] = substr($url1[0],strpos($url1[0],'/assets/')+8);
if($object->latestPublished > 0) {
$object = Versioned::get_latest_version($className, $object->ID);
$url2 = HTTP::findByTagAndAttribute($object->$fieldName,array("img" => "src"));
if($url2 != NULL) $usedThumbnails[] = substr($url2[0],strpos($url2[0],'/assets/')+8);
if($classes) {
foreach($classes as $className) {
$SNG_class = singleton($className);
$objects = DataObject::get($className);

if($objects !== NULL) {
foreach($objects as $object) {
foreach($SNG_class->db() as $fieldName => $fieldType) {
if($fieldType == 'HTMLText') {
$url1 = HTTP::findByTagAndAttribute($object->$fieldName,array('img' => 'src'));

if($url1 != NULL) {
$usedThumbnails[] = substr($url1[0], strpos($url1[0], '/assets/') + 8);
}

if($object->latestPublished > 0) {
$object = Versioned::get_latest_version($className, $object->ID);
$url2 = HTTP::findByTagAndAttribute($object->$fieldName, array('img' => 'src'));

if($url2 != NULL) {
$usedThumbnails[] = substr($url2[0], strpos($url2[0], '/assets/') + 8);
}
}
}
}
}
}
}
}

return array_diff($allThumbnails,$usedThumbnails);
return array_diff($allThumbnails, $usedThumbnails);
}
}

?>
2 changes: 1 addition & 1 deletion javascript/AssetAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ Behaviour.register({
eval(t.responseText);
}
};
new Ajax.Request('admin/assets/deleteUnusedThumbnails',options);
new Ajax.Request('admin/assets/deleteunusedthumbnails',options);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion lang/en_US.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
$lang['en_US']['AssetAdmin']['NOWBROKEN2'] = 'Their owners have been emailed and they will fix up those pages.';
$lang['en_US']['AssetAdmin']['SAVEDFILE'] = 'Saved file %s';
$lang['en_US']['AssetAdmin']['SAVEFOLDERNAME'] = 'Save folder name';
$lang['en_US']['AssetAdmin']['THUMBSDELETED'] = 'All unused thumbnails have been deleted';
$lang['en_US']['AssetAdmin']['THUMBSDELETED'] = '%s unused thumbnails have been deleted';
$lang['en_US']['AssetAdmin']['UPLOAD'] = 'Upload Files Listed Below';
$lang['en_US']['AssetAdmin']['UPLOADEDX'] = 'Uploaded %s files';
$lang['en_US']['AssetAdmin_left.ss']['CREATE'] = 'Create';
Expand Down

0 comments on commit 39b10d2

Please sign in to comment.