This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
forked from fnagel/t3extblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.ext_update.php
207 lines (178 loc) · 5.26 KB
/
class.ext_update.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class ext_update.
*
* Performs update tasks for extension t3extblog
*/
class ext_update
{
/**
* The TYPO3_DB database connection.
*
* @var \TYPO3\CMS\Dbal\Database\DatabaseConnection
*/
protected $database;
/**
* Array of flash messages (params) array[][status,title,message].
*
* @var array
*/
protected $messageArray = [];
/**
* Array of sections (HTML).
*
* @var array
*/
protected $sectionArray = [];
/**
* Called by the extension manager to determine
* if the update menu entry should by showed.
*
* @return bool
*/
public function access()
{
return $GLOBALS['BE_USER']->isAdmin();
}
/**
* Contructor.
*/
public function __construct()
{
$this->database = $GLOBALS['TYPO3_DB'];
}
/**
* Executes the update script.
*
* @return string
*/
public function main()
{
$output = '';
$message = 'These wizards will alter the database. Be careful in production environments!';
$this->messageArray[] = [FlashMessage::WARNING, 'Database update wizards', $message];
$this->renderPostSection();
$this->renderCommentSection();
$output .= $this->generateMessages();
$output .= implode('<br>', $this->sectionArray);
return $output;
}
/**
* Update post records
*/
protected function renderPostSection()
{
$this->updatePostMailsSent();
$this->updatePostCreateUser();
}
/**
*/
protected function updatePostMailsSent()
{
if (!$this->isFieldAvailable('tx_t3blog_post', 'mails_sent')) {
return;
}
$key = 'post_mails_sent';
if (GeneralUtility::_POST('migration') === $key) {
$this->database->exec_UPDATEquery('tx_t3blog_post', 'mails_sent IS NULL', array('mails_sent' => 1));
$this->messageArray[] = [
FlashMessage::INFO,
'Posts updated',
$this->database->sql_affected_rows().' posts have been updated'
];
}
$this->sectionArray[] = $this->renderForm(
$key, 'Set "mails_sent" flag for existing posts (use when updating to v2.1.0)'
);
}
/**
*/
protected function updatePostCreateUser()
{
if (!$this->isFieldAvailable('tx_t3blog_post', 'cruser_id')) {
return;
}
$key = 'post_cruser_id';
if (GeneralUtility::_POST('migration') === $key) {
// Copying field values seems only possible with a raw query
$this->database->sql_query('UPDATE tx_t3blog_post SET cruser_id = author WHERE cruser_id = 0');
$this->messageArray[] = [
FlashMessage::INFO,
'Posts updated',
$this->database->sql_affected_rows().' posts have been updated'
];
}
$this->sectionArray[] = $this->renderForm(
$key, 'Add current post author to "cruser_id" field (use when updating to v3.0.0)'
);
}
/**
* Update comments records
*/
protected function renderCommentSection()
{
if (!$this->isFieldAvailable('tx_t3blog_com', 'mails_sent')) {
return;
}
$key = 'comment';
if (GeneralUtility::_POST('migration') === $key) {
$this->updateCommentRecords();
}
$this->sectionArray[] = $this->renderForm(
$key, 'Set "mails_sent" flag for existing comments (use when migrating from EXT:t3blog)'
);
}
/**
*/
protected function updateCommentRecords()
{
$this->database->exec_UPDATEquery('tx_t3blog_com', 'mails_sent IS NULL', array('mails_sent' => 1));
$message = $this->database->sql_affected_rows().' comments have been updated';
$this->messageArray[] = [FlashMessage::INFO, 'Comments updated', $message];
}
/**
* @return string
*/
protected function renderForm($key, $message)
{
return
'<form action="'.GeneralUtility::getIndpEnv('REQUEST_URI').'" method="POST">
<input type="hidden" name="migration" value="'.$key.'" />
<button class="btn">'.$message.'</button>
</form>';
}
/**
* Check if a tale field is available.
*
* @param string $table
* @param string $field
*
* @return bool
*/
protected function isFieldAvailable($table, $field)
{
return array_key_exists($field, $this->database->admin_get_fields($table));
}
/**
* Generates output by using flash messages.
*
* @return string
*/
protected function generateMessages()
{
$output = '';
foreach ($this->messageArray as $messageItem) {
/** @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
$flashMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
$messageItem[2],
$messageItem[1],
$messageItem[0]
);
$output .= $flashMessage->render();
}
return $output;
}
}