Passed
Push — master ( a7725b...1fbc32 )
by Felipe
08:36 queued 04:40
created

FunctionsController::_getNamedParamsArgs()   C

Complexity

Conditions 12
Paths 58

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 5.1266
c 0
b 0
f 0
cc 12
eloc 33
nc 58
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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