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 implements UpgradeWizardInterface |
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
|
|
|
/** |
28
|
|
|
* Checks whether updates are required. |
29
|
|
|
* |
30
|
|
|
* @param string &$description The description for the update |
31
|
|
|
* |
32
|
|
|
* @return bool Whether an update is required (TRUE) or not (FALSE) |
33
|
|
|
*/ |
34
|
|
|
public function checkForUpdate(&$description) |
35
|
|
|
{ |
36
|
|
|
$table = 'tx_calendarize_domain_model_configuration'; |
37
|
|
|
|
38
|
|
|
$q = HelperUtility::getDatabaseConnection($table)->createQueryBuilder(); |
39
|
|
|
|
40
|
|
|
$q->count('handling') |
41
|
|
|
->from($table) |
42
|
|
|
->where( |
43
|
|
|
$q->expr()->orX( |
44
|
|
|
$q->expr()->eq('handling', $q->quote('')), |
45
|
|
|
$q->expr()->isNull('handling') |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$count = $q->execute()->fetchColumn(0); |
50
|
|
|
|
51
|
|
|
if ($count > 0) { |
52
|
|
|
$description = 'We will update ' . $count . ' calendarize configurations'; |
53
|
|
|
|
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Performs the accordant updates. |
62
|
|
|
* |
63
|
|
|
* @param array &$dbQueries Queries done in this update |
64
|
|
|
* @param mixed &$customMessages Custom messages |
65
|
|
|
* |
66
|
|
|
* @return bool Whether everything went smoothly or not |
67
|
|
|
*/ |
68
|
|
|
public function executeUpdate() : bool |
69
|
|
|
{ |
70
|
|
|
$table = 'tx_calendarize_domain_model_configuration'; |
71
|
|
|
|
72
|
|
|
$q = HelperUtility::getDatabaseConnection($table)->createQueryBuilder(); |
73
|
|
|
$q->update($table) |
74
|
|
|
->where( |
75
|
|
|
$q->expr()->eq('type', $q->quote('timeExclude')) |
76
|
|
|
) |
77
|
|
|
->set('type', ConfigurationInterface::TYPE_TIME) |
78
|
|
|
->set('handling', ConfigurationInterface::HANDLING_INCLUDE); |
79
|
|
|
|
80
|
|
|
$dbQueries[] = $q->getSQL(); |
|
|
|
|
81
|
|
|
$q->execute(); |
82
|
|
|
|
83
|
|
|
$q->resetQueryParts(); |
84
|
|
|
|
85
|
|
|
$q->update($table) |
86
|
|
|
->where( |
87
|
|
|
$q->expr()->eq('type', $q->quote('include')) |
88
|
|
|
) |
89
|
|
|
->set('type', ConfigurationInterface::TYPE_GROUP) |
90
|
|
|
->set('handling', ConfigurationInterface::HANDLING_INCLUDE); |
91
|
|
|
|
92
|
|
|
$dbQueries[] = $q->getSQL(); |
93
|
|
|
$q->execute(); |
94
|
|
|
|
95
|
|
|
$q->resetQueryParts(); |
96
|
|
|
|
97
|
|
|
$q->update($table) |
98
|
|
|
->where( |
99
|
|
|
$q->expr()->eq('type', $q->quote('exclude')) |
100
|
|
|
) |
101
|
|
|
->set('type', ConfigurationInterface::TYPE_GROUP) |
102
|
|
|
->set('handling', ConfigurationInterface::HANDLING_EXCLUDE); |
103
|
|
|
|
104
|
|
|
$dbQueries[] = $q->getSQL(); |
105
|
|
|
$q->execute(); |
106
|
|
|
|
107
|
|
|
$q->resetQueryParts(); |
108
|
|
|
|
109
|
|
|
$q->update($table) |
110
|
|
|
->where( |
111
|
|
|
$q->expr()->orX( |
112
|
|
|
$q->expr()->eq('handling', $q->quote('')), |
113
|
|
|
$q->expr()->isNull('handling') |
114
|
|
|
) |
115
|
|
|
) |
116
|
|
|
->set('handling', ConfigurationInterface::HANDLING_INCLUDE); |
117
|
|
|
|
118
|
|
|
$dbQueries[] = $q->getSQL(); |
119
|
|
|
$q->execute(); |
120
|
|
|
|
121
|
|
|
$customMessages = 'All queries are done! :)'; |
|
|
|
|
122
|
|
|
|
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getIdentifier(): string |
127
|
|
|
{ |
128
|
|
|
return "calendarizeNewIncludeExcludeStructureUpdate"; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getTitle(): string |
132
|
|
|
{ |
133
|
|
|
return ""; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getDescription(): string |
137
|
|
|
{ |
138
|
|
|
return ""; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function updateNecessary(): bool |
142
|
|
|
{ |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getPrerequisites(): array |
147
|
|
|
{ |
148
|
|
|
return [ |
149
|
|
|
DatabaseUpdatedPrerequisite::class |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.