mirror of
https://github.com/silverstripe/silverstripe-dms
synced 2024-10-22 14:05:56 +02:00
Merge branch 'master' of ssh://gitorious.silverstripe.com:2222/electricityauth/dms
This commit is contained in:
commit
9ba84e6211
28
code/DMS.php
28
code/DMS.php
@ -7,56 +7,58 @@ class DMS implements DMSInterface {
|
|||||||
//The number should be a multiple of 10
|
//The number should be a multiple of 10
|
||||||
static $dmsFolderSize = 1000;
|
static $dmsFolderSize = 1000;
|
||||||
static $dmsPath; //DMS path set on creation
|
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.
|
* Factory method that returns an instance of the DMS. This could be any class that implements the DMSInterface.
|
||||||
* @static
|
* @static
|
||||||
* @return DMSInterface An instance of the Document Management System
|
* @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;
|
self::$dmsPath = BASE_PATH . DIRECTORY_SEPARATOR . self::$dmsFolder;
|
||||||
|
|
||||||
$dms = new DMS();
|
$dms = new DMS();
|
||||||
$dms->createStorageFolder(self::$dmsPath);
|
$dms->createStorageFolder(self::$dmsPath);
|
||||||
|
$dms->modelClass = $className;
|
||||||
return $dms;
|
return $dms;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes a File object or a String (path to a file) and copies it into the DMS. The original file remains unchanged.
|
* 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.
|
* 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
|
* @param $file File object, or String that is path to a file to store, e.g. "assets/documents/industry/supplied-v1-0.pdf"
|
||||||
* @return DMSDocumentInstance Document object that we just created
|
|
||||||
*/
|
*/
|
||||||
function storeDocument($file) {
|
function storeDocument($file) {
|
||||||
//confirm we have a file
|
//confirm we have a file
|
||||||
$fromPath = null;
|
$filePath = null;
|
||||||
if (is_string($file)) $fromPath = $file;
|
if (is_string($file)) $filePath = $file;
|
||||||
elseif (is_object($file) && $file->is_a("File")) $fromPath = $file->Filename;
|
elseif (is_object($file) && $file->is_a("File")) $filePath = $file->Filename;
|
||||||
|
|
||||||
if (!$fromPath) throw new FileNotFoundException();
|
|
||||||
|
|
||||||
|
if (!$filePath) throw new FileNotFoundException();
|
||||||
|
|
||||||
//create a new document and get its ID
|
//create a new document and get its ID
|
||||||
$doc = new DMSDocument();
|
$modelClass = $this->modelClass;
|
||||||
|
$doc = new $modelClass();
|
||||||
$docID = $doc->write();
|
$docID = $doc->write();
|
||||||
|
|
||||||
//calculate all the path to copy the file to
|
//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
|
$toFilename = $docID . '~' . $fromFilename; //add the docID to the start of the Filename
|
||||||
$toFolder = self::getStorageFolder($docID);
|
$toFolder = self::getStorageFolder($docID);
|
||||||
$toPath = self::$dmsPath . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename;
|
$toPath = self::$dmsPath . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename;
|
||||||
$this->createStorageFolder(self::$dmsPath . DIRECTORY_SEPARATOR . $toFolder);
|
$this->createStorageFolder(self::$dmsPath . DIRECTORY_SEPARATOR . $toFolder);
|
||||||
|
|
||||||
//copy the file into place
|
//copy the file into place
|
||||||
|
$fromPath = BASE_PATH . DIRECTORY_SEPARATOR . $filePath;
|
||||||
copy($fromPath, $toPath);
|
copy($fromPath, $toPath);
|
||||||
|
|
||||||
//write the filename of the stored document
|
//write the filename of the stored document
|
||||||
$doc->Filename = $toFilename;
|
$doc->Filename = $toFilename;
|
||||||
$doc->Folder = $toFolder;
|
$doc->Folder = $toFolder;
|
||||||
$doc->Title=$fromFilename;
|
$doc->Title=$fromFilename;
|
||||||
|
|
||||||
$doc->write();
|
$doc->write();
|
||||||
|
|
||||||
|
|
||||||
return $doc;
|
return $doc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
class DMSDocument extends DataObject implements DMSDocumentInterface {
|
class DMSDocument extends DataObject implements DMSDocumentInterface {
|
||||||
|
|
||||||
static $db = array(
|
static $db = array(
|
||||||
"Filename" => "Varchar(255)", // eg. 3469~2011-energysaving-report.pdf
|
"Filename" => "Varchar(255)", // eg. 3469~2011-energysaving-report.pdf
|
||||||
"Folder" => "Varchar(255)", // eg. 0
|
"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.
|
* 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
|
* @param $pageObject Page object to associate this Document with
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
function addPage($pageObject) {
|
function addPage($pageObject) {
|
||||||
$this->Pages()->add($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.
|
* Removes the association between this Document and a Page. This method does nothing if the association does not exist.
|
||||||
|
@ -18,12 +18,21 @@ interface DMSDocumentInterface {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Associates this DMSDocument with a Page. This method does nothing if the association already exists.
|
* 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
|
* @abstract
|
||||||
* @param $pageObject Page object to associate this DMSDocument with
|
* @param $pageObject Page object to associate this DMSDocument with
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
function addPage($pageObject);
|
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.
|
* Removes the association between this DMSDocument and a Page. This method does nothing if the association does not exist.
|
||||||
|
Loading…
Reference in New Issue
Block a user