mirror of
https://github.com/silverstripe/silverstripe-dms
synced 2024-10-22 12:05:56 +00:00
API-CHANGE: enabling replace document
This commit is contained in:
parent
6a40448609
commit
0f6e7be3b9
10
code/DMS.php
10
code/DMS.php
@ -18,12 +18,12 @@ class DMS implements DMSInterface {
|
||||
self::$dmsPath = BASE_PATH . DIRECTORY_SEPARATOR . self::$dmsFolder;
|
||||
|
||||
$dms = new DMS();
|
||||
$dms->createStorageFolder(self::$dmsPath);
|
||||
self::createStorageFolder(self::$dmsPath);
|
||||
$dms->modelClass = $className;
|
||||
return $dms;
|
||||
}
|
||||
|
||||
function transformFileToFilePath($file) {
|
||||
static function transformFileToFilePath($file) {
|
||||
//confirm we have a file
|
||||
$filePath = null;
|
||||
if (is_string($file)) $filePath = $file;
|
||||
@ -45,7 +45,7 @@ class DMS implements DMSInterface {
|
||||
|
||||
//create a new document and get its ID
|
||||
$modelClass = $this->modelClass;
|
||||
$doc = new $modelClass($this);
|
||||
$doc = new $modelClass();
|
||||
$doc->write();
|
||||
$doc->storeDocument($filePath);
|
||||
|
||||
@ -90,7 +90,7 @@ class DMS implements DMSInterface {
|
||||
* Creates a storage folder for the given path
|
||||
* @param $path Path to create a folder for
|
||||
*/
|
||||
protected function createStorageFolder($path) {
|
||||
static function createStorageFolder($path) {
|
||||
if (!is_dir($path)) {
|
||||
mkdir($path, 0777);
|
||||
}
|
||||
@ -99,7 +99,7 @@ class DMS implements DMSInterface {
|
||||
/**
|
||||
* Calculates the storage path from a database DMSDocument ID
|
||||
*/
|
||||
function getStorageFolder($id) {
|
||||
static function getStorageFolder($id) {
|
||||
$folderName = intval($id / self::$dmsFolderSize);
|
||||
return $folderName;
|
||||
}
|
||||
|
@ -13,12 +13,6 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
|
||||
'Tags' => 'DMSTag'
|
||||
);
|
||||
|
||||
protected $dms; //this DMSDocument's associated DMS instance
|
||||
|
||||
function __construct($dms = null) {
|
||||
$this->dms = $dms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Associates this document with a Page. This method does nothing if the association already exists.
|
||||
* This could be a simple wrapper around $myDoc->Pages()->add($myPage) to add a many_many relation
|
||||
@ -335,14 +329,13 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
|
||||
|
||||
function storeDocument($filePath) {
|
||||
if (empty($this->ID)) user_error("Document must be written to database before it can store documents",E_USER_ERROR);
|
||||
if (empty($dms) || !(is_a($dms, 'DMS'))) user_error("You need to provide a DMS object when storing a document",E_USER_ERROR);
|
||||
|
||||
//calculate all the path to copy the file to
|
||||
$fromFilename = basename($filePath);
|
||||
$toFilename = $this->ID. '~' . $fromFilename; //add the docID to the start of the Filename
|
||||
$toFolder = $this->dms->getStorageFolder($this->ID);
|
||||
$toFolder = DMS::getStorageFolder($this->ID);
|
||||
$toPath = DMS::$dmsPath . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename;
|
||||
$this->dms->createStorageFolder(DMS::$dmsPath . DIRECTORY_SEPARATOR . $toFolder);
|
||||
DMS::createStorageFolder(DMS::$dmsPath . DIRECTORY_SEPARATOR . $toFolder);
|
||||
|
||||
//copy the file into place
|
||||
$fromPath = BASE_PATH . DIRECTORY_SEPARATOR . $filePath;
|
||||
@ -366,7 +359,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
|
||||
* @return DMSDocumentInstance Document object that we replaced the file in
|
||||
*/
|
||||
function replaceDocument($file) {
|
||||
$filePath = $this->dms->transformFileToFilePath($file);
|
||||
$filePath = DMS::transformFileToFilePath($file);
|
||||
$doc = $this->storeDocument($filePath); //replace the document
|
||||
return $doc;
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ class DMSDocumentTest extends SapphireTest {
|
||||
$s2 = $this->objFromFixture('SiteTree','s2');
|
||||
$s3 = $this->objFromFixture('SiteTree','s3');
|
||||
|
||||
$doc = new DMSDocument(DMS::getDMSInstance());
|
||||
$doc = new DMSDocument();
|
||||
$doc->Filename = "test file";
|
||||
$doc->Folder = "0";
|
||||
$doc->write();
|
||||
|
@ -15,9 +15,7 @@ class DMSTagTest extends SapphireTest {
|
||||
}
|
||||
|
||||
function testAddingTags() {
|
||||
$dms = DMS::getDMSInstance();
|
||||
|
||||
$doc = new DMSDocument($dms);
|
||||
$doc = new DMSDocument();
|
||||
$doc->Filename = "test file";
|
||||
$doc->Folder = "0";
|
||||
$doc->write();
|
||||
@ -34,7 +32,7 @@ class DMSTagTest extends SapphireTest {
|
||||
$this->assertTrue(in_array("banana",$fruits),"correct fruit tags returned");
|
||||
|
||||
//sneakily create another document and link one of the tags to that, too
|
||||
$doc2 = new DMSDocument($dms);
|
||||
$doc2 = new DMSDocument();
|
||||
$doc2->Filename = "sneaky file";
|
||||
$doc2->Folder = "0";
|
||||
$doc2->write();
|
||||
@ -63,9 +61,7 @@ class DMSTagTest extends SapphireTest {
|
||||
}
|
||||
|
||||
function testRemovingTags() {
|
||||
$dms = DMS::getDMSInstance();
|
||||
|
||||
$doc = new DMSDocument($dms);
|
||||
$doc = new DMSDocument();
|
||||
$doc->Filename = "test file";
|
||||
$doc->Folder = "0";
|
||||
$doc->write();
|
||||
|
@ -111,9 +111,9 @@ class DMSTest extends SapphireTest {
|
||||
|
||||
$this->assertNotNull($document, "Document object created");
|
||||
$this->assertTrue(file_exists(DMS::$dmsPath . DIRECTORY_SEPARATOR . $document->Folder . DIRECTORY_SEPARATOR . $document->Filename),"Document file copied into DMS folder");
|
||||
$this->assertContains($document->Filename, "DMS-test-document-2", "Original document filename is contain in the new filename");
|
||||
$this->assertEquals($document->Title, "My custom title", "Custom title not modified");
|
||||
$this->assertEquals($document->Description, "My custom description", "Custom description not modified");
|
||||
$this->assertContains("DMS-test-document-2",$document->Filename, "Original document filename is contain in the new filename");
|
||||
$this->assertEquals("My custom title", $document->Title , "Custom title not modified");
|
||||
$this->assertEquals("My custom description", $document->Description, "Custom description not modified");
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user