2007-07-19 12:40:05 +02:00
< ? php
/**
* AssetAdmin is the 'file store' section of the CMS .
* It provides an interface for maniupating the File and Folder objects in the system .
2008-04-06 10:20:13 +02:00
*
2008-02-25 03:10:37 +01:00
* @ package cms
* @ subpackage assets
2007-07-19 12:40:05 +02:00
*/
class AssetAdmin extends LeftAndMain {
2008-04-06 10:20:13 +02:00
2008-11-02 22:27:55 +01:00
static $url_segment = 'assets' ;
static $url_rule = '/$Action/$ID' ;
static $menu_title = 'Files & Images' ;
2009-11-21 04:20:23 +01:00
public static $tree_class = 'Folder' ;
2008-04-06 10:20:13 +02:00
/**
2008-10-02 02:34:41 +02:00
* @ see Upload -> allowedMaxFileSize
2008-04-06 10:20:13 +02:00
* @ var int
*/
public static $allowed_max_file_size ;
2008-02-25 03:10:37 +01:00
static $allowed_actions = array (
'deleteUnusedThumbnails' ,
'doUpload' ,
'getsubtree' ,
'save' ,
'uploadiframe' ,
2008-08-09 07:57:44 +02:00
'UploadForm' ,
2009-11-21 04:19:05 +01:00
'deleteUnusedThumbnails' => 'ADMIN' ,
'batchactions' ,
'BatchActionsForm'
2008-02-25 03:10:37 +01:00
);
2007-07-19 12:40:05 +02:00
/**
* Return fake - ID " root " if no ID is found ( needed to upload files into the root - folder )
*/
public function currentPageID () {
if ( isset ( $_REQUEST [ 'ID' ]) && is_numeric ( $_REQUEST [ 'ID' ])) {
return $_REQUEST [ 'ID' ];
} elseif ( is_numeric ( $this -> urlParams [ 'ID' ])) {
return $this -> urlParams [ 'ID' ];
} elseif ( is_numeric ( Session :: get ( " { $this -> class } .currentPage " ))) {
return Session :: get ( " { $this -> class } .currentPage " );
} else {
return " root " ;
}
}
/**
* Set up the controller , in particular , re - sync the File database with the assets folder ./
*/
function init () {
parent :: init ();
2007-10-29 03:10:59 +01:00
2009-11-21 04:20:32 +01:00
// Create base folder if it doesnt exist already
if ( ! file_exists ( ASSETS_PATH )) Filesystem :: makeFolder ( ASSETS_PATH );
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
Requirements :: javascript ( CMS_DIR . " /javascript/AssetAdmin.js " );
Requirements :: javascript ( CMS_DIR . " /javascript/CMSMain_upload.js " );
Requirements :: javascript ( CMS_DIR . " /javascript/Upload.js " );
2009-11-21 04:21:16 +01:00
Requirements :: javascript ( SAPPHIRE_DIR . " /thirdparty/swfupload/swfupload/swfupload.js " );
2009-11-21 04:20:34 +01:00
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
Requirements :: css ( CMS_DIR . " /css/AssetAdmin.css " );
2008-11-22 04:47:56 +01:00
Requirements :: customScript ( <<< JS
_TREE_ICONS = {};
_TREE_ICONS [ 'Folder' ] = {
2009-11-21 04:16:54 +01:00
fileIcon : 'sapphire/javascript/tree/images/page-closedfolder.gif' ,
openFolderIcon : 'sapphire/javascript/tree/images/page-openfolder.gif' ,
closedFolderIcon : 'sapphire/javascript/tree/images/page-closedfolder.gif'
2008-11-22 04:47:56 +01:00
};
JS
);
2009-11-21 04:19:05 +01:00
CMSBatchActionHandler :: register ( 'delete' , 'AssetAdmin_DeleteBatchAction' , 'Folder' );
2007-07-19 12:40:05 +02:00
}
2009-11-21 04:19:47 +01:00
2007-07-19 12:40:05 +02:00
/**
* Show the content of the upload iframe . The form is specified by a template .
*/
function uploadiframe () {
Requirements :: clear ();
2009-11-21 03:36:38 +01:00
Requirements :: javascript ( SAPPHIRE_DIR . " /thirdparty/prototype/prototype.js " );
Requirements :: javascript ( SAPPHIRE_DIR . " /thirdparty/behaviour/behaviour.js " );
2009-11-21 04:18:15 +01:00
//Requirements::javascript(CMS_DIR . "/javascript/LeftAndMain.js");
Requirements :: javascript ( CMS_DIR . " /thirdparty/multifile/multifile.js " );
2009-11-21 04:19:35 +01:00
Requirements :: css ( CMS_DIR . " /thirdparty/multifile/multifile.css " );
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
Requirements :: css ( CMS_DIR . " /css/typography.css " );
Requirements :: css ( CMS_DIR . " /css/layout.css " );
Requirements :: css ( CMS_DIR . " /css/cms_left.css " );
Requirements :: css ( CMS_DIR . " /css/cms_right.css " );
2007-07-19 12:40:05 +02:00
if ( isset ( $data [ 'ID' ]) && $data [ 'ID' ] != 'root' ) $folder = DataObject :: get_by_id ( " Folder " , $data [ 'ID' ]);
else $folder = singleton ( 'Folder' );
2008-11-07 13:21:41 +01:00
return array ( 'CanUpload' => $folder -> canEdit ());
2007-07-19 12:40:05 +02:00
}
/**
* Return the form object shown in the uploadiframe .
*/
function UploadForm () {
2009-01-05 07:17:59 +01:00
// disabled flash upload javascript (CMSMain_upload()) below,
// see r54952 (originally committed in r42014)
2008-04-26 08:39:23 +02:00
$form = new Form ( $this , 'UploadForm' , new FieldSet (
2007-07-19 12:40:05 +02:00
new HiddenField ( " ID " , " " , $this -> currentPageID ()),
2009-11-21 03:01:06 +01:00
new HiddenField ( " FolderID " , " " , $this -> currentPageID ()),
2007-07-19 12:40:05 +02:00
// needed because the button-action is triggered outside the iframe
new HiddenField ( " action_doUpload " , " " , " 1 " ),
2009-02-03 04:46:15 +01:00
new FileField ( " Files[0] " , _t ( 'AssetAdmin.CHOOSEFILE' , 'Choose file: ' )),
2007-07-19 12:40:05 +02:00
new LiteralField ( 'UploadButton' , "
2009-07-02 09:23:29 +02:00
< input type = \ " submit \" value= \" " . _t ( 'AssetAdmin.UPLOAD' , 'Upload Files Listed Below' ) . " \" name= \" action_upload \" id= \" Form_UploadForm_action_upload \" class= \" action \" />
2007-07-19 12:40:05 +02:00
" ),
new LiteralField ( 'MultifileCode' , "
2007-09-16 18:33:05 +02:00
< p > " . _t('AssetAdmin.FILESREADY','Files ready to upload:') . " </ p >
2009-07-02 09:23:29 +02:00
< div id = \ " Form_UploadForm_FilesList \" ></div>
2007-07-19 12:40:05 +02:00
" )
), new FieldSet (
));
2008-04-26 08:39:23 +02:00
// Makes ajax easier
$form -> disableSecurityToken ();
return $form ;
2007-07-19 12:40:05 +02:00
}
/**
* This method processes the results of the UploadForm .
* It will save the uploaded files to / assets / and create new File objects as required .
*/
function doUpload ( $data , $form ) {
2009-11-12 23:06:37 +01:00
$processedFiles = array ();
2010-04-12 10:47:46 +02:00
$newFiles = array ();
$fileIDs = array ();
$fileNames = array ();
$fileSizeWarnings = '' ;
$errorsArr = '' ;
$status = '' ;
$statusMessage = '' ;
2009-11-12 23:06:37 +01:00
if ( ! isset ( $data [ 'Files' ])) return Director :: set_status_code ( " 404 " );
if ( is_array ( $data [ 'Files' ])) {
foreach ( $data [ 'Files' ] as $param => $files ) {
if ( ! is_array ( $files )) $files = array ( $files );
foreach ( $files as $key => $value ) {
$processedFiles [ $key ][ $param ] = $value ;
}
2007-07-19 12:40:05 +02:00
}
2010-04-12 10:47:46 +02:00
} else {
2009-11-12 23:06:37 +01:00
$proccessedFiles [] = $data [ 'Files' ];
}
// get the folder to upload to.
2010-04-13 08:59:09 +02:00
if ( isset ( $data [ 'FolderID' ]) && $data [ 'FolderID' ] && $data [ 'FolderID' ] != " root " ) {
2009-11-12 23:06:37 +01:00
$folder = DataObject :: get_by_id ( 'Folder' , $data [ 'FolderID' ]);
2010-04-12 10:47:46 +02:00
} else {
2010-04-13 08:59:09 +02:00
$folder = singleton ( 'Folder' );
2009-11-12 23:06:37 +01:00
}
2007-07-19 12:40:05 +02:00
2008-04-06 10:20:13 +02:00
foreach ( $processedFiles as $tmpFile ) {
if ( $tmpFile [ 'error' ] == UPLOAD_ERR_NO_TMP_DIR ) {
2009-11-21 04:19:35 +01:00
$errorsArr [] = _t ( 'AssetAdmin.NOTEMP' , 'There is no temporary folder for uploads. Please set upload_tmp_dir in php.ini.' );
2007-08-21 04:32:20 +02:00
break ;
}
2008-04-06 10:20:13 +02:00
if ( $tmpFile [ 'tmp_name' ]) {
2007-08-21 04:32:20 +02:00
// Workaround open_basedir problems
if ( ini_get ( " open_basedir " )) {
2008-04-06 10:20:13 +02:00
$newtmp = TEMP_FOLDER . '/' . $tmpFile [ 'name' ];
move_uploaded_file ( $tmpFile [ 'tmp_name' ], $newtmp );
$tmpFile [ 'tmp_name' ] = $newtmp ;
2007-08-21 04:32:20 +02:00
}
2007-07-19 12:40:05 +02:00
2008-04-06 10:20:13 +02:00
// validate files (only if not logged in as admin)
2010-04-13 05:55:52 +02:00
if ( ! File :: $apply_restrictions_to_admin && Permission :: check ( 'ADMIN' )) {
2008-04-06 10:20:13 +02:00
$valid = true ;
2007-07-19 12:40:05 +02:00
} else {
2008-04-06 10:20:13 +02:00
$upload = new Upload ();
2010-04-13 05:55:52 +02:00
$upload -> setAllowedExtensions ( File :: $allowed_extensions );
2008-04-06 10:20:13 +02:00
$upload -> setAllowedMaxFileSize ( self :: $allowed_max_file_size );
$valid = $upload -> validate ( $tmpFile );
if ( ! $valid ) {
2009-11-21 04:19:35 +01:00
$errorsArr = $upload -> getErrors ();
2008-04-06 10:20:13 +02:00
}
2007-07-19 12:40:05 +02:00
}
2008-04-06 10:20:13 +02:00
// move file to given folder
if ( $valid ) $newFiles [] = $folder -> addUploadToFolder ( $tmpFile );
2007-07-19 12:40:05 +02:00
}
}
2009-11-21 04:19:35 +01:00
2007-07-19 12:40:05 +02:00
if ( $newFiles ) {
$numFiles = sizeof ( $newFiles );
2010-04-12 10:47:46 +02:00
$statusMessage = sprintf ( _t ( 'AssetAdmin.UPLOADEDX' , " Uploaded %s files " ), $numFiles );
2007-07-19 12:40:05 +02:00
$status = " good " ;
2009-11-21 04:19:35 +01:00
} else if ( $errorsArr ) {
$statusMessage = implode ( '\n' , $errorsArr );
$status = 'bad' ;
} else {
2007-09-16 18:33:05 +02:00
$statusMessage = _t ( 'AssetAdmin.NOTHINGTOUPLOAD' , 'There was nothing to upload' );
2007-07-19 12:40:05 +02:00
$status = " " ;
}
2009-11-12 23:06:37 +01:00
2008-02-25 03:10:37 +01:00
foreach ( $newFiles as $newFile ) {
$fileIDs [] = $newFile ;
2008-11-24 10:30:41 +01:00
$fileObj = DataObject :: get_one ( 'File' , " \" File \" . \" ID \" = $newFile " );
2009-04-29 03:44:28 +02:00
// notify file object after uploading
if ( method_exists ( $fileObj , 'onAfterUpload' )) $fileObj -> onAfterUpload ();
2008-02-25 03:10:37 +01:00
$fileNames [] = $fileObj -> Name ;
}
2009-11-21 04:19:35 +01:00
// TODO Replace with clientside logic which doesn't have assumptions in the response
2007-07-19 12:40:05 +02:00
echo <<< HTML
< script type = " text/javascript " >
2009-11-21 04:19:35 +01:00
var url = parent . document . getElementById ( 'sitetree' ) . getTreeNodeByIdx ( " { $folder -> ID } " ) . getElementsByTagName ( 'a' )[ 0 ] . href ;
2010-04-13 07:55:56 +02:00
parent . jQuery ( '#Form_EditForm' ) . entwine ( 'ss' ) . loadForm ( url );
2007-07-19 12:40:05 +02:00
parent . statusMessage ( " { $statusMessage } " , " { $status } " );
</ script >
HTML ;
}
2009-04-29 03:44:28 +02:00
/**
* Custom currentPage () method to handle opening the 'root' folder
*/
public function currentPage () {
$id = $this -> currentPageID ();
if ( $id && is_numeric ( $id )) {
2009-11-21 04:20:26 +01:00
return DataObject :: get_by_id ( 'File' , $id );
2009-04-29 03:44:28 +02:00
} else if ( $id == 'root' ) {
2009-11-21 04:20:26 +01:00
return singleton ( 'File' );
2009-04-29 03:44:28 +02:00
}
}
2009-11-21 04:21:00 +01:00
2007-07-19 12:40:05 +02:00
public function SiteTreeAsUL () {
2009-11-21 04:21:00 +01:00
return $this -> getSiteTreeFor ( $this -> stat ( 'tree_class' ), null , 'ChildFolders' );
2007-07-19 12:40:05 +02:00
}
2010-04-12 11:22:43 +02:00
function getSiteTreeFor ( $className , $rootID = null , $childrenMethod = null , $numChildrenMethod = null , $filterFunction = null , $minNodeCount = 30 ) {
2009-11-21 04:21:03 +01:00
if ( ! $childrenMethod ) $childrenMethod = 'ChildFolders' ;
2010-04-12 11:22:43 +02:00
return parent :: getSiteTreeFor ( $className , $rootID , $childrenMethod , $numChildrenMethod , $filterFunction , $minNodeCount );
2009-11-21 04:21:03 +01:00
}
2009-11-21 04:21:00 +01:00
public function getCMSTreeTitle () {
return Director :: absoluteBaseURL () . " assets " ;
}
2007-07-19 12:40:05 +02:00
//------------------------------------------------------------------------------------------//
// Data saving handlers
2008-02-25 03:10:37 +01:00
2009-11-21 04:19:02 +01:00
/**
* @ return Form
*/
function SyncForm () {
$form = new Form (
$this ,
'SyncForm' ,
2009-11-21 04:21:00 +01:00
new FieldSet (
),
2009-11-21 04:19:02 +01:00
new FieldSet (
$btn = new FormAction ( 'doSync' , _t ( 'FILESYSTEMSYNC' , 'Look for new files' ))
)
);
$form -> addExtraClass ( 'actionparams' );
$form -> setFormMethod ( 'GET' );
$form -> setFormAction ( 'dev/tasks/FilesystemSyncTask' );
$btn -> describe ( _t ( 'AssetAdmin_left.ss.FILESYSTEMSYNC_DESC' , 'SilverStripe maintains its own database of the files & images stored in your assets/ folder. Click this button to update that database, if files are added to the assets/ folder from outside SilverStripe, for example, if you have uploaded files via FTP.' ));
return $form ;
}
2007-07-19 12:40:05 +02:00
2007-10-02 23:34:57 +02:00
/**
* #################################
* Garbage collection .
* #################################
*/
/**
2009-02-03 04:46:15 +01:00
* Removes all unused thumbnails from the file store
* and returns the status of the process to the user .
2008-12-04 23:38:58 +01:00
*/
2009-02-03 04:46:15 +01:00
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 ();
2007-10-02 23:34:57 +02:00
}
/**
2008-12-04 23:38:58 +01:00
* Creates array containg all unused thumbnails .
*
* Array is created in three steps :
2009-02-03 04:46:15 +01:00
* 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 )
2008-12-04 23:38:58 +01:00
*
* @ return array
*/
2009-02-03 04:46:15 +01:00
private function getUnusedThumbnails () {
2008-12-04 23:38:58 +01:00
$allThumbnails = array ();
$usedThumbnails = array ();
$dirIterator = new RecursiveIteratorIterator ( new RecursiveDirectoryIterator ( ASSETS_PATH ));
2009-02-03 04:46:15 +01:00
$classes = ClassInfo :: subclassesFor ( 'SiteTree' );
2008-12-04 23:38:58 +01:00
2009-02-03 04:46:15 +01:00
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 );
}
2008-12-04 23:38:58 +01:00
}
}
}
}
2009-02-03 04:46:15 +01:00
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 );
}
}
2008-12-04 23:38:58 +01:00
}
}
}
}
}
}
2009-02-03 04:46:15 +01:00
return array_diff ( $allThumbnails , $usedThumbnails );
2008-12-04 23:38:58 +01:00
}
2009-02-03 04:46:15 +01:00
2007-11-01 21:58:28 +01:00
}
2009-11-21 04:19:05 +01:00
/**
* Delete multiple { @ link Folder } records ( and the associated filesystem nodes ) .
* Usually used through the { @ link AssetAdmin } interface .
*
* @ package cms
* @ subpackage batchactions
*/
class AssetAdmin_DeleteBatchAction extends CMSBatchAction {
function getActionTitle () {
// _t('AssetAdmin_left.ss.SELECTTODEL','Select the folders that you want to delete and then click the button below')
return _t ( 'AssetAdmin_DeleteBatchAction.TITLE' , 'Delete folders' );
}
function run ( DataObjectSet $records ) {
$status = array (
'modified' => array (),
'deleted' => array ()
);
foreach ( $records as $record ) {
$id = $record -> ID ;
// Perform the action
if ( $record -> canDelete ()) $record -> delete ();
$status [ 'deleted' ][ $id ] = array ();
$record -> destroy ();
unset ( $record );
}
return Convert :: raw2json ( $status );
}
}
2009-04-29 03:44:28 +02:00
?>