2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2010-10-13 01:02:10 +02:00
|
|
|
* Represents a folder in the assets/ directory.
|
2010-10-15 05:12:39 +02:00
|
|
|
* The folder path is stored in the "Filename" property.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* Updating the "Name" or "Filename" properties on
|
|
|
|
* a folder object also updates all associated children
|
|
|
|
* (both {@link File} and {@link Folder} records).
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-15 05:20:47 +02:00
|
|
|
* Deleting a folder will also remove the folder from the filesystem,
|
|
|
|
* including any subfolders and contained files. Use {@link deleteDatabaseOnly()}
|
|
|
|
* to avoid touching the filesystem.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* See {@link File} documentation for more details about the
|
2012-03-24 04:38:57 +01:00
|
|
|
* relationship between the database and filesystem in the SilverStripe file APIs.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage filesystem
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Folder extends File {
|
2009-11-21 03:32:40 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $singular_name = "Folder";
|
2009-11-21 03:32:40 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $plural_name = "Folders";
|
2010-10-13 03:01:12 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_sort = "\"Name\"";
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
public function populateDefaults() {
|
2011-03-15 10:30:31 +01:00
|
|
|
parent::populateDefaults();
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2011-03-15 10:30:31 +01:00
|
|
|
if(!$this->Name) $this->Name = _t('AssetAdmin.NEWFOLDER',"NewFolder");
|
|
|
|
}
|
2012-11-16 02:19:19 +01:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* Find the given folder or create it both as {@link Folder} database records
|
2014-03-11 02:26:08 +01:00
|
|
|
* and on the filesystem. If necessary, creates parent folders as well. If it's
|
|
|
|
* unable to find or make the folder, it will return null (as /assets is unable
|
|
|
|
* to be represented by a Folder DataObject)
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2010-10-15 05:21:41 +02:00
|
|
|
* @param $folderPath string Absolute or relative path to the file.
|
|
|
|
* If path is relative, its interpreted relative to the "assets/" directory.
|
2014-03-11 02:26:08 +01:00
|
|
|
* @return Folder|null
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public static function find_or_make($folderPath) {
|
2010-04-14 06:42:59 +02:00
|
|
|
// Create assets directory, if it is missing
|
2010-10-15 05:21:23 +02:00
|
|
|
if(!file_exists(ASSETS_PATH)) Filesystem::makeFolder(ASSETS_PATH);
|
2010-04-14 06:42:59 +02:00
|
|
|
|
2008-03-17 23:51:25 +01:00
|
|
|
$folderPath = trim(Director::makeRelative($folderPath));
|
|
|
|
// replace leading and trailing slashes
|
|
|
|
$folderPath = preg_replace('/^\/?(.*)\/?$/', '$1', $folderPath);
|
|
|
|
$parts = explode("/",$folderPath);
|
2010-10-13 01:02:10 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$parentID = 0;
|
2010-10-15 05:21:41 +02:00
|
|
|
$item = null;
|
2014-02-20 02:03:40 +01:00
|
|
|
$filter = FileNameFilter::create();
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($parts as $part) {
|
2010-10-13 01:02:10 +02:00
|
|
|
if(!$part) continue; // happens for paths with a trailing slash
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2014-02-20 02:03:40 +01:00
|
|
|
// Ensure search includes folders with illegal characters removed, but
|
|
|
|
// err in favour of matching existing folders if $folderPath
|
|
|
|
// includes illegal characters itself.
|
|
|
|
$partSafe = $filter->filter($part);
|
|
|
|
$item = Folder::get()->filter(array(
|
|
|
|
'ParentID' => $parentID,
|
|
|
|
'Name' => array($partSafe, $part)
|
|
|
|
))->first();
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$item) {
|
|
|
|
$item = new Folder();
|
|
|
|
$item->ParentID = $parentID;
|
2014-02-20 02:03:40 +01:00
|
|
|
$item->Name = $partSafe;
|
2008-11-10 04:51:35 +01:00
|
|
|
$item->Title = $part;
|
2007-07-19 12:40:28 +02:00
|
|
|
$item->write();
|
2010-04-13 01:45:53 +02:00
|
|
|
}
|
|
|
|
if(!file_exists($item->getFullPath())) {
|
2010-10-15 05:21:23 +02:00
|
|
|
Filesystem::makeFolder($item->getFullPath());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
$parentID = $item->ID;
|
|
|
|
}
|
2010-10-15 05:21:41 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $item;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-06-21 00:32:08 +02:00
|
|
|
* Synchronize the file database with the actual content of the assets
|
2013-05-11 05:07:12 +02:00
|
|
|
* folder.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function syncChildren() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$parentID = (int)$this->ID; // parentID = 0 on the singleton, used as the 'root node';
|
|
|
|
$added = 0;
|
|
|
|
$deleted = 0;
|
2013-05-11 05:07:12 +02:00
|
|
|
$skipped = 0;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// First, merge any children that are duplicates
|
2013-06-21 00:32:08 +02:00
|
|
|
$duplicateChildrenNames = DB::prepared_query(
|
|
|
|
'SELECT "Name" FROM "File" WHERE "ParentID" = ? GROUP BY "Name" HAVING count(*) > 1',
|
|
|
|
array($parentID)
|
|
|
|
)->column();
|
2007-07-19 12:40:28 +02:00
|
|
|
if($duplicateChildrenNames) foreach($duplicateChildrenNames as $childName) {
|
2012-09-26 23:34:00 +02:00
|
|
|
// Note, we do this in the database rather than object-model; otherwise we get all sorts of problems
|
|
|
|
// about deleting files
|
2013-06-21 00:32:08 +02:00
|
|
|
$children = DB::prepared_query(
|
|
|
|
'SELECT "ID" FROM "File" WHERE "Name" = ? AND "ParentID" = ?',
|
|
|
|
array($childName, $parentID)
|
|
|
|
)->column();
|
2008-08-15 00:20:14 +02:00
|
|
|
if($children) {
|
|
|
|
$keptChild = array_shift($children);
|
|
|
|
foreach($children as $removedChild) {
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::prepared_query('UPDATE "File" SET "ParentID" = ? WHERE "ParentID" = ?',
|
|
|
|
array($keptChild, $removedChild));
|
|
|
|
DB::prepared_query('DELETE FROM "File" WHERE "ID" = ?', array($removedChild));
|
2008-08-15 00:20:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Inconsistent database issue: SELECT ID FROM \"File\" WHERE Name = '$childName'"
|
|
|
|
. " AND ParentID = $parentID should have returned data", E_USER_WARNING);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Get index of database content
|
2008-02-25 03:10:37 +01:00
|
|
|
// We don't use DataObject so that things like subsites doesn't muck with this.
|
2013-06-21 00:32:08 +02:00
|
|
|
$dbChildren = DB::prepared_query('SELECT * FROM "File" WHERE "ParentID" = ?', array($parentID));
|
2008-02-25 03:10:37 +01:00
|
|
|
$hasDbChild = array();
|
2013-05-11 05:07:12 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
if($dbChildren) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($dbChildren as $dbChild) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$className = $dbChild['ClassName'];
|
2009-08-24 09:21:08 +02:00
|
|
|
if(!$className) $className = "File";
|
2008-02-25 03:10:37 +01:00
|
|
|
$hasDbChild[$dbChild['Name']] = new $className($dbChild);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-11 05:07:12 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
$unwantedDbChildren = $hasDbChild;
|
2012-06-15 01:28:29 +02:00
|
|
|
|
|
|
|
// if we're syncing a folder with no ID, we assume we're syncing the root assets folder
|
|
|
|
// however the Filename field is populated with "NewFolder", so we need to set this to empty
|
|
|
|
// to satisfy the baseDir variable below, which is the root folder to scan for new files in
|
|
|
|
if(!$parentID) $this->Filename = '';
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Iterate through the actual children, correcting the database as necessary
|
|
|
|
$baseDir = $this->FullPath;
|
2012-06-15 01:28:29 +02:00
|
|
|
|
|
|
|
// @todo this shouldn't call die() but log instead
|
|
|
|
if($parentID && !$this->Filename) die($this->ID . " - " . $this->FullPath);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if(file_exists($baseDir)) {
|
|
|
|
$actualChildren = scandir($baseDir);
|
2014-04-09 07:37:25 +02:00
|
|
|
$ignoreRules = Filesystem::config()->sync_blacklisted_patterns;
|
|
|
|
$allowedExtensions = File::config()->allowed_extensions;
|
|
|
|
$checkExtensions = $this->config()->apply_restrictions_to_admin || !Permission::check('ADMIN');
|
2013-05-11 05:07:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($actualChildren as $actualChild) {
|
2014-04-09 07:37:25 +02:00
|
|
|
$skip = false;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2014-04-09 07:37:25 +02:00
|
|
|
// Check ignore patterns
|
|
|
|
if($ignoreRules) foreach($ignoreRules as $rule) {
|
|
|
|
if(preg_match($rule, $actualChild)) {
|
|
|
|
$skip = true;
|
|
|
|
break;
|
2013-05-11 05:07:12 +02:00
|
|
|
}
|
2010-01-21 23:59:19 +01:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2014-04-09 07:37:25 +02:00
|
|
|
// Check allowed extensions, unless admin users are allowed to bypass these exclusions
|
|
|
|
if($checkExtensions
|
2016-03-07 20:30:13 +01:00
|
|
|
&& !is_dir($baseDir . $actualChild)
|
2014-04-09 07:37:25 +02:00
|
|
|
&& ($extension = self::get_file_extension($actualChild))
|
|
|
|
&& !in_array(strtolower($extension), $allowedExtensions)
|
|
|
|
) {
|
|
|
|
$skip = true;
|
|
|
|
}
|
2010-01-21 23:59:19 +01:00
|
|
|
|
2014-04-09 07:37:25 +02:00
|
|
|
if($skip) {
|
|
|
|
$skipped++;
|
|
|
|
continue;
|
|
|
|
}
|
2013-05-11 05:07:12 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// A record with a bad class type doesn't deserve to exist. It must be purged!
|
|
|
|
if(isset($hasDbChild[$actualChild])) {
|
|
|
|
$child = $hasDbChild[$actualChild];
|
2013-06-21 00:32:08 +02:00
|
|
|
if(( !( $child instanceof Folder ) && is_dir($baseDir . $actualChild) )
|
2009-03-10 23:08:52 +01:00
|
|
|
|| (( $child instanceof Folder ) && !is_dir($baseDir . $actualChild)) ) {
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::prepared_query('DELETE FROM "File" WHERE "ID" = ?', array($child->ID));
|
|
|
|
unset($hasDbChild[$actualChild]);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($hasDbChild[$actualChild])) {
|
|
|
|
$child = $hasDbChild[$actualChild];
|
|
|
|
unset($unwantedDbChildren[$actualChild]);
|
|
|
|
} else {
|
|
|
|
$added++;
|
|
|
|
$childID = $this->constructChild($actualChild);
|
|
|
|
$child = DataObject::get_by_id("File", $childID);
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if( $child && is_dir($baseDir . $actualChild)) {
|
|
|
|
$childResult = $child->syncChildren();
|
|
|
|
$added += $childResult['added'];
|
|
|
|
$deleted += $childResult['deleted'];
|
2013-05-11 05:07:12 +02:00
|
|
|
$skipped += $childResult['skipped'];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2009-10-21 04:30:48 +02:00
|
|
|
// Clean up the child record from memory after use. Important!
|
|
|
|
$child->destroy();
|
2009-10-21 04:32:11 +02:00
|
|
|
$child = null;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Iterate through the unwanted children, removing them all
|
|
|
|
if(isset($unwantedDbChildren)) foreach($unwantedDbChildren as $unwantedDbChild) {
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::prepared_query('DELETE FROM "File" WHERE "ID" = ?', array($unwantedDbChild->ID));
|
2007-07-19 12:40:28 +02:00
|
|
|
$deleted++;
|
|
|
|
}
|
|
|
|
} else {
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::prepared_query('DELETE FROM "File" WHERE "ID" = ?', array($this->ID));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2013-05-11 05:07:12 +02:00
|
|
|
return array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'added' => $added,
|
2013-05-11 05:07:12 +02:00
|
|
|
'deleted' => $deleted,
|
|
|
|
'skipped' => $skipped
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a child of this Folder with the given name.
|
|
|
|
* It does this without actually using the object model, as this starts messing
|
|
|
|
* with all the data. Rather, it does a direct database insert.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* @param string $name Name of the file or folder
|
|
|
|
* @return integer the ID of the newly saved File record
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function constructChild($name) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Determine the class name - File, Folder or Image
|
|
|
|
$baseDir = $this->FullPath;
|
|
|
|
if(is_dir($baseDir . $name)) {
|
|
|
|
$className = "Folder";
|
|
|
|
} else {
|
2011-08-22 13:04:15 +02:00
|
|
|
$className = File::get_class_for_file_extension(pathinfo($name, PATHINFO_EXTENSION));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$ownerID = Member::currentUserID();
|
|
|
|
|
|
|
|
$filename = $this->Filename . $name;
|
2007-07-19 12:40:28 +02:00
|
|
|
if($className == 'Folder' ) $filename .= '/';
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$nowExpression = DB::get_conn()->now();
|
|
|
|
DB::prepared_query("INSERT INTO \"File\"
|
2008-11-24 10:31:14 +01:00
|
|
|
(\"ClassName\", \"ParentID\", \"OwnerID\", \"Name\", \"Filename\", \"Created\", \"LastEdited\", \"Title\")
|
2013-06-21 00:32:08 +02:00
|
|
|
VALUES (?, ?, ?, ?, ?, $nowExpression, $nowExpression, ?)",
|
|
|
|
array($className, $this->ID, $ownerID, $name, $filename, $name)
|
|
|
|
);
|
|
|
|
|
|
|
|
return DB::get_generated_id("File");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take a file uploaded via a POST form, and save it inside this folder.
|
2011-08-26 17:57:05 +02:00
|
|
|
* File names are filtered through {@link FileNameFilter}, see class documentation
|
|
|
|
* on how to influence this behaviour.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function addUploadToFolder($tmpFile) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!is_array($tmpFile)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Folder::addUploadToFolder() Not passed an array."
|
|
|
|
. " Most likely, the form hasn't got the right enctype", E_USER_ERROR);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-05-25 05:04:55 +02:00
|
|
|
if(!isset($tmpFile['size'])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-06-21 00:32:08 +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
|
|
|
$base = BASE_PATH;
|
2007-07-19 12:40:28 +02:00
|
|
|
// $parentFolder = Folder::findOrMake("Uploads");
|
|
|
|
|
|
|
|
// Generate default filename
|
2012-04-04 16:59:30 +02:00
|
|
|
$nameFilter = FileNameFilter::create();
|
2011-08-26 17:57:05 +02:00
|
|
|
$file = $nameFilter->filter($tmpFile['name']);
|
2010-01-21 23:59:19 +01:00
|
|
|
while($file[0] == '_' || $file[0] == '.') {
|
|
|
|
$file = substr($file, 1);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$file = $this->RelativePath . $file;
|
|
|
|
Filesystem::makeFolder(dirname("$base/$file"));
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2010-05-25 05:04:55 +02:00
|
|
|
$doubleBarrelledExts = array('.gz', '.bz', '.bz2');
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2010-05-25 05:04:55 +02:00
|
|
|
$ext = "";
|
|
|
|
if(preg_match('/^(.*)(\.[^.]+)$/', $file, $matches)) {
|
|
|
|
$file = $matches[1];
|
|
|
|
$ext = $matches[2];
|
2013-06-21 00:32:08 +02:00
|
|
|
// Special case for double-barrelled
|
2010-05-25 05:04:55 +02:00
|
|
|
if(in_array($ext, $doubleBarrelledExts) && preg_match('/^(.*)(\.[^.]+)$/', $file, $matches)) {
|
|
|
|
$file = $matches[1];
|
|
|
|
$ext = $matches[2] . $ext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$origFile = $file;
|
|
|
|
|
|
|
|
$i = 1;
|
|
|
|
while(file_exists("$base/$file$ext")) {
|
|
|
|
$i++;
|
2007-07-19 12:40:28 +02:00
|
|
|
$oldFile = $file;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2010-10-04 06:42:13 +02:00
|
|
|
if(strpos($file, '.') !== false) {
|
2012-02-27 22:14:02 +01:00
|
|
|
$file = preg_replace('/[0-9]*(\.[^.]+$)/', $i . '\\1', $file);
|
2010-10-04 06:44:14 +02:00
|
|
|
} elseif(strpos($file, '_') !== false) {
|
2012-02-27 22:14:02 +01:00
|
|
|
$file = preg_replace('/_([^_]+$)/', '_' . $i, $file);
|
2010-10-04 06:42:13 +02:00
|
|
|
} else {
|
2012-02-27 22:14:02 +01:00
|
|
|
$file .= '_'.$i;
|
2010-10-04 06:42:13 +02:00
|
|
|
}
|
2012-02-27 22:14:02 +01:00
|
|
|
|
2010-05-25 05:04:55 +02:00
|
|
|
if($oldFile == $file && $i > 2) user_error("Couldn't fix $file$ext with $i", E_USER_ERROR);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2010-05-25 05:04:55 +02:00
|
|
|
if (move_uploaded_file($tmpFile['tmp_name'], "$base/$file$ext")) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Update with the new image
|
2010-05-25 05:04:55 +02:00
|
|
|
return $this->constructChild(basename($file . $ext));
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!file_exists($tmpFile['tmp_name'])) {
|
|
|
|
user_error("Folder::addUploadToFolder: '$tmpFile[tmp_name]' doesn't exist", E_USER_ERROR);
|
|
|
|
} else {
|
|
|
|
user_error("Folder::addUploadToFolder: Couldn't copy '$tmpFile[tmp_name]' to '$base/$file$ext'",
|
|
|
|
E_USER_ERROR);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2015-06-16 01:39:32 +02:00
|
|
|
protected function validate() {
|
2010-10-13 01:02:10 +02:00
|
|
|
return new ValidationResult(true);
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
//-------------------------------------------------------------------------------------------------
|
|
|
|
// Data Model Definition
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getRelativePath() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return parent::getRelativePath() . "/";
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function onBeforeDelete() {
|
2009-01-10 12:35:50 +01:00
|
|
|
if($this->ID && ($children = $this->AllChildren())) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($children as $child) {
|
|
|
|
if(!$this->Filename || !$this->Name || !file_exists($this->getFullPath())) {
|
|
|
|
$child->setField('Name',null);
|
|
|
|
$child->Filename = null;
|
|
|
|
}
|
|
|
|
$child->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do this after so a folder's contents are removed before we delete the folder.
|
|
|
|
if($this->Filename && $this->Name && file_exists($this->getFullPath())) {
|
|
|
|
$files = glob( $this->getFullPath() . '/*' );
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if( !$files || ( count( $files ) == 1 && preg_match( '/\/_resampled$/', $files[0] ) ) )
|
|
|
|
Filesystem::removeFolder( $this->getFullPath() );
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::onBeforeDelete();
|
|
|
|
}
|
2011-03-23 01:23:43 +01:00
|
|
|
|
2016-03-15 19:12:38 +01:00
|
|
|
/**
|
|
|
|
* Return the relative URL of an icon for this file type
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function Icon() {
|
|
|
|
return FRAMEWORK_DIR . "/images/app_icons/folder_32.png";
|
|
|
|
}
|
|
|
|
|
2011-03-23 01:23:43 +01:00
|
|
|
/** Override setting the Title of Folders to that Name, Filename and Title are always in sync.
|
|
|
|
* Note that this is not appropriate for files, because someone might want to create a human-readable name
|
|
|
|
* of a file that is different from its name on disk. But folders should always match their name on disk. */
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setTitle($title) {
|
2014-05-02 04:41:10 +02:00
|
|
|
$this->setName($title);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTitle() {
|
|
|
|
return $this->Name;
|
2011-03-23 01:23:43 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setName($name) {
|
2011-03-23 01:23:43 +01:00
|
|
|
parent::setName($name);
|
2014-05-02 04:41:10 +02:00
|
|
|
$this->setField('Title', $this->Name);
|
2011-03-23 01:23:43 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setFilename($filename) {
|
2011-03-30 01:05:05 +02:00
|
|
|
$this->setField('Title',pathinfo($filename, PATHINFO_BASENAME));
|
2011-03-23 01:23:43 +01:00
|
|
|
parent::setFilename($filename);
|
|
|
|
}
|
2012-02-15 19:33:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A folder doesn't have a (meaningful) file size.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
2012-02-15 19:33:14 +01:00
|
|
|
* @return Null
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getSize() {
|
2012-02-15 19:33:14 +01:00
|
|
|
return null;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Delete the database record (recursively for folders) without touching the filesystem
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function deleteDatabaseOnly() {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($children = $this->myChildren()) {
|
|
|
|
foreach($children as $child) $child->deleteDatabaseOnly();
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::deleteDatabaseOnly();
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all children of this folder
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function myChildren() {
|
2013-06-21 00:32:08 +02:00
|
|
|
return File::get()->filter("ParentID", $this->ID);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns true if this folder has children
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function hasChildren() {
|
2013-06-21 00:32:08 +02:00
|
|
|
return $this->myChildren()->exists();
|
2009-07-02 00:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this folder has children
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2009-07-02 00:27:18 +02:00
|
|
|
*/
|
|
|
|
public function hasChildFolders() {
|
2013-06-21 00:32:08 +02:00
|
|
|
return $this->ChildFolders()->exists();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:20:47 +02:00
|
|
|
* Overloaded to call recursively on all contained {@link File} records.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2010-10-15 05:20:47 +02:00
|
|
|
public function updateFilesystem() {
|
|
|
|
parent::updateFilesystem();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// Note: Folders will have been renamed on the filesystem already at this point,
|
|
|
|
// File->updateFilesystem() needs to take this into account.
|
2009-01-10 12:35:50 +01:00
|
|
|
if($this->ID && ($children = $this->AllChildren())) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($children as $child) {
|
2010-10-15 05:20:47 +02:00
|
|
|
$child->updateFilesystem();
|
2007-07-19 12:40:28 +02:00
|
|
|
$child->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2011-10-28 03:37:27 +02:00
|
|
|
* Return the FieldList used to edit this folder in the CMS.
|
|
|
|
* You can modify this FieldList by subclassing folder, or by creating a {@link DataExtension}
|
2011-05-11 09:51:54 +02:00
|
|
|
* and implemeting updateCMSFields(FieldList $fields) on that extension.
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCMSFields() {
|
2012-02-21 23:19:49 +01:00
|
|
|
// Hide field on root level, which can't be renamed
|
2012-03-02 17:30:00 +01:00
|
|
|
if(!$this->ID || $this->ID === "root") {
|
2013-06-21 00:32:08 +02:00
|
|
|
$titleField = new HiddenField("Name");
|
2012-03-02 17:30:00 +01:00
|
|
|
} else {
|
|
|
|
$titleField = new TextField("Name", $this->fieldLabel('Name'));
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList(
|
2012-02-21 23:19:49 +01:00
|
|
|
$titleField,
|
|
|
|
new HiddenField('ParentID')
|
2008-02-25 03:10:37 +01:00
|
|
|
);
|
|
|
|
$this->extend('updateCMSFields', $fields);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2009-07-02 00:27:18 +02:00
|
|
|
/**
|
|
|
|
* Get the children of this folder that are also folders.
|
2013-06-21 00:32:08 +02:00
|
|
|
*
|
|
|
|
* @return DataList
|
2009-07-02 00:27:18 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function ChildFolders() {
|
2014-02-20 02:03:40 +01:00
|
|
|
return Folder::get()->filter('ParentID', $this->ID);
|
2009-07-02 00:27:18 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2015-05-19 23:19:01 +02:00
|
|
|
/**
|
|
|
|
* Get the number of children of this folder that are also folders.
|
|
|
|
*/
|
|
|
|
public function numChildFolders() {
|
|
|
|
return $this->ChildFolders()->count();
|
|
|
|
}
|
2009-11-21 03:33:42 +01:00
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function CMSTreeClasses() {
|
2009-11-21 03:33:42 +01:00
|
|
|
$classes = sprintf('class-%s', $this->class);
|
|
|
|
|
|
|
|
if(!$this->canDelete())
|
|
|
|
$classes .= " nodelete";
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
if(!$this->canEdit())
|
2009-11-21 03:33:42 +01:00
|
|
|
$classes .= " disabled";
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2015-05-19 23:19:01 +02:00
|
|
|
$classes .= $this->markingClasses('numChildFolders');
|
2009-11-21 03:33:42 +01:00
|
|
|
|
|
|
|
return $classes;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2012-04-26 01:42:56 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getTreeTitle() {
|
2012-04-26 01:42:56 +02:00
|
|
|
return $treeTitle = sprintf(
|
|
|
|
"<span class=\"jstree-foldericon\"></span><span class=\"item\">%s</span>",
|
|
|
|
Convert::raw2xml(str_replace(array("\n","\r"),"",$this->Title))
|
|
|
|
);
|
|
|
|
}
|
2011-03-23 01:23:43 +01:00
|
|
|
}
|