Completed
Push — master ( e4ab2b...e8d46a )
by Franco
13s
created

code/DMS.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
class DMS extends Object implements DMSInterface
3
{
4
    /**
5
     * Folder to store the documents in
6
     *
7
     * @config
8
     * @var string
9
     */
10
    private static $folder_name = 'assets/_dmsassets';
11
12
    /**
13
     * How many documents to store in a single folder. The square of this number is the maximum number of documents.
14
     *
15
     * The number should be a multiple of 10
16
     *
17
     * @config
18
     * @var int
19
     */
20
    private static $folder_size = 1000;
21
22
    /**
23
     * Singleton instance of a DMSInterface
24
     *
25
     * @var DMSInterface
26
     */
27
    private static $instance;
28
29
    /**
30
     * The shortcode handler key. Can be changed by user code.
31
     *
32
     * @config
33
     * @var string
34
     */
35
    private static $shortcode_handler_key = 'dms_document_link';
36
37
    /**
38
     * Factory method that returns an instance of the DMS. This could be any class that implements the DMSInterface.
39
     *
40
     * @return DMSInterface An instance of the Document Management System
41
     */
42
    public static function inst()
43
    {
44
        if (!self::$instance) {
45
            self::$instance = new static();
46
47
            $dmsPath = self::$instance->getStoragePath();
48
49
            if (!is_dir($dmsPath)) {
50
                self::$instance->createStorageFolder($dmsPath);
51
            }
52
53
            if (!file_exists($dmsPath . DIRECTORY_SEPARATOR . '.htaccess')) {
54
                // Restrict access to the storage folder
55
                copy(
56
                    BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR . DIRECTORY_SEPARATOR
57
                    . 'resources' . DIRECTORY_SEPARATOR . '.htaccess',
58
                    $dmsPath . DIRECTORY_SEPARATOR . '.htaccess'
59
                );
60
61
                copy(
62
                    BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR . DIRECTORY_SEPARATOR
63
                    . 'resources' . DIRECTORY_SEPARATOR . 'web.config',
64
                    $dmsPath . DIRECTORY_SEPARATOR . 'web.config'
65
                );
66
            }
67
        }
68
        return self::$instance;
69
    }
70
71
    /**
72
     * Get the storage path for DMS documents
73
     *
74
     * @return string
75
     */
76
    public function getStoragePath()
77
    {
78
        return BASE_PATH . DIRECTORY_SEPARATOR . $this->config()->get('folder_name');
79
    }
80
81
    /**
82
     * Gets a file path from either a File or a string
83
     *
84
     * @param  string|File $file
85
     * @return string
86
     * @throws FileNotFoundException If an unexpected value was provided, or the filename was null
87
     */
88
    public function transformFileToFilePath($file)
89
    {
90
        //confirm we have a file
91
        $filePath = null;
92
        if (is_string($file)) {
93
            $filePath = $file;
94
        } elseif (is_object($file) && $file->is_a("File")) {
95
            $filePath = $file->Filename;
96
        }
97
98
        if (!$filePath) {
99
            throw new FileNotFoundException();
100
        }
101
102
        return $filePath;
103
    }
104
105
    /**
106
     * Takes a File object or a String (path to a file) and copies it into the DMS. The original file remains unchanged.
107
     * When storing a document, sets the fields on the File has "tag" metadata.
108
     * @param  File|string $file File object, or String that is path to a file to store,
109
     *              e.g. "assets/documents/industry/supplied-v1-0.pdf"
110
     * @return DMSDocument
111
     */
112
    public function storeDocument($file)
113
    {
114
        $filePath = $this->transformFileToFilePath($file);
115
116
        // Create a new document and get its ID
117
        $doc = DMSDocument::create();
118
        $doc->write();
119
        $doc->storeDocument($filePath);
120
121
        return $doc;
122
    }
123
124
    /**
125
     *
126
     * Returns a number of Document objects based on the a search by tags. You can search by category alone,
127
     * by tag value alone, or by both. I.e:
128
     *
129
     * <code>
130
     * getByTag("fruits", null);
131
     * getByTag(null, "banana");
132
     * getByTag("fruits", "banana");
133
     * </code>
134
     *
135
     * @param null $category The metadata category to search for
136
     * @param null $value The metadata value to search for
137
     * @param bool $showEmbargoed Boolean that specifies if embargoed documents should be included in results
138
     * @return DocumentInterface
0 ignored issues
show
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...
139
     */
140
    public function getByTag($category = null, $value = null, $showEmbargoed = false)
141
    {
142
        // TODO: Implement getByTag() method.
143
    }
144
145
    /**
146
     * Returns a number of Document objects that match a full-text search of the Documents and their contents
147
     * (if contents is searchable and compatible search module is installed - e.g. FullTextSearch module)
148
     * @param $searchText String to search for
149
     * @param bool $showEmbargoed Boolean that specifies if embargoed documents should be included in results
150
     * @return DocumentInterface
151
     */
152
    public function getByFullTextSearch($searchText, $showEmbargoed = false)
153
    {
154
        // TODO: Implement getByFullTextSearch() method.
155
    }
156
157
    public function getByPage(SiteTree $page, $showEmbargoed = false)
158
    {
159
        /** @var ArrayList $documents */
160
        $documents = $page->getAllDocuments();
161
162
        if (!$showEmbargoed) {
163
            foreach ($documents as $document) {
164
                if ($document->isEmbargoed()) {
165
                    $documents->remove($document);
166
                }
167
            }
168
        }
169
170
        return $documents;
171
    }
172
173
    public function getDocumentSetsByPage(SiteTree $page)
174
    {
175
        return $page->getDocumentSets();
176
    }
177
178
    /**
179
     * Creates a storage folder for the given path
180
     *
181
     * @param  string $path Path to create a folder for
182
     * @return $this
183
     */
184
    public function createStorageFolder($path)
185
    {
186
        if (!is_dir($path)) {
187
            mkdir($path, 0777, true);
188
        }
189
        return $this;
190
    }
191
192
    /**
193
     * Calculates the storage path from a database DMSDocument ID
194
     *
195
     * @return int
196
     */
197
    public function getStorageFolder($id)
198
    {
199
        return intval($id / self::config()->get('folder_size'));
200
    }
201
202
    /**
203
     * Get the shortcode handler key
204
     *
205
     * @return string
206
     */
207
    public function getShortcodeHandlerKey()
208
    {
209
        return (string) Config::inst()->get('DMS', 'shortcode_handler_key');
210
    }
211
}
212