| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | class AccordionBlock extends Block | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     private static $singular_name = 'Accordion Block'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private static $plural_name = 'Accordion Blocks'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private static $db = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         'Content' => 'HTMLText', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         'SortOrder' => 'Int', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private static $has_many = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         'Panels' => 'AccordionPanel', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return FieldList | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 33 | 1 | View Code Duplication |     public function getCMSFields() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 1 |  |         $fields = parent::getCMSFields(); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 1 |  |         $fields->removeByName(array( | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  |             'SortOrder', | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 1 |  |             'Panels', | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 1 |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 1 |  |         $config = GridFieldConfig_RecordEditor::create(); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |         if (class_exists('GridFieldOrderableRows')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |             $config->addComponent(new GridFieldOrderableRows()); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 1 |  |         $config->removeComponentsByType('GridFieldAddExistingAutocompleter'); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 1 |  |         $config->removeComponentsByType('GridFieldDeleteAction'); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |         $config->addComponent(new GridFieldDeleteAction(false)); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |         if ($this->ID) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |             $fields->addFieldsToTab('Root.Panels', array( | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 1 |  |                 GridField::create('Panels', 'Accordion Panels', $this->Panels()->sort('Sort'), $config), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 1 |  |             )); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 1 |  |         return $fields; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return DataList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |     public function getPanelList() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |         if ($this->Panels()->exists()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |             return $this->Panels()->sort('Sort'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return $this->Panels(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | class AccordionBlock_Controller extends Block_Controller | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     private static $accordion_class = 'accordion-block'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |     public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $class = $this->AccordionClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/jquery-ui.js'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         Requirements::customScript(' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $(function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 $( ".' . $class . '" ).accordion({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     header: ".accord-header", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     collapsible: true, heightStyle: "content" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     public function AccordionClass() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return Config::inst()->get('AccordionBlock_Controller', 'accordion_class'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  | } | 
            
                        
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.