forked from bobosch/ods_ajaxmailsubscription
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.ext_update.php
184 lines (165 loc) · 6.17 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
<?php
/***************************************************************
* Copyright notice
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
class ext_update {
protected $messageArray = array();
public function access() {
return \TYPO3\CMS\Core\Utility\GeneralUtility::compat_version('6.0');
}
/**
* Main update function called by the extension manager.
*
* @return string
*/
public function main() {
$this->processUpdates();
return $this->generateOutput();
}
/**
* Generates output by using flash messages
*
* @return string
*/
protected function generateOutput() {
$output = '';
foreach ($this->messageArray as $messageItem) {
$flashMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
'TYPO3\CMS\Core\Messaging\FlashMessage',
$messageItem[2],
$messageItem[1],
$messageItem[0]
);
$output .= $flashMessage->render();
}
return $output;
}
/**
* The actual update function. Add your update task in here.
*
* @return void
*/
protected function processUpdates() {
$this->addDatabaseField('fe_users', 'tx_odsajaxmailsubscription_rid', "varchar(8) DEFAULT '' NOT NULL");
$this->addDatabaseField('tt_address', 'tx_odsajaxmailsubscription_rid', "varchar(8) DEFAULT '' NOT NULL");
$this->moveFieldFFtoTable('sDEF','page_records','tt_content','pages');
}
/**
* Add a field to database table
*
* @param string $table
* @param string $field
* @param string $options
* @return int
*/
protected function addDatabaseField($table, $field, $options) {
$title = 'Modify table "' . $table . '": Add field ' . $field;
$message = '';
$status = NULL;
$currentTableFields = $GLOBALS['TYPO3_DB']->admin_get_fields($table);
if ($currentTableFields[$field]) {
$message = 'Field ' . $table . ':' . $field . ' already exists.';
$status = \TYPO3\CMS\Core\Messaging\FlashMessage::OK;
} else {
$sql = 'ALTER TABLE ' . $table . ' ADD ' . $field . ' ' . $options;
if ($GLOBALS['TYPO3_DB']->admin_query($sql) === FALSE) {
$message = ' SQL ERROR: ' . $GLOBALS['TYPO3_DB']->sql_error();
$status = \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR;
} else {
$message = 'OK!';
$status = \TYPO3\CMS\Core\Messaging\FlashMessage::OK;
}
}
$this->messageArray[] = array($status, $title, $message);
return $status;
}
protected function moveFieldFFtoTable($FFsheet,$FFfield,$DBtable,$DBfield) {
$title = 'Move data from flexform sheet "' . $FFsheet . '", field "' . $FFfield . '" to table "' . $DBtable . '", field "' . $DBfield . '"';
$status = \TYPO3\CMS\Core\Messaging\FlashMessage::OK;
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'*',
'tt_content',
'CType=' . $GLOBALS['TYPO3_DB']->fullQuoteStr('list', 'tt_content') . ' AND list_type=' . $GLOBALS['TYPO3_DB']->fullQuoteStr('ods_ajaxmailsubscription_pi1', 'tt_content') . ' AND deleted=0'
);
if ($res) {
$flexObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools');
while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
$xmlArr = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($row['pi_flexform']);
$oldVal = $this->pi_getFFvalue($xmlArr, $FFfield, $FFsheet);
if ($oldVal) {
$message = 'Moved value "' . $oldVal . '" on content id "' . $row['uid'] . '"' . "\n";
unset($xmlArr['data'][$FFsheet]['lDEF'][$FFfield]);
$newff = $flexObj->flexArray2Xml($xmlArr, 1);
$fields_values = array('pi_flexform' => $newff);
if($DBtable=='tt_content') $fields_values[$DBfield]=$oldVal;
$UPDATEres = $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
'tt_content',
'uid=' . $row['uid'],
$fields_values
);
if (!$UPDATEres) {
$status = \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR;
}
}
}
if(empty($message)) $message='No data to move.';
$this->messageArray[] = array($status, $title, $message);
}
}
protected function pi_getFFvalue($T3FlexForm_array, $fieldName, $sheet = 'sDEF', $lang = 'lDEF', $value = 'vDEF') {
$sheetArray = is_array($T3FlexForm_array) ? $T3FlexForm_array['data'][$sheet][$lang] : '';
if (is_array($sheetArray)) {
return $this->pi_getFFvalueFromSheetArray($sheetArray, explode('/', $fieldName), $value);
}
}
/**
* Returns part of $sheetArray pointed to by the keys in $fieldNameArray
*
* @param array Multidimensiona array, typically FlexForm contents
* @param array Array where each value points to a key in the FlexForms content - the input array will have the value returned pointed to by these keys. All integer keys will not take their integer counterparts, but rather traverse the current position in the array an return element number X (whether this is right behavior is not settled yet...)
* @param string Value for outermost key, typ. "vDEF" depending on language.
* @return mixed The value, typ. string.
* @access private
* @see pi_getFFvalue()
*/
protected function pi_getFFvalueFromSheetArray($sheetArray, $fieldNameArr, $value) {
$tempArr = $sheetArray;
foreach ($fieldNameArr as $k => $v) {
if (is_int($v)) {
if (is_array($tempArr)) {
$c = 0;
foreach ($tempArr as $values) {
if ($c == $v) {
$tempArr = $values;
break;
}
$c++;
}
}
} else {
$tempArr = $tempArr[$v];
}
}
return $tempArr[$value];
}
}
?>