Repository navigation
Expand file tree
/
Copy pathcasts.php
More file actions
413 lines (372 loc) · 10.9 KB
/
Copy pathcasts.php
File metadata and controls
413 lines (372 loc) · 10.9 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
use PhpPgAdmin\Core\AppContainer;
use PhpPgAdmin\Database\Actions\CastActions;
use PhpPgAdmin\Database\Actions\TypeActions;
/**
* Manage casts in a database
*
* $Id: casts.php,v 1.16 2007/09/25 16:08:05 ioguix Exp $
*/
// Include application functions
include_once('./libraries/bootstrap.php');
/**
* Show default list of casts in the database
*/
function doDefault($msg = '')
{
$pg = AppContainer::getPostgres();
$misc = AppContainer::getMisc();
$lang = AppContainer::getLang();
$castActions = new CastActions($pg);
$renderCastContext = function ($val) use ($lang) {
switch ($val) {
case 'e':
return $lang['strno'];
case 'a':
return $lang['strinassignment'];
default:
return $lang['stryes'];
}
};
$misc->printTrail('database');
$misc->printTabs('database', 'casts');
$misc->printMsg($msg);
$casts = $castActions->getCasts();
$columns = [
'source_type' => [
'title' => $lang['strsourcetype'],
'field' => field('castsource'),
'icon' => $misc->icon('Cast'),
],
'target_type' => [
'title' => $lang['strtargettype'],
'field' => field('casttarget'),
],
'function' => [
'title' => $lang['strfunction'],
'field' => field('castfunc'),
'params' => ['null' => $lang['strbinarycompat']],
],
'implicit' => [
'title' => $lang['strimplicit'],
'field' => field('castcontext'),
'type' => 'callback',
'params' => ['function' => $renderCastContext, 'align' => 'center'],
],
'actions' => [
'title' => $lang['stractions'],
],
'comment' => [
'title' => $lang['strcomment'],
'field' => field('castcomment'),
],
];
$footer = [
'source_type' => [
'agg' => 'count',
'format' => fn($v) => "$v {$lang['strcasts']}",
'colspan' => 2,
],
'function' => [
'text' => $lang['strtotal'],
'colspan' => 2,
],
];
$actions = [
'drop' => [
'icon' => $misc->icon('Delete'),
'content' => $lang['strdrop'],
'attr' => [
'href' => [
'url' => 'casts.php',
'urlvars' => [
'action' => 'confirm_drop',
'castsource' => field('castsource'),
'casttarget' => field('casttarget'),
'castsourceoid' => field('castsourceoid'),
'casttargetoid' => field('casttargetoid'),
],
],
],
],
];
$pre_fn = function ($tabledata, $actions) {
if (!($tabledata->fields['is_user_cast'] ?? false)) {
$actions['drop']['disable'] = true;
}
return $actions;
};
$misc->printTable(
$casts,
$columns,
$actions,
'casts-casts',
$lang['strnocasts'],
$pre_fn,
$footer
);
$misc->printNavLinks(
[
'create' => [
'attr' => [
'href' => [
'url' => 'casts.php',
'urlvars' => [
'server' => $_REQUEST['server'],
'database' => $_REQUEST['database'],
'action' => 'create',
],
],
],
'icon' => $misc->icon('CreateCast'),
'content' => $lang['strcreatecast'],
],
],
'casts-default',
get_defined_vars()
);
}
function doCreate($msg = '')
{
$pg = AppContainer::getPostgres();
$misc = AppContainer::getMisc();
$lang = AppContainer::getLang();
$typeActions = new TypeActions($pg);
$castActions = new CastActions($pg);
$_POST['castsourceoid'] = $_POST['castsourceoid'] ?? '';
$_POST['casttargetoid'] = $_POST['casttargetoid'] ?? '';
$_POST['method'] = $_POST['method'] ?? 'with_function';
$_POST['function_oid'] = $_POST['function_oid'] ?? '';
$_POST['castcontext'] = $_POST['castcontext'] ?? 'e';
$_POST['comment'] = $_POST['comment'] ?? '';
$misc->printTrail('database');
$misc->printTabs('database', 'casts');
$misc->printTitle($lang['strcreatecast']);
$misc->printMsg($msg);
$types = $typeActions->getTypes(true, false, true);
$typesArr = $types ? $types->getArray() : [];
$fnCandidates = $castActions->getCastFunctionCandidates();
$fnRows = $fnCandidates ? $fnCandidates->getArray() : [];
//var_dump($fnRows);
// Provide candidates to JavaScript (dynamic filtering)
?>
<script type="text/javascript">
window.phpPgAdminCastFunctions = <?php echo json_encode($fnRows, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>;
</script>
<script type="text/javascript" src="js/casts.js"></script>
<form action="casts.php" method="post">
<table>
<tr>
<th class="data left required"><?php echo $lang['strsourcetype']; ?></th>
<td class="data1">
<select name="castsourceoid" id="castsourceoid">
<option value="">(CHOOSE)</option>
<?php foreach ($typesArr as $t) {
$oid = (string) ($t['oid'] ?? '');
$label = htmlspecialchars($t['typname'] ?? '');
$sel = ($oid === (string) $_POST['castsourceoid']) ? ' selected' : '';
?>
<option value="<?php echo html_esc($oid); ?>" <?php echo $sel; ?>><?php echo $label; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<th class="data left required"><?php echo $lang['strtargettype']; ?></th>
<td class="data1">
<select name="casttargetoid" id="casttargetoid">
<option value="">(CHOOSE)</option>
<?php foreach ($typesArr as $t) {
$oid = (string) ($t['oid'] ?? '');
$label = htmlspecialchars($t['typname'] ?? '');
$sel = ($oid === (string) $_POST['casttargetoid']) ? ' selected' : '';
?>
<option value="<?php echo html_esc($oid); ?>" <?php echo $sel; ?>><?php echo $label; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<th class="data left required"><?php echo $lang['strcastmethod']; ?></th>
<td class="data1">
<?php
$method = $_POST['method'];
$checked = function ($v) use ($method) {
return ($method === $v) ? ' checked' : '';
};
?>
<label><input type="radio" name="method" value="with_function" <?php echo $checked('with_function'); ?> /> <?php echo $lang['strwithfunction']; ?></label><br />
<label><input type="radio" name="method" value="without_function" <?php echo $checked('without_function'); ?> /> <?php echo $lang['strwithoutfunction']; ?></label><br />
<label><input type="radio" name="method" value="with_inout" <?php echo $checked('with_inout'); ?> />
<?php echo $lang['strwithinout']; ?></label>
</td>
</tr>
<tr id="cast_function_row">
<th class="data left"><?php echo $lang['strfunction']; ?></th>
<td class="data1">
<select name="function_oid" id="function_oid"
data-selected="<?php echo html_esc((string) $_POST['function_oid']); ?>">
<option value="">(CHOOSE)</option>
</select>
</td>
</tr>
<tr>
<th class="data left"><?php echo $lang['strcastcontext']; ?></th>
<td class="data1">
<?php
$ctx = $_POST['castcontext'];
$ctxChecked = function ($v) use ($ctx) {
return ($ctx === $v) ? ' checked' : '';
};
?>
<label><input type="radio" name="castcontext" value="e" <?php echo $ctxChecked('e'); ?> />
<?php echo $lang['strexplicit']; ?></label><br />
<label><input type="radio" name="castcontext" value="a" <?php echo $ctxChecked('a'); ?> />
<?php echo $lang['strassignment']; ?></label><br />
<label><input type="radio" name="castcontext" value="i" <?php echo $ctxChecked('i'); ?> />
<?php echo $lang['strimplicit']; ?></label>
</td>
</tr>
<tr>
<th class="data left"><?php echo $lang['strcomment']; ?></th>
<td class="data1"><textarea name="comment" rows="3"
cols="40"><?php echo html_esc($_POST['comment']); ?></textarea></td>
</tr>
</table>
<p>
<input type="hidden" name="action" value="save_create" />
<?php echo $misc->form; ?>
<input type="submit" name="create" value="<?php echo $lang['strcreate']; ?>" />
<input type="submit" name="cancel" value="<?php echo $lang['strcancel']; ?>" />
</p>
</form>
<?php
}
function doSaveCreate()
{
//var_dump($_POST);
$pg = AppContainer::getPostgres();
$lang = AppContainer::getLang();
$castActions = new CastActions($pg);
$sourceOid = $_POST['castsourceoid'] ?? '';
$targetOid = $_POST['casttargetoid'] ?? '';
$method = $_POST['method'] ?? 'with_function';
$functionOid = $_POST['function_oid'] ?? '';
$context = $_POST['castcontext'] ?? 'e';
$comment = $_POST['comment'] ?? '';
if ($sourceOid === '' || $targetOid === '') {
doCreate($lang['strcastcreatedbad']);
return;
}
if ($method === 'with_function' && $functionOid === '') {
doCreate($lang['strcastcreatedbad']);
return;
}
$status = $castActions->createCast(
$sourceOid,
$targetOid,
$method,
($functionOid === '') ? null : $functionOid,
$context,
$comment
);
if ($status == 0) {
AppContainer::setShouldReloadTree(true);
doDefault($lang['strcastcreated']);
} else {
doCreate($lang['strcastcreatedbad']);
}
}
function doDrop($confirm)
{
$pg = AppContainer::getPostgres();
$misc = AppContainer::getMisc();
$lang = AppContainer::getLang();
$castActions = new CastActions($pg);
if ($confirm) {
$misc->printTrail('database');
$misc->printTabs('database', 'casts');
$misc->printTitle($lang['strdropcast']);
$castLabel = html_esc(($_REQUEST['castsource'] ?? '') . ' AS ' . ($_REQUEST['casttarget'] ?? ''));
?>
<p><?php echo sprintf($lang['strconfdropcast'], $castLabel); ?></p>
<form action="casts.php" method="post">
<p><input type="checkbox" id="cascade" name="cascade" /> <label
for="cascade"><?php echo $lang['strcascade']; ?></label></p>
<p><input type="hidden" name="action" value="drop" />
<input type="hidden" name="castsourceoid" value="<?php echo html_esc($_REQUEST['castsourceoid'] ?? ''); ?>" />
<input type="hidden" name="casttargetoid" value="<?php echo html_esc($_REQUEST['casttargetoid'] ?? ''); ?>" />
<?php echo $misc->form; ?>
<input type="submit" name="drop" value="<?php echo $lang['strdrop']; ?>" />
<input type="submit" name="cancel" value="<?php echo $lang['strcancel']; ?>" />
</p>
</form>
<?php
} else {
$status = $castActions->dropCast(
$_POST['castsourceoid'] ?? 0,
$_POST['casttargetoid'] ?? 0,
isset($_POST['cascade'])
);
if ($status == 0) {
AppContainer::setShouldReloadTree(true);
doDefault($lang['strcastdropped']);
} else {
doDefault($lang['strcastdroppedbad']);
}
}
}
/**
* Generate XML for the browser tree.
*/
function doTree()
{
$misc = AppContainer::getMisc();
$pg = AppContainer::getPostgres();
$castActions = new CastActions($pg);
$casts = $castActions->getCasts();
$proto = concat(field('castsource'), ' AS ', field('casttarget'));
$attrs = [
'text' => $proto,
'icon' => 'Cast'
];
$misc->printTree($casts, $attrs, 'casts');
exit;
}
// Main program
$misc = AppContainer::getMisc();
$lang = AppContainer::getLang();
$action = $_REQUEST['action'] ?? '';
if ($action == 'tree')
doTree();
$misc->printHeader($lang['strcasts']);
$misc->printBody();
switch ($action) {
case 'save_create':
if (isset($_POST['cancel'])) {
doDefault();
} else {
doSaveCreate();
}
break;
case 'create':
doCreate();
break;
case 'drop':
if (isset($_POST['cancel'])) {
doDefault();
} else {
doDrop(false);
}
break;
case 'confirm_drop':
doDrop(true);
break;
case 'tree':
doTree();
break;
default:
doDefault();
break;
}
$misc->printFooter();