Completed
Pull Request — master (#168)
by Franco
02:41
created

code/extensions/DMSSiteTreeExtension.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
3
/**
4
 * @package dms
5
 */
6
class DMSSiteTreeExtension extends DataExtension
7
{
8
    private static $has_many = array(
9
        'DocumentSets' => 'DMSDocumentSet'
10
    );
11
12
    public function updateCMSFields(FieldList $fields)
13
    {
14
        // Ability to disable document sets for a Page
15
        if (!$this->owner->config()->get('documents_enabled')) {
16
            return;
17
        }
18
19
        $gridField = GridField::create(
20
            'Document Sets',
21
            false,
22
            $this->owner->DocumentSets(), //->Sort('DocumentSort'),
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
            $config = new GridFieldConfig_RelationEditor
24
        );
25
        $gridField->addExtraClass('documentsets');
26
27
        // Only show document sets in the autocompleter that have not been assigned to a page already
28
        $config->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchList(
29
            DMSDocumentSet::get()->filter(array('PageID' => 0))
30
        );
31
32
        $fields->addFieldToTab(
33
            'Root.Document Sets (' . $this->owner->DocumentSets()->count() . ')',
34
            $gridField
35
        );
36
    }
37
38
    /**
39
     * Get a list of document sets for the owner page
40
     *
41
     * @return ArrayList
42
     */
43
    public function getDocumentSets()
44
    {
45
        return $this->owner->DocumentSets();
46
    }
47
48
    /**
49
     * Get a list of all documents from all document sets for the owner page
50
     *
51
     * @return ArrayList
52
     */
53
    public function getAllDocuments()
54
    {
55
        $documents = ArrayList::create();
56
57
        foreach ($this->getDocumentSets() as $documentSet) {
58
            /** @var DocumentSet $documentSet */
59
            $documents->merge($documentSet->getDocuments());
60
        }
61
        $documents->removeDuplicates();
62
63
        return $documents;
64
    }
65
66
    public function onBeforeDelete()
67
    {
68
        if (Versioned::current_stage() == 'Live') {
69
            $existsOnOtherStage = !$this->owner->getIsDeletedFromStage();
70
        } else {
71
            $existsOnOtherStage = $this->owner->getExistsOnLive();
72
        }
73
74
        // Only remove if record doesn't still exist on live stage.
75
        if (!$existsOnOtherStage) {
76
            $dmsDocuments = $this->owner->getAllDocuments();
77
            foreach ($dmsDocuments as $document) {
78
                // If the document is only associated with one page, i.e. only associated with this page
79
                if ($document->getRelatedPages()->count() <= 1) {
80
                    // Delete the document before deleting this page
81
                    $document->delete();
82
                }
83
            }
84
        }
85
    }
86
87
    public function onBeforePublish()
88
    {
89
        $embargoedDocuments = $this->owner->getAllDocuments()->filter('EmbargoedUntilPublished', true);
90
        if ($embargoedDocuments->count() > 0) {
91
            foreach ($embargoedDocuments as $doc) {
92
                $doc->EmbargoedUntilPublished = false;
93
                $doc->write();
94
            }
95
        }
96
    }
97
98
    /**
99
     * Returns the title of the page with the total number of documents it has associated with it across
100
     * all document sets
101
     *
102
     * @return string
103
     */
104
    public function getTitleWithNumberOfDocuments()
105
    {
106
        return $this->owner->Title . ' (' . $this->owner->getAllDocuments()->count() . ')';
107
    }
108
}
109