| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SheaDawson\Blocks\tasks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SheaDawson\Blocks\model\Block; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SheaDawson\Blocks\model\BlockSet; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\ORM\DB; | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\SiteConfig\SiteConfig; | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |  * BlockUpgradeTask | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  * Run this task to migrate from Blocks 0.x to 1.x. | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  * @author Shea Dawson <[email protected]> | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | class BlockUpgradeTask extends BuildTask | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     public function run($request) | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         // update block/set titles | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         // Name field has been reverted back to Title | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         // DB::query("update Block set Name = Title"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         // DB::query("update BlockSet set Name = Title"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         // update block areas | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         DB::query(' | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 			update SiteTree_Blocks | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 			left join Block on SiteTree_Blocks.BlockID = Block.ID | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 			set BlockArea = Block.Area | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 			where BlockID = Block.ID | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 		'); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         // update block sort | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         DB::query(' | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 			update SiteTree_Blocks | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 			left join Block on SiteTree_Blocks.BlockID = Block.ID | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 			set Sort = Block.Weight | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 			where BlockID = Block.ID | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		'); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         echo 'BlockAreas, Sort updated<br />'; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         // migrate global blocks | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $sc = SiteConfig::current_site_config(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         if ($sc->Blocks()->Count()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $set = BlockSet::get()->filter('Title', 'Global')->first(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             if (!$set) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                 $set = BlockSet::create(array( | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                     'Title' => 'Global', | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 )); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 $set->write(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             foreach ($sc->Blocks() as $block) { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 if (!$set->Blocks()->find('ID', $block->ID)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                     $set->Blocks()->add($block, array( | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                         'Sort' => $block->Weight, | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                         'BlockArea' => $block->Area, | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                     )); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                     echo "Block #$block->ID added to Global block set<br />"; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         // publish blocks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $blocks = Block::get()->filter('Published', 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         foreach ($blocks as $block) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $block->publish('Stage', 'Live'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             echo "Published Block #$block->ID<br />"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  | 
            
                        
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.