| 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 |  |  |         // Hides the DocumentSets tab if the user has no permisions | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         if (!Permission::checkMember( | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |             Member::currentUser(), | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             array('ADMIN', 'CMS_ACCESS_DMSDocumentAdmin') | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         ) | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $gridField = GridField::create( | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             'DocumentSets', | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             false, | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             $this->owner->DocumentSets(), //->Sort('DocumentSort'), | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             $config = new GridFieldConfig_RelationEditor | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $gridField->addExtraClass('documentsets'); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         // Only show document sets in the autocompleter that have not been assigned to a page already | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $config->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchList( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             DMSDocumentSet::get()->filter(array('PageID' => 0)) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $fields->addFieldToTab( | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             'Root.DocumentSets', | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             $gridField | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $fields | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             ->findOrMakeTab('Root.DocumentSets') | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             ->setTitle(_t( | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 __CLASS__ . '.DocumentSetsTabTitle', | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 'Document Sets ({count})', | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 array('count' => $this->owner->DocumentSets()->count()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             )); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Get a list of document sets for the owner page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return ArrayList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function getDocumentSets() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $this->owner->DocumentSets(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Get a list of all documents from all document sets for the owner page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return ArrayList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function getAllDocuments() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $documents = ArrayList::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         foreach ($this->getDocumentSets() as $documentSet) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             /** @var DocumentSet $documentSet */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $documents->merge($documentSet->getDocuments()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $documents->removeDuplicates(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $documents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function onBeforeDelete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if (Versioned::current_stage() == 'Live') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $existsOnOtherStage = !$this->owner->getIsDeletedFromStage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $existsOnOtherStage = $this->owner->getExistsOnLive(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         // Only remove if record doesn't still exist on live stage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         if (!$existsOnOtherStage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $dmsDocuments = $this->owner->getAllDocuments(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             foreach ($dmsDocuments as $document) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 // If the document is only associated with one page, i.e. only associated with this page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 if ($document->getRelatedPages()->count() <= 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                     // Delete the document before deleting this page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                     $document->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function onBeforePublish() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $embargoedDocuments = $this->owner->getAllDocuments()->filter('EmbargoedUntilPublished', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if ($embargoedDocuments->count() > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             foreach ($embargoedDocuments as $doc) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 $doc->EmbargoedUntilPublished = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 $doc->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * Returns the title of the page with the total number of documents it has associated with it across | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * all document sets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function getTitleWithNumberOfDocuments() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return $this->owner->Title . ' (' . $this->owner->getAllDocuments()->count() . ')'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  | 
            
                        
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: