From 72b4c51b91deed920b89259c912b4217b9e4aefd Mon Sep 17 00:00:00 2001 From: Normann Lou Date: Fri, 27 Jul 2012 12:28:56 +1200 Subject: [PATCH] BUGFIX: when copy the physical file into the dms-assets folder, use the file absolute path for both from path and to path. FEATURE: hook a DMS instance to manage an model class definable by the caller, instead of hard coded DMSDocument, treat DMSDocument as default managed model class. --- code/DMS.php | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/code/DMS.php b/code/DMS.php index c18d62b..0eadad3 100644 --- a/code/DMS.php +++ b/code/DMS.php @@ -7,56 +7,58 @@ class DMS implements DMSInterface { //The number should be a multiple of 10 static $dmsFolderSize = 1000; static $dmsPath; //DMS path set on creation + protected $modelClass; /** * Factory method that returns an instance of the DMS. This could be any class that implements the DMSInterface. * @static * @return DMSInterface An instance of the Document Management System */ - static function getDMSInstance() { + static function getDMSInstance($className="DMSDocument") { self::$dmsPath = BASE_PATH . DIRECTORY_SEPARATOR . self::$dmsFolder; $dms = new DMS(); $dms->createStorageFolder(self::$dmsPath); - + $dms->modelClass = $className; return $dms; } /** * 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. - * @param $file File object, or String that is path to a file to store - * @return DMSDocumentInstance Document object that we just created + * @param $file File object, or String that is path to a file to store, e.g. "assets/documents/industry/supplied-v1-0.pdf" + */ function storeDocument($file) { //confirm we have a file - $fromPath = null; - if (is_string($file)) $fromPath = $file; - elseif (is_object($file) && $file->is_a("File")) $fromPath = $file->Filename; - - if (!$fromPath) throw new FileNotFoundException(); + $filePath = null; + if (is_string($file)) $filePath = $file; + elseif (is_object($file) && $file->is_a("File")) $filePath = $file->Filename; + if (!$filePath) throw new FileNotFoundException(); + //create a new document and get its ID - $doc = new DMSDocument(); + $modelClass = $this->modelClass; + $doc = new $modelClass(); $docID = $doc->write(); //calculate all the path to copy the file to - $fromFilename = basename($fromPath); + $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->write(); - - return $doc; }