2012-07-16 08:21:48 +02:00
|
|
|
<?php
|
2017-05-17 06:24:25 +02:00
|
|
|
class DMS extends Object implements DMSInterface
|
2015-12-17 19:48:37 +01:00
|
|
|
{
|
2017-05-01 05:54:48 +02:00
|
|
|
/**
|
|
|
|
* Folder to store the documents in
|
|
|
|
*
|
2017-05-17 06:24:25 +02:00
|
|
|
* @config
|
2017-05-01 05:54:48 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
2017-05-17 06:24:25 +02:00
|
|
|
private static $folder_name = 'assets/_dmsassets';
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2017-05-17 06:24:25 +02:00
|
|
|
* @config
|
2017-05-01 05:54:48 +02:00
|
|
|
* @var int
|
|
|
|
*/
|
2017-05-17 06:24:25 +02:00
|
|
|
private static $folder_size = 1000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton instance of a DMSInterface
|
|
|
|
*
|
|
|
|
* @var DMSInterface
|
|
|
|
*/
|
|
|
|
private static $instance;
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-05-17 07:24:50 +02:00
|
|
|
/**
|
|
|
|
* The shortcode handler key. Can be changed by user code.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $shortcode_handler_key = 'dms_document_link';
|
2015-12-17 19:48:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory method that returns an instance of the DMS. This could be any class that implements the DMSInterface.
|
2017-05-17 06:24:25 +02:00
|
|
|
*
|
2015-12-17 19:48:37 +01:00
|
|
|
* @return DMSInterface An instance of the Document Management System
|
|
|
|
*/
|
|
|
|
public static function inst()
|
|
|
|
{
|
2017-05-17 06:24:25 +02:00
|
|
|
if (!self::$instance) {
|
|
|
|
self::$instance = new static();
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-05-17 06:24:25 +02:00
|
|
|
$dmsPath = self::$instance->getStoragePath();
|
|
|
|
|
|
|
|
if (!is_dir($dmsPath)) {
|
|
|
|
self::$instance->createStorageFolder($dmsPath);
|
|
|
|
}
|
2015-12-17 19:48:37 +01:00
|
|
|
|
2017-05-17 06:24:25 +02:00
|
|
|
if (!file_exists($dmsPath . DIRECTORY_SEPARATOR . '.htaccess')) {
|
|
|
|
// Restrict access to the storage folder
|
|
|
|
copy(
|
|
|
|
BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR . DIRECTORY_SEPARATOR
|
|
|
|
. 'resources' . DIRECTORY_SEPARATOR . '.htaccess',
|
|
|
|
$dmsPath . DIRECTORY_SEPARATOR . '.htaccess'
|
|
|
|
);
|
|
|
|
|
|
|
|
copy(
|
|
|
|
BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR . DIRECTORY_SEPARATOR
|
|
|
|
. 'resources' . DIRECTORY_SEPARATOR . 'web.config',
|
|
|
|
$dmsPath . DIRECTORY_SEPARATOR . 'web.config'
|
|
|
|
);
|
|
|
|
}
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
2017-05-17 06:24:25 +02:00
|
|
|
return self::$instance;
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
2017-05-01 05:54:48 +02:00
|
|
|
/**
|
2017-05-17 06:24:25 +02:00
|
|
|
* Get the storage path for DMS documents
|
|
|
|
*
|
2017-05-01 05:54:48 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-05-17 06:24:25 +02:00
|
|
|
public function getStoragePath()
|
2015-12-17 19:48:37 +01:00
|
|
|
{
|
2017-05-17 06:24:25 +02:00
|
|
|
return BASE_PATH . DIRECTORY_SEPARATOR . $this->config()->get('folder_name');
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 06:24:25 +02:00
|
|
|
/**
|
|
|
|
* Gets a file path from either a File or a string
|
|
|
|
*
|
|
|
|
* @param string|File $file
|
|
|
|
* @return string
|
|
|
|
* @throws FileNotFoundException If an unexpected value was provided, or the filename was null
|
|
|
|
*/
|
|
|
|
public function transformFileToFilePath($file)
|
2015-12-17 19:48:37 +01:00
|
|
|
{
|
|
|
|
//confirm we have a file
|
|
|
|
$filePath = null;
|
|
|
|
if (is_string($file)) {
|
|
|
|
$filePath = $file;
|
|
|
|
} elseif (is_object($file) && $file->is_a("File")) {
|
|
|
|
$filePath = $file->Filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$filePath) {
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2017-05-17 06:24:25 +02:00
|
|
|
* @param File|string $file File object, or String that is path to a file to store,
|
2017-05-01 05:54:48 +02:00
|
|
|
* e.g. "assets/documents/industry/supplied-v1-0.pdf"
|
|
|
|
* @return DMSDocument
|
2015-12-17 19:48:37 +01:00
|
|
|
*/
|
|
|
|
public function storeDocument($file)
|
|
|
|
{
|
2017-05-17 06:24:25 +02:00
|
|
|
$filePath = $this->transformFileToFilePath($file);
|
2017-05-01 05:54:48 +02:00
|
|
|
|
2017-05-17 06:24:25 +02:00
|
|
|
// Create a new document and get its ID
|
|
|
|
$doc = DMSDocument::create();
|
2015-12-17 19:48:37 +01:00
|
|
|
$doc->write();
|
|
|
|
$doc->storeDocument($filePath);
|
|
|
|
|
|
|
|
return $doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function getByFullTextSearch($searchText, $showEmbargoed = false)
|
|
|
|
{
|
|
|
|
// TODO: Implement getByFullTextSearch() method.
|
|
|
|
}
|
|
|
|
|
2017-05-02 04:49:41 +02:00
|
|
|
public function getByPage(SiteTree $page, $showEmbargoed = false)
|
|
|
|
{
|
|
|
|
/** @var ArrayList $documents */
|
|
|
|
$documents = $page->getAllDocuments();
|
|
|
|
|
|
|
|
if (!$showEmbargoed) {
|
|
|
|
foreach ($documents as $document) {
|
|
|
|
if ($document->isEmbargoed()) {
|
|
|
|
$documents->remove($document);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDocumentSetsByPage(SiteTree $page)
|
2015-12-17 19:48:37 +01:00
|
|
|
{
|
2018-04-11 04:10:55 +02:00
|
|
|
return $page->getDocumentSets();
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a storage folder for the given path
|
2017-05-17 06:24:25 +02:00
|
|
|
*
|
|
|
|
* @param string $path Path to create a folder for
|
|
|
|
* @return $this
|
2015-12-17 19:48:37 +01:00
|
|
|
*/
|
2017-05-17 06:24:25 +02:00
|
|
|
public function createStorageFolder($path)
|
2015-12-17 19:48:37 +01:00
|
|
|
{
|
|
|
|
if (!is_dir($path)) {
|
2017-05-17 06:24:25 +02:00
|
|
|
mkdir($path, 0777, true);
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
2017-05-17 06:24:25 +02:00
|
|
|
return $this;
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the storage path from a database DMSDocument ID
|
2017-05-17 06:24:25 +02:00
|
|
|
*
|
|
|
|
* @return int
|
2015-12-17 19:48:37 +01:00
|
|
|
*/
|
2017-05-17 06:24:25 +02:00
|
|
|
public function getStorageFolder($id)
|
2015-12-17 19:48:37 +01:00
|
|
|
{
|
2017-05-17 06:24:25 +02:00
|
|
|
return intval($id / self::config()->get('folder_size'));
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|
2017-05-17 07:24:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the shortcode handler key
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getShortcodeHandlerKey()
|
|
|
|
{
|
|
|
|
return (string) Config::inst()->get('DMS', 'shortcode_handler_key');
|
|
|
|
}
|
2015-12-17 19:48:37 +01:00
|
|
|
}
|