| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * NewIncludeExcludeStructureUpdate. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace HDNET\Calendarize\Updates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use HDNET\Calendarize\Domain\Model\ConfigurationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use HDNET\Calendarize\Utility\HelperUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use TYPO3\CMS\Install\Updates\UpgradeWizardInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * NewIncludeExcludeStructureUpdate. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class NewIncludeExcludeStructureUpdate extends AbstractUpdate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * The human-readable title of the upgrade wizard. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $title = 'Migrate the calendarize configurations to the new include/exclude/override/cutout structure'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function getIdentifier(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         return 'newIncludeExcludeStructure'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * Checks whether updates are required. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param string &$description The description for the update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return bool Whether an update is required (TRUE) or not (FALSE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function checkForUpdate(&$description) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $table = 'tx_calendarize_domain_model_configuration'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $q = HelperUtility::getDatabaseConnection($table)->createQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $q->count('handling') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ->from($table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ->where( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $q->expr()->orX( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     $q->expr()->eq('handling', $q->quote('')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     $q->expr()->isNull('handling') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $count = $q->execute()->fetchColumn(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if ($count > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $description = 'We will update ' . $count . ' calendarize configurations'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * Performs the accordant updates. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param array &$dbQueries      Queries done in this update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param mixed &$customMessages Custom messages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return bool Whether everything went smoothly or not | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function executeUpdate(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $table = 'tx_calendarize_domain_model_configuration'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $q = HelperUtility::getDatabaseConnection($table)->createQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $q->update($table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ->where( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $q->expr()->eq('type', $q->quote('timeExclude')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             ->set('type', ConfigurationInterface::TYPE_TIME) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ->set('handling', ConfigurationInterface::HANDLING_INCLUDE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $dbQueries[] = $q->getSQL(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $q->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $q->resetQueryParts(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $q->update($table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ->where( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $q->expr()->eq('type', $q->quote('include')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ->set('type', ConfigurationInterface::TYPE_GROUP) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ->set('handling', ConfigurationInterface::HANDLING_INCLUDE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $dbQueries[] = $q->getSQL(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $q->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $q->resetQueryParts(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $q->update($table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ->where( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $q->expr()->eq('type', $q->quote('exclude')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ->set('type', ConfigurationInterface::TYPE_GROUP) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ->set('handling', ConfigurationInterface::HANDLING_EXCLUDE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $dbQueries[] = $q->getSQL(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $q->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $q->resetQueryParts(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $q->update($table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             ->where( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $q->expr()->orX( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                     $q->expr()->eq('handling', $q->quote('')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     $q->expr()->isNull('handling') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             ->set('handling', ConfigurationInterface::HANDLING_INCLUDE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $dbQueries[] = $q->getSQL(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $q->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $customMessages = 'All queries are done! :)'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |     public function updateNecessary(): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     public function getPrerequisites(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             DatabaseUpdatedPrerequisite::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  | 
            
                        
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.