Passed
Push — develop ( 3902ed...7e0f4a )
by Felipe
08:18
created

FunctionsController::buildJSData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 0
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.44
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class FunctionsController extends BaseController
17
{
18
    public $table_place = 'functions-functions';
19
20
    /**
21
     * Default method to render the controller according to the action parameter.
22
     */
23
    public function render()
24
    {
25
        if ('tree' == $this->action) {
26
            return $this->doTree();
27
        }
28
29
        $header_template = 'header_datatables.twig';
30
        $footer_template = 'footer.twig';
31
        ob_start();
32
        switch ($this->action) {
33
            case 'save_create':
34
                if (isset($_POST['cancel'])) {
35
                    $this->doDefault();
36
                } else {
37
                    $this->doSaveCreate();
38
                }
39
40
                break;
41
            case 'create':
42
                $header_template = 'header_select2.twig';
43
                $this->doCreate();
44
45
                break;
46
            case 'drop':
47
                if (isset($_POST['drop'])) {
48
                    $this->doDrop(false);
49
                } else {
50
                    $this->doDefault();
51
                }
52
53
                break;
54
            case 'confirm_drop':
55
                $this->doDrop(true);
56
57
                break;
58
            case 'save_edit':
59
                if (isset($_POST['cancel'])) {
60
                    $this->doDefault();
61
                } else {
62
                    $this->doSaveEdit();
63
                }
64
65
                break;
66
            case 'edit':
67
                $header_template = 'header_sqledit.twig';
68
                $footer_template = 'footer_sqledit.twig';
69
                $this->doEdit();
70
71
                break;
72
            case 'properties':
73
                $header_template = 'header_highlight.twig';
74
                $this->doProperties();
75
76
                break;
77
            default:
78
                $this->doDefault();
79
80
                break;
81
        }
82
        $output = ob_get_clean();
83
84
        $this->printHeader($this->lang['strfunctions'], null, true, $header_template);
85
        $this->printBody();
86
        echo $output;
87
        $this->printFooter(true, $footer_template);
88
    }
89
90
    /**
91
     * Show default list of functions in the database.
92
     *
93
     * @param mixed $msg
94
     */
95
    public function doDefault($msg = '')
96
    {
97
        $data = $this->misc->getDatabaseAccessor();
98
99
        $this->printTrail('schema');
100
        $this->printTabs('schema', 'functions');
101
        $this->printMsg($msg);
102
103
        $funcs = $data->getFunctions();
104
105
        $columns = [
106
            'function'     => [
107
                'title' => $this->lang['strfunction'],
108
                'field' => Decorator::field('proproto'),
109
                'url'   => \SUBFOLDER . "/redirect/function?action=properties&amp;{$this->misc->href}&amp;",
110
                'vars'  => ['function' => 'proproto', 'function_oid' => 'prooid'],
111
            ],
112
            'returns'      => [
113
                'title' => $this->lang['strreturns'],
114
                'field' => Decorator::field('proreturns'),
115
            ],
116
            'owner'        => [
117
                'title' => $this->lang['strowner'],
118
                'field' => Decorator::field('proowner'),
119
            ],
120
            'proglanguage' => [
121
                'title' => $this->lang['strproglanguage'],
122
                'field' => Decorator::field('prolanguage'),
123
            ],
124
            'actions'      => [
125
                'title' => $this->lang['stractions'],
126
            ],
127
            'comment'      => [
128
                'title' => $this->lang['strcomment'],
129
                'field' => Decorator::field('procomment'),
130
            ],
131
        ];
132
133
        $actions = [
134
            'multiactions' => [
135
                'keycols' => ['function' => 'proproto', 'function_oid' => 'prooid'],
136
                'url'     => 'functions',
137
            ],
138
            'alter'        => [
139
                'content' => $this->lang['stralter'],
140
                'attr'    => [
141
                    'href' => [
142
                        'url'     => 'functions',
143
                        'urlvars' => [
144
                            'action'       => 'edit',
145
                            'function'     => Decorator::field('proproto'),
146
                            'function_oid' => Decorator::field('prooid'),
147
                        ],
148
                    ],
149
                ],
150
            ],
151
            'drop'         => [
152
                'multiaction' => 'confirm_drop',
153
                'content'     => $this->lang['strdrop'],
154
                'attr'        => [
155
                    'href' => [
156
                        'url'     => 'functions',
157
                        'urlvars' => [
158
                            'action'       => 'confirm_drop',
159
                            'function'     => Decorator::field('proproto'),
160
                            'function_oid' => Decorator::field('prooid'),
161
                        ],
162
                    ],
163
                ],
164
            ],
165
            'privileges'   => [
166
                'content' => $this->lang['strprivileges'],
167
                'attr'    => [
168
                    'href' => [
169
                        'url'     => 'privileges',
170
                        'urlvars' => [
171
                            'subject'      => 'function',
172
                            'function'     => Decorator::field('proproto'),
173
                            'function_oid' => Decorator::field('prooid'),
174
                        ],
175
                    ],
176
                ],
177
            ],
178
        ];
179
180
        echo $this->printTable($funcs, $columns, $actions, $this->table_place, $this->lang['strnofunctions']);
181
182
        $this->_printNavLinks('functions-functions');
183
    }
184
185
    private function _printNavLinks($place, $func_full = '')
186
    {
187
188
        if ($place === 'functions-properties') {
189
            $navlinks = [
190
                'showall' => [
191
                    'attr'    => [
192
                        'href' => [
193
                            'url'     => 'functions',
194
                            'urlvars' => [
195
                                'server'   => $_REQUEST['server'],
196
                                'database' => $_REQUEST['database'],
197
                                'schema'   => $_REQUEST['schema'],
198
                            ],
199
                        ],
200
                    ],
201
                    'content' => $this->lang['strshowallfunctions'],
202
                ],
203
                'alter'   => [
204
                    'attr'    => [
205
                        'href' => [
206
                            'url'     => 'functions',
207
                            'urlvars' => [
208
                                'action'       => 'edit',
209
                                'server'       => $_REQUEST['server'],
210
                                'database'     => $_REQUEST['database'],
211
                                'schema'       => $_REQUEST['schema'],
212
                                'function'     => $_REQUEST['function'],
213
                                'function_oid' => $_REQUEST['function_oid'],
214
                            ],
215
                        ],
216
                    ],
217
                    'content' => $this->lang['stralter'],
218
                ],
219
                'drop'    => [
220
                    'attr'    => [
221
                        'href' => [
222
                            'url'     => 'functions',
223
                            'urlvars' => [
224
                                'action'       => 'confirm_drop',
225
                                'server'       => $_REQUEST['server'],
226
                                'database'     => $_REQUEST['database'],
227
                                'schema'       => $_REQUEST['schema'],
228
                                'function'     => $func_full,
229
                                'function_oid' => $_REQUEST['function_oid'],
230
                            ],
231
                        ],
232
                    ],
233
                    'content' => $this->lang['strdrop'],
234
                ],
235
            ];
236
        } else if ($place === 'functions-functions') {
237
            $navlinks = [
238
                'createpl'       => [
239
                    'attr'    => [
240
                        'href' => [
241
                            'url'     => 'functions',
242
                            'urlvars' => [
243
                                'action'   => 'create',
244
                                'server'   => $_REQUEST['server'],
245
                                'database' => $_REQUEST['database'],
246
                                'schema'   => $_REQUEST['schema'],
247
                            ],
248
                        ],
249
                    ],
250
                    'content' => $this->lang['strcreateplfunction'],
251
                ],
252
                'createinternal' => [
253
                    'attr'    => [
254
                        'href' => [
255
                            'url'     => 'functions',
256
                            'urlvars' => [
257
                                'action'   => 'create',
258
                                'language' => 'internal',
259
                                'server'   => $_REQUEST['server'],
260
                                'database' => $_REQUEST['database'],
261
                                'schema'   => $_REQUEST['schema'],
262
                            ],
263
                        ],
264
                    ],
265
                    'content' => $this->lang['strcreateinternalfunction'],
266
                ],
267
                'createc'        => [
268
                    'attr'    => [
269
                        'href' => [
270
                            'url'     => 'functions',
271
                            'urlvars' => [
272
                                'action'   => 'create',
273
                                'language' => 'C',
274
                                'server'   => $_REQUEST['server'],
275
                                'database' => $_REQUEST['database'],
276
                                'schema'   => $_REQUEST['schema'],
277
                            ],
278
                        ],
279
                    ],
280
                    'content' => $this->lang['strcreatecfunction'],
281
                ],
282
            ];
283
        }
284
285
        $this->printNavLinks($navlinks, $place, get_defined_vars());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $navlinks does not seem to be defined for all execution paths leading up to this point.
Loading history...
286
    }
287
288
    /**
289
     * Generate XML for the browser tree.
290
     */
291
    public function doTree()
292
    {
293
        $data = $this->misc->getDatabaseAccessor();
294
295
        $funcs = $data->getFunctions();
296
297
        $proto = Decorator::concat(Decorator::field('proname'), ' (', Decorator::field('proarguments'), ')');
298
299
        $reqvars = $this->misc->getRequestVars('function');
300
301
        $attrs = [
302
            'text'    => $proto,
303
            'icon'    => 'Function',
304
            'toolTip' => Decorator::field('procomment'),
305
            'action'  => Decorator::redirecturl(
306
                'redirect',
307
                $reqvars,
308
                [
309
                    'action'       => 'properties',
310
                    'function'     => $proto,
311
                    'function_oid' => Decorator::field('prooid'),
312
                ]
313
            ),
314
        ];
315
316
        return $this->printTree($funcs, $attrs, 'functions');
317
    }
318
319
    /**
320
     * Function to save after editing a function.
321
     */
322
    public function doSaveEdit()
323
    {
324
        $data = $this->misc->getDatabaseAccessor();
325
326
        $fnlang = strtolower($_POST['original_lang']);
327
328
        if ('c' == $fnlang) {
329
            $def = [$_POST['formObjectFile'], $_POST['formLinkSymbol']];
330
        } elseif ('internal' == $fnlang) {
331
            $def = $_POST['formLinkSymbol'];
332
        } else {
333
            $def = $_POST['formDefinition'];
334
        }
335
        if (!$data->hasFunctionAlterSchema()) {
336
            $_POST['formFuncSchema'] = '';
337
        }
338
339
        $status = $data->setFunction(
340
            $_POST['original_function'],
341
            $_POST['formFunction'],
342
            $_POST['original_arguments'],
343
            $_POST['original_returns'],
344
            $def,
345
            $_POST['original_lang'],
346
            $_POST['formProperties'],
347
            isset($_POST['original_setof']),
348
            $_POST['original_owner'],
349
            $_POST['formFuncOwn'],
350
            $_POST['original_schema'],
351
            $_POST['formFuncSchema'],
352
            isset($_POST['formCost']) ? $_POST['formCost'] : null,
353
            isset($_POST['formRows']) ? $_POST['formRows'] : 0,
354
            $_POST['formComment']
355
        );
356
357
        if (0 == $status) {
358
            // If function has had schema altered, need to change to the new schema
359
            // and reload the browser frame.
360
            if (!empty($_POST['formFuncSchema']) && ($_POST['formFuncSchema'] != $_POST['original_schema'])) {
361
                // Jump them to the new function schema
362
                $this->misc->setCurrentSchema($_POST['formFuncSchema']);
363
                // Force a browser reload
364
                $this->misc->setReloadBrowser(true);
365
            }
366
            $this->doProperties($this->lang['strfunctionupdated']);
367
        } else {
368
            $this->doEdit($this->lang['strfunctionupdatedbad']);
369
        }
370
    }
371
372
    /**
373
     * Function to allow editing of a Function.
374
     *
375
     * @param mixed $msg
376
     */
377
    public function doEdit($msg = '')
378
    {
379
        $data = $this->misc->getDatabaseAccessor();
380
381
        $this->printTrail('function');
382
        $this->printTitle($this->lang['stralter'], 'pg.function.alter');
383
        $this->printMsg($msg);
384
385
        $fndata = $data->getFunction($_REQUEST['function_oid']);
386
387
        if ($fndata->recordCount() > 0) {
388
            $fndata->fields['proretset'] = $data->phpBool($fndata->fields['proretset']);
389
390
            // Initialise variables
391
            $_POST['formDefinition'] = $this->getPostParam('formDefinition', $fndata->fields['prosrc']);
392
393
            $_POST['formProperties'] = $this->getPostParam('formProperties', $data->getFunctionProperties($fndata->fields));
394
395
            $_POST['formFunction'] = $this->getPostParam('formFunction', $fndata->fields['proname']);
396
397
            $_POST['formComment'] = $this->getPostParam('formComment', $fndata->fields['procomment']);
398
399
            $_POST['formObjectFile'] = $this->getPostParam('formObjectFile', $fndata->fields['probin']);
400
401
            $_POST['formLinkSymbol'] = $this->getPostParam('formLinkSymbol', $fndata->fields['prosrc']);
402
403
            $_POST['formFuncOwn'] = $this->getPostParam('formFuncOwn', $fndata->fields['proowner']);
404
405
            $_POST['formFuncSchema'] = $this->getPostParam('formFuncSchema', $fndata->fields['proschema']);
406
407
            if ($data->hasFunctionCosting()) {
408
                $_POST['formCost'] = $this->getPostParam('formCost', $fndata->fields['procost']);
409
410
                $_POST['formRows'] = $this->getPostParam('formRows', $fndata->fields['prorows']);
411
            }
412
413
            // Deal with named parameters
414
            if ($data->hasNamedParams()) {
415
                if (isset($fndata->fields['proallarguments'])) {
416
                    $args_arr = $data->phpArray($fndata->fields['proallarguments']);
417
                } else {
418
                    $args_arr = explode(', ', $fndata->fields['proarguments']);
419
                }
420
                $names_arr     = $data->phpArray($fndata->fields['proargnames']);
421
                $modes_arr     = $data->phpArray($fndata->fields['proargmodes']);
422
                $args          = '';
423
                $args_arr_size = sizeof($args_arr);
424
                for ($i = 0; $i < $args_arr_size; ++$i) {
425
                    if (0 != $i) {
426
                        $args .= ', ';
427
                    }
428
429
                    if (isset($modes_arr[$i])) {
430
                        switch ($modes_arr[$i]) {
431
                            case 'i':
432
                                $args .= ' IN ';
433
434
                                break;
435
                            case 'o':
436
                                $args .= ' OUT ';
437
438
                                break;
439
                            case 'b':
440
                                $args .= ' INOUT ';
441
442
                                break;
443
                            case 'v':
444
                                $args .= ' VARIADIC ';
445
446
                                break;
447
                            case 't':
448
                                $args .= ' TABLE ';
449
450
                                break;
451
                        }
452
                    }
453
                    if (isset($names_arr[$i]) && '' != $names_arr[$i]) {
454
                        $data->fieldClean($names_arr[$i]);
455
                        $args .= '"' . $names_arr[$i] . '" ';
456
                    }
457
                    $args .= $args_arr[$i];
458
                }
459
            } else {
460
                $args = $fndata->fields['proarguments'];
461
            }
462
463
            echo '<form action="' . \SUBFOLDER . "/src/views/functions\" method=\"post\">\n";
464
            echo "<table style=\"width: 90%\">\n";
465
            echo "<tr>\n";
466
            echo "<th class=\"data required\">{$this->lang['strschema']}</th>\n";
467
            echo "<th class=\"data required\">{$this->lang['strfunction']}</th>\n";
468
            echo "<th class=\"data\">{$this->lang['strarguments']}</th>\n";
469
            echo "<th class=\"data required\">{$this->lang['strreturns']}</th>\n";
470
            echo "<th class=\"data required\">{$this->lang['strproglanguage']}</th>\n";
471
            echo "</tr>\n";
472
473
            echo "<tr>\n";
474
            echo '<td class="data1">';
475
            echo '<input type="hidden" name="original_schema" value="', htmlspecialchars($fndata->fields['proschema']), "\" />\n";
476
            if ($data->hasFunctionAlterSchema()) {
477
                $schemas = $data->getSchemas();
478
                echo '<select name="formFuncSchema">';
479
                while (!$schemas->EOF) {
480
                    $schema = $schemas->fields['nspname'];
481
                    echo '<option value="', htmlspecialchars($schema), '"',
482
                    ($schema == $_POST['formFuncSchema']) ? ' selected="selected"' : '', '>', htmlspecialchars($schema), "</option>\n";
483
                    $schemas->moveNext();
484
                }
485
                echo "</select>\n";
486
            } else {
487
                echo $fndata->fields['proschema'];
488
            }
489
490
            echo "</td>\n";
491
            echo '<td class="data1">';
492
            echo '<input type="hidden" name="original_function" value="', htmlspecialchars($fndata->fields['proname']), "\" />\n";
493
            echo "<input name=\"formFunction\" style=\"width: 100%\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_POST['formFunction']), '" />';
494
            echo "</td>\n";
495
496
            echo '<td class="data1">', $this->misc->printVal($args), "\n";
497
            echo '<input type="hidden" name="original_arguments" value="', htmlspecialchars($args), "\" />\n";
498
            echo "</td>\n";
499
500
            echo '<td class="data1">';
501
            if ($fndata->fields['proretset']) {
502
                echo 'setof ';
503
            }
504
505
            echo $this->misc->printVal($fndata->fields['proresult']), "\n";
506
            echo '<input type="hidden" name="original_returns" value="', htmlspecialchars($fndata->fields['proresult']), "\" />\n";
507
            if ($fndata->fields['proretset']) {
508
                echo "<input type=\"hidden\" name=\"original_setof\" value=\"yes\" />\n";
509
            }
510
511
            echo "</td>\n";
512
513
            echo '<td class="data1">', $this->misc->printVal($fndata->fields['prolanguage']), "\n";
514
            echo '<input type="hidden" name="original_lang" value="', htmlspecialchars($fndata->fields['prolanguage']), "\" />\n";
515
            echo "</td>\n";
516
            echo "</tr>\n";
517
518
            $fnlang = strtolower($fndata->fields['prolanguage']);
519
            if ('c' == $fnlang) {
520
                echo "<tr><th class=\"data required\" colspan=\"2\">{$this->lang['strobjectfile']}</th>\n";
521
                echo "<th class=\"data\" colspan=\"2\">{$this->lang['strlinksymbol']}</th></tr>\n";
522
                echo '<tr><td class="data1" colspan="2"><input type="text" name="formObjectFile" style="width:100%" value="',
523
                htmlspecialchars($_POST['formObjectFile']), "\" /></td>\n";
524
                echo '<td class="data1" colspan="2"><input type="text" name="formLinkSymbol" style="width:100%" value="',
525
                htmlspecialchars($_POST['formLinkSymbol']), "\" /></td></tr>\n";
526
            } elseif ('internal' == $fnlang) {
527
                echo "<tr><th class=\"data\" colspan=\"5\">{$this->lang['strlinksymbol']}</th></tr>\n";
528
                echo '<tr><td class="data1" colspan="5"><input type="text" name="formLinkSymbol" style="width:100%" value="',
529
                htmlspecialchars($_POST['formLinkSymbol']), "\" /></td></tr>\n";
530
            } else {
531
                echo "<tr><th class=\"data required\" colspan=\"5\">{$this->lang['strdefinition']}</th></tr>\n";
532
                echo '<tr><td class="data1" colspan="5">';
533
                $textarea_id = ($fnlang === 'sql' || $fnlang === 'plpgsql') ? 'query' : 'formDefinition';
534
                echo '<textarea style="width:100%;" rows="20" cols="50" id="' . $textarea_id . '" name="formDefinition">';
535
                echo htmlspecialchars($_POST['formDefinition']);
536
                echo "</textarea></td></tr>\n";
537
            }
538
539
            // Display function comment
540
            echo "<tr><th class=\"data\" colspan=\"5\">{$this->lang['strcomment']}</th></tr>\n";
541
            echo '<tr><td class="data1" colspan="5">';
542
            echo '<textarea style="width:100%;" name="formComment" rows="3" cols="50">';
543
            echo htmlspecialchars($_POST['formComment']);
544
            echo "</textarea></td></tr>\n";
545
546
            // Display function cost options
547
            if ($data->hasFunctionCosting()) {
548
                echo "<tr><th class=\"data required\" colspan=\"5\">{$this->lang['strfunctioncosting']}</th></tr>\n";
549
                echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strexecutioncost']}: <input name=\"formCost\" size=\"16\" value=\"" .
550
                htmlspecialchars($_POST['formCost']) . '" /></td>';
551
                echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strresultrows']}: <input name=\"formRows\" size=\"16\" value=\"",
552
                htmlspecialchars($_POST['formRows']), '"', (!$fndata->fields['proretset']) ? 'disabled' : '', '/></td>';
553
            }
554
555
            // Display function properties
556
            if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) {
557
                echo "<tr><th class=\"data\" colspan=\"5\">{$this->lang['strproperties']}</th></tr>\n";
558
                echo "<tr><td class=\"data1\" colspan=\"5\">\n";
559
                $i = 0;
560
                foreach ($data->funcprops as $k => $v) {
561
                    echo "<select name=\"formProperties[{$i}]\">\n";
562
                    foreach ($v as $p) {
563
                        echo '<option value="', htmlspecialchars($p), '"',
564
                        ($_POST['formProperties'][$i] == $p) ? ' selected="selected"' : '',
565
                        '>', $this->misc->printVal($p), "</option>\n";
566
                    }
567
                    echo "</select><br />\n";
568
                    ++$i;
569
                }
570
                echo "</td></tr>\n";
571
            }
572
573
            // function owner
574
            if ($data->hasFunctionAlterOwner()) {
575
                $users = $data->getUsers();
576
                echo "<tr><td class=\"data1\" colspan=\"5\">{$this->lang['strowner']}: <select name=\"formFuncOwn\">";
577
                while (!$users->EOF) {
578
                    $uname = $users->fields['usename'];
579
                    echo '<option value="', htmlspecialchars($uname), '"',
580
                    ($uname == $_POST['formFuncOwn']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
581
                    $users->moveNext();
582
                }
583
                echo "</select>\n";
584
                echo '<input type="hidden" name="original_owner" value="', htmlspecialchars($fndata->fields['proowner']), "\" />\n";
585
                echo "</td></tr>\n";
586
            }
587
            echo "</table>\n";
588
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
589
            echo '<input type="hidden" name="function" value="', htmlspecialchars($_REQUEST['function']), "\" />\n";
590
            echo '<input type="hidden" name="function_oid" value="', htmlspecialchars($_REQUEST['function_oid']), "\" />\n";
591
            echo $this->misc->form;
592
            echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n";
593
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
594
            echo "</form>\n";
595
        } else {
596
            echo "<p>{$this->lang['strnodata']}</p>\n";
597
        }
598
    }
599
600
    /**
601
     * Show read only properties of a function.
602
     *
603
     * @param mixed $msg
604
     */
605
    public function doProperties($msg = '')
606
    {
607
        $data = $this->misc->getDatabaseAccessor();
608
609
        $this->printTrail('function');
610
        $this->printTitle($this->lang['strproperties'], 'pg.function');
611
        $this->printMsg($msg);
612
613
        $funcdata  = $data->getFunction($_REQUEST['function_oid']);
614
        $func_full = '';
615
        if ($funcdata->recordCount() > 0) {
616
            // Deal with named parameters
617
            $args = $this->_getPropertiesArgs($funcdata);
618
619
            // Show comment if any
620
            if (null !== $funcdata->fields['procomment']) {
621
                echo '<p class="comment">', $this->misc->printVal($funcdata->fields['procomment']), "</p>\n";
622
            }
623
624
            $funcdata->fields['proretset'] = $data->phpBool($funcdata->fields['proretset']);
625
            $func_full                     = $funcdata->fields['proname'] . '(' . $funcdata->fields['proarguments'] . ')';
626
            echo "<table style=\"width: 90%\">\n";
627
            echo sprintf('<tr><th class="data">%s</th>%s', $this->lang['strfunction'], "\n");
628
            echo sprintf('<th class="data">%s</th>%s', $this->lang['strarguments'], "\n");
629
            echo sprintf('<th class="data">%s</th>%s', $this->lang['strreturns'], "\n");
630
            echo sprintf('<th class="data">%s</th></tr>%s', $this->lang['strproglanguage'], "\n");
631
            echo '<tr><td class="data1">', $this->misc->printVal($funcdata->fields['proname']), "</td>\n";
632
            echo '<td class="data1">', $this->misc->printVal($args), "</td>\n";
633
            echo '<td class="data1">';
634
            if ($funcdata->fields['proretset']) {
635
                echo 'setof ';
636
            }
637
638
            echo $this->misc->printVal($funcdata->fields['proresult']), "</td>\n";
639
            echo '<td class="data1">', $this->misc->printVal($funcdata->fields['prolanguage']), "</td></tr>\n";
640
641
            $fnlang = strtolower($funcdata->fields['prolanguage']);
642
            if ('c' == $fnlang) {
643
                echo "<tr><th class=\"data\" colspan=\"2\">{$this->lang['strobjectfile']}</th>\n";
644
                echo "<th class=\"data\" colspan=\"2\">{$this->lang['strlinksymbol']}</th></tr>\n";
645
                echo '<tr><td class="data1" colspan="2">', $this->misc->printVal($funcdata->fields['probin']), "</td>\n";
646
                echo '<td class="data1" colspan="2">', $this->misc->printVal($funcdata->fields['prosrc']), "</td></tr>\n";
647
            } elseif ('internal' == $fnlang) {
648
                echo "<tr><th class=\"data\" colspan=\"4\">{$this->lang['strlinksymbol']}</th></tr>\n";
649
                echo '<tr><td class="data1" colspan="4">', $this->misc->printVal($funcdata->fields['prosrc']), "</td></tr>\n";
650
            } else {
651
                echo '<tr><td class="data1" colspan="4">';
652
                echo sprintf('<pre><code class="%s hljs">%s</code></pre>', $fnlang, $funcdata->fields['prosrc']);
653
                echo "</td></tr>\n";
654
            }
655
656
            // Display function cost options
657
            if ($data->hasFunctionCosting()) {
658
                echo "<tr><th class=\"data required\" colspan=\"4\">{$this->lang['strfunctioncosting']}</th></tr>\n";
659
                echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strexecutioncost']}: ", $this->misc->printVal($funcdata->fields['procost']), ' </td>';
660
                echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strresultrows']}: ", $this->misc->printVal($funcdata->fields['prorows']), ' </td>';
661
            }
662
663
            // Show flags
664
            if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) {
665
                // Fetch an array of the function properties
666
                $funcprops = $data->getFunctionProperties($funcdata->fields);
667
                echo "<tr><th class=\"data\" colspan=\"4\">{$this->lang['strproperties']}</th></tr>\n";
668
                echo "<tr><td class=\"data1\" colspan=\"4\">\n";
669
                foreach ($funcprops as $v) {
670
                    echo $this->misc->printVal($v), "<br />\n";
671
                }
672
                echo "</td></tr>\n";
673
            }
674
675
            echo "<tr><td class=\"data1\" colspan=\"5\">{$this->lang['strowner']}: ", htmlspecialchars($funcdata->fields['proowner']), "\n";
676
            echo "</td></tr>\n";
677
            echo "</table>\n";
678
        } else {
679
            echo "<p>{$this->lang['strnodata']}</p>\n";
680
        }
681
682
        $this->_printNavLinks('functions-properties', $func_full);
683
    }
684
685
    /**
686
     * Show confirmation of drop and perform actual drop.
687
     *
688
     * @param mixed $confirm
689
     */
690
    public function doDrop($confirm)
691
    {
692
        $data = $this->misc->getDatabaseAccessor();
693
694
        if (empty($_REQUEST['function']) && empty($_REQUEST['ma'])) {
695
            return $this->doDefault($this->lang['strspecifyfunctiontodrop']);
696
        }
697
698
        if ($confirm) {
699
            $this->printTrail('schema');
700
            $this->printTitle($this->lang['strdrop'], 'pg.function.drop');
701
702
            echo '<form action="' . \SUBFOLDER . "/src/views/functions\" method=\"post\">\n";
703
704
            //If multi drop
705
            if (isset($_REQUEST['ma'])) {
706
                foreach ($_REQUEST['ma'] as $v) {
707
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
708
                    echo '<p>', sprintf($this->lang['strconfdropfunction'], $this->misc->printVal($a['function'])), "</p>\n";
709
                    echo '<input type="hidden" name="function[]" value="', htmlspecialchars($a['function']), "\" />\n";
710
                    echo '<input type="hidden" name="function_oid[]" value="', htmlspecialchars($a['function_oid']), "\" />\n";
711
                }
712
            } else {
713
                echo '<p>', sprintf($this->lang['strconfdropfunction'], $this->misc->printVal($_REQUEST['function'])), "</p>\n";
714
                echo '<input type="hidden" name="function" value="', htmlspecialchars($_REQUEST['function']), "\" />\n";
715
                echo '<input type="hidden" name="function_oid" value="', htmlspecialchars($_REQUEST['function_oid']), "\" />\n";
716
            }
717
718
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
719
720
            echo $this->misc->form;
721
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /><label for=\"cascade\">{$this->lang['strcascade']}</label></p>\n";
722
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />\n";
723
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n";
724
            echo "</form>\n";
725
        } else {
726
            if (is_array($_POST['function_oid'])) {
727
                $msg    = '';
728
                $status = $data->beginTransaction();
729
                if (0 == $status) {
730
                    foreach ($_POST['function_oid'] as $k => $s) {
731
                        $status = $data->dropFunction($s, isset($_POST['cascade']));
732
                        if (0 == $status) {
733
                            $msg .= sprintf('%s: %s<br />', htmlentities($_POST['function'][$k], ENT_QUOTES, 'UTF-8'), $this->lang['strfunctiondropped']);
734
                        } else {
735
                            $data->endTransaction();
736
                            $this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($_POST['function'][$k], ENT_QUOTES, 'UTF-8'), $this->lang['strfunctiondroppedbad']));
737
738
                            return;
739
                        }
740
                    }
741
                }
742
                if (0 == $data->endTransaction()) {
743
                    // Everything went fine, back to the Default page....
744
                    $this->misc->setReloadBrowser(true);
745
                    $this->doDefault($msg);
746
                } else {
747
                    $this->doDefault($this->lang['strfunctiondroppedbad']);
748
                }
749
            } else {
750
                $status = $data->dropFunction($_POST['function_oid'], isset($_POST['cascade']));
751
                if (0 == $status) {
752
                    $this->misc->setReloadBrowser(true);
753
                    $this->doDefault($this->lang['strfunctiondropped']);
754
                } else {
755
                    $this->doDefault($this->lang['strfunctiondroppedbad']);
756
                }
757
            }
758
        }
759
    }
760
761
    /**
762
     * Displays a screen where they can enter a new function.
763
     *
764
     * @param string $msg  message to display
765
     * @param mixed  $szJS
766
     */
767
    public function doCreate($msg = '', $szJS = '')
768
    {
769
        $data = $this->misc->getDatabaseAccessor();
770
771
        $this->printTrail('schema');
772
        $_POST['formFunction'] = $this->getPostParam('formFunction', '');
773
774
        $_POST['formArguments'] = $this->getPostParam('formArguments', '');
775
776
        $_POST['formReturns'] = $this->getPostParam('formReturns', '');
777
778
        if (!isset($_POST['formLanguage'])) {
779
            $_POST['formLanguage'] = isset($_REQUEST['language']) ? $_REQUEST['language'] : 'sql';
780
        }
781
782
        $_POST['formDefinition'] = $this->getPostParam('formDefinition', '');
783
784
        $_POST['formObjectFile'] = $this->getPostParam('formObjectFile', '');
785
786
        $_POST['formLinkSymbol'] = $this->getPostParam('formLinkSymbol', '');
787
788
        $_POST['formProperties'] = $this->getPostParam('formProperties', $data->defaultprops);
789
790
        $_POST['formSetOf'] = $this->getPostParam('formSetOf', '');
791
792
        $_POST['formArray'] = $this->getPostParam('formArray', '');
793
794
        $_POST['formCost'] = $this->getPostParam('formCost', '');
795
796
        $_POST['formRows'] = $this->getPostParam('formRows', '');
797
798
        $_POST['formComment'] = $this->getPostParam('formComment', '');
799
800
        $types  = $data->getTypes(true, true, true);
801
        $langs  = $data->getLanguages(true);
802
        $fnlang = strtolower($_POST['formLanguage']);
803
804
        switch ($fnlang) {
805
            case 'c':
806
                $this->printTitle($this->lang['strcreatecfunction'], 'pg.function.create.c');
807
808
                break;
809
            case 'internal':
810
                $this->printTitle($this->lang['strcreateinternalfunction'], 'pg.function.create.internal');
811
812
                break;
813
            default:
814
                $this->printTitle($this->lang['strcreateplfunction'], 'pg.function.create.pl');
815
816
                break;
817
        }
818
        $this->printMsg($msg);
819
820
        // Create string for return type list
821
        $szTypes = '';
822
        while (!$types->EOF) {
823
            $szSelected = '';
824
            if ($types->fields['typname'] == $_POST['formReturns']) {
825
                $szSelected = ' selected="selected"';
826
            }
827
            // this variable is include in the JS code bellow, so we need to ENT_QUOTES
828
            $szTypes .= '<option value="' . htmlspecialchars($types->fields['typname'], ENT_QUOTES) . "\"{$szSelected}>";
829
            $szTypes .= htmlspecialchars($types->fields['typname'], ENT_QUOTES) . '</option>';
830
            $types->moveNext();
831
        }
832
833
        $szFunctionName = "<td class=\"data1\"><input name=\"formFunction\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"" .
834
        htmlspecialchars($_POST['formFunction']) . '" /></td>';
835
836
        $szArguments = '<td class="data1"><input name="formArguments" style="width:100%;" size="16" value="' .
837
        htmlspecialchars($_POST['formArguments']) . '" /></td>';
838
839
        $szSetOfSelected    = '';
840
        $szNotSetOfSelected = '';
841
        if ('' == $_POST['formSetOf']) {
842
            $szNotSetOfSelected = ' selected="selected"';
843
        } elseif ('SETOF' == $_POST['formSetOf']) {
844
            $szSetOfSelected = ' selected="selected"';
845
        }
846
        $szReturns = '<td class="data1" colspan="2">';
847
        $szReturns .= '<select name="formSetOf">';
848
        $szReturns .= "<option value=\"\"{$szNotSetOfSelected}></option>";
849
        $szReturns .= "<option value=\"SETOF\"{$szSetOfSelected}>SETOF</option>";
850
        $szReturns .= '</select>';
851
852
        $szReturns .= '<select class="select2" name="formReturns">' . $szTypes . '</select>';
853
854
        // Create string array type selector
855
856
        $szArraySelected    = '';
857
        $szNotArraySelected = '';
858
        if ('' == $_POST['formArray']) {
859
            $szNotArraySelected = ' selected="selected"';
860
        } elseif ('[]' == $_POST['formArray']) {
861
            $szArraySelected = ' selected="selected"';
862
        }
863
864
        $szReturns .= '<select name="formArray">';
865
        $szReturns .= "<option value=\"\"{$szNotArraySelected}></option>";
866
        $szReturns .= "<option value=\"[]\"{$szArraySelected}>[ ]</option>";
867
        $szReturns .= "</select>\n</td>";
868
869
        // Create string for language
870
        $szLanguage = '<td class="data1">';
871
        if ('c' == $fnlang || 'internal' == $fnlang) {
872
            $szLanguage .= $_POST['formLanguage'] . "\n";
873
            $szLanguage .= "<input type=\"hidden\" name=\"formLanguage\" value=\"{$_POST['formLanguage']}\" />\n";
874
        } else {
875
            $szLanguage .= "<select name=\"formLanguage\">\n";
876
            while (!$langs->EOF) {
877
                $szSelected = '';
878
                if ($langs->fields['lanname'] == $_POST['formLanguage']) {
879
                    $szSelected = ' selected="selected"';
880
                }
881
                if ('c' != strtolower($langs->fields['lanname']) && 'internal' != strtolower($langs->fields['lanname'])) {
882
                    $szLanguage .= '<option value="' . htmlspecialchars($langs->fields['lanname']) . "\"{$szSelected}>\n" .
883
                    $this->misc->printVal($langs->fields['lanname']) . '</option>';
884
                }
885
886
                $langs->moveNext();
887
            }
888
            $szLanguage .= "</select>\n";
889
        }
890
891
        $szLanguage .= '</td>';
892
        $szJSArguments = "<tr><th class=\"data\" colspan=\"7\">{$this->lang['strarguments']}</th></tr>";
893
        $arrayModes    = ['IN', 'OUT', 'INOUT'];
894
        $szModes       = '<select name="formArgModes[]" style="width:100%;">';
895
        foreach ($arrayModes as $pV) {
896
            $szModes .= "<option value=\"{$pV}\">{$pV}</option>";
897
        }
898
        $szModes .= '</select>';
899
        $szArgReturns = '<select name="formArgArray[]">';
900
        $szArgReturns .= '<option value=""></option>';
901
        $szArgReturns .= '<option value="[]">[]</option>';
902
        $szArgReturns .= '</select>';
903
        $subfolder = \SUBFOLDER;
904
        if (!empty($this->conf['theme'])) {
905
            $szImgPath = \SUBFOLDER . "/assets/images/themes/{$this->conf['theme']}";
906
        } else {
907
            $szImgPath = \SUBFOLDER . '/assets/images/themes/default';
908
        }
909
        if (empty($msg)) {
910
            // $this->prtrace($subfolder);
911
            $szJSTRArg = "<script type=\"text/javascript\" >addArg('{$subfolder}');</script>\n";
912
        } else {
913
            $szJSTRArg = '';
914
        }
915
        $szJSAddTR = "<tr id=\"parent_add_tr\" onclick=\"addArg('{$subfolder}');\" onmouseover=\"this.style.cursor='pointer'\">\n";
916
        $szJSAddTR .= '<td style="text-align: right" colspan="6" class="data3"><table><tr><td class="data3">';
917
        $szJSAddTR .= "<img src=\"{$szImgPath}/AddArguments.png\" alt=\"Add Argument\" /></td>";
918
        $szJSAddTR .= "<td class=\"data3\"><span style=\"font-size: 8pt\">{$this->lang['strargadd']}</span></td></tr></table></td>\n</tr>\n";
919
920
        echo '<script src="' . \SUBFOLDER . "/assets/js/functions.js\" type=\"text/javascript\"></script>
921
		<script type=\"text/javascript\">
922
			//<![CDATA[
923
			var g_types_select = '<select class=\"select2\" name=\"formArgType[]\">{$szTypes}</select>{$szArgReturns}';
924
			var g_modes_select = '{$szModes}';
925
			var g_name = '';
926
			var g_lang_strargremove = '", htmlspecialchars($this->lang['strargremove'], ENT_QUOTES), "';
927
			var g_lang_strargnoargs = '", htmlspecialchars($this->lang['strargnoargs'], ENT_QUOTES), "';
928
			var g_lang_strargenableargs = '", htmlspecialchars($this->lang['strargenableargs'], ENT_QUOTES), "';
929
			var g_lang_strargnorowabove = '", htmlspecialchars($this->lang['strargnorowabove'], ENT_QUOTES), "';
930
			var g_lang_strargnorowbelow = '", htmlspecialchars($this->lang['strargnorowbelow'], ENT_QUOTES), "';
931
			var g_lang_strargremoveconfirm = '", htmlspecialchars($this->lang['strargremoveconfirm'], ENT_QUOTES), "';
932
			var g_lang_strargraise = '", htmlspecialchars($this->lang['strargraise'], ENT_QUOTES), "';
933
			var g_lang_strarglower = '", htmlspecialchars($this->lang['strarglower'], ENT_QUOTES), "';
934
			//]]>
935
		</script>
936
		";
937
        echo '<form action="' . \SUBFOLDER . "/src/views/functions\" method=\"post\">\n";
938
        echo "<table><tbody id=\"args_table\">\n";
939
        echo "<tr><th class=\"data required\">{$this->lang['strname']}</th>\n";
940
        echo "<th class=\"data required\" colspan=\"2\">{$this->lang['strreturns']}</th>\n";
941
        echo "<th class=\"data required\">{$this->lang['strproglanguage']}</th></tr>\n";
942
        echo "<tr>\n";
943
        echo "{$szFunctionName}\n";
944
        echo "{$szReturns}\n";
945
        echo "{$szLanguage}\n";
946
        echo "</tr>\n";
947
        echo "{$szJSArguments}\n";
948
        echo "<tr>\n";
949
        echo "<th class=\"data required\">{$this->lang['strargmode']}</th>\n";
950
        echo "<th class=\"data required\">{$this->lang['strname']}</th>\n";
951
        echo "<th class=\"data required\" colspan=\"2\">{$this->lang['strargtype']}</th>\n";
952
        echo "</tr>\n";
953
        echo "{$szJSAddTR}\n";
954
955
        if ('c' == $fnlang) {
956
            echo "<tr><th class=\"data required\" colspan=\"2\">{$this->lang['strobjectfile']}</th>\n";
957
            echo "<th class=\"data\" colspan=\"2\">{$this->lang['strlinksymbol']}</th></tr>\n";
958
            echo '<tr><td class="data1" colspan="2"><input type="text" name="formObjectFile" style="width:100%" value="',
959
            htmlspecialchars($_POST['formObjectFile']), "\" /></td>\n";
960
            echo '<td class="data1" colspan="2"><input type="text" name="formLinkSymbol" style="width:100%" value="',
961
            htmlspecialchars($_POST['formLinkSymbol']), "\" /></td></tr>\n";
962
        } elseif ('internal' == $fnlang) {
963
            echo "<tr><th class=\"data\" colspan=\"4\">{$this->lang['strlinksymbol']}</th></tr>\n";
964
            echo '<tr><td class="data1" colspan="4"><input type="text" name="formLinkSymbol" style="width:100%" value="',
965
            htmlspecialchars($_POST['formLinkSymbol']), "\" /></td></tr>\n";
966
        } else {
967
            echo "<tr><th class=\"data required\" colspan=\"4\">{$this->lang['strdefinition']}</th></tr>\n";
968
            echo '<tr><td class="data1" colspan="4">';
969
            echo '<textarea style="width:100%;" rows="20" cols="50" name="formDefinition">';
970
            echo htmlspecialchars($_POST['formDefinition']);
971
            echo "</textarea></td></tr>\n";
972
        }
973
974
        // Display function comment
975
        echo "<tr><th class=\"data\" colspan=\"4\">{$this->lang['strcomment']}</th></tr>\n";
976
        echo '<tr><td class="data1" colspan="4"><textarea style="width:100%;" name="formComment" rows="3" cols="50">',
977
        htmlspecialchars($_POST['formComment']), "</textarea></td></tr>\n";
978
979
        // Display function cost options
980
        if ($data->hasFunctionCosting()) {
981
            echo "<tr><th class=\"data required\" colspan=\"4\">{$this->lang['strfunctioncosting']}</th></tr>\n";
982
            echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strexecutioncost']}: <input name=\"formCost\" size=\"16\" value=\"" .
983
            htmlspecialchars($_POST['formCost']) . '" /></td>';
984
            echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strresultrows']}: <input name=\"formRows\" size=\"16\" value=\"" .
985
            htmlspecialchars($_POST['formRows']) . '" /></td>';
986
        }
987
988
        // Display function properties
989
        if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) {
990
            echo "<tr><th class=\"data required\" colspan=\"4\">{$this->lang['strproperties']}</th></tr>\n";
991
            echo "<tr><td class=\"data1\" colspan=\"4\">\n";
992
            $i = 0;
993
            foreach ($data->funcprops as $k => $v) {
994
                echo "<select name=\"formProperties[{$i}]\">\n";
995
                foreach ($v as $p) {
996
                    echo '<option value="', htmlspecialchars($p), '"',
997
                    ($_POST['formProperties'][$i] == $p) ? ' selected="selected"' : '',
998
                    '>', $this->misc->printVal($p), "</option>\n";
999
                }
1000
                echo "</select><br />\n";
1001
                ++$i;
1002
            }
1003
            echo "</td></tr>\n";
1004
        }
1005
        echo "</tbody></table>\n";
1006
        echo $szJSTRArg;
1007
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
1008
        echo $this->misc->form;
1009
        echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n";
1010
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
1011
        echo "</form>\n";
1012
        echo $szJS;
1013
    }
1014
1015
    /**
1016
     * Actually creates the new function in the database.
1017
     */
1018
    public function doSaveCreate()
1019
    {
1020
        $data = $this->misc->getDatabaseAccessor();
1021
1022
        $fnlang = strtolower($_POST['formLanguage']);
1023
1024
        if ('c' == $fnlang) {
1025
            $def = [$_POST['formObjectFile'], $_POST['formLinkSymbol']];
1026
        } elseif ('internal' == $fnlang) {
1027
            $def = $_POST['formLinkSymbol'];
1028
        } else {
1029
            $def = $_POST['formDefinition'];
1030
        }
1031
1032
        $szJS = '';
1033
1034
        echo '<script src="' . \SUBFOLDER . '/assets/js/functions.js" type="text/javascript"></script>';
1035
        echo '<script type="text/javascript">' . $this->_buildJSData() . '</script>';
1036
        if (!empty($_POST['formArgName'])) {
1037
            $szJS = $this->_buildJSRows($this->_buildFunctionArguments($_POST));
1038
        } else {
1039
            $subfolder = \SUBFOLDER;
1040
            // $this->prtrace($subfolder);
1041
            $szJS = '<script type="text/javascript" src="' . \SUBFOLDER . '/assets/js/functions.js">noArgsRebuild(addArg("' . $subfolder . '"));</script>';
1042
        }
1043
1044
        $cost = (isset($_POST['formCost'])) ? $_POST['formCost'] : null;
1045
        if ('' == $cost || !is_numeric($cost) || $cost != (int) $cost || $cost < 0) {
1046
            $cost = null;
1047
        }
1048
1049
        $rows = (isset($_POST['formRows'])) ? $_POST['formRows'] : null;
1050
        if ('' == $rows || !is_numeric($rows) || $rows != (int) $rows) {
1051
            $rows = null;
1052
        }
1053
1054
        // Check that they've given a name and a definition
1055
        if ('' == $_POST['formFunction']) {
1056
            $this->doCreate($this->lang['strfunctionneedsname'], $szJS);
1057
        } elseif ('internal' != $fnlang && !$def) {
1058
            $this->doCreate($this->lang['strfunctionneedsdef'], $szJS);
1059
        } else {
1060
            // Append array symbol to type if chosen
1061
            $status = $data->createFunction(
1062
                $_POST['formFunction'],
1063
                empty($_POST['nojs']) ? $this->_buildFunctionArguments($_POST) : $_POST['formArguments'],
1064
                $_POST['formReturns'] . $_POST['formArray'],
1065
                $def,
1066
                $_POST['formLanguage'],
1067
                $_POST['formProperties'],
1068
                'SETOF' == $_POST['formSetOf'],
1069
                $cost,
1070
                $rows,
1071
                $_POST['formComment'],
1072
                false
1073
            );
1074
            if (0 == $status) {
1075
                $this->doDefault($this->lang['strfunctioncreated']);
1076
            } else {
1077
                $this->doCreate($this->lang['strfunctioncreatedbad'], $szJS);
1078
            }
1079
        }
1080
    }
1081
1082
    /**
1083
     * Build out the function arguments string.
1084
     *
1085
     * @param array $arrayVars
1086
     *
1087
     * @return string the imploded array vars
1088
     */
1089
    private function _buildFunctionArguments($arrayVars)
1090
    {
1091
        if (isset($_POST['formArgName'])) {
1092
            $arrayArgs = [];
1093
            foreach ($arrayVars['formArgName'] as $pK => $pV) {
1094
                $arrayArgs[] = $arrayVars['formArgModes'][$pK] . ' ' . trim($pV) . ' ' . trim($arrayVars['formArgType'][$pK]) . $arrayVars['formArgArray'][$pK];
1095
            }
1096
1097
            return implode(',', $arrayArgs);
1098
        }
1099
1100
        return '';
1101
    }
1102
1103
    /**
1104
     * Build out JS to re-create table rows for arguments.
1105
     *
1106
     * @param string $szArgs args to parse
1107
     */
1108
    private function _buildJSRows($szArgs)
1109
    {
1110
        $arrayModes      = ['IN', 'OUT', 'INOUT'];
1111
        $arrayArgs       = explode(',', $szArgs);
1112
        $arrayProperArgs = [];
1113
        $nC              = 0;
1114
        $szReturn        = '';
1115
        $szMode          = [];
1116
        foreach ($arrayArgs as $pV) {
1117
            $arrayWords = explode(' ', $pV);
1118
            if (true === in_array($arrayWords[0], $arrayModes, true)) {
1119
                $szMode = $arrayWords[0];
1120
                array_shift($arrayWords);
1121
            }
1122
            $szArgName = array_shift($arrayWords);
1123
            if (false === strpos($arrayWords[count($arrayWords) - 1], '[]')) {
1124
                $szArgType   = implode(' ', $arrayWords);
1125
                $bArgIsArray = 'false';
1126
            } else {
1127
                $szArgType   = str_replace('[]', '', implode(' ', $arrayWords));
1128
                $bArgIsArray = 'true';
1129
            }
1130
            $arrayProperArgs[] = [$szMode, $szArgName, $szArgType, $bArgIsArray];
1131
            $subfolder         = \SUBFOLDER;
1132
            // $this->prtrace($subfolder);
1133
            $szReturn .= '<script type="text/javascript">';
1134
            $szReturn .= "RebuildArgTR('{$szMode}','{$szArgName}','{$szArgType}',new Boolean({$bArgIsArray},{$subfolder}));";
1135
            $szReturn .= '</script>;';
1136
            ++$nC;
1137
        }
1138
1139
        return $szReturn;
1140
    }
1141
1142
    private function _buildJSData()
1143
    {
1144
        $data = $this->misc->getDatabaseAccessor();
1145
1146
        $arrayModes  = ['IN', 'OUT', 'INOUT'];
1147
        $arrayTypes  = $data->getTypes(true, true, true);
1148
        $arrayPTypes = [];
1149
        $arrayPModes = [];
1150
1151
        while (!$arrayTypes->EOF) {
1152
            $arrayPTypes[] = "'" . $arrayTypes->fields['typname'] . "'";
1153
            $arrayTypes->moveNext();
1154
        }
1155
1156
        foreach ($arrayModes as $pV) {
1157
            $arrayPModes[] = "'{$pV}'";
1158
        }
1159
1160
        $szTypes = 'g_main_types = new Array(' . implode(',', $arrayPTypes) . ');';
1161
        $szModes = 'g_main_modes = new Array(' . implode(',', $arrayPModes) . ');';
1162
1163
        return $szTypes . $szModes;
1164
    }
1165
1166
    /**
1167
     * Get the concatenated arguments for a function
1168
     *
1169
     * @param \PHPPgAdmin\ADORecordSet  $funcdata  The funcdata record
1170
     *
1171
     * @return string  The arguments of the function
1172
     */
1173
    private function _getPropertiesArgs($funcdata)
1174
    {
1175
        $data = $this->misc->getDatabaseAccessor();
1176
1177
        if ($data->hasNamedParams()) {
1178
            if (isset($funcdata->fields['proallarguments'])) {
1179
                $args_arr = $data->phpArray($funcdata->fields['proallarguments']);
1180
            } else {
1181
                $args_arr = explode(', ', $funcdata->fields['proarguments']);
1182
            }
1183
            $names_arr     = $data->phpArray($funcdata->fields['proargnames']);
1184
            $modes_arr     = $data->phpArray($funcdata->fields['proargmodes']);
1185
            $args          = '';
1186
            $args_arr_size = sizeof($args_arr);
1187
            for ($i = 0; $i < $args_arr_size; ++$i) {
1188
                if (0 != $i) {
1189
                    $args .= ', ';
1190
                }
1191
1192
                if (isset($modes_arr[$i])) {
1193
                    switch ($modes_arr[$i]) {
1194
                        case 'i':
1195
                            $args .= ' IN ';
1196
1197
                            break;
1198
                        case 'o':
1199
                            $args .= ' OUT ';
1200
1201
                            break;
1202
                        case 'b':
1203
                            $args .= ' INOUT ';
1204
1205
                            break;
1206
                        case 'v':
1207
                            $args .= ' VARIADIC ';
1208
1209
                            break;
1210
                        case 't':
1211
                            $args .= ' TABLE ';
1212
1213
                            break;
1214
                    }
1215
                }
1216
                if (isset($names_arr[$i]) && '' != $names_arr[$i]) {
1217
                    $data->fieldClean($names_arr[$i]);
1218
                    $args .= '"' . $names_arr[$i] . '" ';
1219
                }
1220
                $args .= $args_arr[$i];
1221
            }
1222
        } else {
1223
            $args = $funcdata->fields['proarguments'];
1224
        }
1225
        return $args;
1226
    }
1227
}
1228