Skip to content

Commit 103ada1

Browse files
Merge pull request #18366 from kamil-tekiela/InsertEdit-refactoring2
InsertEdit refactoring - part 2
2 parents 0d24325 + a01eefc commit 103ada1

13 files changed

Lines changed: 142 additions & 386 deletions

libraries/classes/Controllers/Table/ChangeController.php

Lines changed: 38 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
use function is_array;
2323
use function is_string;
2424
use function str_contains;
25+
use function str_starts_with;
2526
use function strlen;
26-
use function strpos;
2727
use function trim;
2828

2929
/**
@@ -48,26 +48,8 @@ public function __invoke(ServerRequest $request): void
4848
$GLOBALS['errorUrl'] ??= null;
4949
$GLOBALS['where_clause'] ??= null;
5050
$GLOBALS['unsaved_values'] ??= null;
51-
$GLOBALS['insert_mode'] ??= null;
52-
$GLOBALS['where_clause_array'] ??= null;
53-
$GLOBALS['where_clauses'] ??= null;
5451
$GLOBALS['result'] ??= null;
55-
$GLOBALS['rows'] ??= null;
56-
$GLOBALS['found_unique_key'] ??= null;
57-
$GLOBALS['after_insert'] ??= null;
58-
$GLOBALS['comments_map'] ??= null;
59-
$GLOBALS['table_columns'] ??= null;
60-
$GLOBALS['timestamp_seen'] ??= null;
61-
$GLOBALS['columns_cnt'] ??= null;
62-
$GLOBALS['tabindex'] ??= null;
63-
$GLOBALS['tabindex_for_value'] ??= null;
64-
$GLOBALS['o_rows'] ??= null;
65-
$GLOBALS['has_blob_field'] ??= null;
66-
$GLOBALS['jsvkey'] ??= null;
67-
$GLOBALS['vkey'] ??= null;
6852
$GLOBALS['current_result'] ??= null;
69-
$GLOBALS['repopulate'] ??= null;
70-
$GLOBALS['checked'] ??= null;
7153

7254
$pageSettings = new PageSettings('Edit');
7355
$this->response->addHTML($pageSettings->getErrorHTML());
@@ -86,22 +68,22 @@ public function __invoke(ServerRequest $request): void
8668
* Determine whether Insert or Edit and set global variables
8769
*/
8870
[
89-
$GLOBALS['insert_mode'],
71+
$insertMode,
9072
$GLOBALS['where_clause'],
91-
$GLOBALS['where_clause_array'],
92-
$GLOBALS['where_clauses'],
73+
$whereClauseArray,
74+
$whereClauses,
9375
$GLOBALS['result'],
94-
$GLOBALS['rows'],
95-
$GLOBALS['found_unique_key'],
96-
$GLOBALS['after_insert'],
76+
$rows,
77+
$foundUniqueIndex,
78+
$afterInsert,
9779
] = $this->insertEdit->determineInsertOrEdit(
9880
$GLOBALS['where_clause'] ?? null,
9981
$GLOBALS['db'],
10082
$GLOBALS['table'],
10183
);
10284
// Increase number of rows if unsaved rows are more
103-
if (! empty($GLOBALS['unsaved_values']) && count($GLOBALS['rows']) < count($GLOBALS['unsaved_values'])) {
104-
$GLOBALS['rows'] = array_fill(0, count($GLOBALS['unsaved_values']), false);
85+
if (! empty($GLOBALS['unsaved_values']) && count($rows) < count($GLOBALS['unsaved_values'])) {
86+
$rows = array_fill(0, count($GLOBALS['unsaved_values']), false);
10587
}
10688

10789
/**
@@ -121,7 +103,7 @@ public function __invoke(ServerRequest $request): void
121103
$sqlQuery = $request->getParsedBodyParam('sql_query');
122104
$GLOBALS['urlParams'] = ['db' => $GLOBALS['db'], 'sql_query' => is_string($sqlQuery) ? $sqlQuery : ''];
123105

124-
if (strpos($GLOBALS['goto'] ?? '', 'index.php?route=/table') === 0) {
106+
if (str_starts_with($GLOBALS['goto'] ?? '', 'index.php?route=/table')) {
125107
$GLOBALS['urlParams']['table'] = $GLOBALS['table'];
126108
}
127109

@@ -131,7 +113,7 @@ public function __invoke(ServerRequest $request): void
131113
);
132114
unset($GLOBALS['urlParams']);
133115

134-
$GLOBALS['comments_map'] = $this->insertEdit->getCommentsMap($GLOBALS['db'], $GLOBALS['table']);
116+
$commentsMap = $this->insertEdit->getCommentsMap($GLOBALS['db'], $GLOBALS['table']);
135117

136118
/**
137119
* START REGULAR OUTPUT
@@ -151,10 +133,10 @@ public function __invoke(ServerRequest $request): void
151133
* $disp_message come from /table/replace
152134
*/
153135
if (! empty($GLOBALS['disp_message'])) {
154-
$this->response->addHTML(Generator::getMessage($GLOBALS['disp_message'], null));
136+
$this->response->addHTML(Generator::getMessage($GLOBALS['disp_message']));
155137
}
156138

157-
$GLOBALS['table_columns'] = $this->insertEdit->getTableColumns($GLOBALS['db'], $GLOBALS['table']);
139+
$tableColumns = $this->insertEdit->getTableColumns($GLOBALS['db'], $GLOBALS['table']);
158140

159141
// retrieve keys into foreign fields, if any
160142
$foreigners = $this->relation->getForeigners($GLOBALS['db'], $GLOBALS['table']);
@@ -163,8 +145,8 @@ public function __invoke(ServerRequest $request): void
163145
$formParams = $this->insertEdit->getFormParametersForInsertForm(
164146
$GLOBALS['db'],
165147
$GLOBALS['table'],
166-
$GLOBALS['where_clauses'],
167-
$GLOBALS['where_clause_array'],
148+
$whereClauses,
149+
$whereClauseArray,
168150
$GLOBALS['errorUrl'],
169151
);
170152

@@ -175,30 +157,23 @@ public function __invoke(ServerRequest $request): void
175157
// some browsers send wrongly this form to the http server.
176158

177159
$htmlOutput = '';
178-
// Set if we passed the first timestamp field
179-
$GLOBALS['timestamp_seen'] = false;
180-
$GLOBALS['columns_cnt'] = count($GLOBALS['table_columns']);
181-
182-
$GLOBALS['tabindex'] = 0;
183-
$GLOBALS['tabindex_for_value'] = 0;
184-
$GLOBALS['o_rows'] = 0;
185160

186161
$GLOBALS['urlParams']['db'] = $GLOBALS['db'];
187162
$GLOBALS['urlParams']['table'] = $GLOBALS['table'];
188-
$GLOBALS['urlParams'] = $this->urlParamsInEditMode($GLOBALS['urlParams'], $GLOBALS['where_clause_array']);
163+
$GLOBALS['urlParams'] = $this->urlParamsInEditMode($GLOBALS['urlParams'], $whereClauseArray);
189164

190-
$GLOBALS['has_blob_field'] = false;
191-
foreach ($GLOBALS['table_columns'] as $column) {
165+
$hasBlobField = false;
166+
foreach ($tableColumns as $column) {
192167
if ($this->insertEdit->isColumn($column, ['blob', 'tinyblob', 'mediumblob', 'longblob'])) {
193-
$GLOBALS['has_blob_field'] = true;
168+
$hasBlobField = true;
194169
break;
195170
}
196171
}
197172

198173
//Insert/Edit form
199174
//If table has blob fields we have to disable ajax.
200175
$isUpload = $GLOBALS['config']->get('enable_upload');
201-
$htmlOutput .= $this->insertEdit->getHtmlForInsertEditFormHeader($GLOBALS['has_blob_field'], $isUpload);
176+
$htmlOutput .= $this->insertEdit->getHtmlForInsertEditFormHeader($hasBlobField, $isUpload);
202177

203178
$htmlOutput .= Url::getHiddenInputs($formParams);
204179

@@ -217,67 +192,49 @@ public function __invoke(ServerRequest $request): void
217192
}
218193

219194
$GLOBALS['plugin_scripts'] = [];
220-
foreach ($GLOBALS['rows'] as $rowId => $currentRow) {
221-
if (empty($currentRow)) {
222-
$currentRow = [];
223-
}
224-
225-
$GLOBALS['jsvkey'] = (string) $rowId;
226-
$GLOBALS['vkey'] = '[multi_edit][' . $GLOBALS['jsvkey'] . ']';
227-
195+
foreach ($rows as $rowId => $currentRow) {
228196
$GLOBALS['current_result'] = (isset($GLOBALS['result'])
229197
&& is_array($GLOBALS['result']) && isset($GLOBALS['result'][$rowId])
230198
? $GLOBALS['result'][$rowId]
231199
: $GLOBALS['result']);
232-
$GLOBALS['repopulate'] = [];
233-
$GLOBALS['checked'] = true;
200+
$repopulate = [];
201+
$checked = true;
234202
if (isset($GLOBALS['unsaved_values'][$rowId])) {
235-
$GLOBALS['repopulate'] = $GLOBALS['unsaved_values'][$rowId];
236-
$GLOBALS['checked'] = false;
203+
$repopulate = $GLOBALS['unsaved_values'][$rowId];
204+
$checked = false;
237205
}
238206

239-
if ($GLOBALS['insert_mode'] && $rowId > 0) {
240-
$htmlOutput .= $this->insertEdit->getHtmlForIgnoreOption($rowId, $GLOBALS['checked']);
207+
if ($insertMode && $rowId > 0) {
208+
$htmlOutput .= $this->insertEdit->getHtmlForIgnoreOption($rowId, $checked);
241209
}
242210

243211
$htmlOutput .= $this->insertEdit->getHtmlForInsertEditRow(
244212
$GLOBALS['urlParams'],
245-
$GLOBALS['table_columns'],
246-
$GLOBALS['comments_map'],
247-
$GLOBALS['timestamp_seen'],
213+
$tableColumns,
214+
$commentsMap,
248215
$GLOBALS['current_result'],
249-
$GLOBALS['jsvkey'],
250-
$GLOBALS['vkey'],
251-
$GLOBALS['insert_mode'],
252-
$currentRow,
253-
$GLOBALS['o_rows'],
254-
$GLOBALS['tabindex'],
255-
$GLOBALS['columns_cnt'],
216+
$insertMode,
217+
$currentRow ?: [],
256218
$isUpload,
257219
$foreigners,
258-
$GLOBALS['tabindex_for_value'],
259220
$GLOBALS['table'],
260221
$GLOBALS['db'],
261222
$rowId,
262223
$GLOBALS['text_dir'],
263-
$GLOBALS['repopulate'],
264-
$GLOBALS['where_clause_array'],
224+
$repopulate,
225+
$whereClauseArray,
265226
);
266227
}
267228

268229
$this->addScriptFiles($GLOBALS['plugin_scripts']);
269230

270-
unset($GLOBALS['unsaved_values'], $GLOBALS['checked'], $GLOBALS['repopulate'], $GLOBALS['plugin_scripts']);
271-
272-
if (! isset($GLOBALS['after_insert'])) {
273-
$GLOBALS['after_insert'] = 'back';
274-
}
231+
unset($GLOBALS['unsaved_values'], $GLOBALS['plugin_scripts']);
275232

276233
$isNumeric = InsertEdit::isWhereClauseNumeric($GLOBALS['where_clause']);
277234
$htmlOutput .= $this->template->render('table/insert/actions_panel', [
278235
'where_clause' => $GLOBALS['where_clause'],
279-
'after_insert' => $GLOBALS['after_insert'],
280-
'found_unique_key' => $GLOBALS['found_unique_key'],
236+
'after_insert' => $afterInsert ?? 'back',
237+
'found_unique_key' => $foundUniqueIndex,
281238
'is_numeric' => $isNumeric,
282239
]);
283240

@@ -286,12 +243,12 @@ public function __invoke(ServerRequest $request): void
286243
$htmlOutput .= $this->insertEdit->getHtmlForGisEditor();
287244
// end Insert/Edit form
288245

289-
if ($GLOBALS['insert_mode']) {
246+
if ($insertMode) {
290247
//Continue insertion form
291248
$htmlOutput .= $this->insertEdit->getContinueInsertionForm(
292249
$GLOBALS['table'],
293250
$GLOBALS['db'],
294-
$GLOBALS['where_clause_array'],
251+
$whereClauseArray,
295252
$GLOBALS['errorUrl'],
296253
);
297254
}

libraries/classes/Controllers/Table/ReplaceController.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,18 +314,22 @@ public function __invoke(ServerRequest $request): void
314314
return;
315315
}
316316

317+
$returnToSqlQuery = '';
318+
if (! empty($GLOBALS['sql_query'])) {
319+
$GLOBALS['urlParams']['sql_query'] = $GLOBALS['sql_query'];
320+
$returnToSqlQuery = $GLOBALS['sql_query'];
321+
}
322+
317323
/**
318324
* Executes the sql query and get the result, then move back to the calling
319325
* page
320326
*/
321327
[
322-
$GLOBALS['urlParams'],
323328
$totalAffectedRows,
324329
$lastMessages,
325330
$warningMessages,
326331
$errorMessages,
327-
$returnToSqlQuery,
328-
] = $this->insertEdit->executeSqlQuery($GLOBALS['urlParams'], $GLOBALS['query']);
332+
] = $this->insertEdit->executeSqlQuery($GLOBALS['query']);
329333

330334
if ($isInsert && ($valueSets !== [] || $rowSkipped)) {
331335
$GLOBALS['message'] = Message::getMessageForInsertedRows($totalAffectedRows);

0 commit comments

Comments
 (0)