API-CHANGE: refactoring to support replace document functionality

This commit is contained in:
Julian Seidenberg 2012-07-27 14:02:46 +12:00
parent cb2f61c260
commit 6a40448609
6 changed files with 80 additions and 32 deletions

View File

@ -23,6 +23,17 @@ class DMS implements DMSInterface {
return $dms;
}
function transformFileToFilePath($file) {
//confirm we have a file
$filePath = null;
if (is_string($file)) $filePath = $file;
elseif (is_object($file) && $file->is_a("File")) $filePath = $file->Filename;
if (!$filePath) throw new FileNotFoundException();
return $filePath;
}
/**
* Takes a File object or a String (path to a file) and copies it into the DMS. The original file remains unchanged.
* When storing a document, sets the fields on the File has "tag" metadata.
@ -30,35 +41,14 @@ class DMS implements DMSInterface {
*/
function storeDocument($file) {
//confirm we have a file
$filePath = null;
if (is_string($file)) $filePath = $file;
elseif (is_object($file) && $file->is_a("File")) $filePath = $file->Filename;
if (!$filePath) throw new FileNotFoundException();
$filePath = self::transformFileToFilePath($file);
//create a new document and get its ID
$modelClass = $this->modelClass;
$doc = new $modelClass();
$docID = $doc->write();
//calculate all the path to copy the file to
$fromFilename = basename($filePath);
$toFilename = $docID . '~' . $fromFilename; //add the docID to the start of the Filename
$toFolder = self::getStorageFolder($docID);
$toPath = self::$dmsPath . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename;
$this->createStorageFolder(self::$dmsPath . DIRECTORY_SEPARATOR . $toFolder);
//copy the file into place
$fromPath = BASE_PATH . DIRECTORY_SEPARATOR . $filePath;
copy($fromPath, $toPath);
//write the filename of the stored document
$doc->Filename = $toFilename;
$doc->Folder = $toFolder;
$doc->Title=$fromFilename;
$doc = new $modelClass($this);
$doc->write();
$doc->storeDocument($filePath);
return $doc;
}
@ -109,7 +99,7 @@ class DMS implements DMSInterface {
/**
* Calculates the storage path from a database DMSDocument ID
*/
static function getStorageFolder($id) {
function getStorageFolder($id) {
$folderName = intval($id / self::$dmsFolderSize);
return $folderName;
}

View File

@ -5,7 +5,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
"Folder" => "Varchar(255)", // eg. 0
"Title" => 'Varchar(1024)', // eg. "Energy Saving Report for Year 2011, New Zealand LandCorp"
"Description" => 'Text',
"LastChanged" => 'SS_DateTime' //when this document was created or last replaced (small changes like updating title don't count)
"LastChanged" => 'SS_DateTime' //when this document's file was created or last replaced (small changes like updating title don't count)
);
static $many_many = array(
@ -13,6 +13,12 @@ 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
@ -327,6 +333,32 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
parent::delete();
}
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);
$toPath = DMS::$dmsPath . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename;
$this->dms->createStorageFolder(DMS::$dmsPath . DIRECTORY_SEPARATOR . $toFolder);
//copy the file into place
$fromPath = BASE_PATH . DIRECTORY_SEPARATOR . $filePath;
copy($fromPath, $toPath); //this will overwrite the existing file (if present)
//write the filename of the stored document
$this->Filename = $toFilename;
$this->Folder = $toFolder;
if (empty($this->Title)) $this->Title = $fromFilename; //don't overwrite existing document titles
$this->LastChanged = SS_Datetime::now()->Rfc2822();
$this->write();
return $this;
}
/**
* Takes a File object or a String (path to a file) and copies it into the DMS, replacing the original document file
* but keeping the rest of the document unchanged.
@ -334,7 +366,9 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
* @return DMSDocumentInstance Document object that we replaced the file in
*/
function replaceDocument($file) {
// TODO: Implement replace() method.
$filePath = $this->dms->transformFileToFilePath($file);
$doc = $this->storeDocument($filePath); //replace the document
return $doc;
}
}

Binary file not shown.

View File

@ -39,7 +39,7 @@ class DMSDocumentTest extends SapphireTest {
$s2 = $this->objFromFixture('SiteTree','s2');
$s3 = $this->objFromFixture('SiteTree','s3');
$doc = new DMSDocument();
$doc = new DMSDocument(DMS::getDMSInstance());
$doc->Filename = "test file";
$doc->Folder = "0";
$doc->write();

View File

@ -15,7 +15,9 @@ class DMSTagTest extends SapphireTest {
}
function testAddingTags() {
$doc = new DMSDocument();
$dms = DMS::getDMSInstance();
$doc = new DMSDocument($dms);
$doc->Filename = "test file";
$doc->Folder = "0";
$doc->write();
@ -32,7 +34,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();
$doc2 = new DMSDocument($dms);
$doc2->Filename = "sneaky file";
$doc2->Folder = "0";
$doc2->write();
@ -61,7 +63,9 @@ class DMSTagTest extends SapphireTest {
}
function testRemovingTags() {
$doc = new DMSDocument();
$dms = DMS::getDMSInstance();
$doc = new DMSDocument($dms);
$doc->Filename = "test file";
$doc->Folder = "0";
$doc->write();

View File

@ -2,6 +2,7 @@
class DMSTest extends SapphireTest {
static $testFile = 'dms/tests/DMS-test-lorum-file.pdf';
static $testFile2 = 'dms/tests/DMS-test-document-2.pdf';
//store values to reset back to after this test runs
static $dmsFolderOld;
@ -96,5 +97,24 @@ class DMSTest extends SapphireTest {
}
}
function testReplaceDocument() {
$dms = DMS::getDMSInstance();
//store the first document
$document = $dms->storeDocument(self::$testFile);
$document->Title = "My custom title";
$document->Description = "My custom description";
$document->write();
//then overwrite with a second document
$document = $document->replaceDocument(self::$testFile2);
$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");
}
}