Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

src/controllers/FunctionsController.php (11 issues)

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