DMSDocument   F
last analyzed

Complexity

Total Complexity 165

Size/Duplication

Total Lines 1338
Duplicated Lines 2.39 %

Coupling/Cohesion

Components 1
Dependencies 46

Importance

Changes 0
Metric Value
wmc 165
lcom 1
cbo 46
dl 32
loc 1338
rs 0.8
c 0
b 0
f 0

47 Methods

Rating   Name   Duplication   Size   Complexity  
D canView() 3 41 18
C canEdit() 3 29 14
B canCreate() 3 23 9
B canDelete() 3 16 7
A trackView() 0 14 2
A getLink() 0 12 2
A Link() 0 4 1
A embargoIndefinitely() 0 10 2
A embargoUntilPublished() 0 10 2
A isHidden() 0 13 4
B isEmbargoed() 0 20 6
A embargoUntilDate() 10 10 2
A clearEmbargo() 0 12 2
A isExpired() 0 16 4
A expireAtDate() 10 10 2
A clearExpiry() 0 10 2
A getVersions() 0 8 2
A getFullPath() 0 9 2
A getFilename() 0 7 2
A getName() 0 4 1
A getFilenameWithoutID() 0 7 1
A getStorageFolder() 0 4 1
B delete() 0 41 10
B storeDocument() 0 44 5
A replaceDocument() 0 7 1
A get_file_type() 0 28 2
A getDescriptionWithLineBreak() 0 4 1
C getCMSFields() 0 191 8
A getPermissionsActionPanel() 0 35 4
A getTitle() 0 7 2
D onBeforeWrite() 0 52 13
A Icon() 0 12 3
A getExtension() 0 4 1
A getSize() 0 5 2
A getAbsoluteSize() 0 4 2
A getFileSizeFormatted() 0 4 1
B getFieldsForFile() 0 75 3
A ingestFile() 0 7 1
A getRelatedDocuments() 0 8 1
A getRelatedPages() 0 14 2
A getRelatedDocumentsGridField() 0 30 1
A getRelatedDocumentsForAutocompleter() 0 6 1
A validate() 0 24 5
A getPermissionDeniedReason() 0 17 3
A addActionPanelTask() 0 5 1
A getActionTaskHtml() 0 19 2
A removeActionPanelTask() 0 7 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like DMSDocument often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DMSDocument, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @package dms
5
 *
6
 * @property Varchar Filename
7
 * @property Varchar Folder
8
 * @property Varchar Title
9
 * @property Text Description
10
 * @property int ViewCount
11
 * @property Boolean EmbargoedIndefinitely
12
 * @property Boolean EmbargoedUntilPublished
13
 * @property DateTime EmbargoedUntilDate
14
 * @property DateTime ExpireAtDate
15
 * @property Enum DownloadBehavior
16
 * @property Enum CanViewType Enum('Anyone, LoggedInUsers, OnlyTheseUsers', 'Anyone')
17
 * @property Enum CanEditType Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')
18
 *
19
 * @method ManyManyList RelatedDocuments
20
 * @method ManyManyList ViewerGroups
21
 * @method ManyManyList EditorGroups
22
 *
23
 * @method Member CreatedBy
24
 * @property Int CreatedByID
25
 * @method Member LastEditedBy
26
 * @property Int LastEditedByID
27
 *
28
 */
29
class DMSDocument extends DataObject implements DMSDocumentInterface
30
{
31
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by helpfulrobot
Consider using a different property name as you override a private property of the parent class.
Loading history...
32
        "Filename" => "Varchar(255)", // eg. 3469~2011-energysaving-report.pdf
33
        "Folder" => "Varchar(255)",    // eg.	0
34
        "Title" => 'Varchar(1024)', // eg. "Energy Saving Report for Year 2011, New Zealand LandCorp"
35
        "Description" => 'Text',
36
        "ViewCount" => 'Int',
37
        "EmbargoedIndefinitely" => 'Boolean(false)',
38
        "EmbargoedUntilPublished" => 'Boolean(false)',
39
        "EmbargoedUntilDate" => 'SS_DateTime',
40
        "ExpireAtDate" => 'SS_DateTime',
41
        "DownloadBehavior" => 'Enum(array("open","download"), "download")',
42
        "CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers', 'Anyone')",
43
        "CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')",
44
    );
45
46
    private static $belongs_many_many = array(
0 ignored issues
show
Comprehensibility introduced by helpfulrobot
Consider using a different property name as you override a private property of the parent class.
Loading history...
47
        'Sets' => 'DMSDocumentSet'
48
    );
49
50
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by Robbie Averill
Consider using a different property name as you override a private property of the parent class.
Loading history...
51
        'CoverImage' => 'Image',
52
        'CreatedBy' => 'Member',
53
        'LastEditedBy' => 'Member',
54
    );
55
56
    private static $many_many = array(
0 ignored issues
show
Comprehensibility introduced by helpfulrobot
Consider using a different property name as you override a private property of the parent class.
Loading history...
57
        'RelatedDocuments' => 'DMSDocument',
58
        'ViewerGroups' => 'Group',
59
        'EditorGroups' => 'Group',
60
    );
61
62
    private static $display_fields = array(
63
        'ID' => 'ID',
64
        'Title' => 'Title',
65
        'FilenameWithoutID' => 'Filename',
66
        'LastEdited' => 'Last Edited'
67
    );
68
69
    private static $singular_name = 'Document';
0 ignored issues
show
Comprehensibility introduced by helpfulrobot
Consider using a different property name as you override a private property of the parent class.
Loading history...
70
71
    private static $plural_name = 'Documents';
0 ignored issues
show
Comprehensibility introduced by helpfulrobot
Consider using a different property name as you override a private property of the parent class.
Loading history...
72
73
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by helpfulrobot
Consider using a different property name as you override a private property of the parent class.
Loading history...
74
        'Filename' => 'Filename',
75
        'Title' => 'Title',
76
        'getRelatedPages.count' => 'Page Use',
77
        'ViewCount' => 'ViewCount',
78
    );
79
80
    /**
81
     * @var string download|open
82
     * @config
83
     */
84
    private static $default_download_behaviour = 'download';
85
86
    /**
87
     * A key value map of the "actions" tabs that will be added to the CMS fields
88
     *
89
     * @var array
90
     */
91
    protected $actionTasks = array(
92
        'embargo' => 'Embargo',
93
        'expiry' => 'Expiry',
94
        'replace' => 'Replace',
95
        'find-usage' => 'Usage',
96
        'find-references' => 'References',
97
        'find-relateddocuments' => 'Related Documents',
98
        'permissions' => 'Permissions'
99
    );
100
101
    public function canView($member = null)
102
    {
103 View Code Duplication
        if (!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
0 ignored issues
show
Duplication introduced by helpfulrobot
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
            $member = Member::currentUser();
105
        }
106
107
        // extended access checks
108
        $results = $this->extend('canView', $member);
109
110
        if ($results && is_array($results)) {
0 ignored issues
show
Bug Best Practice introduced by helpfulrobot
The expression $results of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
111
            if (!min($results)) {
112
                return false;
113
            }
114
        }
115
116
        if (!$this->CanViewType || $this->CanViewType == 'Anyone') {
117
            return true;
118
        }
119
        if ($member && Permission::checkMember($member, array(
120
                'ADMIN',
121
                'SITETREE_EDIT_ALL',
122
                'SITETREE_VIEW_ALL',
123
            ))
124
        ) {
125
            return true;
126
        }
127
128
        if ($this->isHidden()) {
129
            return false;
130
        }
131
132
        if ($this->CanViewType == 'LoggedInUsers') {
133
            return $member && $member->exists();
134
        }
135
136
        if ($this->CanViewType == 'OnlyTheseUsers' && $this->ViewerGroups()->count()) {
0 ignored issues
show
Documentation Bug introduced by Franco Springveldt
The method ViewerGroups does not exist on object<DMSDocument>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
137
            return ($member && $member->inGroups($this->ViewerGroups()) || $this->canEdit($member));
0 ignored issues
show
Documentation Bug introduced by Franco Springveldt
The method ViewerGroups does not exist on object<DMSDocument>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
138
        }
139
140
        return $this->canEdit($member);
141
    }
142
143
    public function canEdit($member = null)
144
    {
145 View Code Duplication
        if (!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
0 ignored issues
show
Duplication introduced by helpfulrobot
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
            $member = Member::currentUser();
147
        }
148
149
        $results = $this->extend('canEdit', $member);
150
151
        if ($results && is_array($results)) {
0 ignored issues
show
Bug Best Practice introduced by helpfulrobot
The expression $results of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
152
            if (!min($results)) {
153
                return false;
154
            }
155
        }
156
157
        // Do early admin check
158
        if ($member && Permission::checkMember($member, array('ADMIN','SITETREE_EDIT_ALL'))) {
159
            return true;
160
        }
161
162
        if ($this->CanEditType === 'LoggedInUsers') {
163
            return $member && $member->exists();
164
        }
165
166
        if ($this->CanEditType === 'OnlyTheseUsers' && $this->EditorGroups()->count()) {
0 ignored issues
show
Documentation Bug introduced by Franco Springveldt
The method EditorGroups does not exist on object<DMSDocument>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
167
            return $member && $member->inGroups($this->EditorGroups());
0 ignored issues
show
Documentation Bug introduced by Franco Springveldt
The method EditorGroups does not exist on object<DMSDocument>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
168
        }
169
170
        return false;
171
    }
172
173
    /**
174
     * @param Member $member
0 ignored issues
show
Documentation introduced by helpfulrobot
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
175
     *
176
     * @return boolean
177
     */
178
    public function canCreate($member = null)
179
    {
180 View Code Duplication
        if (!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
0 ignored issues
show
Duplication introduced by helpfulrobot
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
            $member = Member::currentUser();
182
        }
183
184
        $results = $this->extend('canCreate', $member);
185
186
        if ($results && is_array($results)) {
0 ignored issues
show
Bug Best Practice introduced by helpfulrobot
The expression $results of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
187
            if (!min($results)) {
188
                return false;
189
            }
190
        }
191
192
        // Do early admin check
193
        if ($member &&
194
            Permission::checkMember($member, array('CMS_ACCESS_DMSDocumentAdmin'))
195
        ) {
196
            return true;
197
        }
198
199
        return $this->canEdit($member);
200
    }
201
202
    /**
203
     * @param Member $member
0 ignored issues
show
Documentation introduced by helpfulrobot
Should the type for parameter $member not be Member|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
204
     *
205
     * @return boolean
206
     */
207
    public function canDelete($member = null)
208
    {
209 View Code Duplication
        if (!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
0 ignored issues
show
Duplication introduced by helpfulrobot
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
            $member = Member::currentUser();
211
        }
212
213
        $results = $this->extend('canDelete', $member);
214
215
        if ($results && is_array($results)) {
0 ignored issues
show
Bug Best Practice introduced by helpfulrobot
The expression $results of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
216
            if (!min($results)) {
217
                return false;
218
            }
219
        }
220
221
        return $this->canEdit($member);
222
    }
223
224
    /**
225
     * Increase ViewCount by 1, without update any other record fields such as
226
     * LastEdited.
227
     *
228
     * @return DMSDocument
229
     */
230
    public function trackView()
231
    {
232
        if ($this->ID > 0) {
233
            $count = $this->ViewCount + 1;
234
235
            $this->ViewCount = $count;
236
237
            DB::query("UPDATE \"DMSDocument\" SET \"ViewCount\"='$count' WHERE \"ID\"={$this->ID}");
238
239
            $this->extend('trackView');
240
        }
241
242
        return $this;
243
    }
244
245
    /**
246
     * Returns a link to download this document from the DMS store.
247
     * Alternatively a basic javascript alert will be shown should the user not have view permissions. An extension
248
     * point for this was also added.
249
     *
250
     * To extend use the following from within an Extension subclass:
251
     *
252
     * <code>
253
     * public function updateGetLink($result)
254
     * {
255
     *     // Do something here
256
     * }
257
     * </code>
258
     *
259
     * @return string
260
     */
261
    public function getLink()
262
    {
263
        $urlSegment = sprintf('%d-%s', $this->ID, URLSegmentFilter::create()->filter($this->getTitle()));
264
        $result = Controller::join_links(Director::baseURL(), 'dmsdocument/' . $urlSegment);
265
        if (!$this->canView()) {
266
            $result = sprintf("javascript:alert('%s')", $this->getPermissionDeniedReason());
267
        }
268
269
        $this->extend('updateGetLink', $result);
270
271
        return $result;
272
    }
273
274
    /**
275
     * @return string
276
     */
277
    public function Link()
278
    {
279
        return $this->getLink();
280
    }
281
282
    /**
283
     * Hides the document, so it does not show up when getByPage($myPage) is
284
     * called (without specifying the $showEmbargoed = true parameter).
285
     *
286
     * This is similar to expire, except that this method should be used to hide
287
     * documents that have not yet gone live.
288
     *
289
     * @param bool $write Save change to the database
290
     *
291
     * @return DMSDocument
292
     */
293
    public function embargoIndefinitely($write = true)
294
    {
295
        $this->EmbargoedIndefinitely = true;
296
297
        if ($write) {
298
            $this->write();
299
        }
300
301
        return $this;
302
    }
303
304
    /**
305
     * Hides the document until any page it is linked to is published
306
     *
307
     * @param bool $write Save change to database
308
     *
309
     * @return DMSDocument
310
     */
311
    public function embargoUntilPublished($write = true)
312
    {
313
        $this->EmbargoedUntilPublished = true;
314
315
        if ($write) {
316
            $this->write();
317
        }
318
319
        return $this;
320
    }
321
322
    /**
323
     * Returns if this is Document is embargoed or expired.
324
     *
325
     * Also, returns if the document should be displayed on the front-end,
326
     * respecting the current reading mode of the site and the embargo status.
327
     *
328
     * I.e. if a document is embargoed until published, then it should still
329
     * show up in draft mode.
330
     *
331
     * @return bool
332
     */
333
    public function isHidden()
334
    {
335
        $hidden = $this->isEmbargoed() || $this->isExpired();
336
        $readingMode = Versioned::get_reading_mode();
337
338
        if ($readingMode == "Stage.Stage") {
339
            if ($this->EmbargoedUntilPublished == true) {
0 ignored issues
show
Coding Style Best Practice introduced by helpfulrobot
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
340
                $hidden = false;
341
            }
342
        }
343
344
        return $hidden;
345
    }
346
347
    /**
348
     * Returns if this is Document is embargoed.
349
     *
350
     * @return bool
351
     */
352
    public function isEmbargoed()
353
    {
354
        if (is_object($this->EmbargoedUntilDate)) {
355
            $this->EmbargoedUntilDate = $this->EmbargoedUntilDate->Value;
0 ignored issues
show
Bug introduced by helpfulrobot
The property Value does not seem to exist in DateTime.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
356
        }
357
358
        $embargoed = false;
359
360
        if ($this->EmbargoedIndefinitely) {
361
            $embargoed = true;
362
        } elseif ($this->EmbargoedUntilPublished) {
363
            $embargoed = true;
364
        } elseif (!empty($this->EmbargoedUntilDate)) {
365
            if (SS_Datetime::now()->Value < $this->EmbargoedUntilDate) {
366
                $embargoed = true;
367
            }
368
        }
369
370
        return $embargoed;
371
    }
372
373
    /**
374
     * Hides the document, so it does not show up when getByPage($myPage) is
375
     * called. Automatically un-hides the Document at a specific date.
376
     *
377
     * @param string $datetime date time value when this Document should expire.
378
     * @param bool $write
379
     *
380
     * @return DMSDocument
381
     */
382 View Code Duplication
    public function embargoUntilDate($datetime, $write = true)
0 ignored issues
show
Duplication introduced by helpfulrobot
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
383
    {
384
        $this->EmbargoedUntilDate = DBField::create_field('SS_Datetime', $datetime)->Format('Y-m-d H:i:s');
385
386
        if ($write) {
387
            $this->write();
388
        }
389
390
        return $this;
391
    }
392
393
    /**
394
     * Clears any previously set embargos, so the Document always shows up in
395
     * all queries.
396
     *
397
     * @param bool $write
398
     *
399
     * @return DMSDocument
400
     */
401
    public function clearEmbargo($write = true)
402
    {
403
        $this->EmbargoedIndefinitely = false;
404
        $this->EmbargoedUntilPublished = false;
405
        $this->EmbargoedUntilDate = null;
406
407
        if ($write) {
408
            $this->write();
409
        }
410
411
        return $this;
412
    }
413
414
    /**
415
     * Returns if this is Document is expired.
416
     *
417
     * @return bool
418
     */
419
    public function isExpired()
420
    {
421
        if (is_object($this->ExpireAtDate)) {
422
            $this->ExpireAtDate = $this->ExpireAtDate->Value;
0 ignored issues
show
Bug introduced by helpfulrobot
The property Value does not seem to exist in DateTime.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
423
        }
424
425
        $expired = false;
426
427
        if (!empty($this->ExpireAtDate)) {
428
            if (SS_Datetime::now()->Value >= $this->ExpireAtDate) {
429
                $expired = true;
430
            }
431
        }
432
433
        return $expired;
434
    }
435
436
    /**
437
     * Hides the document at a specific date, so it does not show up when
438
     * getByPage($myPage) is called.
439
     *
440
     * @param string $datetime date time value when this Document should expire
441
     * @param bool $write
442
     *
443
     * @return DMSDocument
444
     */
445 View Code Duplication
    public function expireAtDate($datetime, $write = true)
0 ignored issues
show
Duplication introduced by helpfulrobot
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
446
    {
447
        $this->ExpireAtDate = DBField::create_field('SS_Datetime', $datetime)->Format('Y-m-d H:i:s');
448
449
        if ($write) {
450
            $this->write();
451
        }
452
453
        return $this;
454
    }
455
456
    /**
457
     * Clears any previously set expiry.
458
     *
459
     * @param bool $write
460
     *
461
     * @return DMSDocument
462
     */
463
    public function clearExpiry($write = true)
464
    {
465
        $this->ExpireAtDate = null;
466
467
        if ($write) {
468
            $this->write();
469
        }
470
471
        return $this;
472
    }
473
474
    /**
475
     * Returns a DataList of all previous Versions of this document (check the
476
     * LastEdited date of each object to find the correct one).
477
     *
478
     * If {@link DMSDocument_versions::$enable_versions} is disabled then an
479
     * Exception is thrown
480
     *
481
     * @throws Exception
482
     *
483
     * @return DataList List of Document objects
484
     */
485
    public function getVersions()
486
    {
487
        if (!DMSDocument_versions::$enable_versions) {
488
            throw new Exception("DMSDocument versions are disabled");
489
        }
490
491
        return DMSDocument_versions::get_versions($this);
492
    }
493
494
    /**
495
     * Returns the full filename of the document stored in this object.
496
     *
497
     * @return string
0 ignored issues
show
Documentation introduced by helpfulrobot
Should the return type not be string|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...
498
     */
499
    public function getFullPath()
500
    {
501
        if ($this->Filename) {
502
            return DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR
503
                . $this->Folder . DIRECTORY_SEPARATOR . $this->Filename;
504
        }
505
506
        return null;
507
    }
508
509
    /**
510
     * Returns the filename of this asset.
511
     *
512
     * @return string
513
     */
514
    public function getFilename()
515
    {
516
        if ($this->getField('Filename')) {
517
            return $this->getField('Filename');
518
        }
519
        return ASSETS_DIR . '/';
520
    }
521
522
    /**
523
     * @return string
524
     */
525
    public function getName()
526
    {
527
        return $this->getField('Title');
528
    }
529
530
531
    /**
532
     * Returns the filename of a document without the prefix, e.g. 0~filename.jpg -> filename.jpg
533
     *
534
     * @return string
535
     */
536
    public function getFilenameWithoutID()
537
    {
538
        $filenameParts = explode('~', $this->Filename);
539
        $filename = array_pop($filenameParts);
540
541
        return $filename;
542
    }
543
544
    /**
545
     * @return string
546
     */
547
    public function getStorageFolder()
548
    {
549
        return DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . DMS::inst()->getStorageFolder($this->ID);
550
    }
551
552
    /**
553
     * Deletes the DMSDocument and its underlying file. Also calls the parent DataObject's delete method in
554
     * order to complete an cascade.
555
     *
556
     * @return void
557
     */
558
    public function delete()
559
    {
560
        // delete the file (and previous versions of files)
561
        $filesToDelete = array();
562
        $storageFolder = $this->getStorageFolder();
563
564
        if (file_exists($storageFolder)) {
565
            if ($handle = opendir($storageFolder)) {
566
                while (false !== ($entry = readdir($handle))) {
567
                    // only delete if filename starts the the relevant ID
568
                    if (strpos($entry, $this->ID.'~') === 0) {
569
                        $filesToDelete[] = $entry;
570
                    }
571
                }
572
573
                closedir($handle);
574
575
                //delete all this files that have the id of this document
576
                foreach ($filesToDelete as $file) {
577
                    $filePath = $storageFolder .DIRECTORY_SEPARATOR . $file;
578
579
                    if (is_file($filePath)) {
580
                        unlink($filePath);
581
                    }
582
                }
583
            }
584
        }
585
586
        // get rid of any versions have saved for this DMSDocument, too
587
        if (DMSDocument_versions::$enable_versions) {
588
            $versions = $this->getVersions();
589
590
            if ($versions->Count() > 0) {
591
                foreach ($versions as $v) {
592
                    $v->delete();
593
                }
594
            }
595
        }
596
597
        return parent::delete();
598
    }
599
600
    /**
601
     * Relate an existing file on the filesystem to the document.
602
     *
603
     * Copies the file to the new destination, as defined in {@link DMS::getStoragePath()}.
604
     *
605
     * @param string $filePath Path to file, relative to webroot.
606
     *
607
     * @return DMSDocument
608
     */
609
    public function storeDocument($filePath)
610
    {
611
        if (empty($this->ID)) {
612
            user_error("Document must be written to database before it can store documents", E_USER_ERROR);
613
        }
614
615
        // calculate all the path to copy the file to
616
        $fromFilename = basename($filePath);
617
        $toFilename = $this->ID. '~' . $fromFilename; //add the docID to the start of the Filename
618
        $toFolder = DMS::inst()->getStorageFolder($this->ID);
619
        $toPath = DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename;
620
621
        DMS::inst()->createStorageFolder(DMS::inst()->getStoragePath() . DIRECTORY_SEPARATOR . $toFolder);
622
623
        //copy the file into place
624
        $fromPath = BASE_PATH . DIRECTORY_SEPARATOR . $filePath;
625
626
        //version the existing file (copy it to a new "very specific" filename
627
        if (DMSDocument_versions::$enable_versions) {
628
            DMSDocument_versions::create_version($this);
629
        } else {    //otherwise delete the old document file
630
            $oldPath = $this->getFullPath();
631
            if (file_exists($oldPath)) {
632
                unlink($oldPath);
633
            }
634
        }
635
636
        copy($fromPath, $toPath);   //this will overwrite the existing file (if present)
637
638
        //write the filename of the stored document
639
        $this->Filename = $toFilename;
640
        $this->Folder = strval($toFolder);
641
642
        $extension = pathinfo($this->Filename, PATHINFO_EXTENSION);
643
644
        if (empty($this->Title)) {
645
            // don't overwrite existing document titles
646
            $this->Title = basename($filePath, '.'.$extension);
647
        }
648
649
        $this->write();
650
651
        return $this;
652
    }
653
654
    /**
655
     * Takes a File object or a String (path to a file) and copies it into the
656
     * DMS, replacing the original document file but keeping the rest of the
657
     * document unchanged.
658
     *
659
     * @param File|string $file path to a file to store
660
     *
661
     * @return DMSDocument object that we replaced the file in
662
     */
663
    public function replaceDocument($file)
664
    {
665
        $filePath = DMS::inst()->transformFileToFilePath($file);
666
        $doc = $this->storeDocument($filePath); // replace the document
667
668
        return $doc;
669
    }
670
671
672
    /**
673
     * Return the type of file for the given extension
674
     * on the current file name.
675
     *
676
     * @param string $ext
677
     *
678
     * @return string
679
     */
680
    public static function get_file_type($ext)
681
    {
682
        $types = array(
683
            'gif' => 'GIF image - good for diagrams',
684
            'jpg' => 'JPEG image - good for photos',
685
            'jpeg' => 'JPEG image - good for photos',
686
            'png' => 'PNG image - good general-purpose format',
687
            'ico' => 'Icon image',
688
            'tiff' => 'Tagged image format',
689
            'doc' => 'Word document',
690
            'xls' => 'Excel spreadsheet',
691
            'zip' => 'ZIP compressed file',
692
            'gz' => 'GZIP compressed file',
693
            'dmg' => 'Apple disk image',
694
            'pdf' => 'Adobe Acrobat PDF file',
695
            'mp3' => 'MP3 audio file',
696
            'wav' => 'WAV audo file',
697
            'avi' => 'AVI video file',
698
            'mpg' => 'MPEG video file',
699
            'mpeg' => 'MPEG video file',
700
            'js' => 'Javascript file',
701
            'css' => 'CSS file',
702
            'html' => 'HTML file',
703
            'htm' => 'HTML file'
704
        );
705
706
        return isset($types[$ext]) ? $types[$ext] : $ext;
707
    }
708
709
710
    /**
711
     * Returns the Description field with HTML <br> tags added when there is a
712
     * line break.
713
     *
714
     * @return string
715
     */
716
    public function getDescriptionWithLineBreak()
717
    {
718
        return nl2br($this->getField('Description'));
719
    }
720
721
    /**
722
     * @return FieldList
723
     */
724
    public function getCMSFields()
725
    {
726
        //include JS to handling showing and hiding of bottom "action" tabs
727
        Requirements::javascript(DMS_DIR . '/javascript/DMSDocumentCMSFields.js');
728
        Requirements::css(DMS_DIR . '/dist/css/cmsbundle.css');
729
730
        $fields = new FieldList();  //don't use the automatic scaffolding, it is slow and unnecessary here
731
732
        $extraTasks = '';   //additional text to inject into the list of tasks at the bottom of a DMSDocument CMSfield
0 ignored issues
show
Unused Code introduced by helpfulrobot
$extraTasks is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
733
734
        //get list of shortcode page relations
735
        $relationFinder = new ShortCodeRelationFinder();
736
        $relationList = $relationFinder->getList($this->ID);
737
738
        $fieldsTop = $this->getFieldsForFile($relationList->count());
739
        $fields->add($fieldsTop);
740
741
        $fields->add(TextField::create('Title', _t('DMSDocument.TITLE', 'Title')));
742
        $fields->add(TextareaField::create('Description', _t('DMSDocument.DESCRIPTION', 'Description')));
743
744
        $coverImageField = UploadField::create('CoverImage', _t('DMSDocument.COVERIMAGE', 'Cover Image'));
745
        $coverImageField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
746
        $coverImageField->setConfig('allowedMaxFileNumber', 1);
747
        $fields->add($coverImageField);
748
749
750
        $downloadBehaviorSource = array(
751
            'open' => _t('DMSDocument.OPENINBROWSER', 'Open in browser'),
752
            'download' => _t('DMSDocument.FORCEDOWNLOAD', 'Force download'),
753
        );
754
        $defaultDownloadBehaviour = Config::inst()->get('DMSDocument', 'default_download_behaviour');
755
        if (!isset($downloadBehaviorSource[$defaultDownloadBehaviour])) {
756
            user_error('Default download behaviour "' . $defaultDownloadBehaviour . '" not supported.', E_USER_WARNING);
757
        } else {
758
            $downloadBehaviorSource[$defaultDownloadBehaviour] .= ' (' . _t('DMSDocument.DEFAULT', 'default') . ')';
759
        }
760
761
        $fields->add(
762
            OptionsetField::create(
763
                'DownloadBehavior',
764
                _t('DMSDocument.DOWNLOADBEHAVIOUR', 'Download behavior'),
765
                $downloadBehaviorSource,
766
                $defaultDownloadBehaviour
767
            )
768
            ->setDescription(
769
                'How the visitor will view this file. <strong>Open in browser</strong> '
770
                . 'allows files to be opened in a new tab.'
771
            )
772
        );
773
774
        //create upload field to replace document
775
        $uploadField = new DMSUploadField('ReplaceFile', 'Replace file');
776
        $uploadField->setConfig('allowedMaxFileNumber', 1);
777
        $uploadField->setConfig('downloadTemplateName', 'ss-dmsuploadfield-downloadtemplate');
778
        $uploadField->setRecord($this);
779
780
        $gridFieldConfig = GridFieldConfig::create()->addComponents(
781
            new GridFieldToolbarHeader(),
782
            new GridFieldSortableHeader(),
783
            new GridFieldDataColumns(),
784
            new GridFieldPaginator(30),
785
            //new GridFieldEditButton(),
786
            new GridFieldDetailForm()
787
        );
788
789
        $gridFieldConfig->getComponentByType('GridFieldDataColumns')
0 ignored issues
show
Bug introduced by helpfulrobot
It seems like you code against a concrete implementation and not the interface GridFieldComponent as the method setDisplayFields() does only exist in the following implementations of said interface: GridFieldDataColumns, GridFieldEditableColumns, GridFieldExternalLink.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
790
            ->setDisplayFields(array(
791
                'Title' => 'Title',
792
                'ClassName' => 'Page Type',
793
                'ID' => 'Page ID'
794
            ))
795
            ->setFieldFormatting(array(
796
                'Title' => sprintf(
797
                    '<a class=\"cms-panel-link\" href=\"%s/$ID\">$Title</a>',
798
                    singleton('CMSPageEditController')->Link('show')
799
                )
800
            ));
801
802
        $pagesGrid = GridField::create(
803
            'Pages',
804
            _t('DMSDocument.RelatedPages', 'Related Pages'),
805
            $this->getRelatedPages(),
806
            $gridFieldConfig
807
        );
808
809
        $referencesGrid = GridField::create(
810
            'References',
811
            _t('DMSDocument.RelatedReferences', 'Related References'),
812
            $relationList,
813
            $gridFieldConfig
814
        );
815
816
        if (DMSDocument_versions::$enable_versions) {
817
            $versionsGridFieldConfig = GridFieldConfig::create()->addComponents(
818
                new GridFieldToolbarHeader(),
819
                new GridFieldSortableHeader(),
820
                new GridFieldDataColumns(),
821
                new GridFieldPaginator(30)
822
            );
823
            $versionsGridFieldConfig->getComponentByType('GridFieldDataColumns')
0 ignored issues
show
Bug introduced by Robbie Averill
It seems like you code against a concrete implementation and not the interface GridFieldComponent as the method setDisplayFields() does only exist in the following implementations of said interface: GridFieldDataColumns, GridFieldEditableColumns, GridFieldExternalLink.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
824
                ->setDisplayFields(Config::inst()->get('DMSDocument_versions', 'display_fields'))
825
                ->setFieldFormatting(
826
                    array(
827
                        'FilenameWithoutID' => '<a target="_blank" class="file-url" href="$Link">'
828
                            . '$FilenameWithoutID</a>'
829
                    )
830
                );
831
832
            $versionsGrid =  GridField::create(
833
                'Versions',
834
                _t('DMSDocument.Versions', 'Versions'),
835
                $this->getVersions(),
836
                $versionsGridFieldConfig
837
            );
838
            $this->addActionPanelTask('find-versions', 'Versions');
839
        }
840
841
        $embargoValue = 'None';
842
        if ($this->EmbargoedIndefinitely) {
843
            $embargoValue = 'Indefinitely';
844
        } elseif ($this->EmbargoedUntilPublished) {
845
            $embargoValue = 'Published';
846
        } elseif (!empty($this->EmbargoedUntilDate)) {
847
            $embargoValue = 'Date';
848
        }
849
        $embargo = new OptionsetField(
850
            'Embargo',
851
            _t('DMSDocument.EMBARGO', 'Embargo'),
852
            array(
853
                'None' => _t('DMSDocument.EMBARGO_NONE', 'None'),
854
                'Published' => _t('DMSDocument.EMBARGO_PUBLISHED', 'Hide document until page is published'),
855