2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* This class handles the representation of a file on the filesystem within the framework.
|
|
|
|
* Most of the methods also handle the {@link Folder} subclass.
|
|
|
|
*
|
2010-10-13 01:02:10 +02:00
|
|
|
* Note: The files are stored in the assets/ directory, but sapphire
|
2007-07-19 12:40:28 +02:00
|
|
|
* looks at the db object to gather information about a file such as URL
|
2010-10-13 01:02:10 +02:00
|
|
|
* It then uses this for all processing functions (like image manipulation).
|
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* <b>Security</b>
|
|
|
|
*
|
2010-10-15 05:04:27 +02:00
|
|
|
* Caution: It is recommended to disable any script execution in the "assets/"
|
|
|
|
* directory in the webserver configuration, to reduce the risk of exploits.
|
|
|
|
* See http://doc.silverstripe.org/secure-development#filesystem
|
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* <b>Properties</b>
|
|
|
|
*
|
|
|
|
* - "Name": File name (including extension) or folder name.
|
|
|
|
* Should be the same as the actual filesystem.
|
|
|
|
* - "Title": Optional title of the file (for display purposes only).
|
2011-03-23 01:23:43 +01:00
|
|
|
* Defaults to "Name". Note that the Title field of Folder (subclass of File)
|
|
|
|
* is linked to Name, so Name and Title will always be the same.
|
2010-10-15 05:12:39 +02:00
|
|
|
* - "Filename": Path of the file or folder, relative to the webroot.
|
|
|
|
* Usually starts with the "assets/" directory, and has no trailing slash.
|
|
|
|
* Defaults to the "assets/" directory plus "Name" property if not set.
|
|
|
|
* Setting the "Filename" property will override the "Name" property.
|
|
|
|
* The value should be in sync with "ParentID".
|
|
|
|
* - "Content": Typically unused, but handy for a textual representation of
|
|
|
|
* files, e.g. for fulltext indexing of PDF documents.
|
|
|
|
* - "ParentID": Points to a {@link Folder} record. Should be in sync with
|
|
|
|
* "Filename". A ParentID=0 value points to the "assets/" folder, not the webroot.
|
|
|
|
*
|
|
|
|
* <b>Synchronization</b>
|
|
|
|
*
|
|
|
|
* Changes to a File database record can change the filesystem entry,
|
|
|
|
* but not the other way around. If the filesystem path is renamed outside
|
|
|
|
* of SilverStripe, there's no way for the database to recover this linkage.
|
|
|
|
* New physical files on the filesystem can be "discovered" via {@link Filesystem::sync()},
|
|
|
|
* the equivalent {@link File} and {@link Folder} records are automatically
|
|
|
|
* created by this method.
|
|
|
|
*
|
|
|
|
* Certain property changes within the File API that can cause a "delayed" filesystem change:
|
|
|
|
* The change is enforced in {@link onBeforeWrite()} later on.
|
|
|
|
* - setParentID()
|
|
|
|
* - setFilename()
|
|
|
|
* - setName()
|
|
|
|
* It is recommended that you use {@link write()} directly after setting any of these properties,
|
|
|
|
* otherwise getters like {@link getFullPath()} and {@link getRelativePath()}
|
|
|
|
* will result paths that are inconsistent with the filesystem.
|
|
|
|
*
|
|
|
|
* Caution: Calling {@link delete()} will also delete from the filesystem.
|
|
|
|
* Call {@link deleteDatabaseOnly()} if you want to avoid this.
|
|
|
|
*
|
|
|
|
* <b>Creating Files and Folders</b>
|
|
|
|
*
|
|
|
|
* Typically both files and folders should be created first on the filesystem,
|
|
|
|
* and then reflected in as database records. Folders can be created recursively
|
|
|
|
* from sapphire both in the database and filesystem through {@link Folder::findOrMake()}.
|
|
|
|
* Ensure that you always set a "Filename" property when writing to the database,
|
|
|
|
* leaving it out can lead to unexpected results.
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage filesystem
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class File extends DataObject {
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-02-04 20:45:14 +01:00
|
|
|
static $default_sort = "\"Name\"";
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $singular_name = "File";
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $plural_name = "Files";
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $db = array(
|
|
|
|
"Name" => "Varchar(255)",
|
|
|
|
"Title" => "Varchar(255)",
|
2010-10-19 06:05:29 +02:00
|
|
|
"Filename" => "Text",
|
2007-07-19 12:40:28 +02:00
|
|
|
"Content" => "Text",
|
2007-10-02 06:53:19 +02:00
|
|
|
"Sort" => "Int"
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2008-04-06 10:20:13 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $has_one = array(
|
|
|
|
"Parent" => "File",
|
|
|
|
"Owner" => "Member"
|
|
|
|
);
|
2008-04-06 10:20:13 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
static $has_many = array();
|
|
|
|
|
|
|
|
static $many_many = array();
|
2008-04-06 10:20:13 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
static $defaults = array();
|
|
|
|
|
|
|
|
static $extensions = array(
|
|
|
|
"Hierarchy",
|
|
|
|
);
|
2010-04-12 04:35:12 +02:00
|
|
|
|
|
|
|
/**
|
2010-10-15 05:04:27 +02:00
|
|
|
* @var array List of allowed file extensions, enforced through {@link validate()}.
|
2010-12-05 01:05:05 +01:00
|
|
|
*
|
|
|
|
* Note: if you modify this, you should also change a configuration file in the assets directory.
|
|
|
|
* Otherwise, the files will be able to be uploaded but they won't be able to be served by the
|
|
|
|
* webserver.
|
|
|
|
*
|
|
|
|
* - If you are running Apahce you will need to change assets/.htaccess
|
|
|
|
* - If you are running IIS you will need to change assets/web.config
|
|
|
|
*
|
|
|
|
* Instructions for the change you need to make are included in a comment in the config file.
|
2010-04-12 04:35:12 +02:00
|
|
|
*/
|
2010-04-12 04:42:51 +02:00
|
|
|
public static $allowed_extensions = array(
|
2010-10-04 06:42:25 +02:00
|
|
|
'','html','htm','xhtml','js','css',
|
2010-10-15 02:32:26 +02:00
|
|
|
'bmp','png','gif','jpg','jpeg','ico','pcx','tif','tiff',
|
2010-05-25 06:26:31 +02:00
|
|
|
'au','mid','midi','mpa','mp3','ogg','m4a','ra','wma','wav','cda',
|
|
|
|
'avi','mpg','mpeg','asf','wmv','m4v','mov','mkv','mp4','swf','flv','ram','rm',
|
2010-04-12 04:42:51 +02:00
|
|
|
'doc','docx','txt','rtf','xls','xlsx','pages',
|
|
|
|
'ppt','pptx','pps','csv',
|
2010-05-25 06:26:31 +02:00
|
|
|
'cab','arj','tar','zip','zipx','sit','sitx','gz','tgz','bz2','ace','arc','pkg','dmg','hqx','jar',
|
2010-04-12 04:42:51 +02:00
|
|
|
'xml','pdf',
|
|
|
|
);
|
2010-04-12 04:35:12 +02:00
|
|
|
|
|
|
|
/**
|
2010-10-15 05:04:27 +02:00
|
|
|
* @var If this is true, then restrictions set in {@link $allowed_max_file_size} and
|
|
|
|
* {@link $allowed_extensions} will be applied to users with admin privileges as
|
2010-04-12 04:35:12 +02:00
|
|
|
* well.
|
|
|
|
*/
|
2010-04-12 05:13:41 +02:00
|
|
|
public static $apply_restrictions_to_admin = true;
|
2009-02-02 00:49:53 +01:00
|
|
|
|
|
|
|
|
2008-03-07 04:19:49 +01:00
|
|
|
/**
|
|
|
|
* Cached result of a "SHOW FIELDS" call
|
|
|
|
* in instance_get() for performance reasons.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $cache_file_fields = null;
|
2008-04-06 10:20:13 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* Find a File object by the given filename.
|
2010-10-15 05:12:39 +02:00
|
|
|
*
|
|
|
|
* @param String $filename Matched against the "Name" property.
|
2008-12-04 23:38:32 +01:00
|
|
|
* @return mixed null if not found, File object of found file
|
|
|
|
*/
|
|
|
|
static function find($filename) {
|
|
|
|
// Get the base file if $filename points to a resampled file
|
|
|
|
$filename = ereg_replace('_resampled/[^-]+-','',$filename);
|
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// Split to folders and the actual filename, and traverse the structure.
|
2008-12-04 23:38:32 +01:00
|
|
|
$parts = explode("/", $filename);
|
|
|
|
$parentID = 0;
|
|
|
|
$item = null;
|
|
|
|
foreach($parts as $part) {
|
2010-10-15 04:37:41 +02:00
|
|
|
if($part == ASSETS_DIR && !$parentID) continue;
|
2009-03-20 02:36:19 +01:00
|
|
|
$SQL_part = Convert::raw2sql($part);
|
|
|
|
$item = DataObject::get_one("File", "\"Name\" = '$SQL_part' AND \"ParentID\" = $parentID");
|
2008-12-04 23:38:32 +01:00
|
|
|
if(!$item) break;
|
|
|
|
$parentID = $item->ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
function Link($action = null) {
|
|
|
|
return Director::baseURL() . $this->RelativeLink($action);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
function RelativeLink($action = null){
|
|
|
|
return $this->Filename;
|
|
|
|
}
|
2010-12-16 06:16:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 3.0 Use getTreeTitle()
|
|
|
|
*/
|
|
|
|
function TreeTitle() {
|
|
|
|
return $this->getTreeTitle();
|
|
|
|
}
|
2008-04-06 10:20:13 +02:00
|
|
|
|
2010-12-16 06:16:06 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2009-11-21 03:31:27 +01:00
|
|
|
function getTreeTitle() {
|
2010-10-13 03:17:06 +02:00
|
|
|
return $this->Title;
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-04-06 10:20:13 +02:00
|
|
|
* Event handler called before deleting from the database.
|
|
|
|
* You can overload this to clean up or otherwise process data before delete this
|
2010-10-15 05:12:39 +02:00
|
|
|
* record. Don't forget to call {@link parent::onBeforeDelete()}, though!
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-04-06 10:20:13 +02:00
|
|
|
protected function onBeforeDelete() {
|
|
|
|
parent::onBeforeDelete();
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// ensure that the record is synced with the filesystem before deleting
|
|
|
|
$this->updateFilesystem();
|
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
if($this->Filename && $this->Name && file_exists($this->getFullPath()) && !is_dir($this->getFullPath())) {
|
|
|
|
unlink($this->getFullPath());
|
|
|
|
}
|
2010-10-04 06:52:46 +02:00
|
|
|
}
|
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
/**
|
|
|
|
* @todo Enforce on filesystem URL level via mod_rewrite
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canView($member = null) {
|
|
|
|
if(!$member) $member = Member::currentUser();
|
|
|
|
|
|
|
|
$results = $this->extend('canView', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the following conditions are met:
|
|
|
|
* - CMS_ACCESS_AssetAdmin
|
|
|
|
*
|
|
|
|
* @todo Decouple from CMS view access
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canEdit($member = null) {
|
|
|
|
if(!$member) $member = Member::currentUser();
|
|
|
|
|
2011-01-11 05:40:04 +01:00
|
|
|
$result = $this->extendedCan('canEdit', $member);
|
|
|
|
if($result !== null) return $result;
|
2008-11-07 13:17:42 +01:00
|
|
|
|
|
|
|
return Permission::checkMember($member, 'CMS_ACCESS_AssetAdmin');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canCreate($member = null) {
|
|
|
|
if(!$member) $member = Member::currentUser();
|
|
|
|
|
2011-01-11 05:40:04 +01:00
|
|
|
$result = $this->extendedCan('canCreate', $member);
|
|
|
|
if($result !== null) return $result;
|
2008-11-07 13:17:42 +01:00
|
|
|
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canDelete($member = null) {
|
|
|
|
if(!$member) $member = Member::currentUser();
|
|
|
|
|
|
|
|
$results = $this->extend('canDelete', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
|
|
|
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* Returns a category based on the file extension.
|
|
|
|
* This can be useful when grouping files by type,
|
|
|
|
* showing icons on filelinks, etc.
|
|
|
|
* Possible group values are: "audio", "mov", "zip", "image".
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function appCategory() {
|
|
|
|
$ext = $this->Extension;
|
|
|
|
switch($ext) {
|
|
|
|
case "aif": case "au": case "mid": case "midi": case "mp3": case "ra": case "ram": case "rm":
|
|
|
|
case "mp3": case "wav": case "m4a": case "snd": case "aifc": case "aiff": case "wma": case "apl":
|
|
|
|
case "avr": case "cda": case "mp4": case "ogg":
|
|
|
|
return "audio";
|
|
|
|
|
|
|
|
case "mpeg": case "mpg": case "m1v": case "mp2": case "mpa": case "mpe": case "ifo": case "vob":
|
|
|
|
case "avi": case "wmv": case "asf": case "m2v": case "qt":
|
|
|
|
return "mov";
|
|
|
|
|
|
|
|
case "arc": case "rar": case "tar": case "gz": case "tgz": case "bz2": case "dmg": case "jar":
|
|
|
|
case "ace": case "arj": case "bz": case "cab":
|
|
|
|
return "zip";
|
|
|
|
|
|
|
|
case "bmp": case "gif": case "jpg": case "jpeg": case "pcx": case "tif": case "png": case "alpha":
|
|
|
|
case "als": case "cel": case "icon": case "ico": case "ps":
|
|
|
|
return "image";
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function CMSThumbnail() {
|
|
|
|
$filename = $this->Icon();
|
2009-06-27 05:30:47 +02:00
|
|
|
return "<div style=\"text-align:center;width: 100px;padding-top: 15px;\"><a target=\"_blank\" href=\"$this->URL\" title=\"Download: $this->URL\"><img src=\"$filename\" alt=\"$filename\" /></a><br /><br /><a style=\"color: #0074C6;\"target=\"_blank\" href=\"$this->URL\" title=\"Download: $this->URL\">Download</a><br /><em>$this->Size</em></div>";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* Return the relative URL of an icon for the file type,
|
|
|
|
* based on the {@link appCategory()} value.
|
|
|
|
* Images are searched for in "sapphire/images/app_icons/".
|
|
|
|
*
|
|
|
|
* @return String
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function Icon() {
|
|
|
|
$ext = $this->Extension;
|
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/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
if(!Director::fileExists(SAPPHIRE_DIR . "/images/app_icons/{$ext}_32.gif")) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$ext = $this->appCategory();
|
|
|
|
}
|
|
|
|
|
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/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
if(!Director::fileExists(SAPPHIRE_DIR . "/images/app_icons/{$ext}_32.gif")) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$ext = "generic";
|
|
|
|
}
|
2007-09-14 21:19:32 +02: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/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
return SAPPHIRE_DIR . "/images/app_icons/{$ext}_32.gif";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-04-29 01:52:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Should be called after the file was uploaded
|
|
|
|
*/
|
|
|
|
function onAfterUpload() {
|
|
|
|
$this->extend('onAfterUpload');
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Delete the database record (recursively for folders) without touching the filesystem
|
|
|
|
*/
|
2008-05-24 03:21:04 +02:00
|
|
|
public function deleteDatabaseOnly() {
|
2008-11-24 10:31:14 +01:00
|
|
|
if(is_numeric($this->ID)) DB::query("DELETE FROM \"File\" WHERE \"ID\" = $this->ID");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-19 06:00:57 +02:00
|
|
|
* Make sure the file has a name
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected function onBeforeWrite() {
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// Set default name
|
2010-10-15 05:20:47 +02:00
|
|
|
if(!$this->getField('Name')) $this->Name = "new-" . strtolower($this->class);
|
2010-10-19 06:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set name on filesystem. If the current object is a "Folder", will also update references
|
|
|
|
* to subfolders and contained file records (both in database and filesystem)
|
|
|
|
*/
|
|
|
|
protected function onAfterWrite() {
|
|
|
|
parent::onAfterWrite();
|
2010-10-15 05:20:47 +02:00
|
|
|
|
|
|
|
$this->updateFilesystem();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moving the file if appropriate according to updated database content.
|
|
|
|
* Throws an Exception if the new file already exists.
|
|
|
|
*
|
|
|
|
* Caution: This method should just be called during a {@link write()} invocation,
|
|
|
|
* as it relies on {@link DataObject->isChanged()}, which is reset after a {@link write()} call.
|
|
|
|
* Might be called as {@link File->updateFilesystem()} from within {@link Folder->updateFilesystem()},
|
|
|
|
* so it has to handle both files and folders.
|
|
|
|
*
|
|
|
|
* Assumes that the "Filename" property was previously updated, either directly or indirectly.
|
|
|
|
* (it might have been influenced by {@link setName()} or {@link setParentID()} before).
|
|
|
|
*/
|
|
|
|
public function updateFilesystem() {
|
|
|
|
// Regenerate "Filename", just to be sure
|
|
|
|
$this->setField('Filename', $this->getRelativePath());
|
|
|
|
|
|
|
|
// If certain elements are changed, update the filesystem reference
|
|
|
|
if(!$this->isChanged('Filename')) return false;
|
|
|
|
|
|
|
|
$changedFields = $this->getChangedFields();
|
|
|
|
$pathBefore = $changedFields['Filename']['before'];
|
|
|
|
$pathAfter = $changedFields['Filename']['after'];
|
|
|
|
|
|
|
|
// If the file or folder didn't exist before, don't rename - its created
|
|
|
|
if(!$pathBefore) return;
|
|
|
|
|
|
|
|
$pathBeforeAbs = Director::getAbsFile($pathBefore);
|
|
|
|
$pathAfterAbs = Director::getAbsFile($pathAfter);
|
|
|
|
|
|
|
|
// TODO Fix Filetest->testCreateWithFilenameWithSubfolder() to enable this
|
|
|
|
// // Create parent folders recursively in database and filesystem
|
|
|
|
// if(!is_a($this, 'Folder')) {
|
|
|
|
// $folder = Folder::findOrMake(dirname($pathAfterAbs));
|
|
|
|
// if($folder) $this->ParentID = $folder->ID;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Check that original file or folder exists, and rename on filesystem if required.
|
|
|
|
// The folder of the path might've already been renamed by Folder->updateFilesystem()
|
|
|
|
// before any filesystem update on contained file or subfolder records is triggered.
|
|
|
|
if(!file_exists($pathAfterAbs)) {
|
|
|
|
if(!is_a($this, 'Folder')) {
|
|
|
|
// Only throw a fatal error if *both* before and after paths don't exist.
|
|
|
|
if(!file_exists($pathBeforeAbs)) throw new Exception("Cannot move $pathBefore to $pathAfter - $pathBefore doesn't exist");
|
|
|
|
|
|
|
|
// Check that target directory (not the file itself) exists.
|
|
|
|
// Only check if we're dealing with a file, otherwise the folder will need to be created
|
|
|
|
if(!file_exists(dirname($pathAfterAbs))) throw new Exception("Cannot move $pathBefore to $pathAfter - Directory " . dirname($pathAfter) . " doesn't exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename file or folder
|
|
|
|
$success = rename($pathBeforeAbs, $pathAfterAbs);
|
|
|
|
if(!$success) throw new Exception("Cannot move $pathBeforeAbs to $pathAfterAbs");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update any database references
|
|
|
|
$this->updateLinks($pathBefore, $pathAfter);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:19:32 +02:00
|
|
|
* Collate selected descendants of this page.
|
2007-07-19 12:40:28 +02:00
|
|
|
* $condition will be evaluated on each descendant, and if it is succeeds, that item will be added
|
|
|
|
* to the $collator array.
|
|
|
|
* @param condition The PHP condition to be evaluated. The page will be called $item
|
|
|
|
* @param collator An array, passed by reference, to collect all of the matching descendants.
|
|
|
|
*/
|
|
|
|
public function collateDescendants($condition, &$collator) {
|
|
|
|
if($children = $this->Children()) {
|
|
|
|
foreach($children as $item) {
|
|
|
|
if(!$condition || eval("return $condition;")) $collator[] = $item;
|
|
|
|
$item->collateDescendants($condition, $collator);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* Setter function for Name. Automatically sets a default title,
|
2010-10-15 05:20:47 +02:00
|
|
|
* and removes characters that might be invalid on the filesystem.
|
2010-10-15 05:12:39 +02:00
|
|
|
* Also adds a suffix to the name if the filename already exists
|
|
|
|
* on the filesystem, and is associated to a different {@link File} database record
|
|
|
|
* in the same folder. This means "myfile.jpg" might become "myfile-1.jpg".
|
|
|
|
*
|
2010-10-15 05:20:47 +02:00
|
|
|
* Does not change the filesystem itself, please use {@link write()} for this.
|
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* @param String $name
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function setName($name) {
|
|
|
|
$oldName = $this->Name;
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// It can't be blank, default to Title
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$name) $name = $this->Title;
|
|
|
|
|
|
|
|
// Fix illegal characters
|
2010-10-15 05:12:39 +02:00
|
|
|
$name = ereg_replace(' +','-',trim($name)); // Replace any spaces
|
|
|
|
$name = ereg_replace('[^A-Za-z0-9.+_\-]','',$name); // Replace non alphanumeric characters
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// Remove all leading dots or underscores
|
2010-04-13 05:19:28 +02:00
|
|
|
while(!empty($name) && ($name[0] == '_' || $name[0] == '.')) {
|
2010-01-21 23:59:19 +01:00
|
|
|
$name = substr($name, 1);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// We might have just turned it blank, so check again.
|
|
|
|
if(!$name) $name = 'new-folder';
|
|
|
|
|
|
|
|
// If it's changed, check for duplicates
|
|
|
|
if($oldName && $oldName != $name) {
|
2010-10-15 05:15:38 +02:00
|
|
|
$base = pathinfo($name, PATHINFO_BASENAME);
|
|
|
|
$ext = self::get_file_extension($name);
|
2007-07-19 12:40:28 +02:00
|
|
|
$suffix = 1;
|
2010-10-15 05:15:38 +02:00
|
|
|
while(DataObject::get_one("File", "\"Name\" = '" . Convert::raw2sql($name) . "' AND \"ParentID\" = " . (int)$this->ParentID)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$suffix++;
|
|
|
|
$name = "$base-$suffix$ext";
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// Update title
|
2007-09-14 21:19:32 +02:00
|
|
|
if(!$this->getField('Title')) $this->__set('Title', str_replace(array('-','_'),' ',ereg_replace('\.[^.]+$','',$name)));
|
2010-10-15 05:12:39 +02:00
|
|
|
|
|
|
|
// Update actual field value
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->setField('Name', $name);
|
2010-10-15 05:12:39 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// Ensure that the filename is updated as well (only in-memory)
|
|
|
|
// Important: Circumvent the getter to avoid infinite loops
|
|
|
|
$this->setField('Filename', $this->getRelativePath());
|
2010-10-15 05:12:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->getField('Name');
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* @param String $old File path relative to the webroot
|
|
|
|
* @param String $new File path relative to the webroot
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected function updateLinks($old, $new) {
|
2011-03-22 22:40:29 +01:00
|
|
|
$this->extend('updateLinks', $old, $new);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
/**
|
|
|
|
* Does not change the filesystem itself, please use {@link write()} for this.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function setParentID($parentID) {
|
2009-11-21 03:33:30 +01:00
|
|
|
$this->setField('ParentID', (int)$parentID);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// Don't change on the filesystem, we'll handle that in onBeforeWrite()
|
|
|
|
$this->setField('Filename', $this->getRelativePath());
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->getField('ParentID');
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2008-03-18 00:50:18 +01:00
|
|
|
/**
|
|
|
|
* Gets the absolute URL accessible through the web.
|
|
|
|
*
|
|
|
|
* @uses Director::absoluteBaseURL()
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getAbsoluteURL() {
|
2010-10-19 02:52:36 +02:00
|
|
|
return Director::absoluteBaseURL() . $this->getFilename();
|
2008-03-18 00:50:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-12 05:29:56 +02:00
|
|
|
* Gets the relative URL accessible through the web.
|
2008-03-18 00:50:18 +01:00
|
|
|
*
|
2010-04-12 05:29:56 +02:00
|
|
|
* @uses Director::baseURL()
|
2008-03-18 00:50:18 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function getURL() {
|
2010-04-12 05:29:56 +02:00
|
|
|
return Director::baseURL() . $this->getFilename();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:15:26 +02:00
|
|
|
* Return the last 50 characters of the URL.
|
|
|
|
*
|
|
|
|
* @deprecated 2.4
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function getLinkedURL() {
|
|
|
|
return "$this->Name";
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* Returns an absolute filesystem path to the file.
|
|
|
|
* Use {@link getRelativePath()} to get the same path relative to the webroot.
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function getFullPath() {
|
2008-08-13 00:26:52 +02:00
|
|
|
$baseFolder = Director::baseFolder();
|
|
|
|
|
|
|
|
if(strpos($this->getFilename(), $baseFolder) === 0) {
|
|
|
|
// if path is absolute already, just return
|
|
|
|
return $this->getFilename();
|
|
|
|
} else {
|
|
|
|
// otherwise assume silverstripe-basefolder
|
|
|
|
return Director::baseFolder() . '/' . $this->getFilename();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-13 01:02:10 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* Returns path relative to webroot.
|
2010-10-15 05:20:47 +02:00
|
|
|
* Serves as a "fallback" method to create the "Filename" property if it isn't set.
|
2010-10-15 05:12:39 +02:00
|
|
|
* If no {@link Folder} is set ("ParentID" property),
|
|
|
|
* defaults to a filename relative to the ASSETS_DIR (usually "assets/").
|
2010-10-13 01:02:10 +02:00
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function getRelativePath() {
|
|
|
|
if($this->ParentID) {
|
2010-10-19 06:00:57 +02:00
|
|
|
$p = DataObject::get_by_id('Folder', $this->ParentID, false); // Don't use the cache, the parent has just been changed
|
2009-08-20 01:39:39 +02:00
|
|
|
if($p && $p->exists()) return $p->getRelativePath() . $this->getField("Name");
|
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/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
else return ASSETS_DIR . "/" . $this->getField("Name");
|
2007-07-19 12:40:28 +02:00
|
|
|
} else if($this->getField("Name")) {
|
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/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
return ASSETS_DIR . "/" . $this->getField("Name");
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
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/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
return ASSETS_DIR;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* @todo Coupling with cms module, remove this method.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function DeleteLink() {
|
|
|
|
return Director::absoluteBaseURL()."admin/assets/removefile/".$this->ID;
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function getFilename() {
|
2010-10-15 05:20:47 +02:00
|
|
|
// Default behaviour: Return field if its set
|
2008-06-25 07:52:58 +02:00
|
|
|
if($this->getField('Filename')) {
|
|
|
|
return $this->getField('Filename');
|
|
|
|
} else {
|
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/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
return ASSETS_DIR . '/';
|
2008-06-25 07:52:58 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
/**
|
|
|
|
* Does not change the filesystem itself, please use {@link write()} for this.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function setFilename($val) {
|
|
|
|
$this->setField('Filename', $val);
|
2010-10-15 05:12:39 +02:00
|
|
|
|
|
|
|
// "Filename" is the "master record" (existing on the filesystem),
|
|
|
|
// meaning we have to adjust the "Name" property in the database as well.
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->setField('Name', basename($val));
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* Returns the file extension
|
|
|
|
*
|
|
|
|
* @todo This overrides getExtension() in DataObject, but it does something completely different.
|
2007-07-19 12:40:28 +02:00
|
|
|
* This should be renamed to getFileExtension(), but has not been yet as it may break
|
|
|
|
* legacy code.
|
2010-10-15 05:12:39 +02:00
|
|
|
*
|
|
|
|
* @return String
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function getExtension() {
|
2008-04-09 13:08:58 +02:00
|
|
|
return self::get_file_extension($this->getField('Filename'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the extension of a filepath or filename,
|
|
|
|
* by stripping away everything before the last "dot".
|
2010-10-15 05:14:59 +02:00
|
|
|
* Caution: Only returns the last extension in "double-barrelled"
|
|
|
|
* extensions (e.g. "gz" for "tar.gz").
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* - "myfile" returns ""
|
|
|
|
* - "myfile.txt" returns "txt"
|
|
|
|
* - "myfile.tar.gz" returns "gz"
|
2008-04-09 13:08:58 +02:00
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get_file_extension($filename) {
|
2010-10-15 05:14:59 +02:00
|
|
|
return pathinfo($filename, PATHINFO_EXTENSION);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-04-06 10:20:13 +02:00
|
|
|
|
2008-10-16 05:55:33 +02:00
|
|
|
/**
|
|
|
|
* Return the type of file for the given extension
|
|
|
|
* on the current file name.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function getFileType() {
|
|
|
|
$types = array(
|
2008-10-16 05:55:33 +02:00
|
|
|
'gif' => 'GIF image - good for diagrams',
|
|
|
|
'jpg' => 'JPEG image - good for photos',
|
|
|
|
'jpeg' => 'JPEG image - good for photos',
|
|
|
|
'png' => 'PNG image - good general-purpose format',
|
2008-11-22 04:33:00 +01:00
|
|
|
'ico' => 'Icon image',
|
|
|
|
'tiff' => 'Tagged image format',
|
2008-10-16 05:55:33 +02:00
|
|
|
'doc' => 'Word document',
|
|
|
|
'xls' => 'Excel spreadsheet',
|
2007-07-19 12:40:28 +02:00
|
|
|
'zip' => 'ZIP compressed file',
|
|
|
|
'gz' => 'GZIP compressed file',
|
2008-10-16 05:55:33 +02:00
|
|
|
'dmg' => 'Apple disk image',
|
2007-07-19 12:40:28 +02:00
|
|
|
'pdf' => 'Adobe Acrobat PDF file',
|
2008-10-16 05:55:33 +02:00
|
|
|
'mp3' => 'MP3 audio file',
|
|
|
|
'wav' => 'WAV audo file',
|
|
|
|
'avi' => 'AVI video file',
|
|
|
|
'mpg' => 'MPEG video file',
|
2008-11-22 04:33:00 +01:00
|
|
|
'mpeg' => 'MPEG video file',
|
|
|
|
'js' => 'Javascript file',
|
|
|
|
'css' => 'CSS file',
|
|
|
|
'html' => 'HTML file',
|
|
|
|
'htm' => 'HTML file'
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2008-10-16 05:55:33 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$ext = $this->getExtension();
|
2008-04-06 10:20:13 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return isset($types[$ext]) ? $types[$ext] : 'unknown';
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the size of the file type in an appropriate format.
|
|
|
|
*/
|
|
|
|
function getSize() {
|
|
|
|
$size = $this->getAbsoluteSize();
|
2008-04-06 10:20:13 +02:00
|
|
|
|
|
|
|
return ($size) ? self::format_size($size) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function format_size($size) {
|
|
|
|
if($size < 1024) return $size . ' bytes';
|
|
|
|
if($size < 1024*10) return (round($size/1024*10)/10). ' KB';
|
|
|
|
if($size < 1024*1024) return round($size/1024) . ' KB';
|
|
|
|
if($size < 1024*1024*10) return (round(($size/1024)/1024*10)/10) . ' MB';
|
|
|
|
if($size < 1024*1024*1024) return round(($size/1024)/1024) . ' MB';
|
2008-08-25 03:42:27 +02:00
|
|
|
return round($size/(1024*1024*1024)*10)/10 . ' GB';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-12-04 23:38:32 +01:00
|
|
|
* Return file size in bytes.
|
|
|
|
* @return int
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function getAbsoluteSize(){
|
2008-12-04 23:38:32 +01:00
|
|
|
if(file_exists($this->getFullPath())) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$size = filesize($this->getFullPath());
|
|
|
|
return $size;
|
2008-12-04 23:38:32 +01:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* We've overridden the DataObject::get function for File so that the very large content field
|
2007-09-14 21:19:32 +02:00
|
|
|
* is excluded!
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
|
|
|
* @todo Admittedly this is a bit of a hack; but we need a way of ensuring that large
|
|
|
|
* TEXT fields don't stuff things up for the rest of us. Perhaps a separate search table would
|
|
|
|
* be a better way of approaching this?
|
2008-03-07 04:19:49 +01:00
|
|
|
* @deprecated alternative_instance_get()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function instance_get($filter = "", $sort = "", $join = "", $limit="", $containerClass = "DataObjectSet", $having="") {
|
|
|
|
$query = $this->extendedSQL($filter, $sort, $limit, $join, $having);
|
|
|
|
$baseTable = reset($query->from);
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2008-03-07 04:19:49 +01:00
|
|
|
$excludeDbColumns = array('Content');
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
// Work out which columns we're actually going to select
|
|
|
|
// In short, we select everything except File.Content
|
2008-04-26 08:47:29 +02:00
|
|
|
$dataobject_select = array();
|
|
|
|
foreach($query->select as $item) {
|
2008-11-24 10:31:14 +01:00
|
|
|
/*
|
2008-11-23 01:31:06 +01:00
|
|
|
if($item == "\"File\".*") {
|
|
|
|
$fileColumns = DB::query("SHOW FIELDS IN \"File\"")->column();
|
2008-04-26 08:47:29 +02:00
|
|
|
$columnsToAdd = array_diff($fileColumns, $excludeDbColumns);
|
2008-11-23 01:31:06 +01:00
|
|
|
foreach($columnsToAdd as $otherItem) $dataobject_select[] = '"File".' . $otherItem;
|
2008-03-07 04:19:49 +01:00
|
|
|
} else {
|
2008-11-24 10:31:14 +01:00
|
|
|
*/
|
2008-04-26 08:47:29 +02:00
|
|
|
$dataobject_select[] = $item;
|
2008-11-24 10:31:14 +01:00
|
|
|
//}
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2008-04-26 08:47:29 +02:00
|
|
|
|
|
|
|
$query->select = $dataobject_select;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$records = $query->execute();
|
|
|
|
$ret = $this->buildDataObjectSet($records, $containerClass);
|
|
|
|
if($ret) $ret->parseQueryLimit($query);
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
2008-03-07 04:19:49 +01:00
|
|
|
|
|
|
|
public function flushCache() {
|
|
|
|
parent::flushCache();
|
|
|
|
|
2008-03-09 06:08:08 +01:00
|
|
|
self::$cache_file_fields = null;
|
2008-03-07 04:19:49 +01:00
|
|
|
}
|
2008-04-06 10:20:13 +02:00
|
|
|
|
2009-04-29 02:07:39 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function fieldLabels($includerelations = true) {
|
|
|
|
$labels = parent::fieldLabels($includerelations);
|
2008-11-02 21:04:10 +01:00
|
|
|
$labels['Name'] = _t('File.Name', 'Name');
|
|
|
|
$labels['Title'] = _t('File.Title', 'Title');
|
|
|
|
$labels['Filename'] = _t('File.Filename', 'Filename');
|
|
|
|
$labels['Filename'] = _t('File.Filename', 'Filename');
|
|
|
|
$labels['Content'] = _t('File.Content', 'Content');
|
|
|
|
$labels['Sort'] = _t('File.Sort', 'Sort Order');
|
|
|
|
|
|
|
|
return $labels;
|
|
|
|
}
|
|
|
|
|
2010-04-12 04:34:38 +02:00
|
|
|
function validate() {
|
2010-04-12 05:14:52 +02:00
|
|
|
if(File::$apply_restrictions_to_admin || !Permission::check('ADMIN')) {
|
2010-10-15 05:14:59 +02:00
|
|
|
// Extension validation
|
|
|
|
// TODO Merge this with Upload_Validator
|
|
|
|
$extension = $this->getExtension();
|
2010-10-15 05:53:54 +02:00
|
|
|
$allowed = array_map('strtolower', self::$allowed_extensions);
|
|
|
|
if($extension && !in_array(strtolower($extension), $allowed)) {
|
|
|
|
$exts = $allowed;
|
2010-04-12 04:42:51 +02:00
|
|
|
sort($exts);
|
2010-04-12 04:34:38 +02:00
|
|
|
$message = sprintf(
|
|
|
|
_t(
|
|
|
|
'File.INVALIDEXTENSION',
|
|
|
|
'Extension is not allowed (valid: %s)',
|
|
|
|
PR_MEDIUM,
|
|
|
|
'Argument 1: Comma-separated list of valid extensions'
|
|
|
|
),
|
2010-10-15 05:21:04 +02:00
|
|
|
wordwrap(implode(', ',$exts))
|
2010-04-12 04:34:38 +02:00
|
|
|
);
|
|
|
|
return new ValidationResult(false, $message);
|
|
|
|
}
|
|
|
|
}
|
2010-10-15 05:20:47 +02:00
|
|
|
|
|
|
|
// We aren't validating for an existing "Filename" on the filesystem.
|
|
|
|
// A record should still be saveable even if the underlying record has been removed.
|
|
|
|
|
2010-04-12 04:34:38 +02:00
|
|
|
return new ValidationResult(true);
|
|
|
|
}
|
2010-04-14 06:09:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow custom fields for uploads in {@link AssetAdmin}.
|
|
|
|
* Similar to {@link getCMSFields()}, but a more restricted
|
|
|
|
* set of fields which can be reliably set on any file type.
|
|
|
|
*
|
2010-10-04 06:47:19 +02:00
|
|
|
* Needs to be enabled through {@link AssetAdmin::$metadata_upload_enabled}
|
|
|
|
*
|
2010-04-14 06:09:09 +02:00
|
|
|
* @return FieldSet
|
|
|
|
*/
|
|
|
|
function uploadMetadataFields() {
|
|
|
|
$fields = new FieldSet();
|
2010-04-14 06:10:42 +02:00
|
|
|
$fields->push(new TextField('Title', $this->fieldLabel('Title')));
|
2010-04-14 06:09:09 +02:00
|
|
|
$this->extend('updateUploadMetadataFields', $fields);
|
2010-10-04 06:47:19 +02:00
|
|
|
|
2010-04-14 06:09:09 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
2010-04-12 04:34:38 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-04-29 01:52:15 +02:00
|
|
|
?>
|