| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_ActionProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  |     public function augmentColumns($gridField, &$columns) | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |         if (!in_array('Actions', $columns)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |             $columns[] = 'Actions'; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function getColumnAttributes($gridField, $record, $columnName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         return array('class' => 'col-buttons'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function getColumnMetadata($gridField, $columnName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         if ($columnName == 'Actions') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             return array('title' => ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function getColumnsHandled($gridField) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         return array('Actions'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function getColumnContent($gridField, $record, $columnName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if (!$record->canEdit()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $field = ""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 52 |  | View Code Duplication |         if (!$record->IsSpam || !$record->Moderated) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $field .= GridField_FormAction::create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $gridField, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 'CustomAction' . $record->ID . 'Spam', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 'Spam', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 'spam', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 array('RecordID' => $record->ID) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             )->Field(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 62 |  | View Code Duplication |         if ($record->IsSpam || !$record->Moderated) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $field .= GridField_FormAction::create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $gridField, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 'CustomAction' . $record->ID . 'Approve', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 'Approve', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 'approve', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 array('RecordID' => $record->ID) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             )->Field(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function getActions($gridField) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return array('spam', 'approve'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function handleAction(GridField $gridField, $actionName, $arguments, $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 88 |  | View Code Duplication |         if ($actionName == 'spam') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $comment = Comment::get()->byID($arguments["RecordID"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $comment->markSpam(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             // output a success message to the user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             Controller::curr()->getResponse()->setStatusCode( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 200, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 'Comment marked as spam.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 99 |  | View Code Duplication |         if ($actionName == 'approve') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $comment = Comment::get()->byID($arguments["RecordID"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $comment->markApproved(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             // output a success message to the user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             Controller::curr()->getResponse()->setStatusCode( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 200, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 'Comment approved.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  | 
            
                        
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.