forked from in2code-de/roq_newsevent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.ext_update.php
188 lines (167 loc) · 7.09 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
<?php
use TYPO3\CMS\Backend\Module\BaseScriptClass;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Copyright (c) 2012, ROQUIN B.V. (C), http://www.roquin.nl
*
* @author: Jochem de Groot <[email protected]>
* @file: class.ext_update.php
* @created: 26-9-12 16:28
* @description: Update class for updating news event from version 2.0.X to newer versions
*/
class ext_update extends BaseScriptClass
{
/**
* Main method that is called whenever UPDATE! menu was clicked. This method outputs the result of the update in
* HTML
*
* @return string: HTML to display
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function main()
{
if ($this->canUpdateNewsType()) {
$affectedRows = 0;
$errorMessage = '';
$this->content = '';
$this->doc = GeneralUtility::makeInstance('noDoc');
$this->doc->backPath = $GLOBALS['BACK_PATH'];
if ($this->updateNewsEventRecords($errorMessage, $affectedRows) == 0) {
$this->content .= $this->doc->section(
'',
'The update has been performed successfully: ' . $affectedRows . ' row(s) affected.'
);
} else {
$this->content .= $this->doc->section(
'',
'An error occurred while preforming updates. Error: ' . $errorMessage
);
}
return $this->content;
} elseif ($this->canUpdateEventStartEndFields()) {
$selectStatement = $this->getDatabase()->exec_SELECTquery(
'uid,tx_roqnewsevent_start,tx_roqnewsevent_startdate,tx_roqnewsevent_starttime,tx_roqnewsevent_end,tx_roqnewsevent_enddate,tx_roqnewsevent_endtime',
'tx_news_domain_model_news',
'tx_roqnewsevent_start = 0 AND tx_roqnewsevent_end = 0 AND (tx_roqnewsevent_startdate != 0 OR tx_roqnewsevent_starttime != 0 OR tx_roqnewsevent_enddate != 0 OR tx_roqnewsevent_endtime != 0)'
);
while ($row = $selectStatement->fetch_assoc()) {
$uid = (int)$row['uid'];
$row['tx_roqnewsevent_start'] = $row['tx_roqnewsevent_startdate'] + $row['tx_roqnewsevent_starttime'];
// HF-MOD: Start
if( $row['tx_roqnewsevent_enddate'] > 0 && $row['tx_roqnewsevent_endtime'] >= 0) {
$row['tx_roqnewsevent_end'] = $row['tx_roqnewsevent_enddate'] + $row['tx_roqnewsevent_endtime'];
}
if( $row['tx_roqnewsevent_enddate'] == 0 && $row['tx_roqnewsevent_endtime'] > 0) {
$row['tx_roqnewsevent_end'] = $row['tx_roqnewsevent_startdate'] + $row['tx_roqnewsevent_endtime'];
}
if( $row['tx_roqnewsevent_enddate'] == 0 && $row['tx_roqnewsevent_endtime'] == 0) {
$row['tx_roqnewsevent_end'] = 0;
}
// HF-MOD: End
$this->getDatabase()->exec_UPDATEquery('tx_news_domain_model_news', 'uid=' . $uid, $row);
}
return sprintf('Update of %d rows successful', $selectStatement->num_rows);
}
}
/**
* Updates news type and news event tx_roqnewsevent_is_event attribute in database news event records
*
* @param $errorMessage : stores the error message, if an error has been occurred
* @param $affectedRows : stores the affected rows, when the query has been executed
* @return integer: returns error code (0 == success)
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
protected function updateNewsEventRecords(&$errorMessage, &$affectedRows)
{
$errorCode = 0;
$affectedRows = 0;
$result = false;
$result = $this->getDatabase()->exec_UPDATEquery(
'tx_news_domain_model_news',
"tx_news_domain_model_news.type LIKE 'Tx_RoqNewsevent_Event'",
[
'type' => 0,
'tx_roqnewsevent_is_event' => 1,
]
);
if ($result) {
$affectedRows = $this->getDatabase()->sql_affected_rows();
} else {
$errorCode = $this->getDatabase()->sql_errno();
$errorMessage = 'Could not update table tx_news_domain_model_news. '
. $this->getDatabase()->sql_error()
. ' (Error code: '
. $errorCode
. ').';
}
return $errorCode;
}
/**
* Check if the update is necessary, and whether the "UPDATE!" menu item should be shown.
*
* @return boolean: returns true if update should be performed
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function access()
{
return $this->canUpdateNewsType() || $this->canUpdateEventStartEndFields();
}
/**
* @return bool
*/
protected function canUpdateNewsType()
{
$result = $this->getDatabase()->exec_SELECTquery(
'tx_news_domain_model_news.type',
'tx_news_domain_model_news',
"tx_news_domain_model_news.type LIKE 'Tx_RoqNewsevent_Event'"
);
return ($result !== false) && ($this->getDatabase()->sql_num_rows($result) > 0);
}
/**
* @return bool
*/
protected function canUpdateEventStartEndFields()
{
$fields = $this->getDatabase()->admin_get_fields('tx_news_domain_model_news');
// assume all old fields exist if one is there
if (array_key_exists('tx_roqnewsevent_startdate', $fields)) {
if (!array_key_exists('tx_roqnewsevent_start', $fields)) {
$message = GeneralUtility::makeInstance(
FlashMessage::class,
'Can not check for possible updates if new tables are missing. Please run Database Compare and add all missing fields',
'roq_eventnews: Missing Database Fields',
FlashMessage::WARNING,
true
);
GeneralUtility::makeInstance(FlashMessageService::class)
->getMessageQueueByIdentifier(
'extbase.flashmessages.tx_extensionmanager_tools_extensionmanagerextensionmanager'
)
->addMessage($message);
} else {
$count = $this->getDatabase()->exec_SELECTcountRows(
'uid',
'tx_news_domain_model_news',
'tx_roqnewsevent_start = 0 AND tx_roqnewsevent_end = 0 AND (tx_roqnewsevent_startdate != 0 OR tx_roqnewsevent_starttime != 0 OR tx_roqnewsevent_enddate != 0 OR tx_roqnewsevent_endtime != 0)'
);
return $count > 0;
}
}
}
/**
* @return DatabaseConnection
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
protected function getDatabase()
{
return $GLOBALS['TYPO3_DB'];
}
}