/** * Find the given folder or create it as a database record * * @param string $folderPath Directory path relative to assets root * @return Folder|null */ public static function find_or_make($folderPath) { // replace leading and trailing slashes $folderPath = preg_replace('/^\\/?(.*)\\/?$/', '$1', trim($folderPath)); $parts = explode("/", $folderPath); $parentID = 0; $item = null; $filter = FileNameFilter::create(); foreach ($parts as $part) { if (!$part) { continue; // happens for paths with a trailing slash } // 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(); if (!$item) { $item = new Folder(); $item->ParentID = $parentID; $item->Name = $partSafe; $item->Title = $part; $item->write(); } $parentID = $item->ID; } return $item; }
public function testCreateFromNameAndParentIDSetsFilename() { $folder1 = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'folder1'); $newFolder = new Folder(); $newFolder->Name = 'CreateFromNameAndParentID'; $newFolder->ParentID = $folder1->ID; $newFolder->write(); $this->assertEquals($folder1->Filename . 'CreateFromNameAndParentID/', $newFolder->Filename); }