forked from dwenzel/t3events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.ext_update.php
166 lines (152 loc) · 4.63 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
<?php
/**
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Update class for the extension manager.
*/
class ext_update
{
/**
* @var \DWenzel\T3events\Update\MigrateTaskRecords
*/
protected $taskUpdater;
/**
* @var \DWenzel\T3events\Update\MigratePluginRecords
*/
protected $pluginUpdater;
/**
* Array of flash messages (params) array[][status,title,message]
*
* @var array
*/
protected $messageArray = [];
/**
* @var \TYPO3\CMS\Install\Updates\InitialDatabaseSchemaUpdate
*/
protected $dataBaseSchemaUpdate;
public function __construct()
{
$this->taskUpdater = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\DWenzel\T3events\Update\MigrateTaskRecords::class);
$this->pluginUpdater = GeneralUtility::makeInstance(\DWenzel\T3events\Update\MigratePluginRecords::class);
$this->dataBaseSchemaUpdate = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Updates\InitialDatabaseSchemaUpdate::class);
}
/**
* Main update function called by the extension manager.
*
* @return string
*/
public function main()
{
$this->processUpdates();
return $this->generateOutput();
}
/**
* Called by the extension manager to determine if the update menu entry
* should by showed.
*
* @return bool
*/
public function access()
{
$description = '';
$showMenu = ($this->taskUpdater->checkForUpdate($description) || $this->pluginUpdater->checkForUpdate($description));
return $showMenu;
}
/**
* The actual update function.
*
* @return void
*/
protected function processUpdates()
{
$messages = [];
$dbQueries = [];
if ($this->canPerformUpdate($messages)) {
$this->taskUpdater->performUpdate($dbQueries, $messages);
$this->pluginUpdater->performUpdate($dbQueries, $messages);
}
$this->messageArray = $messages;
}
/**
* Tells if the update can be performed.
*
* @param array $messages
* @return bool Returns false if database fields are missing (in any table!) otherwise true
*/
protected function canPerformUpdate(array &$messages)
{
$schemaDescription = '';
$schemaNeedsUpdate = $this->dataBaseSchemaUpdate->checkForUpdate($schemaDescription);
if ($schemaNeedsUpdate) {
$message = 'Database schema must be up-to-date before running this script. Please update your database using the Install Tool.';
$title = 'Update Database';
$severity = FlashMessage::ERROR;
$messages[] = [
$severity,
$title,
$message
];
}
return !$schemaNeedsUpdate;
}
/**
* Generates output by using flash messages
*
* @return string
*/
protected function generateOutput()
{
$output = '';
foreach ($this->messageArray as $messageItem) {
/** @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
$flashMessage = GeneralUtility::makeInstance(
FlashMessage::class,
$messageItem[2],
$messageItem[1],
$messageItem[0]);
$output .= $flashMessage->render();
}
return $output;
}
/**
* Renders a flash message.
*
* @param FlashMessage $message
* @return string The flash message as HTML.
*/
public function renderFlashMessage($message)
{
$title = '';
if (!empty($message->getTitle())) {
$title = '<h4 class="alert-title">' . $message->getTitle() . '</h4>';
}
$message = '
<div class="alert ' . $message->getClass() . '">
<div class="media">
<div class="media-left">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-' . $message->getIconName() . ' fa-stack-1x"></i>
</span>
</div>
<div class="media-body">
' . $title . '
<div class="alert-message">' . $message->getMessage() . '</div>
</div>
</div>
</div>';
return $message;
}
}