| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dynamic\DynamicBlocks\Block; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dynamic\DynamicBlocks\Model\PhotoGalleryBlockImage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SheaDawson\Blocks\Controllers\BlockController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SheaDawson\Blocks\Model\Block; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 | 12 |  | use SilverStripe\Forms\GridField\GridField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 | 12 |  | use SilverStripe\Forms\GridField\GridFieldDeleteAction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\View\Requirements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symbiote\GridFieldExtensions\GridFieldOrderableRows; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 14 |  | View Code Duplication | class PhotoGalleryBlock extends Block | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 1 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function singular_name() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         return _t('PhotoGalleryBlock.SINGULARNAME', 'Photo Gallery Block'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function plural_name() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         return _t('PhotoGalleryBlock.PLURALNAME', 'Photo Gallery Blocks'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 1 |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 1 |  |     private static $has_many = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 1 |  |         'Images' => PhotoGalleryBlockImage::class | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 1 |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 1 |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 1 |  |      * @return \SilverStripe\Forms\FieldList | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 1 |  |     public function getCMSFields() | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |         $fields = parent::getCMSFields(); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $fields->removeByName([ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 1 |  |             'Images', | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         if ($this->owner->ID) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |             $config = GridFieldConfig_RecordEditor::create(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             $config->addComponent(new GridFieldOrderableRows('SortOrder')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $config->removeComponentsByType('GridFieldAddExistingAutocompleter'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $config->removeComponentsByType('GridFieldDeleteAction'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $config->addComponent(new GridFieldDeleteAction(false)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $imagesField = GridField::create('Images', 'Images', $this->Images()->sort('SortOrder'), $config); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $fields->addFieldToTab('Root.Photos', $imagesField); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  | class PhotoGalleryBlock_Controller extends BlockController | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         Requirements::css('dynamic-blocks/thirdparty/lightbox/lightbox.css'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         Requirements::javascript('dynamic-blocks/thirdparty/lightbox/lightbox.min.js'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  | } | 
            
                        
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.