-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSaveData.php
More file actions
346 lines (315 loc) · 11.6 KB
/
SaveData.php
File metadata and controls
346 lines (315 loc) · 11.6 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
namespace exface\Core\Actions;
use exface\Core\CommonLogic\ActionInputValidator;
use exface\Core\Interfaces\DataSheets\DataSheetInterface;
use exface\Core\Interfaces\Actions\iModifyData;
use exface\Core\Interfaces\Actions\iCanBeUndone;
use exface\Core\CommonLogic\UxonObject;
use exface\Core\Factories\DataSheetFactory;
use exface\Core\CommonLogic\AbstractAction;
use exface\Core\Exceptions\Actions\ActionUndoFailedError;
use exface\Core\Interfaces\DataSources\DataTransactionInterface;
use exface\Core\CommonLogic\Constants\Icons;
use exface\Core\Interfaces\Tasks\TaskInterface;
use exface\Core\Factories\ResultFactory;
use exface\Core\Interfaces\Tasks\ResultInterface;
use exface\Core\CommonLogic\DataSheets\DataColumn;
use exface\Core\Exceptions\Actions\ActionRuntimeError;
class SaveData extends AbstractAction implements iModifyData, iCanBeUndone
{
private $affected_rows = 0;
private $undo_data_sheet = null;
private $ignore_rows_if_empty_except_column_names = null;
private $ignore_rows_if_empty_column_names = null;
private $ignore_rows_if_empty = false;
/**
*
* {@inheritDoc}
* @see \exface\Core\CommonLogic\AbstractAction::init()
*/
protected function init()
{
$this->setIcon(Icons::CHECK);
$this->setInputRowsMin(0);
$this->setInputRowsMax(null);
}
/**
* @inheritDoc
*/
protected function validateApplicability(ActionInputValidator $validator): void
{
parent::validateApplicability($validator);
$expectedColumns = $validator->getExpectedColumns();
$validator->validateTaskColumns($expectedColumns);
}
/**
*
* {@inheritDoc}
* @see \exface\Core\CommonLogic\AbstractAction::perform()
*/
protected function perform(TaskInterface $task, DataTransactionInterface $transaction) : ResultInterface
{
$data_sheet = $this->getInputDataSheet($task);
if ($data_sheet->isEmpty()) {
$affected_rows = 0;
} else {
try {
$affected_rows = $data_sheet->dataSave($transaction);
} catch (\Throwable $e) {
throw new ActionRuntimeError($this, 'Cannot save data of object ' . $this->getMetaObject()->__toString() . '. ' . $e->getMessage(), null, $e);
}
}
$message = $this->getResultMessageText() ?? $this->getWorkbench()->getCoreApp()->getTranslator()->translate('ACTION.SAVEDATA.RESULT', ['%number%' => $affected_rows], $affected_rows);
$result = ResultFactory::createDataResult($task, $data_sheet, $message);
if ($affected_rows > 0) {
$result->setDataModified(true);
}
return $result;
}
/**
*
* @return DataSheetInterface
*/
protected function getUndoDataSheet()
{
return $this->undo_data_sheet;
}
/**
*
* @param DataSheetInterface $data_sheet
*/
protected function setUndoDataSheet(DataSheetInterface $data_sheet)
{
$this->undo_data_sheet = $data_sheet;
}
/**
*
* {@inheritDoc}
* @see \exface\Core\Interfaces\Actions\iCanBeUndone::getUndoDataUxon()
*/
public function getUndoDataUxon()
{
if ($this->getUndoDataSheet()) {
return $this->getUndoDataSheet()->exportUxonObject();
} else {
return new UxonObject();
}
}
/**
*
* {@inheritDoc}
* @see \exface\Core\Interfaces\Actions\iCanBeUndone::setUndoData()
*/
public function setUndoData(UxonObject $uxon_object) : iCanBeUndone
{
$exface = $this->getApp()->getWorkbench();
$this->undo_data_sheet = DataSheetFactory::createFromUxon($exface, $uxon_object);
}
/**
*
* {@inheritDoc}
* @see \exface\Core\Interfaces\Actions\iCanBeUndone::undo()
*/
public function undo(DataTransactionInterface $transaction) : DataSheetInterface
{
throw new ActionUndoFailedError($this, 'Undo functionality not implemented yet for action "' . $this->getAlias() . '"!', '6T5DS00');
}
protected function getIgnoreRowsIfEmpty() : bool
{
return $this->ignore_rows_if_empty;
}
/**
* Set to TRUE to ignore input data rows that do not have any values.
*
* **NOTE:** These rows will be removed and will not be present in the result data!
*
* @uxon-property ignore_rows_if_empty
* @uxon-type boolean
* @uxon-default false
*
* @param bool $value
* @return SaveData
*/
protected function setIgnoreRowsIfEmpty(bool $value) : SaveData
{
$this->ignore_rows_if_empty = $value;
return $this;
}
/**
*
* @return array|NULL
*/
protected function getIgnoreRowsIfEmptyExceptColumnNames() : ?array
{
return $this->ignore_rows_if_empty_except_column_names;
}
/**
* If a row is empty except in these columns, it will still be concidered empty and ignored
*
* Setting the property with automatically set `ignore_rows_if_empty` to `true`!
*
* This is particularly handy if you save matrix data like values per date where values
* exist only fro certain "coordinates" (e.g. some dates do not have values) - if rows
* without meaningfull values should not besaved, they can be ignored by adding the
* "coordinate" column to this list.
*
* @uxon-property ignore_rows_if_empty_except_column_names
* @uxon-type array
* @uxon-template [""]
*
* @param UxonObject $arrayOfColumnNames
* @return SaveData
*/
protected function setIgnoreRowsIfEmptyExceptColumnNames(UxonObject $arrayOfColumnNames) : SaveData
{
$this->ignore_rows_if_empty_except_column_names = $arrayOfColumnNames->toArray();
$this->ignore_rows_if_empty = true;
return $this;
}
/**
* If a row has an empty value for one of these columns it will be concidered empty and ignored.
*
* Setting the property with automatically set `ignore_rows_if_empty` to `true`!
*
* This is particularly handy if you have an in table input field and wnat to sort out the rows
* where the input field is empty.
*
* @uxon-property ignore_rows_if_empty_column_names
* @uxon-type array
* @uxon-template [""]
*
* @param UxonObject $arrayOfColumnNames
* @return SaveData
*/
protected function setIgnoreRowsIfEmptyColumnNames(UxonObject $arrayOfColumnNames) : SaveData
{
$this->ignore_rows_if_empty_column_names = $arrayOfColumnNames->toArray();
$this->ignore_rows_if_empty = true;
return $this;
}
/**
* If a row has an empty value for one of these attributes it will be concidered empty and ignored.
*
* Setting the property with automatically set `ignore_rows_if_empty` to `true`!
*
* This is particularly handy if you have an in table input field and wnat to sort out the rows
* where the input field is empty.
*
* @uxon-property ignore_rows_if_empty_attributes
* @uxon-type metamodel:attribute[]
* @uxon-template [""]
*
* @param UxonObject $arrayOfAliases
* @return SaveData
*/
protected function setIgnoreRowsIfEmptyAttributes(UxonObject $arrayOfAliases) : SaveData
{
foreach ($arrayOfAliases->toArray() as $alias) {
$this->ignore_rows_if_empty_column_names[] = DataColumn::sanitizeColumnName($alias);
}
$this->ignore_rows_if_empty = true;
return $this;
}
/**
*
* @return array|NULL
*/
protected function getIgnoreRowsIfEmptyColumnNames() : ?array
{
return $this->ignore_rows_if_empty_column_names;
}
/**
* If a row is empty except for these attributes, it will still be concidered empty and ignored
*
* Setting the property with automatically set `ignore_rows_if_empty` to `true`!
*
* This is particularly handy if you save matrix data like values per date where values
* exist only fro certain "coordinates" (e.g. some dates do not have values) - if rows
* without meaningfull values should not besaved, they can be ignored by adding the
* "coordinate" column to this list.
*
* @uxon-property ignore_rows_if_empty_except_attributes
* @uxon-type metamodel:attribute[]
* @uxon-template [""]
*
* @param UxonObject $arrayOfAliases
* @return SaveData
*/
protected function setIgnoreRowsIfEmptyExceptAttributes(UxonObject $arrayOfAliases) : SaveData
{
foreach ($arrayOfAliases->toArray() as $alias) {
$this->ignore_rows_if_empty_except_column_names[] = DataColumn::sanitizeColumnName($alias);
}
$this->ignore_rows_if_empty = true;
return $this;
}
/**
*
* @return bool
*/
protected function isIgnoringRowsIfEmpty() : bool
{
return $this->ignore_rows_if_empty;
}
/**
*
* {@inheritDoc}
* @see \exface\Core\CommonLogic\AbstractAction::getInputDataSheet()
*/
public function getInputDataSheet(TaskInterface $task) : DataSheetInterface
{
$sheet = parent::getInputDataSheet($task);
if ($this->isIgnoringRowsIfEmpty()) {
$lb = $this->getLogBook($task);
$lb->addSection('Removing empty rows');
$lb->setIndentActive(1);
$exceptCols = $this->getIgnoreRowsIfEmptyExceptColumnNames();
if ($exceptCols !== null) {
$lb->addLine('Removing rows if empty except for columns "' . implode('", "', $exceptCols) . '"');
$rowIdxRemoved = [];
for ($i = ($sheet->countRows()-1); $i >= 0; $i--) {
$row = $sheet->getRow($i);
foreach ($row as $colName => $val) {
if (in_array($colName, $exceptCols)) {
continue;
}
if ($sheet->getColumns()->get($colName)->getDataType()->isValueEmpty($val) === false) {
continue 2;
}
}
$rowIdxRemoved[] = $i;
$sheet->removeRow($i);
}
if (empty($rowIdxRemoved)) {
$lb->addLine('Removed rows ' . implode(', ', $rowIdxRemoved), 1);
} else {
$lb->addLine('No rows removed', 1);
}
}
$cols = $this->getIgnoreRowsIfEmptyColumnNames();
if ($cols !== null) {
$lb->addLine('Removing rows if at least one of these columns empty: "' . implode('", "', $cols) . '"');
$rowIdxRemoved = [];
for ($i = ($sheet->countRows()-1); $i >= 0; $i--) {
$row = $sheet->getRow($i);
foreach ($row as $colName => $val) {
if (! in_array($colName, $cols)) {
continue;
}
if ($sheet->getColumns()->get($colName)->getDataType()->isValueEmpty($val) === false) {
continue 2;
}
}
$rowIdxRemoved[] = $i;
$sheet->removeRow($i);
}
if (empty($rowIdxRemoved)) {
$lb->addLine('Removed rows ' . implode(', ', $rowIdxRemoved), 1);
} else {
$lb->addLine('No rows removed', 1);
}
}
}
return $sheet;
}
}