Merge branch 'master' of ssh://gitorious.silverstripe.com:2222/electricityauth/dms

This commit is contained in:
Julian Seidenberg 2012-07-27 12:45:36 +12:00
commit 9ba84e6211
3 changed files with 42 additions and 16 deletions

View File

@ -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;
}

View File

@ -1,6 +1,5 @@
<?php
class DMSDocument extends DataObject implements DMSDocumentInterface {
static $db = array(
"Filename" => "Varchar(255)", // eg. 3469~2011-energysaving-report.pdf
"Folder" => "Varchar(255)", // eg. 0
@ -16,13 +15,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.

View File

@ -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.