silverstripe-dms/code/DMS.php

191 lines
5.3 KiB
PHP
Raw Normal View History

<?php
class DMS extends Object implements DMSInterface
2015-12-17 19:48:37 +01:00
{
/**
* Folder to store the documents in
*
* @config
* @var string
*/
private static $folder_name = 'assets/_dmsassets';
2015-12-17 19:48:37 +01: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
*
* @config
* @var int
*/
private static $folder_size = 1000;
/**
* Singleton instance of a DMSInterface
*
* @var DMSInterface
*/
private static $instance;
2015-12-17 19:48:37 +01: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.
*
2015-12-17 19:48:37 +01:00
* @return DMSInterface An instance of the Document Management System
*/
public static function inst()
{
if (!self::$instance) {
self::$instance = new static();
2015-12-17 19:48:37 +01:00
$dmsPath = self::$instance->getStoragePath();
if (!is_dir($dmsPath)) {
self::$instance->createStorageFolder($dmsPath);
}
2015-12-17 19:48:37 +01: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
}
return self::$instance;
2015-12-17 19:48:37 +01:00
}
/**
* Get the storage path for DMS documents
*
* @return string
*/
public function getStoragePath()
2015-12-17 19:48:37 +01:00
{
return BASE_PATH . DIRECTORY_SEPARATOR . $this->config()->get('folder_name');
2015-12-17 19:48:37 +01: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.
* @param File|string $file File object, or String that is path to a file to store,
* e.g. "assets/documents/industry/supplied-v1-0.pdf"
* @return DMSDocument
2015-12-17 19:48:37 +01:00
*/
public function storeDocument($file)
{
$filePath = $this->transformFileToFilePath($file);
// 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.
}
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
{
return $page->getDocumentSets();
2015-12-17 19:48:37 +01:00
}
/**
* Creates a storage folder for the given path
*
* @param string $path Path to create a folder for
* @return $this
2015-12-17 19:48:37 +01:00
*/
public function createStorageFolder($path)
2015-12-17 19:48:37 +01:00
{
if (!is_dir($path)) {
mkdir($path, 0777, true);
2015-12-17 19:48:37 +01:00
}
return $this;
2015-12-17 19:48:37 +01:00
}
/**
* Calculates the storage path from a database DMSDocument ID
*
* @return int
2015-12-17 19:48:37 +01:00
*/
public function getStorageFolder($id)
2015-12-17 19:48:37 +01:00
{
return intval($id / self::config()->get('folder_size'));
2015-12-17 19:48:37 +01: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
}