1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPPgAdmin\Controller; |
4
|
|
|
|
5
|
|
|
use \PHPPgAdmin\Decorators\Decorator; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Base controller class |
9
|
|
|
*/ |
10
|
|
|
class TablesController extends BaseController |
11
|
|
|
{ |
12
|
|
|
use AdminTrait; |
13
|
|
|
public $script = 'tables.php'; |
14
|
|
|
public $_name = 'TablesController'; |
15
|
|
|
public $table_place = 'tables-tables'; |
16
|
|
|
|
17
|
|
|
public function render() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$conf = $this->conf; |
20
|
|
|
$misc = $this->misc; |
21
|
|
|
$lang = $this->lang; |
22
|
|
|
$action = $this->action; |
23
|
|
|
|
24
|
|
|
if ($action == 'tree') { |
25
|
|
|
return $this->doTree(); |
26
|
|
|
} elseif ($action == 'subtree') { |
27
|
|
|
return $this->doSubTree(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$data = $misc->getDatabaseAccessor(); |
31
|
|
|
|
32
|
|
|
$header_template = 'header.twig'; |
33
|
|
|
$footer_template = 'footer.twig'; |
34
|
|
|
|
35
|
|
|
ob_start(); |
36
|
|
|
|
37
|
|
|
switch ($action) { |
38
|
|
|
case 'create': |
39
|
|
|
|
40
|
|
|
if (isset($_POST['cancel'])) { |
41
|
|
|
$this->doDefault(); |
42
|
|
|
} else { |
43
|
|
|
$header_template = 'header_select2.twig'; |
44
|
|
|
$this->doCreate(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
break; |
48
|
|
|
case 'createlike': |
49
|
|
|
$header_template = 'header_select2.twig'; |
50
|
|
|
$this->doCreateLike(false); |
51
|
|
|
break; |
52
|
|
|
case 'confcreatelike': |
53
|
|
|
if (isset($_POST['cancel'])) { |
54
|
|
|
$header_template = 'header_datatables.twig'; |
55
|
|
|
$this->doDefault(); |
56
|
|
|
} else { |
57
|
|
|
//$header_template = 'header_select2.twig'; |
58
|
|
|
$this->doCreateLike(true); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
break; |
62
|
|
|
case 'selectrows': |
63
|
|
|
if (!isset($_POST['cancel'])) { |
64
|
|
|
$this->doSelectRows(false); |
65
|
|
|
} else { |
66
|
|
|
$header_template = 'header_datatables.twig'; |
67
|
|
|
$this->doDefault(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
break; |
71
|
|
|
case 'confselectrows': |
72
|
|
|
$this->doSelectRows(true); |
73
|
|
|
break; |
74
|
|
|
case 'insertrow': |
75
|
|
|
if (!isset($_POST['cancel'])) { |
76
|
|
|
$this->doInsertRow(false); |
77
|
|
|
} else { |
78
|
|
|
$header_template = 'header_datatables.twig'; |
79
|
|
|
$this->doDefault(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
break; |
83
|
|
|
case 'confinsertrow': |
84
|
|
|
$this->doInsertRow(true); |
85
|
|
|
break; |
86
|
|
|
case 'empty': |
87
|
|
|
if (isset($_POST['empty'])) { |
88
|
|
|
$this->doEmpty(false); |
89
|
|
|
} else { |
90
|
|
|
$header_template = 'header_datatables.twig'; |
91
|
|
|
$this->doDefault(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
break; |
95
|
|
|
case 'confirm_empty': |
96
|
|
|
$this->doEmpty(true); |
97
|
|
|
break; |
98
|
|
|
case 'drop': |
99
|
|
|
if (isset($_POST['drop'])) { |
100
|
|
|
$this->doDrop(false); |
101
|
|
|
} else { |
102
|
|
|
$header_template = 'header_datatables.twig'; |
103
|
|
|
$this->doDefault(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
break; |
107
|
|
|
case 'confirm_drop': |
108
|
|
|
$this->doDrop(true); |
109
|
|
|
break; |
110
|
|
|
default: |
111
|
|
|
if ($this->adminActions($action, 'table') === false) { |
112
|
|
|
$header_template = 'header_datatables.twig'; |
113
|
|
|
$this->doDefault(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
break; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$output = ob_get_clean(); |
120
|
|
|
|
121
|
|
|
$this->printHeader($lang['strtables'], null, true, $header_template); |
122
|
|
|
$this->printBody(); |
123
|
|
|
|
124
|
|
|
echo $output; |
125
|
|
|
|
126
|
|
|
return $this->printFooter(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Show default list of tables in the database |
131
|
|
|
*/ |
132
|
|
|
public function doDefault($msg = '') |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$conf = $this->conf; |
135
|
|
|
$misc = $this->misc; |
136
|
|
|
$lang = $this->lang; |
137
|
|
|
$data = $misc->getDatabaseAccessor(); |
138
|
|
|
|
139
|
|
|
$this->printTrail('schema'); |
140
|
|
|
$this->printTabs('schema', 'tables'); |
141
|
|
|
$this->printMsg($msg); |
142
|
|
|
|
143
|
|
|
$tables = $data->getTables(); |
144
|
|
|
|
145
|
|
|
$columns = [ |
146
|
|
|
'table' => [ |
147
|
|
|
'title' => $lang['strtable'], |
148
|
|
|
'field' => Decorator::field('relname'), |
149
|
|
|
'url' => SUBFOLDER . "/redirect/table?{$misc->href}&", |
150
|
|
|
'vars' => ['table' => 'relname'], |
151
|
|
|
], |
152
|
|
|
'owner' => [ |
153
|
|
|
'title' => $lang['strowner'], |
154
|
|
|
'field' => Decorator::field('relowner'), |
155
|
|
|
], |
156
|
|
|
'tablespace' => [ |
157
|
|
|
'title' => $lang['strtablespace'], |
158
|
|
|
'field' => Decorator::field('tablespace'), |
159
|
|
|
], |
160
|
|
|
'tuples' => [ |
161
|
|
|
'title' => $lang['strestimatedrowcount'], |
162
|
|
|
'field' => Decorator::field('reltuples'), |
163
|
|
|
'type' => 'numeric', |
164
|
|
|
], |
165
|
|
|
'actions' => [ |
166
|
|
|
'title' => $lang['stractions'], |
167
|
|
|
], |
168
|
|
|
'comment' => [ |
169
|
|
|
'title' => $lang['strcomment'], |
170
|
|
|
'field' => Decorator::field('relcomment'), |
171
|
|
|
], |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
$actions = [ |
175
|
|
|
'multiactions' => [ |
176
|
|
|
'keycols' => ['table' => 'relname'], |
177
|
|
|
'url' => 'tables.php', |
178
|
|
|
'default' => 'analyze', |
179
|
|
|
], |
180
|
|
|
'browse' => [ |
181
|
|
|
'content' => $lang['strbrowse'], |
182
|
|
|
'attr' => [ |
183
|
|
|
'href' => [ |
184
|
|
|
'url' => 'display.php', |
185
|
|
|
'urlvars' => [ |
186
|
|
|
'subject' => 'table', |
187
|
|
|
'return' => 'table', |
188
|
|
|
'table' => Decorator::field('relname'), |
189
|
|
|
], |
190
|
|
|
], |
191
|
|
|
], |
192
|
|
|
], |
193
|
|
|
'select' => [ |
194
|
|
|
'content' => $lang['strselect'], |
195
|
|
|
'attr' => [ |
196
|
|
|
'href' => [ |
197
|
|
|
'url' => 'tables.php', |
198
|
|
|
'urlvars' => [ |
199
|
|
|
'action' => 'confselectrows', |
200
|
|
|
'table' => Decorator::field('relname'), |
201
|
|
|
], |
202
|
|
|
], |
203
|
|
|
], |
204
|
|
|
], |
205
|
|
|
'insert' => [ |
206
|
|
|
'content' => $lang['strinsert'], |
207
|
|
|
'attr' => [ |
208
|
|
|
'href' => [ |
209
|
|
|
'url' => 'tables.php', |
210
|
|
|
'urlvars' => [ |
211
|
|
|
'action' => 'confinsertrow', |
212
|
|
|
'table' => Decorator::field('relname'), |
213
|
|
|
], |
214
|
|
|
], |
215
|
|
|
], |
216
|
|
|
], |
217
|
|
|
'empty' => [ |
218
|
|
|
'multiaction' => 'confirm_empty', |
219
|
|
|
'content' => $lang['strempty'], |
220
|
|
|
'attr' => [ |
221
|
|
|
'href' => [ |
222
|
|
|
'url' => 'tables.php', |
223
|
|
|
'urlvars' => [ |
224
|
|
|
'action' => 'confirm_empty', |
225
|
|
|
'table' => Decorator::field('relname'), |
226
|
|
|
], |
227
|
|
|
], |
228
|
|
|
], |
229
|
|
|
], |
230
|
|
|
'alter' => [ |
231
|
|
|
'content' => $lang['stralter'], |
232
|
|
|
'attr' => [ |
233
|
|
|
'href' => [ |
234
|
|
|
'url' => 'tblproperties.php', |
235
|
|
|
'urlvars' => [ |
236
|
|
|
'action' => 'confirm_alter', |
237
|
|
|
'table' => Decorator::field('relname'), |
238
|
|
|
], |
239
|
|
|
], |
240
|
|
|
], |
241
|
|
|
], |
242
|
|
|
'drop' => [ |
243
|
|
|
'multiaction' => 'confirm_drop', |
244
|
|
|
'content' => $lang['strdrop'], |
245
|
|
|
'attr' => [ |
246
|
|
|
'href' => [ |
247
|
|
|
'url' => 'tables.php', |
248
|
|
|
'urlvars' => [ |
249
|
|
|
'action' => 'confirm_drop', |
250
|
|
|
'table' => Decorator::field('relname'), |
251
|
|
|
], |
252
|
|
|
], |
253
|
|
|
], |
254
|
|
|
], |
255
|
|
|
'vacuum' => [ |
256
|
|
|
'multiaction' => 'confirm_vacuum', |
257
|
|
|
'content' => $lang['strvacuum'], |
258
|
|
|
'attr' => [ |
259
|
|
|
'href' => [ |
260
|
|
|
'url' => 'tables.php', |
261
|
|
|
'urlvars' => [ |
262
|
|
|
'action' => 'confirm_vacuum', |
263
|
|
|
'table' => Decorator::field('relname'), |
264
|
|
|
], |
265
|
|
|
], |
266
|
|
|
], |
267
|
|
|
], |
268
|
|
|
'analyze' => [ |
269
|
|
|
'multiaction' => 'confirm_analyze', |
270
|
|
|
'content' => $lang['stranalyze'], |
271
|
|
|
'attr' => [ |
272
|
|
|
'href' => [ |
273
|
|
|
'url' => 'tables.php', |
274
|
|
|
'urlvars' => [ |
275
|
|
|
'action' => 'confirm_analyze', |
276
|
|
|
'table' => Decorator::field('relname'), |
277
|
|
|
], |
278
|
|
|
], |
279
|
|
|
], |
280
|
|
|
], |
281
|
|
|
'reindex' => [ |
282
|
|
|
'multiaction' => 'confirm_reindex', |
283
|
|
|
'content' => $lang['strreindex'], |
284
|
|
|
'attr' => [ |
285
|
|
|
'href' => [ |
286
|
|
|
'url' => 'tables.php', |
287
|
|
|
'urlvars' => [ |
288
|
|
|
'action' => 'confirm_reindex', |
289
|
|
|
'table' => Decorator::field('relname'), |
290
|
|
|
], |
291
|
|
|
], |
292
|
|
|
], |
293
|
|
|
], |
294
|
|
|
//'cluster' TODO ? |
295
|
|
|
]; |
296
|
|
|
|
297
|
|
|
if (!$data->hasTablespaces()) { |
298
|
|
|
unset($columns['tablespace']); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
//\Kint::dump($tables); |
302
|
|
|
|
303
|
|
|
echo $this->printTable($tables, $columns, $actions, $this->table_place, $lang['strnotables']); |
304
|
|
|
|
305
|
|
|
$navlinks = [ |
306
|
|
|
'create' => [ |
307
|
|
|
'attr' => [ |
308
|
|
|
'href' => [ |
309
|
|
|
'url' => 'tables.php', |
310
|
|
|
'urlvars' => [ |
311
|
|
|
'action' => 'create', |
312
|
|
|
'server' => $_REQUEST['server'], |
313
|
|
|
'database' => $_REQUEST['database'], |
314
|
|
|
'schema' => $_REQUEST['schema'], |
315
|
|
|
], |
316
|
|
|
], |
317
|
|
|
], |
318
|
|
|
'content' => $lang['strcreatetable'], |
319
|
|
|
], |
320
|
|
|
]; |
321
|
|
|
|
322
|
|
|
if (($tables->recordCount() > 0) && $data->hasCreateTableLike()) { |
323
|
|
|
$navlinks['createlike'] = [ |
324
|
|
|
'attr' => [ |
325
|
|
|
'href' => [ |
326
|
|
|
'url' => 'tables.php', |
327
|
|
|
'urlvars' => [ |
328
|
|
|
'action' => 'createlike', |
329
|
|
|
'server' => $_REQUEST['server'], |
330
|
|
|
'database' => $_REQUEST['database'], |
331
|
|
|
'schema' => $_REQUEST['schema'], |
332
|
|
|
], |
333
|
|
|
], |
334
|
|
|
], |
335
|
|
|
'content' => $lang['strcreatetablelike'], |
336
|
|
|
]; |
337
|
|
|
} |
338
|
|
|
$this->printNavLinks($navlinks, 'tables-tables', get_defined_vars()); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Generate XML for the browser tree. |
343
|
|
|
*/ |
344
|
|
|
public function doTree() |
345
|
|
|
{ |
346
|
|
|
$conf = $this->conf; |
347
|
|
|
$misc = $this->misc; |
348
|
|
|
$lang = $this->lang; |
349
|
|
|
$data = $misc->getDatabaseAccessor(); |
350
|
|
|
|
351
|
|
|
//\PC::debug($misc->getDatabase(), 'getDatabase'); |
352
|
|
|
|
353
|
|
|
$tables = $data->getTables(); |
354
|
|
|
|
355
|
|
|
$reqvars = $misc->getRequestVars('table'); |
356
|
|
|
|
357
|
|
|
$attrs = [ |
358
|
|
|
'text' => Decorator::field('relname'), |
359
|
|
|
'icon' => 'Table', |
360
|
|
|
'iconAction' => Decorator::url('display.php', $reqvars, ['table' => Decorator::field('relname')]), |
361
|
|
|
'toolTip' => Decorator::field('relcomment'), |
362
|
|
|
'action' => Decorator::redirecturl('redirect.php', $reqvars, ['table' => Decorator::field('relname')]), |
363
|
|
|
'branch' => Decorator::url('tables.php', $reqvars, ['action' => 'subtree', 'table' => Decorator::field('relname')]), |
364
|
|
|
]; |
365
|
|
|
|
366
|
|
|
return $this->printTree($tables, $attrs, 'tables'); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function doSubTree() |
|
|
|
|
370
|
|
|
{ |
371
|
|
|
$conf = $this->conf; |
372
|
|
|
$misc = $this->misc; |
373
|
|
|
$lang = $this->lang; |
374
|
|
|
$data = $misc->getDatabaseAccessor(); |
375
|
|
|
|
376
|
|
|
$tabs = $misc->getNavTabs('table'); |
377
|
|
|
$items = $this->adjustTabsForTree($tabs); |
378
|
|
|
$reqvars = $misc->getRequestVars('table'); |
379
|
|
|
|
380
|
|
|
$attrs = [ |
381
|
|
|
'text' => Decorator::field('title'), |
382
|
|
|
'icon' => Decorator::field('icon'), |
383
|
|
|
'action' => Decorator::actionurl( |
384
|
|
|
Decorator::field('url'), |
385
|
|
|
$reqvars, |
386
|
|
|
Decorator::field('urlvars'), |
387
|
|
|
['table' => $_REQUEST['table']] |
388
|
|
|
), |
389
|
|
|
'branch' => Decorator::ifempty( |
390
|
|
|
Decorator::field('branch'), '', Decorator::url(Decorator::field('url'), $reqvars, [ |
391
|
|
|
'action' => 'tree', |
392
|
|
|
'table' => $_REQUEST['table'], |
393
|
|
|
] |
394
|
|
|
) |
395
|
|
|
), |
396
|
|
|
]; |
397
|
|
|
|
398
|
|
|
return $this->printTree($items, $attrs, 'table'); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Displays a screen where they can enter a new table |
403
|
|
|
*/ |
404
|
|
|
public function doCreate($msg = '') |
|
|
|
|
405
|
|
|
{ |
406
|
|
|
$conf = $this->conf; |
407
|
|
|
$misc = $this->misc; |
408
|
|
|
$lang = $this->lang; |
409
|
|
|
$data = $misc->getDatabaseAccessor(); |
410
|
|
|
|
411
|
|
|
if (!isset($_REQUEST['stage'])) { |
412
|
|
|
$_REQUEST['stage'] = 1; |
413
|
|
|
$default_with_oids = $data->getDefaultWithOid(); |
414
|
|
|
if ($default_with_oids == 'off') { |
415
|
|
|
$_REQUEST['withoutoids'] = 'on'; |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
if (!isset($_REQUEST['name'])) { |
420
|
|
|
$_REQUEST['name'] = ''; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
if (!isset($_REQUEST['fields'])) { |
424
|
|
|
$_REQUEST['fields'] = ''; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
if (!isset($_REQUEST['tblcomment'])) { |
428
|
|
|
$_REQUEST['tblcomment'] = ''; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
if (!isset($_REQUEST['spcname'])) { |
432
|
|
|
$_REQUEST['spcname'] = ''; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
switch ($_REQUEST['stage']) { |
436
|
|
|
case 1: |
437
|
|
|
// You are presented with a form in which you describe the table, pick the tablespace and state how many fields it will have |
438
|
|
|
// Fetch all tablespaces from the database |
439
|
|
|
if ($data->hasTablespaces()) { |
440
|
|
|
$tablespaces = $data->getTablespaces(); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
$this->printTrail('schema'); |
444
|
|
|
$this->printTitle($lang['strcreatetable'], 'pg.table.create'); |
445
|
|
|
$this->printMsg($msg); |
446
|
|
|
|
447
|
|
|
echo '<form action="' . SUBFOLDER . '/src/views/' . $this->script . '" method="post">'; |
448
|
|
|
echo "\n"; |
449
|
|
|
echo "<table>\n"; |
450
|
|
|
echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
451
|
|
|
echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
452
|
|
|
htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n"; |
453
|
|
|
echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strnumcols']}</th>\n"; |
454
|
|
|
echo "\t\t<td class=\"data\"><input name=\"fields\" size=\"5\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
455
|
|
|
htmlspecialchars($_REQUEST['fields']), "\" /></td>\n\t</tr>\n"; |
456
|
|
|
echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['stroptions']}</th>\n"; |
457
|
|
|
echo "\t\t<td class=\"data\"><label for=\"withoutoids\"><input type=\"checkbox\" id=\"withoutoids\" name=\"withoutoids\"", isset($_REQUEST['withoutoids']) ? ' checked="checked"' : '', " />WITHOUT OIDS</label></td>\n\t</tr>\n"; |
458
|
|
|
|
459
|
|
|
// Tablespace (if there are any) |
460
|
|
View Code Duplication |
if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) { |
|
|
|
|
461
|
|
|
echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n"; |
462
|
|
|
echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"spcname\">\n"; |
463
|
|
|
// Always offer the default (empty) option |
464
|
|
|
echo "\t\t\t\t<option value=\"\"", |
465
|
|
|
($_REQUEST['spcname'] == '') ? ' selected="selected"' : '', "></option>\n"; |
466
|
|
|
// Display all other tablespaces |
467
|
|
|
while (!$tablespaces->EOF) { |
468
|
|
|
$spcname = htmlspecialchars($tablespaces->fields['spcname']); |
469
|
|
|
echo "\t\t\t\t<option value=\"{$spcname}\"", |
470
|
|
|
($tablespaces->fields['spcname'] == $_REQUEST['spcname']) ? ' selected="selected"' : '', ">{$spcname}</option>\n"; |
471
|
|
|
$tablespaces->moveNext(); |
472
|
|
|
} |
473
|
|
|
echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n"; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n"; |
477
|
|
|
echo "\t\t<td><textarea name=\"tblcomment\" rows=\"3\" cols=\"32\">", |
478
|
|
|
htmlspecialchars($_REQUEST['tblcomment']), "</textarea></td>\n\t</tr>\n"; |
479
|
|
|
|
480
|
|
|
echo "</table>\n"; |
481
|
|
|
echo "<p><input type=\"hidden\" name=\"action\" value=\"create\" />\n"; |
482
|
|
|
echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n"; |
483
|
|
|
echo $misc->form; |
484
|
|
|
echo "<input type=\"submit\" value=\"{$lang['strnext']}\" />\n"; |
485
|
|
|
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
486
|
|
|
echo "</form>\n"; |
487
|
|
|
break; |
488
|
|
|
case 2: |
489
|
|
|
|
490
|
|
|
// Check inputs |
491
|
|
|
$fields = trim($_REQUEST['fields']); |
492
|
|
View Code Duplication |
if (trim($_REQUEST['name']) == '') { |
|
|
|
|
493
|
|
|
$_REQUEST['stage'] = 1; |
494
|
|
|
$this->doCreate($lang['strtableneedsname']); |
495
|
|
|
return; |
496
|
|
|
} elseif ($fields == '' || !is_numeric($fields) || $fields != (int) $fields || $fields < 1) { |
497
|
|
|
$_REQUEST['stage'] = 1; |
498
|
|
|
$this->doCreate($lang['strtableneedscols']); |
499
|
|
|
return; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
$types = $data->getTypes(true, false, true); |
503
|
|
|
$types_for_js = []; |
504
|
|
|
|
505
|
|
|
$this->printTrail('schema'); |
506
|
|
|
$this->printTitle($lang['strcreatetable'], 'pg.table.create'); |
507
|
|
|
$this->printMsg($msg); |
508
|
|
|
|
509
|
|
|
echo '<script src="' . SUBFOLDER . '/js/tables.js" type="text/javascript"></script>'; |
510
|
|
|
echo '<form action="' . SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n"; |
511
|
|
|
|
512
|
|
|
// Output table header |
513
|
|
|
echo "<table>\n"; |
514
|
|
|
echo "\t<tr><th colspan=\"2\" class=\"data required\">{$lang['strcolumn']}</th><th colspan=\"2\" class=\"data required\">{$lang['strtype']}</th>"; |
515
|
|
|
echo "<th class=\"data\">{$lang['strlength']}</th><th class=\"data\">{$lang['strnotnull']}</th>"; |
516
|
|
|
echo "<th class=\"data\">{$lang['struniquekey']}</th><th class=\"data\">{$lang['strprimarykey']}</th>"; |
517
|
|
|
echo "<th class=\"data\">{$lang['strdefault']}</th><th class=\"data\">{$lang['strcomment']}</th></tr>\n"; |
518
|
|
|
|
519
|
|
|
for ($i = 0; $i < $_REQUEST['fields']; $i++) { |
520
|
|
|
if (!isset($_REQUEST['field'][$i])) { |
521
|
|
|
$_REQUEST['field'][$i] = ''; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
if (!isset($_REQUEST['length'][$i])) { |
525
|
|
|
$_REQUEST['length'][$i] = ''; |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
if (!isset($_REQUEST['default'][$i])) { |
529
|
|
|
$_REQUEST['default'][$i] = ''; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
if (!isset($_REQUEST['colcomment'][$i])) { |
533
|
|
|
$_REQUEST['colcomment'][$i] = ''; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
echo "\t<tr>\n\t\t<td>", $i + 1, ". </td>\n"; |
537
|
|
|
echo "\t\t<td><input name=\"field[{$i}]\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
538
|
|
|
htmlspecialchars($_REQUEST['field'][$i]), "\" /></td>\n"; |
539
|
|
|
echo "\t\t<td>\n\t\t\t<select name=\"type[{$i}]\" class=\"select2\" id=\"types{$i}\" onchange=\"checkLengths(this.options[this.selectedIndex].value,{$i});\">\n"; |
540
|
|
|
// Output any "magic" types |
541
|
|
|
foreach ($data->extraTypes as $v) { |
542
|
|
|
$types_for_js[strtolower($v)] = 1; |
543
|
|
|
echo "\t\t\t\t<option value=\"", htmlspecialchars($v), '"', |
544
|
|
|
(isset($_REQUEST['type'][$i]) && $v == $_REQUEST['type'][$i]) ? ' selected="selected"' : '', '>', |
545
|
|
|
$misc->printVal($v), "</option>\n"; |
546
|
|
|
} |
547
|
|
|
$types->moveFirst(); |
548
|
|
View Code Duplication |
while (!$types->EOF) { |
|
|
|
|
549
|
|
|
$typname = $types->fields['typname']; |
550
|
|
|
$types_for_js[$typname] = 1; |
551
|
|
|
echo "\t\t\t\t<option value=\"", htmlspecialchars($typname), '"', |
552
|
|
|
(isset($_REQUEST['type'][$i]) && $typname == $_REQUEST['type'][$i]) ? ' selected="selected"' : '', '>', |
553
|
|
|
$misc->printVal($typname), "</option>\n"; |
554
|
|
|
$types->moveNext(); |
555
|
|
|
} |
556
|
|
|
echo "\t\t\t</select>\n\t\t\n"; |
557
|
|
|
if ($i == 0) { |
558
|
|
|
// only define js types array once |
559
|
|
|
$predefined_size_types = array_intersect($data->predefined_size_types, array_keys($types_for_js)); |
560
|
|
|
$escaped_predef_types = []; // the JS escaped array elements |
561
|
|
|
foreach ($predefined_size_types as $value) { |
562
|
|
|
$escaped_predef_types[] = "'{$value}'"; |
563
|
|
|
} |
564
|
|
|
echo '<script type="text/javascript">predefined_lengths = new Array(' . implode(',', $escaped_predef_types) . ");</script>\n\t</td>"; |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
// Output array type selector |
568
|
|
|
echo "\t\t<td>\n\t\t\t<select name=\"array[{$i}]\">\n"; |
569
|
|
|
echo "\t\t\t\t<option value=\"\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '') ? ' selected="selected"' : '', "></option>\n"; |
570
|
|
|
echo "\t\t\t\t<option value=\"[]\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '[]') ? ' selected="selected"' : '', ">[ ]</option>\n"; |
571
|
|
|
echo "\t\t\t</select>\n\t\t</td>\n"; |
572
|
|
|
|
573
|
|
|
echo "\t\t<td><input name=\"length[{$i}]\" id=\"lengths{$i}\" size=\"10\" value=\"", |
574
|
|
|
htmlspecialchars($_REQUEST['length'][$i]), "\" /></td>\n"; |
575
|
|
|
echo "\t\t<td><input type=\"checkbox\" name=\"notnull[{$i}]\"", (isset($_REQUEST['notnull'][$i])) ? ' checked="checked"' : '', " /></td>\n"; |
576
|
|
|
echo "\t\t<td style=\"text-align: center\"><input type=\"checkbox\" name=\"uniquekey[{$i}]\"" |
577
|
|
|
. (isset($_REQUEST['uniquekey'][$i]) ? ' checked="checked"' : '') . " /></td>\n"; |
578
|
|
|
echo "\t\t<td style=\"text-align: center\"><input type=\"checkbox\" name=\"primarykey[{$i}]\" " |
579
|
|
|
. (isset($_REQUEST['primarykey'][$i]) ? ' checked="checked"' : '') |
580
|
|
|
. " /></td>\n"; |
581
|
|
|
echo "\t\t<td><input name=\"default[{$i}]\" size=\"20\" value=\"", |
582
|
|
|
htmlspecialchars($_REQUEST['default'][$i]), "\" /></td>\n"; |
583
|
|
|
echo "\t\t<td><input name=\"colcomment[{$i}]\" size=\"40\" value=\"", |
584
|
|
|
htmlspecialchars($_REQUEST['colcomment'][$i]), "\" /> |
585
|
|
|
<script type=\"text/javascript\">checkLengths(document.getElementById('types{$i}').value,{$i});</script> |
586
|
|
|
</td>\n\t</tr>\n"; |
587
|
|
|
} |
588
|
|
|
echo "</table>\n"; |
589
|
|
|
echo "<p><input type=\"hidden\" name=\"action\" value=\"create\" />\n"; |
590
|
|
|
echo "<input type=\"hidden\" name=\"stage\" value=\"3\" />\n"; |
591
|
|
|
echo $misc->form; |
592
|
|
|
echo '<input type="hidden" name="name" value="', htmlspecialchars($_REQUEST['name']), "\" />\n"; |
593
|
|
|
echo '<input type="hidden" name="fields" value="', htmlspecialchars($_REQUEST['fields']), "\" />\n"; |
594
|
|
|
if (isset($_REQUEST['withoutoids'])) { |
595
|
|
|
echo "<input type=\"hidden\" name=\"withoutoids\" value=\"true\" />\n"; |
596
|
|
|
} |
597
|
|
|
echo '<input type="hidden" name="tblcomment" value="', htmlspecialchars($_REQUEST['tblcomment']), "\" />\n"; |
598
|
|
|
if (isset($_REQUEST['spcname'])) { |
599
|
|
|
echo '<input type="hidden" name="spcname" value="', htmlspecialchars($_REQUEST['spcname']), "\" />\n"; |
600
|
|
|
} |
601
|
|
|
echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n"; |
602
|
|
|
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
603
|
|
|
echo "</form>\n"; |
604
|
|
|
|
605
|
|
|
break; |
606
|
|
|
case 3: |
607
|
|
|
|
608
|
|
|
if (!isset($_REQUEST['notnull'])) { |
609
|
|
|
$_REQUEST['notnull'] = []; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
if (!isset($_REQUEST['uniquekey'])) { |
613
|
|
|
$_REQUEST['uniquekey'] = []; |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
if (!isset($_REQUEST['primarykey'])) { |
617
|
|
|
$_REQUEST['primarykey'] = []; |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
if (!isset($_REQUEST['length'])) { |
621
|
|
|
$_REQUEST['length'] = []; |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
// Default tablespace to null if it isn't set |
625
|
|
|
if (!isset($_REQUEST['spcname'])) { |
626
|
|
|
$_REQUEST['spcname'] = null; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
// Check inputs |
630
|
|
|
$fields = trim($_REQUEST['fields']); |
631
|
|
View Code Duplication |
if (trim($_REQUEST['name']) == '') { |
|
|
|
|
632
|
|
|
$_REQUEST['stage'] = 1; |
633
|
|
|
$this->doCreate($lang['strtableneedsname']); |
634
|
|
|
return; |
635
|
|
|
} elseif ($fields == '' || !is_numeric($fields) || $fields != (int) $fields || $fields <= 0) { |
636
|
|
|
$_REQUEST['stage'] = 1; |
637
|
|
|
$this->doCreate($lang['strtableneedscols']); |
638
|
|
|
return; |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
$status = $data->createTable($_REQUEST['name'], $_REQUEST['fields'], $_REQUEST['field'], |
642
|
|
|
$_REQUEST['type'], $_REQUEST['array'], $_REQUEST['length'], $_REQUEST['notnull'], $_REQUEST['default'], |
643
|
|
|
isset($_REQUEST['withoutoids']), $_REQUEST['colcomment'], $_REQUEST['tblcomment'], $_REQUEST['spcname'], |
644
|
|
|
$_REQUEST['uniquekey'], $_REQUEST['primarykey']); |
645
|
|
|
|
646
|
|
|
if ($status == 0) { |
647
|
|
|
$misc->setReloadBrowser(true); |
648
|
|
|
return $this->doDefault($lang['strtablecreated']); |
649
|
|
|
} elseif ($status == -1) { |
650
|
|
|
$_REQUEST['stage'] = 2; |
651
|
|
|
$this->doCreate($lang['strtableneedsfield']); |
652
|
|
|
return; |
653
|
|
|
} else { |
654
|
|
|
$_REQUEST['stage'] = 2; |
655
|
|
|
$this->doCreate($lang['strtablecreatedbad']); |
656
|
|
|
return; |
657
|
|
|
} |
658
|
|
|
break; |
659
|
|
|
default: |
660
|
|
|
echo "<p>{$lang['strinvalidparam']}</p>\n"; |
661
|
|
|
} |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
/** |
665
|
|
|
* Dsiplay a screen where user can create a table from an existing one. |
666
|
|
|
* We don't have to check if pg supports schema cause create table like |
667
|
|
|
* is available under pg 7.4+ which has schema. |
668
|
|
|
*/ |
669
|
|
|
public function doCreateLike($confirm, $msg = '') |
|
|
|
|
670
|
|
|
{ |
671
|
|
|
$conf = $this->conf; |
672
|
|
|
$misc = $this->misc; |
673
|
|
|
$lang = $this->lang; |
674
|
|
|
$data = $misc->getDatabaseAccessor(); |
675
|
|
|
|
676
|
|
|
if (!$confirm) { |
677
|
|
|
if (!isset($_REQUEST['name'])) { |
678
|
|
|
$_REQUEST['name'] = ''; |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
if (!isset($_REQUEST['like'])) { |
682
|
|
|
$_REQUEST['like'] = ''; |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
if (!isset($_REQUEST['tablespace'])) { |
686
|
|
|
$_REQUEST['tablespace'] = ''; |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
$this->printTrail('schema'); |
690
|
|
|
$this->printTitle($lang['strcreatetable'], 'pg.table.create'); |
691
|
|
|
$this->printMsg($msg); |
692
|
|
|
|
693
|
|
|
$tbltmp = $data->getTables(true); |
694
|
|
|
$tbltmp = $tbltmp->getArray(); |
695
|
|
|
|
696
|
|
|
$tables = []; |
697
|
|
|
$tblsel = ''; |
698
|
|
|
foreach ($tbltmp as $a) { |
699
|
|
|
$data->fieldClean($a['nspname']); |
700
|
|
|
$data->fieldClean($a['relname']); |
701
|
|
|
$tables["\"{$a['nspname']}\".\"{$a['relname']}\""] = serialize(['schema' => $a['nspname'], 'table' => $a['relname']]); |
702
|
|
|
if ($_REQUEST['like'] == $tables["\"{$a['nspname']}\".\"{$a['relname']}\""]) { |
703
|
|
|
$tblsel = htmlspecialchars($tables["\"{$a['nspname']}\".\"{$a['relname']}\""]); |
704
|
|
|
} |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
unset($tbltmp); |
708
|
|
|
|
709
|
|
|
echo '<form action="' . SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n"; |
710
|
|
|
echo "<table>\n\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
711
|
|
|
echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n"; |
712
|
|
|
echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strcreatetablelikeparent']}</th>\n"; |
713
|
|
|
echo "\t\t<td class=\"data\">"; |
714
|
|
|
echo \PHPPgAdmin\XHtml\HTMLController::printCombo($tables, 'like', true, $tblsel, false); |
715
|
|
|
echo "</td>\n\t</tr>\n"; |
716
|
|
|
if ($data->hasTablespaces()) { |
717
|
|
|
$tblsp_ = $data->getTablespaces(); |
718
|
|
|
if ($tblsp_->recordCount() > 0) { |
719
|
|
|
$tblsp_ = $tblsp_->getArray(); |
720
|
|
|
$tblsp = []; |
721
|
|
|
foreach ($tblsp_ as $a) { |
722
|
|
|
$tblsp[$a['spcname']] = $a['spcname']; |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n"; |
726
|
|
|
echo "\t\t<td class=\"data\">"; |
727
|
|
|
echo \PHPPgAdmin\XHtml\HTMLController::printCombo($tblsp, 'tablespace', true, $_REQUEST['tablespace'], false); |
728
|
|
|
echo "</td>\n\t</tr>\n"; |
729
|
|
|
} |
730
|
|
|
} |
731
|
|
|
echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['stroptions']}</th>\n\t\t<td class=\"data\">"; |
732
|
|
|
echo '<label for="withdefaults"><input type="checkbox" id="withdefaults" name="withdefaults"', |
733
|
|
|
isset($_REQUEST['withdefaults']) ? ' checked="checked"' : '', |
734
|
|
|
"/>{$lang['strcreatelikewithdefaults']}</label>"; |
735
|
|
|
if ($data->hasCreateTableLikeWithConstraints()) { |
736
|
|
|
echo '<br /><label for="withconstraints"><input type="checkbox" id="withconstraints" name="withconstraints"', |
737
|
|
|
isset($_REQUEST['withconstraints']) ? ' checked="checked"' : '', |
738
|
|
|
"/>{$lang['strcreatelikewithconstraints']}</label>"; |
739
|
|
|
} |
740
|
|
|
if ($data->hasCreateTableLikeWithIndexes()) { |
741
|
|
|
echo '<br /><label for="withindexes"><input type="checkbox" id="withindexes" name="withindexes"', |
742
|
|
|
isset($_REQUEST['withindexes']) ? ' checked="checked"' : '', |
743
|
|
|
"/>{$lang['strcreatelikewithindexes']}</label>"; |
744
|
|
|
} |
745
|
|
|
echo "</td>\n\t</tr>\n"; |
746
|
|
|
echo '</table>'; |
747
|
|
|
|
748
|
|
|
echo "<input type=\"hidden\" name=\"action\" value=\"confcreatelike\" />\n"; |
749
|
|
|
echo $misc->form; |
750
|
|
|
echo "<p><input type=\"submit\" value=\"{$lang['strcreate']}\" />\n"; |
751
|
|
|
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
752
|
|
|
echo "</form>\n"; |
753
|
|
|
} else { |
754
|
|
|
if (trim($_REQUEST['name']) == '') { |
755
|
|
|
$this->doCreateLike(false, $lang['strtableneedsname']); |
756
|
|
|
return; |
757
|
|
|
} |
758
|
|
|
if (trim($_REQUEST['like']) == '') { |
759
|
|
|
$this->doCreateLike(false, $lang['strtablelikeneedslike']); |
760
|
|
|
return; |
761
|
|
|
} |
762
|
|
|
|
763
|
|
|
if (!isset($_REQUEST['tablespace'])) { |
764
|
|
|
$_REQUEST['tablespace'] = ''; |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
$status = $data->createTableLike($_REQUEST['name'], unserialize($_REQUEST['like']), isset($_REQUEST['withdefaults']), |
|
|
|
|
768
|
|
|
isset($_REQUEST['withconstraints']), isset($_REQUEST['withindexes']), $_REQUEST['tablespace']); |
769
|
|
|
|
770
|
|
|
if ($status == 0) { |
771
|
|
|
$misc->setReloadBrowser(true); |
772
|
|
|
return $this->doDefault($lang['strtablecreated']); |
773
|
|
|
} else { |
774
|
|
|
$this->doCreateLike(false, $lang['strtablecreatedbad']); |
775
|
|
|
return; |
776
|
|
|
} |
777
|
|
|
} |
778
|
|
|
} |
779
|
|
|
|
780
|
|
|
/** |
781
|
|
|
* Ask for select parameters and perform select |
782
|
|
|
*/ |
783
|
|
|
public function doSelectRows($confirm, $msg = '') |
|
|
|
|
784
|
|
|
{ |
785
|
|
|
$conf = $this->conf; |
786
|
|
|
$misc = $this->misc; |
787
|
|
|
$lang = $this->lang; |
788
|
|
|
$data = $misc->getDatabaseAccessor(); |
789
|
|
|
|
790
|
|
|
if ($confirm) { |
791
|
|
|
$this->printTrail('table'); |
792
|
|
|
$this->printTabs('table', 'select'); |
793
|
|
|
$this->printMsg($msg); |
794
|
|
|
|
795
|
|
|
$attrs = $data->getTableAttributes($_REQUEST['table']); |
796
|
|
|
|
797
|
|
|
echo '<form action="' . SUBFOLDER . "/src/views/tables.php\" method=\"post\" id=\"selectform\">\n"; |
798
|
|
|
if ($attrs->recordCount() > 0) { |
799
|
|
|
// JavaScript for select all feature |
800
|
|
|
echo "<script type=\"text/javascript\">\n"; |
801
|
|
|
echo "//<![CDATA[\n"; |
802
|
|
|
echo " function selectAll() {\n"; |
803
|
|
|
echo " for (var i=0; i<document.getElementById('selectform').elements.length; i++) {\n"; |
804
|
|
|
echo " var e = document.getElementById('selectform').elements[i];\n"; |
805
|
|
|
echo " if (e.name.indexOf('show') == 0) e.checked = document.getElementById('selectform').selectall.checked;\n"; |
806
|
|
|
echo " }\n"; |
807
|
|
|
echo " }\n"; |
808
|
|
|
echo "//]]>\n"; |
809
|
|
|
echo "</script>\n"; |
810
|
|
|
|
811
|
|
|
echo "<table>\n"; |
812
|
|
|
|
813
|
|
|
// Output table header |
814
|
|
|
echo "<tr><th class=\"data\">{$lang['strshow']}</th><th class=\"data\">{$lang['strcolumn']}</th>"; |
815
|
|
|
echo "<th class=\"data\">{$lang['strtype']}</th><th class=\"data\">{$lang['stroperator']}</th>"; |
816
|
|
|
echo "<th class=\"data\">{$lang['strvalue']}</th></tr>"; |
817
|
|
|
|
818
|
|
|
$i = 0; |
819
|
|
|
while (!$attrs->EOF) { |
820
|
|
|
$attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']); |
821
|
|
|
// Set up default value if there isn't one already |
822
|
|
|
if (!isset($_REQUEST['values'][$attrs->fields['attname']])) { |
823
|
|
|
$_REQUEST['values'][$attrs->fields['attname']] = null; |
824
|
|
|
} |
825
|
|
|
|
826
|
|
|
if (!isset($_REQUEST['ops'][$attrs->fields['attname']])) { |
827
|
|
|
$_REQUEST['ops'][$attrs->fields['attname']] = null; |
828
|
|
|
} |
829
|
|
|
|
830
|
|
|
// Continue drawing row |
831
|
|
|
$id = (($i % 2) == 0 ? '1' : '2'); |
832
|
|
|
echo "<tr class=\"data{$id}\">\n"; |
833
|
|
|
echo '<td style="white-space:nowrap;">'; |
834
|
|
|
echo '<input type="checkbox" name="show[', htmlspecialchars($attrs->fields['attname']), ']"', |
835
|
|
|
isset($_REQUEST['show'][$attrs->fields['attname']]) ? ' checked="checked"' : '', ' /></td>'; |
836
|
|
|
echo '<td style="white-space:nowrap;">', $misc->printVal($attrs->fields['attname']), '</td>'; |
837
|
|
|
echo '<td style="white-space:nowrap;">', $misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])), '</td>'; |
838
|
|
|
echo '<td style="white-space:nowrap;">'; |
839
|
|
|
echo "<select name=\"ops[{$attrs->fields['attname']}]\">\n"; |
840
|
|
|
foreach (array_keys($data->selectOps) as $v) { |
841
|
|
|
echo '<option value="', htmlspecialchars($v), '"', ($v == $_REQUEST['ops'][$attrs->fields['attname']]) ? ' selected="selected"' : '', |
842
|
|
|
'>', htmlspecialchars($v), "</option>\n"; |
843
|
|
|
} |
844
|
|
|
echo "</select>\n</td>\n"; |
845
|
|
|
echo '<td style="white-space:nowrap;">', $data->printField("values[{$attrs->fields['attname']}]", |
846
|
|
|
$_REQUEST['values'][$attrs->fields['attname']], $attrs->fields['type']), '</td>'; |
847
|
|
|
echo "</tr>\n"; |
848
|
|
|
$i++; |
849
|
|
|
$attrs->moveNext(); |
850
|
|
|
} |
851
|
|
|
// Select all checkbox |
852
|
|
|
echo "<tr><td colspan=\"5\"><input type=\"checkbox\" id=\"selectall\" name=\"selectall\" accesskey=\"a\" onclick=\"javascript:selectAll()\" /><label for=\"selectall\">{$lang['strselectallfields']}</label></td>"; |
853
|
|
|
echo "</tr></table>\n"; |
854
|
|
|
} else { |
855
|
|
|
echo "<p>{$lang['strinvalidparam']}</p>\n"; |
856
|
|
|
} |
857
|
|
|
|
858
|
|
|
echo "<p><input type=\"hidden\" name=\"action\" value=\"selectrows\" />\n"; |
859
|
|
|
echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
860
|
|
|
echo "<input type=\"hidden\" name=\"subject\" value=\"table\" />\n"; |
861
|
|
|
echo $misc->form; |
862
|
|
|
echo "<input type=\"submit\" name=\"select\" accesskey=\"r\" value=\"{$lang['strselect']}\" />\n"; |
863
|
|
|
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
864
|
|
|
echo "</form>\n"; |
865
|
|
|
|
866
|
|
|
return; |
867
|
|
|
} else { |
868
|
|
|
if (!isset($_POST['show'])) { |
869
|
|
|
$_POST['show'] = []; |
870
|
|
|
} |
871
|
|
|
|
872
|
|
|
if (!isset($_POST['values'])) { |
873
|
|
|
$_POST['values'] = []; |
874
|
|
|
} |
875
|
|
|
|
876
|
|
|
if (!isset($_POST['nulls'])) { |
877
|
|
|
$_POST['nulls'] = []; |
878
|
|
|
} |
879
|
|
|
|
880
|
|
|
// Verify that they haven't supplied a value for unary operators |
881
|
|
|
foreach ($_POST['ops'] as $k => $v) { |
882
|
|
|
if ($data->selectOps[$v] == 'p' && $_POST['values'][$k] != '') { |
883
|
|
|
$this->doSelectRows(true, $lang['strselectunary']); |
884
|
|
|
return; |
885
|
|
|
} |
886
|
|
|
} |
887
|
|
|
|
888
|
|
|
if (sizeof($_POST['show']) == 0) { |
889
|
|
|
$this->doSelectRows(true, $lang['strselectneedscol']); |
890
|
|
|
} else { |
891
|
|
|
// Generate query SQL |
892
|
|
|
$query = $data->getSelectSQL($_REQUEST['table'], array_keys($_POST['show']), |
893
|
|
|
$_POST['values'], $_POST['ops']); |
894
|
|
|
$_REQUEST['query'] = $query; |
895
|
|
|
$_REQUEST['return'] = 'selectrows'; |
896
|
|
|
|
897
|
|
|
$this->setNoOutput(true); |
898
|
|
|
|
899
|
|
|
$display_controller = new DisplayController($this->getContainer()); |
900
|
|
|
|
901
|
|
|
return $display_controller->render(); |
902
|
|
|
} |
903
|
|
|
} |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
/** |
907
|
|
|
* Ask for insert parameters and then actually insert row |
908
|
|
|
*/ |
909
|
|
|
public function doInsertRow($confirm, $msg = '') |
|
|
|
|
910
|
|
|
{ |
911
|
|
|
$conf = $this->conf; |
912
|
|
|
$misc = $this->misc; |
913
|
|
|
$lang = $this->lang; |
914
|
|
|
$data = $misc->getDatabaseAccessor(); |
915
|
|
|
|
916
|
|
|
if ($confirm) { |
917
|
|
|
$this->printTrail('table'); |
918
|
|
|
$this->printTabs('table', 'insert'); |
919
|
|
|
|
920
|
|
|
$this->printMsg($msg); |
921
|
|
|
|
922
|
|
|
$attrs = $data->getTableAttributes($_REQUEST['table']); |
923
|
|
|
|
924
|
|
View Code Duplication |
if (($conf['autocomplete'] != 'disable')) { |
|
|
|
|
925
|
|
|
$fksprops = $misc->getAutocompleteFKProperties($_REQUEST['table']); |
926
|
|
|
if ($fksprops !== false) { |
927
|
|
|
echo $fksprops['code']; |
928
|
|
|
} |
929
|
|
|
} else { |
930
|
|
|
$fksprops = false; |
931
|
|
|
} |
932
|
|
|
|
933
|
|
|
echo '<form action="' . SUBFOLDER . "/src/views/tables.php\" method=\"post\" id=\"ac_form\">\n"; |
934
|
|
|
if ($attrs->recordCount() > 0) { |
935
|
|
|
echo "<table>\n"; |
936
|
|
|
|
937
|
|
|
// Output table header |
938
|
|
|
echo "<tr><th class=\"data\">{$lang['strcolumn']}</th><th class=\"data\">{$lang['strtype']}</th>"; |
939
|
|
|
echo "<th class=\"data\">{$lang['strformat']}</th>"; |
940
|
|
|
echo "<th class=\"data\">{$lang['strnull']}</th><th class=\"data\">{$lang['strvalue']}</th></tr>"; |
941
|
|
|
|
942
|
|
|
$i = 0; |
943
|
|
|
$fields = []; |
944
|
|
|
while (!$attrs->EOF) { |
945
|
|
|
$fields[$attrs->fields['attnum']] = $attrs->fields['attname']; |
946
|
|
|
$attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']); |
947
|
|
|
// Set up default value if there isn't one already |
948
|
|
View Code Duplication |
if (!isset($_REQUEST['values'][$attrs->fields['attnum']])) { |
|
|
|
|
949
|
|
|
$_REQUEST['values'][$attrs->fields['attnum']] = $attrs->fields['adsrc']; |
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
// Default format to 'VALUE' if there is no default, |
953
|
|
|
// otherwise default to 'EXPRESSION' |
954
|
|
View Code Duplication |
if (!isset($_REQUEST['format'][$attrs->fields['attnum']])) { |
|
|
|
|
955
|
|
|
$_REQUEST['format'][$attrs->fields['attnum']] = ($attrs->fields['adsrc'] === null) ? 'VALUE' : 'EXPRESSION'; |
956
|
|
|
} |
957
|
|
|
|
958
|
|
|
// Continue drawing row |
959
|
|
|
$id = (($i % 2) == 0 ? '1' : '2'); |
960
|
|
|
echo "<tr class=\"data{$id}\">\n"; |
961
|
|
|
echo '<td style="white-space:nowrap;">', $misc->printVal($attrs->fields['attname']), '</td>'; |
962
|
|
|
echo "<td style=\"white-space:nowrap;\">\n"; |
963
|
|
|
echo $misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])); |
964
|
|
|
echo "<input type=\"hidden\" name=\"types[{$attrs->fields['attnum']}]\" value=\"", |
965
|
|
|
htmlspecialchars($attrs->fields['type']), '" /></td>'; |
966
|
|
|
echo "<td style=\"white-space:nowrap;\">\n"; |
967
|
|
|
echo "<select name=\"format[{$attrs->fields['attnum']}]\">\n"; |
968
|
|
|
echo '<option value="VALUE"', ($_REQUEST['format'][$attrs->fields['attnum']] == 'VALUE') ? ' selected="selected"' : '', ">{$lang['strvalue']}</option>\n"; |
969
|
|
|
echo '<option value="EXPRESSION"', ($_REQUEST['format'][$attrs->fields['attnum']] == 'EXPRESSION') ? ' selected="selected"' : '', ">{$lang['strexpression']}</option>\n"; |
970
|
|
|
echo "</select>\n</td>\n"; |
971
|
|
|
echo '<td style="white-space:nowrap;">'; |
972
|
|
|
// Output null box if the column allows nulls (doesn't look at CHECKs or ASSERTIONS) |
973
|
|
|
if (!$attrs->fields['attnotnull']) { |
974
|
|
|
echo "<label><span><input type=\"checkbox\" name=\"nulls[{$attrs->fields['attnum']}]\"", |
975
|
|
|
isset($_REQUEST['nulls'][$attrs->fields['attnum']]) ? ' checked="checked"' : '', ' /></span></label></td>'; |
976
|
|
|
} else { |
977
|
|
|
echo ' </td>'; |
978
|
|
|
} |
979
|
|
|
echo "<td id=\"row_att_{$attrs->fields['attnum']}\" style=\"white-space:nowrap;\">"; |
980
|
|
|
if (($fksprops !== false) && isset($fksprops['byfield'][$attrs->fields['attnum']])) { |
981
|
|
|
echo $data->printField("values[{$attrs->fields['attnum']}]", $_REQUEST['values'][$attrs->fields['attnum']], 'fktype' /*force FK*/, |
982
|
|
|
[ |
983
|
|
|
'id' => "attr_{$attrs->fields['attnum']}", |
984
|
|
|
'autocomplete' => 'off', |
985
|
|
|
] |
986
|
|
|
); |
987
|
|
|
} else { |
988
|
|
|
echo $data->printField("values[{$attrs->fields['attnum']}]", $_REQUEST['values'][$attrs->fields['attnum']], $attrs->fields['type']); |
989
|
|
|
} |
990
|
|
|
echo "</td>\n"; |
991
|
|
|
echo "</tr>\n"; |
992
|
|
|
$i++; |
993
|
|
|
$attrs->moveNext(); |
994
|
|
|
} |
995
|
|
|
echo "</table>\n"; |
996
|
|
|
|
997
|
|
|
if (!isset($_SESSION['counter'])) { |
998
|
|
|
$_SESSION['counter'] = 0; |
999
|
|
|
} |
1000
|
|
|
|
1001
|
|
|
echo "<input type=\"hidden\" name=\"action\" value=\"insertrow\" />\n"; |
1002
|
|
|
echo '<input type="hidden" name="fields" value="', htmlentities(serialize($fields), ENT_QUOTES, 'UTF-8'), "\" />\n"; |
1003
|
|
|
echo '<input type="hidden" name="protection_counter" value="' . $_SESSION['counter'] . "\" />\n"; |
1004
|
|
|
echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
1005
|
|
|
echo "<p><input type=\"submit\" name=\"insert\" value=\"{$lang['strinsert']}\" />\n"; |
1006
|
|
|
echo "<input type=\"submit\" name=\"insertandrepeat\" accesskey=\"r\" value=\"{$lang['strinsertandrepeat']}\" />\n"; |
1007
|
|
|
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n"; |
1008
|
|
|
|
1009
|
|
View Code Duplication |
if ($fksprops !== false) { |
|
|
|
|
1010
|
|
|
if ($conf['autocomplete'] != 'default off') { |
1011
|
|
|
echo "<input type=\"checkbox\" id=\"no_ac\" value=\"1\" checked=\"checked\" /><label for=\"no_ac\">{$lang['strac']}</label>\n"; |
1012
|
|
|
} else { |
1013
|
|
|
echo "<input type=\"checkbox\" id=\"no_ac\" value=\"0\" /><label for=\"no_ac\">{$lang['strac']}</label>\n"; |
1014
|
|
|
} |
1015
|
|
|
} |
1016
|
|
|
echo "</p>\n"; |
1017
|
|
|
} else { |
1018
|
|
|
echo "<p>{$lang['strnofieldsforinsert']}</p>\n"; |
1019
|
|
|
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n"; |
1020
|
|
|
} |
1021
|
|
|
echo $misc->form; |
1022
|
|
|
echo "</form>\n"; |
1023
|
|
|
} else { |
1024
|
|
|
if (!isset($_POST['values'])) { |
1025
|
|
|
$_POST['values'] = []; |
1026
|
|
|
} |
1027
|
|
|
|
1028
|
|
|
if (!isset($_POST['nulls'])) { |
1029
|
|
|
$_POST['nulls'] = []; |
1030
|
|
|
} |
1031
|
|
|
|
1032
|
|
|
$_POST['fields'] = unserialize(htmlspecialchars_decode($_POST['fields'], ENT_QUOTES)); |
|
|
|
|
1033
|
|
|
|
1034
|
|
|
if ($_SESSION['counter']++ == $_POST['protection_counter']) { |
1035
|
|
|
$status = $data->insertRow($_POST['table'], $_POST['fields'], $_POST['values'], $_POST['nulls'], $_POST['format'], $_POST['types']); |
1036
|
|
|
if ($status == 0) { |
1037
|
|
|
if (isset($_POST['insert'])) { |
1038
|
|
|
return $this->doDefault($lang['strrowinserted']); |
1039
|
|
|
} else { |
1040
|
|
|
$_REQUEST['values'] = []; |
1041
|
|
|
$_REQUEST['nulls'] = []; |
1042
|
|
|
$this->doInsertRow(true, $lang['strrowinserted']); |
1043
|
|
|
} |
1044
|
|
|
} else { |
1045
|
|
|
$this->doInsertRow(true, $lang['strrowinsertedbad']); |
1046
|
|
|
} |
1047
|
|
|
} else { |
1048
|
|
|
$this->doInsertRow(true, $lang['strrowduplicate']); |
1049
|
|
|
} |
1050
|
|
|
} |
1051
|
|
|
} |
1052
|
|
|
|
1053
|
|
|
/** |
1054
|
|
|
* Show confirmation of empty and perform actual empty |
1055
|
|
|
*/ |
1056
|
|
|
public function doEmpty($confirm) |
|
|
|
|
1057
|
|
|
{ |
1058
|
|
|
$conf = $this->conf; |
1059
|
|
|
$misc = $this->misc; |
1060
|
|
|
$lang = $this->lang; |
1061
|
|
|
$data = $misc->getDatabaseAccessor(); |
1062
|
|
|
|
1063
|
|
View Code Duplication |
if (empty($_REQUEST['table']) && empty($_REQUEST['ma'])) { |
|
|
|
|
1064
|
|
|
return $this->doDefault($lang['strspecifytabletoempty']); |
1065
|
|
|
exit(); |
|
|
|
|
1066
|
|
|
} |
1067
|
|
|
|
1068
|
|
|
if ($confirm) { |
1069
|
|
View Code Duplication |
if (isset($_REQUEST['ma'])) { |
|
|
|
|
1070
|
|
|
$this->printTrail('schema'); |
1071
|
|
|
$this->printTitle($lang['strempty'], 'pg.table.empty'); |
1072
|
|
|
|
1073
|
|
|
echo '<form action="' . SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n"; |
1074
|
|
|
foreach ($_REQUEST['ma'] as $v) { |
1075
|
|
|
$a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES)); |
|
|
|
|
1076
|
|
|
echo '<p>', sprintf($lang['strconfemptytable'], $misc->printVal($a['table'])), "</p>\n"; |
1077
|
|
|
printf('<input type="hidden" name="table[]" value="%s" />', htmlspecialchars($a['table'])); |
1078
|
|
|
} |
1079
|
|
|
} // END mutli empty |
1080
|
|
|
else { |
1081
|
|
|
$this->printTrail('table'); |
1082
|
|
|
$this->printTitle($lang['strempty'], 'pg.table.empty'); |
1083
|
|
|
|
1084
|
|
|
echo '<p>', sprintf($lang['strconfemptytable'], $misc->printVal($_REQUEST['table'])), "</p>\n"; |
1085
|
|
|
|
1086
|
|
|
echo '<form action="' . SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n"; |
1087
|
|
|
echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
1088
|
|
|
} // END not mutli empty |
1089
|
|
|
|
1090
|
|
|
echo "<input type=\"hidden\" name=\"action\" value=\"empty\" />\n"; |
1091
|
|
|
echo $misc->form; |
1092
|
|
|
echo "<input type=\"submit\" name=\"empty\" value=\"{$lang['strempty']}\" /> <input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n"; |
1093
|
|
|
echo "</form>\n"; |
1094
|
|
|
} // END if confirm |
1095
|
|
|
else { |
1096
|
|
|
// Do Empty |
1097
|
|
|
if (is_array($_REQUEST['table'])) { |
1098
|
|
|
$msg = ''; |
1099
|
|
|
foreach ($_REQUEST['table'] as $t) { |
1100
|
|
|
$status = $data->emptyTable($t); |
1101
|
|
|
if ($status == 0) { |
1102
|
|
|
$msg .= sprintf('%s: %s<br />', htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtableemptied']); |
1103
|
|
|
} else { |
1104
|
|
|
$this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtableemptiedbad'])); |
1105
|
|
|
return; |
1106
|
|
|
} |
1107
|
|
|
} |
1108
|
|
|
$this->doDefault($msg); |
1109
|
|
|
} // END mutli empty |
1110
|
|
|
else { |
1111
|
|
|
$status = $data->emptyTable($_POST['table']); |
1112
|
|
|
if ($status == 0) { |
1113
|
|
|
return $this->doDefault($lang['strtableemptied']); |
1114
|
|
|
} else { |
1115
|
|
|
return $this->doDefault($lang['strtableemptiedbad']); |
1116
|
|
|
} |
1117
|
|
|
} // END not mutli empty |
1118
|
|
|
} // END do Empty |
1119
|
|
|
} |
1120
|
|
|
|
1121
|
|
|
/** |
1122
|
|
|
* Show confirmation of drop and perform actual drop |
1123
|
|
|
*/ |
1124
|
|
|
public function doDrop($confirm) |
|
|
|
|
1125
|
|
|
{ |
1126
|
|
|
$conf = $this->conf; |
1127
|
|
|
$misc = $this->misc; |
1128
|
|
|
$lang = $this->lang; |
1129
|
|
|
$data = $misc->getDatabaseAccessor(); |
1130
|
|
|
|
1131
|
|
View Code Duplication |
if (empty($_REQUEST['table']) && empty($_REQUEST['ma'])) { |
|
|
|
|
1132
|
|
|
return $this->doDefault($lang['strspecifytabletodrop']); |
1133
|
|
|
exit(); |
|
|
|
|
1134
|
|
|
} |
1135
|
|
|
|
1136
|
|
|
if ($confirm) { |
1137
|
|
|
//If multi drop |
1138
|
|
View Code Duplication |
if (isset($_REQUEST['ma'])) { |
|
|
|
|
1139
|
|
|
$this->printTrail('schema'); |
1140
|
|
|
$this->printTitle($lang['strdrop'], 'pg.table.drop'); |
1141
|
|
|
|
1142
|
|
|
echo '<form action="' . SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n"; |
1143
|
|
|
foreach ($_REQUEST['ma'] as $v) { |
1144
|
|
|
$a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES)); |
|
|
|
|
1145
|
|
|
echo '<p>', sprintf($lang['strconfdroptable'], $misc->printVal($a['table'])), "</p>\n"; |
1146
|
|
|
printf('<input type="hidden" name="table[]" value="%s" />', htmlspecialchars($a['table'])); |
1147
|
|
|
} |
1148
|
|
|
} else { |
1149
|
|
|
$this->printTrail('table'); |
1150
|
|
|
$this->printTitle($lang['strdrop'], 'pg.table.drop'); |
1151
|
|
|
|
1152
|
|
|
echo '<p>', sprintf($lang['strconfdroptable'], $misc->printVal($_REQUEST['table'])), "</p>\n"; |
1153
|
|
|
|
1154
|
|
|
echo '<form action="' . SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n"; |
1155
|
|
|
echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
1156
|
|
|
} // END if multi drop |
1157
|
|
|
|
1158
|
|
|
echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n"; |
1159
|
|
|
echo $misc->form; |
1160
|
|
|
echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n"; |
1161
|
|
|
echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n"; |
1162
|
|
|
echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n"; |
1163
|
|
|
echo "</form>\n"; |
1164
|
|
|
} // END confirm |
1165
|
|
|
else { |
1166
|
|
|
//If multi drop |
1167
|
|
|
if (is_array($_REQUEST['table'])) { |
1168
|
|
|
$msg = ''; |
1169
|
|
|
$status = $data->beginTransaction(); |
1170
|
|
|
if ($status == 0) { |
1171
|
|
|
foreach ($_REQUEST['table'] as $t) { |
1172
|
|
|
$status = $data->dropTable($t, isset($_POST['cascade'])); |
1173
|
|
|
if ($status == 0) { |
1174
|
|
|
$msg .= sprintf('%s: %s<br />', htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtabledropped']); |
1175
|
|
|
} else { |
1176
|
|
|
$data->endTransaction(); |
1177
|
|
|
return $this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtabledroppedbad'])); |
1178
|
|
|
return; |
|
|
|
|
1179
|
|
|
} |
1180
|
|
|
} |
1181
|
|
|
} |
1182
|
|
|
if ($data->endTransaction() == 0) { |
1183
|
|
|
// Everything went fine, back to the Default page.... |
1184
|
|
|
$misc->setReloadBrowser(true); |
1185
|
|
|
return $this->doDefault($msg); |
1186
|
|
|
} else { |
1187
|
|
|
return $this->doDefault($lang['strtabledroppedbad']); |
1188
|
|
|
} |
1189
|
|
|
} else { |
1190
|
|
|
$status = $data->dropTable($_POST['table'], isset($_POST['cascade'])); |
1191
|
|
|
if ($status == 0) { |
1192
|
|
|
$misc->setReloadBrowser(true); |
1193
|
|
|
return $this->doDefault($lang['strtabledropped']); |
1194
|
|
|
} else { |
1195
|
|
|
return $this->doDefault($lang['strtabledroppedbad']); |
1196
|
|
|
} |
1197
|
|
|
} |
1198
|
|
|
} // END DROP |
1199
|
|
|
} |
1200
|
|
|
|
1201
|
|
|
// END Function |
1202
|
|
|
} |
1203
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: