Completed
Push — master ( fb989d...03fe48 )
by Franco
02:26
created

DMS::getShortcodeHandlerKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
class DMS implements DMSInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
3
{
4
    /**
5
     * Folder to store the documents in
6
     *
7
     * @var string
8
     */
9
    public static $dmsFolder = 'dms-assets';
10
11
    /**
12
     * How many documents to store in a single folder. The square of this number is the maximum number of documents.
13
     *
14
     * The number should be a multiple of 10
15
     *
16
     * @var int
17
     */
18
    public static $dmsFolderSize = 1000;
19
20
    /**
21
     * The shortcode handler key. Can be changed by user code.
22
     *
23
     * @config
24
     * @var string
25
     */
26
    private static $shortcode_handler_key = 'dms_document_link';
0 ignored issues
show
Unused Code introduced by
The property $shortcode_handler_key is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
27
28
    /**
29
     * Factory method that returns an instance of the DMS. This could be any class that implements the DMSInterface.
30
     * @static
31
     * @return DMSInterface An instance of the Document Management System
32
     */
33
    public static function inst()
34
    {
35
        $dmsPath = self::get_dms_path();
36
37
        $dms = new DMS();
38
        if (!is_dir($dmsPath)) {
39
            self::create_storage_folder($dmsPath);
40
        }
41
42
        if (!file_exists($dmsPath . DIRECTORY_SEPARATOR . '.htaccess')) {
43
            // Restrict access to the storage folder
44
            copy(
45
                BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR . DIRECTORY_SEPARATOR
46
                . 'resources' . DIRECTORY_SEPARATOR . '.htaccess',
47
                $dmsPath . DIRECTORY_SEPARATOR . '.htaccess'
48
            );
49
50
            copy(
51
                BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR . DIRECTORY_SEPARATOR
52
                . 'resources' . DIRECTORY_SEPARATOR . 'web.config',
53
                $dmsPath . DIRECTORY_SEPARATOR . 'web.config'
54
            );
55
        }
56
        return $dms;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public static function get_dms_path()
63
    {
64
        return BASE_PATH . DIRECTORY_SEPARATOR . self::$dmsFolder;
65
    }
66
67
    public static function transform_file_to_file_path($file)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
68
    {
69
        //confirm we have a file
70
        $filePath = null;
71
        if (is_string($file)) {
72
            $filePath = $file;
73
        } elseif (is_object($file) && $file->is_a("File")) {
74
            $filePath = $file->Filename;
75
        }
76
77
        if (!$filePath) {
78
            throw new FileNotFoundException();
79
        }
80
81
        return $filePath;
82
    }
83
84
    /**
85
     * Takes a File object or a String (path to a file) and copies it into the DMS. The original file remains unchanged.
86
     * When storing a document, sets the fields on the File has "tag" metadata.
87
     * @param $file File object, or String that is path to a file to store,
88
     *              e.g. "assets/documents/industry/supplied-v1-0.pdf"
89
     * @return DMSDocument
90
     */
91
    public function storeDocument($file)
92
    {
93
        $filePath = self::transform_file_to_file_path($file);
94
95
        //create a new document and get its ID
96
        $doc = new DMSDocument();
97
        $doc->write();
98
        $doc->storeDocument($filePath);
99
100
        return $doc;
101
    }
102
103
    /**
104
     *
105
     * Returns a number of Document objects based on the a search by tags. You can search by category alone,
106
     * by tag value alone, or by both. I.e:
107
     *
108
     * <code>
109
     * getByTag("fruits", null);
110
     * getByTag(null, "banana");
111
     * getByTag("fruits", "banana");
112
     * </code>
113
     *
114
     * @param null $category The metadata category to search for
115
     * @param null $value The metadata value to search for
116
     * @param bool $showEmbargoed Boolean that specifies if embargoed documents should be included in results
117
     * @return DocumentInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be DocumentInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
118
     */
119
    public function getByTag($category = null, $value = null, $showEmbargoed = false)
120
    {
121
        // TODO: Implement getByTag() method.
122
    }
123
124
    /**
125
     * Returns a number of Document objects that match a full-text search of the Documents and their contents
126
     * (if contents is searchable and compatible search module is installed - e.g. FullTextSearch module)
127
     * @param $searchText String to search for
128
     * @param bool $showEmbargoed Boolean that specifies if embargoed documents should be included in results
129
     * @return DocumentInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be DocumentInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
130
     */
131
    public function getByFullTextSearch($searchText, $showEmbargoed = false)
132
    {
133
        // TODO: Implement getByFullTextSearch() method.
134
    }
135
136
    public function getByPage(SiteTree $page, $showEmbargoed = false)
137
    {
138
        /** @var ArrayList $documents */
139
        $documents = $page->getAllDocuments();
140
141
        if (!$showEmbargoed) {
142
            foreach ($documents as $document) {
143
                if ($document->isEmbargoed()) {
144
                    $documents->remove($document);
145
                }
146
            }
147
        }
148
149
        return $documents;
150
    }
151
152
    public function getDocumentSetsByPage(SiteTree $page)
153
    {
154
        return $page->getDocumentSets();
155
    }
156
157
    /**
158
     * Creates a storage folder for the given path
159
     * @param $path Path to create a folder for
160
     */
161
    public static function create_storage_folder($path)
162
    {
163
        if (!is_dir($path)) {
164
            mkdir($path, 0777);
165
        }
166
    }
167
168
    /**
169
     * Calculates the storage path from a database DMSDocument ID
170
     */
171
    public static function get_storage_folder($id)
172
    {
173
        $folderName = intval($id / self::$dmsFolderSize);
174
        return $folderName;
175
    }
176
177
    /**
178
     * Get the shortcode handler key
179
     *
180
     * @return string
181
     */
182
    public function getShortcodeHandlerKey()
183
    {
184
        return (string) Config::inst()->get('DMS', 'shortcode_handler_key');
185
    }
186
}
187