2012-07-16 08:21:48 +02:00
|
|
|
<?php
|
|
|
|
class DMS implements DMSInterface {
|
|
|
|
|
|
|
|
static $dmsFolder = 'dms-assets'; //folder to store the documents in
|
|
|
|
|
|
|
|
//How many documents to store in a single folder. The square of this number is the maximum number of documents.
|
|
|
|
//The number should be a multiple of 10
|
|
|
|
static $dmsFolderSize = 1000;
|
|
|
|
static $dmsPath; //DMS path set on creation
|
2012-07-27 02:28:56 +02:00
|
|
|
protected $modelClass;
|
2012-07-16 08:21:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-07-27 02:28:56 +02:00
|
|
|
static function getDMSInstance($className="DMSDocument") {
|
2012-07-16 08:21:48 +02:00
|
|
|
self::$dmsPath = BASE_PATH . DIRECTORY_SEPARATOR . self::$dmsFolder;
|
|
|
|
|
|
|
|
$dms = new DMS();
|
|
|
|
$dms->createStorageFolder(self::$dmsPath);
|
2012-07-27 02:28:56 +02:00
|
|
|
$dms->modelClass = $className;
|
2012-07-16 08:21:48 +02:00
|
|
|
return $dms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a File object or a String (path to a file) and copies it into the DMS. The original file remains unchanged.
|
2012-07-26 02:50:52 +02:00
|
|
|
* When storing a document, sets the fields on the File has "tag" metadata.
|
2012-07-27 02:28:56 +02:00
|
|
|
* @param $file File object, or String that is path to a file to store, e.g. "assets/documents/industry/supplied-v1-0.pdf"
|
|
|
|
|
2012-07-16 08:21:48 +02:00
|
|
|
*/
|
|
|
|
function storeDocument($file) {
|
|
|
|
//confirm we have a file
|
2012-07-27 02:28:56 +02:00
|
|
|
$filePath = null;
|
|
|
|
if (is_string($file)) $filePath = $file;
|
|
|
|
elseif (is_object($file) && $file->is_a("File")) $filePath = $file->Filename;
|
2012-07-16 08:21:48 +02:00
|
|
|
|
2012-07-27 02:28:56 +02:00
|
|
|
if (!$filePath) throw new FileNotFoundException();
|
|
|
|
|
2012-07-16 08:21:48 +02:00
|
|
|
//create a new document and get its ID
|
2012-07-27 02:28:56 +02:00
|
|
|
$modelClass = $this->modelClass;
|
|
|
|
$doc = new $modelClass();
|
2012-07-16 08:21:48 +02:00
|
|
|
$docID = $doc->write();
|
|
|
|
|
|
|
|
//calculate all the path to copy the file to
|
2012-07-27 02:28:56 +02:00
|
|
|
$fromFilename = basename($filePath);
|
2012-07-16 08:21:48 +02:00
|
|
|
$toFilename = $docID . '~' . $fromFilename; //add the docID to the start of the Filename
|
|
|
|
$toFolder = self::getStorageFolder($docID);
|
2012-07-17 03:52:46 +02:00
|
|
|
$toPath = self::$dmsPath . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename;
|
|
|
|
$this->createStorageFolder(self::$dmsPath . DIRECTORY_SEPARATOR . $toFolder);
|
2012-07-16 08:21:48 +02:00
|
|
|
|
|
|
|
//copy the file into place
|
2012-07-27 02:28:56 +02:00
|
|
|
$fromPath = BASE_PATH . DIRECTORY_SEPARATOR . $filePath;
|
2012-07-17 03:52:46 +02:00
|
|
|
copy($fromPath, $toPath);
|
2012-07-16 08:21:48 +02:00
|
|
|
|
|
|
|
//write the filename of the stored document
|
2012-07-17 03:52:46 +02:00
|
|
|
$doc->Filename = $toFilename;
|
|
|
|
$doc->Folder = $toFolder;
|
2012-07-26 02:50:52 +02:00
|
|
|
$doc->Title=$fromFilename;
|
2012-07-16 08:21:48 +02:00
|
|
|
|
2012-07-27 02:28:56 +02:00
|
|
|
$doc->write();
|
2012-07-16 08:21:48 +02:00
|
|
|
return $doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Returns a number of Document objects based on the a search by tags. You can search by category alone,
|
|
|
|
* by tag value alone, or by both. I.e: getByTag("fruits",null); getByTag(null,"banana"); getByTag("fruits","banana")
|
|
|
|
* @param null $category The metadata category to search for
|
|
|
|
* @param null $value The metadata value to search for
|
|
|
|
* @param bool $showEmbargoed Boolean that specifies if embargoed documents should be included in results
|
|
|
|
* @return DocumentInterface
|
|
|
|
*/
|
|
|
|
function getByTag($category = null, $value = null, $showEmbargoed = false) {
|
|
|
|
// TODO: Implement getByTag() method.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a number of Document objects that match a full-text search of the Documents and their contents
|
|
|
|
* (if contents is searchable and compatible search module is installed - e.g. FullTextSearch module)
|
|
|
|
* @param $searchText String to search for
|
|
|
|
* @param bool $showEmbargoed Boolean that specifies if embargoed documents should be included in results
|
|
|
|
* @return DocumentInterface
|
|
|
|
*/
|
|
|
|
function getByFullTextSearch($searchText, $showEmbargoed = false) {
|
|
|
|
// TODO: Implement getByFullTextSearch() method.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of Document objects associated with a Page
|
|
|
|
* @param $page SiteTree to fetch the associated Documents from
|
|
|
|
* @param bool $showEmbargoed Boolean that specifies if embargoed documents should be included in results
|
|
|
|
* @return DataList Document list associated with the Page
|
|
|
|
*/
|
|
|
|
function getByPage($page, $showEmbargoed = false) {
|
|
|
|
// TODO: Implement getByPage() method.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a storage folder for the given path
|
|
|
|
* @param $path Path to create a folder for
|
|
|
|
*/
|
|
|
|
protected function createStorageFolder($path) {
|
|
|
|
if (!is_dir($path)) {
|
|
|
|
mkdir($path, 0777);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the storage path from a database DMSDocument ID
|
|
|
|
*/
|
|
|
|
static function getStorageFolder($id) {
|
|
|
|
$folderName = intval($id / self::$dmsFolderSize);
|
2012-07-17 03:52:46 +02:00
|
|
|
return $folderName;
|
2012-07-16 08:21:48 +02:00
|
|
|
}
|
|
|
|
}
|