-
Notifications
You must be signed in to change notification settings - Fork 27
/
DataStructureUpdateHandler.php
172 lines (144 loc) · 4.76 KB
/
DataStructureUpdateHandler.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
<?php
namespace Ppi\TemplaVoilaPlus\Controller\Update;
/*
* 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\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use Ppi\TemplaVoilaPlus\Domain\Repository\DataStructureRepository;
use Ppi\TemplaVoilaPlus\Domain\Repository\TemplateRepository;
/**
* Handles Updates in DataStructure via Callbacks
*
* @author Alexander Opitz <opitz.alexander@pluspol-interactive.de>
*/
class DataStructureUpdateHandler
{
public function updateAllDs(array $rootCallbacks, array $elementCallbacks)
{
$count = 0;
$dsRepo = GeneralUtility::makeInstance(DataStructureRepository::class);
foreach ($dsRepo->getAll() as $ds) {
if ($this->updateDs($ds, $rootCallbacks, $elementCallbacks)) {
$count++;
}
}
return $count;
}
public function updateAllToLocals(array $rootCallbacks, array $elementCallbacks)
{
$count = 0;
$tsRepo = GeneralUtility::makeInstance(TemplateRepository::class);
foreach ($tsRepo->getAll() as $to) {
if ($this->updateTo($to, $rootCallbacks, $elementCallbacks)) {
$count++;
}
}
return $count;
}
public function updateDs(
\Ppi\TemplaVoilaPlus\Domain\Model\AbstractDataStructure $ds,
array $rootCallbacks,
array $elementCallbacks
) {
$data = $ds->getDataprotArray();
$changed = $this->processUpdate($data, $rootCallbacks, $elementCallbacks);
if ($changed) {
$this->saveChange(
$ds->getKey(),
'tx_templavoilaplus_datastructure',
'dataprot',
GeneralUtility::array2xml_cs(
$data,
'T3DataStructure',
['useCDATA' => 1]
),
$ds->isFilebased()
);
return true;
}
return false;
}
public function updateTo(
\Ppi\TemplaVoilaPlus\Domain\Model\Template $to,
array $rootCallbacks,
array $elementCallbacks
) {
$data = $to->getLocalDataprotArray(true);
$changed = $this->processUpdate($data, $rootCallbacks, $elementCallbacks);
if ($changed) {
$this->saveChange(
$to->getKey(),
'tx_templavoilaplus_tmplobj',
'localprocessing',
GeneralUtility::array2xml_cs(
$data,
'T3DataStructure',
['useCDATA' => 1]
)
);
return true;
}
return false;
}
public function processUpdate(
array &$data,
array $rootCallbacks,
array $elementCallbacks
) {
$changed = false;
if (empty($data)) {
return false;
}
foreach ($rootCallbacks as $callback) {
if (is_callable($callback)) {
$changed = $callback($data) || $changed;
}
}
foreach($data['ROOT']['el'] as &$element) {
$changed = $this->fixPerElement($element, $elementCallbacks) || $changed;
}
return $changed;
}
protected function saveChange($key, $table, $field, $dataProtXML, $filebased = false)
{
if ($filebased) {
$path = PATH_site . $key;
GeneralUtility::writeFile($path, $dataProtXML);
} else {
$tce = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
$tce->stripslashes_values = 0;
$dataArr = [];
$dataArr[$table][$key][$field] = $dataProtXML;
// process data
$tce->start($dataArr, []);
$tce->process_datamap();
}
}
protected function fixPerElement(array &$element, array $elementCallbacks)
{
$changed = false;
foreach ($elementCallbacks as $callback) {
if (is_callable($callback)) {
$changed = $callback($element) || $changed;
}
}
if (isset($element['type']) && $element['type'] === 'array') {
if (is_array($element['el'])) {
foreach($element['el'] as &$subElement) {
$changed = $this->fixPerElement($subElement, $elementCallbacks) || $changed;
}
}
}
return $changed;
}
}