From 399754c551035c9b1ceac7afeec1af1cda942264 Mon Sep 17 00:00:00 2001 From: Normann Lou Date: Fri, 27 Jul 2012 12:22:27 +1200 Subject: [PATCH 1/2] APICHANGE: add one more handy interface method addPages($pageIDs). MINOR: correct inline document to reflect the evolved data model. --- code/DMSDocument.php | 19 +++++++++++++++++-- code/interface/DMSDocumentInterface.php | 11 ++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/code/DMSDocument.php b/code/DMSDocument.php index e63b867..75c2ee1 100644 --- a/code/DMSDocument.php +++ b/code/DMSDocument.php @@ -1,6 +1,5 @@ "Varchar(255)", // eg. 3469~2011-energysaving-report.pdf "Folder" => "Varchar(255)", // eg. 0 @@ -15,13 +14,29 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { /** * 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 has_many relation + * This could be a simple wrapper around $myDoc->Pages()->add($myPage) to add a many_many relation * @param $pageObject Page object to associate this Document with * @return null */ function addPage($pageObject) { $this->Pages()->add($pageObject); } + + /** + * Associates this DMSDocument with a set of Pages. This method loops through a set of page ids, and then associates this + * DMSDocument with the individual Page with the each page id in the set + * @abstract + * @param $pageIDs array of page ids used for the page objects associate this DMSDocument with + * @return null + */ + function addPages($pageIDs){ + foreach($pageIDs as $id){ + $pageObject = DataObject::get_by_id("SiteTree", $id); + if($pageObject && $pageObject->exists()) { + $this->addPage($pageObject); + } + } + } /** * Removes the association between this Document and a Page. This method does nothing if the association does not exist. diff --git a/code/interface/DMSDocumentInterface.php b/code/interface/DMSDocumentInterface.php index 702e855..646638b 100644 --- a/code/interface/DMSDocumentInterface.php +++ b/code/interface/DMSDocumentInterface.php @@ -18,12 +18,21 @@ interface DMSDocumentInterface { /** * Associates this DMSDocument 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 has_many relation + * This could be a simple wrapper around $myDoc->Pages()->add($myPage) to add a many_many relation * @abstract * @param $pageObject Page object to associate this DMSDocument with * @return null */ function addPage($pageObject); + + /** + * Associates this DMSDocument with a set of Pages. This method loops through a set of page ids, and then associates this + * DMSDocument with the individual Page with the each page id in the set + * @abstract + * @param $pageIDs array of page ids used for the page objects associate this DMSDocument with + * @return null + */ + function addPages($pageIDs); /** * Removes the association between this DMSDocument and a Page. This method does nothing if the association does not exist. From 72b4c51b91deed920b89259c912b4217b9e4aefd Mon Sep 17 00:00:00 2001 From: Normann Lou Date: Fri, 27 Jul 2012 12:28:56 +1200 Subject: [PATCH 2/2] 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; }