Passed
Push — develop ( 51bd2c...501708 )
by Felipe
05:51
created

FunctionsController::doSaveEdit()   C

Complexity

Conditions 9
Paths 18

Size

Total Lines 48
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 5.5102
c 0
b 0
f 0
cc 9
eloc 34
nc 18
nop 0
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.40
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 $controller_name = 'FunctionsController';
19
    public $table_place     = 'functions-functions';
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
        $this->printHeader($this->lang['strfunctions'], null, true, 'header_datatables.twig');
31
        $this->printBody();
32
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
                $this->doCreate();
44
45
                break;
46
            case 'drop':
47
                if (isset($_POST['drop'])) {
48
                    $this->doDrop(false);
49
                } else {
50
                    $this->doDefault();
51
                }
52
53
                break;
54
            case 'confirm_drop':
55
                $this->doDrop(true);
56
57
                break;
58
            case 'save_edit':
59
                if (isset($_POST['cancel'])) {
60
                    $this->doDefault();
61
                } else {
62
                    $this->doSaveEdit();
63
                }
64
65
                break;
66
            case 'edit':
67
                $this->doEdit();
68
69
                break;
70
            case 'properties':
71
                $this->doProperties();
72
73
                break;
74
            default:
75
                $this->doDefault();
76
77
                break;
78
        }
79
80
        $this->printFooter();
81
    }
82
83
    /**
84
     * Show default list of functions in the database.
85
     *
86
     * @param mixed $msg
87
     */
88
    public function doDefault($msg = '')
89
    {
90
        $data = $this->misc->getDatabaseAccessor();
91
92
        $this->printTrail('schema');
93
        $this->printTabs('schema', 'functions');
94
        $this->printMsg($msg);
95
96
        $funcs = $data->getFunctions();
97
98
        $columns = [
99
            'function'     => [
100
                'title' => $this->lang['strfunction'],
101
                'field' => Decorator::field('proproto'),
102
                'url'   => \SUBFOLDER."/redirect/function?action=properties&amp;{$this->misc->href}&amp;",
103
                'vars'  => ['function' => 'proproto', 'function_oid' => 'prooid'],
104
            ],
105
            'returns'      => [
106
                'title' => $this->lang['strreturns'],
107
                'field' => Decorator::field('proreturns'),
108
            ],
109
            'owner'        => [
110
                'title' => $this->lang['strowner'],
111
                'field' => Decorator::field('proowner'),
112
            ],
113
            'proglanguage' => [
114
                'title' => $this->lang['strproglanguage'],
115
                'field' => Decorator::field('prolanguage'),
116
            ],
117
            'actions'      => [
118
                'title' => $this->lang['stractions'],
119
            ],
120
            'comment'      => [
121
                'title' => $this->lang['strcomment'],
122
                'field' => Decorator::field('procomment'),
123
            ],
124
        ];
125
126
        $actions = [
127
            'multiactions' => [
128
                'keycols' => ['function' => 'proproto', 'function_oid' => 'prooid'],
129
                'url'     => 'functions',
130
            ],
131
            'alter'        => [
132
                'content' => $this->lang['stralter'],
133
                'attr'    => [
134
                    'href' => [
135
                        'url'     => 'functions',
136
                        'urlvars' => [
137
                            'action'       => 'edit',
138
                            'function'     => Decorator::field('proproto'),
139
                            'function_oid' => Decorator::field('prooid'),
140
                        ],
141
                    ],
142
                ],
143
            ],
144
            'drop'         => [
145
                'multiaction' => 'confirm_drop',
146
                'content'     => $this->lang['strdrop'],
147
                'attr'        => [
148
                    'href' => [
149
                        'url'     => 'functions',
150
                        'urlvars' => [
151
                            'action'       => 'confirm_drop',
152
                            'function'     => Decorator::field('proproto'),
153
                            'function_oid' => Decorator::field('prooid'),
154
                        ],
155
                    ],
156
                ],
157
            ],
158
            'privileges'   => [
159
                'content' => $this->lang['strprivileges'],
160
                'attr'    => [
161
                    'href' => [
162
                        'url'     => 'privileges',
163
                        'urlvars' => [
164
                            'subject'      => 'function',
165
                            'function'     => Decorator::field('proproto'),
166
                            'function_oid' => Decorator::field('prooid'),
167
                        ],
168
                    ],
169
                ],
170
            ],
171
        ];
172
173
        echo $this->printTable($funcs, $columns, $actions, $this->table_place, $this->lang['strnofunctions']);
174
175
        $navlinks = [
176
            'createpl'       => [
177
                'attr'    => [
178
                    'href' => [
179
                        'url'     => 'functions',
180
                        'urlvars' => [
181
                            'action'   => 'create',
182
                            'server'   => $_REQUEST['server'],
183
                            'database' => $_REQUEST['database'],
184
                            'schema'   => $_REQUEST['schema'],
185
                        ],
186
                    ],
187
                ],
188
                'content' => $this->lang['strcreateplfunction'],
189
            ],
190
            'createinternal' => [
191
                'attr'    => [
192
                    'href' => [
193
                        'url'     => 'functions',
194
                        'urlvars' => [
195
                            'action'   => 'create',
196
                            'language' => 'internal',
197
                            'server'   => $_REQUEST['server'],
198
                            'database' => $_REQUEST['database'],
199
                            'schema'   => $_REQUEST['schema'],
200
                        ],
201
                    ],
202
                ],
203
                'content' => $this->lang['strcreateinternalfunction'],
204
            ],
205
            'createc'        => [
206
                'attr'    => [
207
                    'href' => [
208
                        'url'     => 'functions',
209
                        'urlvars' => [
210
                            'action'   => 'create',
211
                            'language' => 'C',
212
                            'server'   => $_REQUEST['server'],
213
                            'database' => $_REQUEST['database'],
214
                            'schema'   => $_REQUEST['schema'],
215
                        ],
216
                    ],
217
                ],
218
                'content' => $this->lang['strcreatecfunction'],
219
            ],
220
        ];
221
222
        $this->printNavLinks($navlinks, 'functions-functions', get_defined_vars());
223
    }
224
225
    /**
226
     * Generate XML for the browser tree.
227
     */
228
    public function doTree()
229
    {
230
        $data = $this->misc->getDatabaseAccessor();
231
232
        $funcs = $data->getFunctions();
233
234
        $proto = Decorator::concat(Decorator::field('proname'), ' (', Decorator::field('proarguments'), ')');
235
236
        $reqvars = $this->misc->getRequestVars('function');
237
238
        $attrs = [
239
            'text'    => $proto,
240
            'icon'    => 'Function',
241
            'toolTip' => Decorator::field('procomment'),
242
            'action'  => Decorator::redirecturl(
243
                'redirect',
244
                $reqvars,
245
                [
246
                    'action'       => 'properties',
247
                    'function'     => $proto,
248
                    'function_oid' => Decorator::field('prooid'),
249
                ]
250
            ),
251
        ];
252
253
        return $this->printTree($funcs, $attrs, 'functions');
254
    }
255
256
    /**
257
     * Function to save after editing a function.
258
     */
259
    public function doSaveEdit()
260
    {
261
        $data = $this->misc->getDatabaseAccessor();
262
263
        $fnlang = strtolower($_POST['original_lang']);
264
265
        if ('c' == $fnlang) {
266
            $def = [$_POST['formObjectFile'], $_POST['formLinkSymbol']];
267
        } elseif ('internal' == $fnlang) {
268
            $def = $_POST['formLinkSymbol'];
269
        } else {
270
            $def = $_POST['formDefinition'];
271
        }
272
        if (!$data->hasFunctionAlterSchema()) {
273
            $_POST['formFuncSchema'] = '';
274
        }
275
276
        $status = $data->setFunction(
277
            $_POST['function_oid'],
278
            $_POST['original_function'],
279
            $_POST['formFunction'],
280
            $_POST['original_arguments'],
281
            $_POST['original_returns'],
282
            $def,
283
            $_POST['original_lang'],
284
            $_POST['formProperties'],
285
            isset($_POST['original_setof']),
286
            $_POST['original_owner'],
287
            $_POST['formFuncOwn'],
288
            $_POST['original_schema'],
289
            $_POST['formFuncSchema'],
290
            isset($_POST['formCost']) ? $_POST['formCost'] : null,
291
            isset($_POST['formRows']) ? $_POST['formRows'] : 0,
292
            $_POST['formComment']
293
        );
294
295
        if (0 == $status) {
296
            // If function has had schema altered, need to change to the new schema
297
            // and reload the browser frame.
298
            if (!empty($_POST['formFuncSchema']) && ($_POST['formFuncSchema'] != $_POST['original_schema'])) {
299
                // Jump them to the new function schema
300
                $this->misc->setCurrentSchema($_POST['formFuncSchema']);
301
                // Force a browser reload
302
                $this->misc->setReloadBrowser(true);
303
            }
304
            $this->doProperties($this->lang['strfunctionupdated']);
305
        } else {
306
            $this->doEdit($this->lang['strfunctionupdatedbad']);
307
        }
308
    }
309
310
    /**
311
     * Function to allow editing of a Function.
312
     *
313
     * @param mixed $msg
314
     */
315
    public function doEdit($msg = '')
316
    {
317
        $data = $this->misc->getDatabaseAccessor();
318
319
        $this->printTrail('function');
320
        $this->printTitle($this->lang['stralter'], 'pg.function.alter');
321
        $this->printMsg($msg);
322
323
        $fndata = $data->getFunction($_REQUEST['function_oid']);
324
325
        if ($fndata->recordCount() > 0) {
326
            $fndata->fields['proretset'] = $data->phpBool($fndata->fields['proretset']);
327
328
            // Initialise variables
329
            if (!isset($_POST['formDefinition'])) {
330
                $_POST['formDefinition'] = $fndata->fields['prosrc'];
331
            }
332
333
            if (!isset($_POST['formProperties'])) {
334
                $_POST['formProperties'] = $data->getFunctionProperties($fndata->fields);
335
            }
336
337
            if (!isset($_POST['formFunction'])) {
338
                $_POST['formFunction'] = $fndata->fields['proname'];
339
            }
340
341
            if (!isset($_POST['formComment'])) {
342
                $_POST['formComment'] = $fndata->fields['procomment'];
343
            }
344
345
            if (!isset($_POST['formObjectFile'])) {
346
                $_POST['formObjectFile'] = $fndata->fields['probin'];
347
            }
348
349
            if (!isset($_POST['formLinkSymbol'])) {
350
                $_POST['formLinkSymbol'] = $fndata->fields['prosrc'];
351
            }
352
353
            if (!isset($_POST['formFuncOwn'])) {
354
                $_POST['formFuncOwn'] = $fndata->fields['proowner'];
355
            }
356
357
            if (!isset($_POST['formFuncSchema'])) {
358
                $_POST['formFuncSchema'] = $fndata->fields['proschema'];
359
            }
360
361
            if ($data->hasFunctionCosting()) {
362
                if (!isset($_POST['formCost'])) {
363
                    $_POST['formCost'] = $fndata->fields['procost'];
364
                }
365
366
                if (!isset($_POST['formRows'])) {
367
                    $_POST['formRows'] = $fndata->fields['prorows'];
368
                }
369
            }
370
371
            // Deal with named parameters
372
            if ($data->hasNamedParams()) {
373
                if (isset($fndata->fields['proallarguments'])) {
374
                    $args_arr = $data->phpArray($fndata->fields['proallarguments']);
375
                } else {
376
                    $args_arr = explode(', ', $fndata->fields['proarguments']);
377
                }
378
                $names_arr = $data->phpArray($fndata->fields['proargnames']);
379
                $modes_arr = $data->phpArray($fndata->fields['proargmodes']);
380
                $args      = '';
381
                $i         = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $i is dead and can be removed.
Loading history...
382
                for ($i = 0; $i < sizeof($args_arr); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
383
                    if (0 != $i) {
384
                        $args .= ', ';
385
                    }
386
387
                    if (isset($modes_arr[$i])) {
388
                        switch ($modes_arr[$i]) {
389
                            case 'i':$args .= ' IN ';
390
391
                                break;
392
                            case 'o':$args .= ' OUT ';
393
394
                                break;
395
                            case 'b':$args .= ' INOUT ';
396
397
                                break;
398
                            case 'v':$args .= ' VARIADIC ';
399
400
                                break;
401
                            case 't':$args .= ' TABLE ';
402
403
                                break;
404
                        }
405
                    }
406
                    if (isset($names_arr[$i]) && '' != $names_arr[$i]) {
407
                        $data->fieldClean($names_arr[$i]);
408
                        $args .= '"'.$names_arr[$i].'" ';
409
                    }
410
                    $args .= $args_arr[$i];
411
                }
412
            } else {
413
                $args = $fndata->fields['proarguments'];
414
            }
415
416
            $func_full = $fndata->fields['proname'].'('.$fndata->fields['proarguments'].')';
0 ignored issues
show
Unused Code introduced by
The assignment to $func_full is dead and can be removed.
Loading history...
417
            echo '<form action="'.\SUBFOLDER."/src/views/functions\" method=\"post\">\n";
418
            echo "<table style=\"width: 90%\">\n";
419
            echo "<tr>\n";
420
            echo "<th class=\"data required\">{$this->lang['strschema']}</th>\n";
421
            echo "<th class=\"data required\">{$this->lang['strfunction']}</th>\n";
422
            echo "<th class=\"data\">{$this->lang['strarguments']}</th>\n";
423
            echo "<th class=\"data required\">{$this->lang['strreturns']}</th>\n";
424
            echo "<th class=\"data required\">{$this->lang['strproglanguage']}</th>\n";
425
            echo "</tr>\n";
426
427
            echo "<tr>\n";
428
            echo '<td class="data1">';
429
            echo '<input type="hidden" name="original_schema" value="', htmlspecialchars($fndata->fields['proschema']), "\" />\n";
430
            if ($data->hasFunctionAlterSchema()) {
431
                $schemas = $data->getSchemas();
432
                echo '<select name="formFuncSchema">';
433
                while (!$schemas->EOF) {
434
                    $schema = $schemas->fields['nspname'];
435
                    echo '<option value="', htmlspecialchars($schema), '"',
436
                    ($schema == $_POST['formFuncSchema']) ? ' selected="selected"' : '', '>', htmlspecialchars($schema), "</option>\n";
437
                    $schemas->moveNext();
438
                }
439
                echo "</select>\n";
440
            } else {
441
                echo $fndata->fields['proschema'];
442
            }
443
444
            echo "</td>\n";
445
            echo '<td class="data1">';
446
            echo '<input type="hidden" name="original_function" value="', htmlspecialchars($fndata->fields['proname']), "\" />\n";
447
            echo "<input name=\"formFunction\" style=\"width: 100%\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_POST['formFunction']), '" />';
448
            echo "</td>\n";
449
450
            echo '<td class="data1">', $this->misc->printVal($args), "\n";
451
            echo '<input type="hidden" name="original_arguments" value="', htmlspecialchars($args), "\" />\n";
452
            echo "</td>\n";
453
454
            echo '<td class="data1">';
455
            if ($fndata->fields['proretset']) {
456
                echo 'setof ';
457
            }
458
459
            echo $this->misc->printVal($fndata->fields['proresult']), "\n";
460
            echo '<input type="hidden" name="original_returns" value="', htmlspecialchars($fndata->fields['proresult']), "\" />\n";
461
            if ($fndata->fields['proretset']) {
462
                echo "<input type=\"hidden\" name=\"original_setof\" value=\"yes\" />\n";
463
            }
464
465
            echo "</td>\n";
466
467
            echo '<td class="data1">', $this->misc->printVal($fndata->fields['prolanguage']), "\n";
468
            echo '<input type="hidden" name="original_lang" value="', htmlspecialchars($fndata->fields['prolanguage']), "\" />\n";
469
            echo "</td>\n";
470
            echo "</tr>\n";
471
472
            $fnlang = strtolower($fndata->fields['prolanguage']);
473
            if ('c' == $fnlang) {
474
                echo "<tr><th class=\"data required\" colspan=\"2\">{$this->lang['strobjectfile']}</th>\n";
475
                echo "<th class=\"data\" colspan=\"2\">{$this->lang['strlinksymbol']}</th></tr>\n";
476
                echo '<tr><td class="data1" colspan="2"><input type="text" name="formObjectFile" style="width:100%" value="',
477
                htmlspecialchars($_POST['formObjectFile']), "\" /></td>\n";
478
                echo '<td class="data1" colspan="2"><input type="text" name="formLinkSymbol" style="width:100%" value="',
479
                htmlspecialchars($_POST['formLinkSymbol']), "\" /></td></tr>\n";
480
            } elseif ('internal' == $fnlang) {
481
                echo "<tr><th class=\"data\" colspan=\"5\">{$this->lang['strlinksymbol']}</th></tr>\n";
482
                echo '<tr><td class="data1" colspan="5"><input type="text" name="formLinkSymbol" style="width:100%" value="',
483
                htmlspecialchars($_POST['formLinkSymbol']), "\" /></td></tr>\n";
484
            } else {
485
                echo "<tr><th class=\"data required\" colspan=\"5\">{$this->lang['strdefinition']}</th></tr>\n";
486
                echo '<tr><td class="data1" colspan="5"><textarea style="width:100%;" rows="20" cols="50" name="formDefinition">',
487
                htmlspecialchars($_POST['formDefinition']), "</textarea></td></tr>\n";
488
            }
489
490
            // Display function comment
491
            echo "<tr><th class=\"data\" colspan=\"5\">{$this->lang['strcomment']}</th></tr>\n";
492
            echo '<tr><td class="data1" colspan="5"><textarea style="width:100%;" name="formComment" rows="3" cols="50">',
493
            htmlspecialchars($_POST['formComment']), "</textarea></td></tr>\n";
494
495
            // Display function cost options
496
            if ($data->hasFunctionCosting()) {
497
                echo "<tr><th class=\"data required\" colspan=\"5\">{$this->lang['strfunctioncosting']}</th></tr>\n";
498
                echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strexecutioncost']}: <input name=\"formCost\" size=\"16\" value=\"".
499
                htmlspecialchars($_POST['formCost']).'" /></td>';
500
                echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strresultrows']}: <input name=\"formRows\" size=\"16\" value=\"",
501
                htmlspecialchars($_POST['formRows']), '"', (!$fndata->fields['proretset']) ? 'disabled' : '', '/></td>';
502
            }
503
504
            // Display function properties
505
            if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) {
506
                echo "<tr><th class=\"data\" colspan=\"5\">{$this->lang['strproperties']}</th></tr>\n";
507
                echo "<tr><td class=\"data1\" colspan=\"5\">\n";
508
                $i = 0;
509
                foreach ($data->funcprops as $k => $v) {
510
                    echo "<select name=\"formProperties[{$i}]\">\n";
511
                    foreach ($v as $p) {
512
                        echo '<option value="', htmlspecialchars($p), '"',
513
                        ($_POST['formProperties'][$i] == $p) ? ' selected="selected"' : '',
514
                        '>', $this->misc->printVal($p), "</option>\n";
515
                    }
516
                    echo "</select><br />\n";
517
                    ++$i;
518
                }
519
                echo "</td></tr>\n";
520
            }
521
522
            // function owner
523
            if ($data->hasFunctionAlterOwner()) {
524
                $users = $data->getUsers();
525
                echo "<tr><td class=\"data1\" colspan=\"5\">{$this->lang['strowner']}: <select name=\"formFuncOwn\">";
526
                while (!$users->EOF) {
527
                    $uname = $users->fields['usename'];
528
                    echo '<option value="', htmlspecialchars($uname), '"',
529
                    ($uname == $_POST['formFuncOwn']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
530
                    $users->moveNext();
531
                }
532
                echo "</select>\n";
533
                echo '<input type="hidden" name="original_owner" value="', htmlspecialchars($fndata->fields['proowner']), "\" />\n";
534
                echo "</td></tr>\n";
535
            }
536
            echo "</table>\n";
537
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
538
            echo '<input type="hidden" name="function" value="', htmlspecialchars($_REQUEST['function']), "\" />\n";
539
            echo '<input type="hidden" name="function_oid" value="', htmlspecialchars($_REQUEST['function_oid']), "\" />\n";
540
            echo $this->misc->form;
541
            echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n";
542
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
543
            echo "</form>\n";
544
        } else {
545
            echo "<p>{$this->lang['strnodata']}</p>\n";
546
        }
547
    }
548
549
    /**
550
     * Show read only properties of a function.
551
     *
552
     * @param mixed $msg
553
     */
554
    public function doProperties($msg = '')
555
    {
556
        $data = $this->misc->getDatabaseAccessor();
557
558
        $this->printTrail('function');
559
        $this->printTitle($this->lang['strproperties'], 'pg.function');
560
        $this->printMsg($msg);
561
562
        $funcdata = $data->getFunction($_REQUEST['function_oid']);
563
564
        if ($funcdata->recordCount() > 0) {
565
            // Deal with named parameters
566
            if ($data->hasNamedParams()) {
567
                if (isset($funcdata->fields['proallarguments'])) {
568
                    $args_arr = $data->phpArray($funcdata->fields['proallarguments']);
569
                } else {
570
                    $args_arr = explode(', ', $funcdata->fields['proarguments']);
571
                }
572
                $names_arr = $data->phpArray($funcdata->fields['proargnames']);
573
                $modes_arr = $data->phpArray($funcdata->fields['proargmodes']);
574
                $args      = '';
575
                $i         = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $i is dead and can be removed.
Loading history...
576
                for ($i = 0; $i < sizeof($args_arr); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
577
                    if (0 != $i) {
578
                        $args .= ', ';
579
                    }
580
581
                    if (isset($modes_arr[$i])) {
582
                        switch ($modes_arr[$i]) {
583
                            case 'i':
584
                                $args .= ' IN ';
585
586
                                break;
587
                            case 'o':
588
                                $args .= ' OUT ';
589
590
                                break;
591
                            case 'b':
592
                                $args .= ' INOUT ';
593
594
                                break;
595
                            case 'v':
596
                                $args .= ' VARIADIC ';
597
598
                                break;
599
                            case 't':
600
                                $args .= ' TABLE ';
601
602
                                break;
603
                        }
604
                    }
605
                    if (isset($names_arr[$i]) && '' != $names_arr[$i]) {
606
                        $data->fieldClean($names_arr[$i]);
607
                        $args .= '"'.$names_arr[$i].'" ';
608
                    }
609
                    $args .= $args_arr[$i];
610
                }
611
            } else {
612
                $args = $funcdata->fields['proarguments'];
613
            }
614
615
            // Show comment if any
616
            if (null !== $funcdata->fields['procomment']) {
617
                echo '<p class="comment">', $this->misc->printVal($funcdata->fields['procomment']), "</p>\n";
618
            }
619
620
            $funcdata->fields['proretset'] = $data->phpBool($funcdata->fields['proretset']);
621
            $func_full                     = $funcdata->fields['proname'].'('.$funcdata->fields['proarguments'].')';
622
            echo "<table style=\"width: 90%\">\n";
623
            echo "<tr><th class=\"data\">{$this->lang['strfunction']}</th>\n";
624
            echo "<th class=\"data\">{$this->lang['strarguments']}</th>\n";
625
            echo "<th class=\"data\">{$this->lang['strreturns']}</th>\n";
626
            echo "<th class=\"data\">{$this->lang['strproglanguage']}</th></tr>\n";
627
            echo '<tr><td class="data1">', $this->misc->printVal($funcdata->fields['proname']), "</td>\n";
628
            echo '<td class="data1">', $this->misc->printVal($args), "</td>\n";
629
            echo '<td class="data1">';
630
            if ($funcdata->fields['proretset']) {
631
                echo 'setof ';
632
            }
633
634
            echo $this->misc->printVal($funcdata->fields['proresult']), "</td>\n";
635
            echo '<td class="data1">', $this->misc->printVal($funcdata->fields['prolanguage']), "</td></tr>\n";
636
637
            $fnlang = strtolower($funcdata->fields['prolanguage']);
638
            if ('c' == $fnlang) {
639
                echo "<tr><th class=\"data\" colspan=\"2\">{$this->lang['strobjectfile']}</th>\n";
640
                echo "<th class=\"data\" colspan=\"2\">{$this->lang['strlinksymbol']}</th></tr>\n";
641
                echo '<tr><td class="data1" colspan="2">', $this->misc->printVal($funcdata->fields['probin']), "</td>\n";
642
                echo '<td class="data1" colspan="2">', $this->misc->printVal($funcdata->fields['prosrc']), "</td></tr>\n";
643
            } elseif ('internal' == $fnlang) {
644
                echo "<tr><th class=\"data\" colspan=\"4\">{$this->lang['strlinksymbol']}</th></tr>\n";
645
                echo '<tr><td class="data1" colspan="4">', $this->misc->printVal($funcdata->fields['prosrc']), "</td></tr>\n";
646
            } else {
647
                $highlight = new \PHPPgAdmin\Highlight();
648
649
                echo "<tr><th class=\"data\" colspan=\"4\">{$this->lang['strdefinition']}</th></tr>\n";
650
                // Check to see if we have syntax highlighting for this language
651
                if (array_key_exists($fnlang, $data->langmap)) {
652
                    $temp = $highlight->syntax_highlight(htmlspecialchars($funcdata->fields['prosrc']), $data->langmap[$fnlang]);
653
                    $tag  = 'prenoescape';
654
                } else {
655
                    $temp = $funcdata->fields['prosrc'];
656
                    $tag  = 'pre';
657
                }
658
                echo '<tr><td class="data1" colspan="4">', $this->misc->printVal($temp, $tag, ['lineno' => true, 'class' => 'data1']), "</td></tr>\n";
659
            }
660
661
            // Display function cost options
662
            if ($data->hasFunctionCosting()) {
663
                echo "<tr><th class=\"data required\" colspan=\"4\">{$this->lang['strfunctioncosting']}</th></tr>\n";
664
                echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strexecutioncost']}: ", $this->misc->printVal($funcdata->fields['procost']), ' </td>';
665
                echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strresultrows']}: ", $this->misc->printVal($funcdata->fields['prorows']), ' </td>';
666
            }
667
668
            // Show flags
669
            if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) {
670
                // Fetch an array of the function properties
671
                $funcprops = $data->getFunctionProperties($funcdata->fields);
672
                echo "<tr><th class=\"data\" colspan=\"4\">{$this->lang['strproperties']}</th></tr>\n";
673
                echo "<tr><td class=\"data1\" colspan=\"4\">\n";
674
                foreach ($funcprops as $v) {
675
                    echo $this->misc->printVal($v), "<br />\n";
676
                }
677
                echo "</td></tr>\n";
678
            }
679
680
            echo "<tr><td class=\"data1\" colspan=\"5\">{$this->lang['strowner']}: ", htmlspecialchars($funcdata->fields['proowner']), "\n";
681
            echo "</td></tr>\n";
682
            echo "</table>\n";
683
        } else {
684
            echo "<p>{$this->lang['strnodata']}</p>\n";
685
        }
686
687
        $navlinks = [
688
            'showall' => [
689
                'attr'    => [
690
                    'href' => [
691
                        'url'     => 'functions',
692
                        'urlvars' => [
693
                            'server'   => $_REQUEST['server'],
694
                            'database' => $_REQUEST['database'],
695
                            'schema'   => $_REQUEST['schema'],
696
                        ],
697
                    ],
698
                ],
699
                'content' => $this->lang['strshowallfunctions'],
700
            ],
701
            'alter'   => [
702
                'attr'    => [
703
                    'href' => [
704
                        'url'     => 'functions',
705
                        'urlvars' => [
706
                            'action'       => 'edit',
707
                            'server'       => $_REQUEST['server'],
708
                            'database'     => $_REQUEST['database'],
709
                            'schema'       => $_REQUEST['schema'],
710
                            'function'     => $_REQUEST['function'],
711
                            'function_oid' => $_REQUEST['function_oid'],
712
                        ],
713
                    ],
714
                ],
715
                'content' => $this->lang['stralter'],
716
            ],
717
            'drop'    => [
718
                'attr'    => [
719
                    'href' => [
720
                        'url'     => 'functions',
721
                        'urlvars' => [
722
                            'action'       => 'confirm_drop',
723
                            'server'       => $_REQUEST['server'],
724
                            'database'     => $_REQUEST['database'],
725
                            'schema'       => $_REQUEST['schema'],
726
                            'function'     => $func_full,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $func_full does not seem to be defined for all execution paths leading up to this point.
Loading history...
727
                            'function_oid' => $_REQUEST['function_oid'],
728
                        ],
729
                    ],
730
                ],
731
                'content' => $this->lang['strdrop'],
732
            ],
733
        ];
734
735
        $this->printNavLinks($navlinks, 'functions-properties', get_defined_vars());
736
    }
737
738
    /**
739
     * Show confirmation of drop and perform actual drop.
740
     *
741
     * @param mixed $confirm
742
     */
743
    public function doDrop($confirm)
744
    {
745
        $data = $this->misc->getDatabaseAccessor();
746
747
        if (empty($_REQUEST['function']) && empty($_REQUEST['ma'])) {
748
            return $this->doDefault($this->lang['strspecifyfunctiontodrop']);
749
        }
750
751
        if ($confirm) {
752
            $this->printTrail('schema');
753
            $this->printTitle($this->lang['strdrop'], 'pg.function.drop');
754
755
            echo '<form action="'.\SUBFOLDER."/src/views/functions\" method=\"post\">\n";
756
757
            //If multi drop
758
            if (isset($_REQUEST['ma'])) {
759
                foreach ($_REQUEST['ma'] as $v) {
760
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
761
                    echo '<p>', sprintf($this->lang['strconfdropfunction'], $this->misc->printVal($a['function'])), "</p>\n";
762
                    echo '<input type="hidden" name="function[]" value="', htmlspecialchars($a['function']), "\" />\n";
763
                    echo '<input type="hidden" name="function_oid[]" value="', htmlspecialchars($a['function_oid']), "\" />\n";
764
                }
765
            } else {
766
                echo '<p>', sprintf($this->lang['strconfdropfunction'], $this->misc->printVal($_REQUEST['function'])), "</p>\n";
767
                echo '<input type="hidden" name="function" value="', htmlspecialchars($_REQUEST['function']), "\" />\n";
768
                echo '<input type="hidden" name="function_oid" value="', htmlspecialchars($_REQUEST['function_oid']), "\" />\n";
769
            }
770
771
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
772
773
            echo $this->misc->form;
774
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /><label for=\"cascade\">{$this->lang['strcascade']}</label></p>\n";
775
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />\n";
776
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n";
777
            echo "</form>\n";
778
        } else {
779
            if (is_array($_POST['function_oid'])) {
780
                $msg    = '';
781
                $status = $data->beginTransaction();
782
                if (0 == $status) {
783
                    foreach ($_POST['function_oid'] as $k => $s) {
784
                        $status = $data->dropFunction($s, isset($_POST['cascade']));
785
                        if (0 == $status) {
786
                            $msg .= sprintf('%s: %s<br />', htmlentities($_POST['function'][$k], ENT_QUOTES, 'UTF-8'), $this->lang['strfunctiondropped']);
787
                        } else {
788
                            $data->endTransaction();
789
                            $this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($_POST['function'][$k], ENT_QUOTES, 'UTF-8'), $this->lang['strfunctiondroppedbad']));
790
791
                            return;
792
                        }
793
                    }
794
                }
795
                if (0 == $data->endTransaction()) {
796
                    // Everything went fine, back to the Default page....
797
                    $this->misc->setReloadBrowser(true);
798
                    $this->doDefault($msg);
799
                } else {
800
                    $this->doDefault($this->lang['strfunctiondroppedbad']);
801
                }
802
            } else {
803
                $status = $data->dropFunction($_POST['function_oid'], isset($_POST['cascade']));
804
                if (0 == $status) {
805
                    $this->misc->setReloadBrowser(true);
806
                    $this->doDefault($this->lang['strfunctiondropped']);
807
                } else {
808
                    $this->doDefault($this->lang['strfunctiondroppedbad']);
809
                }
810
            }
811
        }
812
    }
813
814
    /**
815
     * Displays a screen where they can enter a new function.
816
     *
817
     * @param mixed $msg
818
     * @param mixed $szJS
819
     */
820
    public function doCreate($msg = '', $szJS = '')
821
    {
822
        $data = $this->misc->getDatabaseAccessor();
823
824
        $this->printTrail('schema');
825
        if (!isset($_POST['formFunction'])) {
826
            $_POST['formFunction'] = '';
827
        }
828
829
        if (!isset($_POST['formArguments'])) {
830
            $_POST['formArguments'] = '';
831
        }
832
833
        if (!isset($_POST['formReturns'])) {
834
            $_POST['formReturns'] = '';
835
        }
836
837
        if (!isset($_POST['formLanguage'])) {
838
            $_POST['formLanguage'] = isset($_REQUEST['language']) ? $_REQUEST['language'] : 'sql';
839
        }
840
841
        if (!isset($_POST['formDefinition'])) {
842
            $_POST['formDefinition'] = '';
843
        }
844
845
        if (!isset($_POST['formObjectFile'])) {
846
            $_POST['formObjectFile'] = '';
847
        }
848
849
        if (!isset($_POST['formLinkSymbol'])) {
850
            $_POST['formLinkSymbol'] = '';
851
        }
852
853
        if (!isset($_POST['formProperties'])) {
854
            $_POST['formProperties'] = $data->defaultprops;
855
        }
856
857
        if (!isset($_POST['formSetOf'])) {
858
            $_POST['formSetOf'] = '';
859
        }
860
861
        if (!isset($_POST['formArray'])) {
862
            $_POST['formArray'] = '';
863
        }
864
865
        if (!isset($_POST['formCost'])) {
866
            $_POST['formCost'] = '';
867
        }
868
869
        if (!isset($_POST['formRows'])) {
870
            $_POST['formRows'] = '';
871
        }
872
873
        if (!isset($_POST['formComment'])) {
874
            $_POST['formComment'] = '';
875
        }
876
877
        $types  = $data->getTypes(true, true, true);
878
        $langs  = $data->getLanguages(true);
879
        $fnlang = strtolower($_POST['formLanguage']);
880
881
        switch ($fnlang) {
882
            case 'c':
883
                $this->printTitle($this->lang['strcreatecfunction'], 'pg.function.create.c');
884
885
                break;
886
            case 'internal':
887
                $this->printTitle($this->lang['strcreateinternalfunction'], 'pg.function.create.internal');
888
889
                break;
890
            default:
891
                $this->printTitle($this->lang['strcreateplfunction'], 'pg.function.create.pl');
892
893
                break;
894
        }
895
        $this->printMsg($msg);
896
897
        // Create string for return type list
898
        $szTypes = '';
899
        while (!$types->EOF) {
900
            $szSelected = '';
901
            if ($types->fields['typname'] == $_POST['formReturns']) {
902
                $szSelected = ' selected="selected"';
903
            }
904
            // this variable is include in the JS code bellow, so we need to ENT_QUOTES
905
            $szTypes .= '<option value="'.htmlspecialchars($types->fields['typname'], ENT_QUOTES)."\"{$szSelected}>";
906
            $szTypes .= htmlspecialchars($types->fields['typname'], ENT_QUOTES).'</option>';
907
            $types->moveNext();
908
        }
909
910
        $szFunctionName = "<td class=\"data1\"><input name=\"formFunction\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"".
911
        htmlspecialchars($_POST['formFunction']).'" /></td>';
912
913
        $szArguments = '<td class="data1"><input name="formArguments" style="width:100%;" size="16" value="'.
1 ignored issue
show
Unused Code introduced by
The assignment to $szArguments is dead and can be removed.
Loading history...
914
        htmlspecialchars($_POST['formArguments']).'" /></td>';
915
916
        $szSetOfSelected    = '';
917
        $szNotSetOfSelected = '';
918
        if ('' == $_POST['formSetOf']) {
919
            $szNotSetOfSelected = ' selected="selected"';
920
        } elseif ('SETOF' == $_POST['formSetOf']) {
921
            $szSetOfSelected = ' selected="selected"';
922
        }
923
        $szReturns = '<td class="data1" colspan="2">';
924
        $szReturns .= '<select name="formSetOf">';
925
        $szReturns .= "<option value=\"\"{$szNotSetOfSelected}></option>";
926
        $szReturns .= "<option value=\"SETOF\"{$szSetOfSelected}>SETOF</option>";
927
        $szReturns .= '</select>';
928
929
        $szReturns .= '<select name="formReturns">'.$szTypes.'</select>';
930
931
        // Create string array type selector
932
933
        $szArraySelected    = '';
934
        $szNotArraySelected = '';
935
        if ('' == $_POST['formArray']) {
936
            $szNotArraySelected = ' selected="selected"';
937
        } elseif ('[]' == $_POST['formArray']) {
938
            $szArraySelected = ' selected="selected"';
939
        }
940
941
        $szReturns .= '<select name="formArray">';
942
        $szReturns .= "<option value=\"\"{$szNotArraySelected}></option>";
943
        $szReturns .= "<option value=\"[]\"{$szArraySelected}>[ ]</option>";
944
        $szReturns .= "</select>\n</td>";
945
946
        // Create string for language
947
        $szLanguage = '<td class="data1">';
948
        if ('c' == $fnlang || 'internal' == $fnlang) {
949
            $szLanguage .= $_POST['formLanguage']."\n";
950
            $szLanguage .= "<input type=\"hidden\" name=\"formLanguage\" value=\"{$_POST['formLanguage']}\" />\n";
951
        } else {
952
            $szLanguage .= "<select name=\"formLanguage\">\n";
953
            while (!$langs->EOF) {
954
                $szSelected = '';
955
                if ($langs->fields['lanname'] == $_POST['formLanguage']) {
956
                    $szSelected = ' selected="selected"';
957
                }
958
                if ('c' != strtolower($langs->fields['lanname']) && 'internal' != strtolower($langs->fields['lanname'])) {
959
                    $szLanguage .= '<option value="'.htmlspecialchars($langs->fields['lanname'])."\"{$szSelected}>\n".
960
                    $this->misc->printVal($langs->fields['lanname']).'</option>';
961
                }
962
963
                $langs->moveNext();
964
            }
965
            $szLanguage .= "</select>\n";
966
        }
967
968
        $szLanguage .= '</td>';
969
        $szJSArguments = "<tr><th class=\"data\" colspan=\"7\">{$this->lang['strarguments']}</th></tr>";
970
        $arrayModes    = ['IN', 'OUT', 'INOUT'];
971
        $szModes       = '<select name="formArgModes[]" style="width:100%;">';
972
        foreach ($arrayModes as $pV) {
973
            $szModes .= "<option value=\"{$pV}\">{$pV}</option>";
974
        }
975
        $szModes .= '</select>';
976
        $szArgReturns = '<select name="formArgArray[]">';
977
        $szArgReturns .= '<option value=""></option>';
978
        $szArgReturns .= '<option value="[]">[]</option>';
979
        $szArgReturns .= '</select>';
980
        if (!empty($this->conf['theme'])) {
981
            $szImgPath = "images/themes/{$this->conf['theme']}";
982
        } else {
983
            $szImgPath = 'images/themes/default';
984
        }
985
        if (empty($msg)) {
986
            $szJSTRArg = "<script type=\"text/javascript\" >addArg();</script>\n";
987
        } else {
988
            $szJSTRArg = '';
989
        }
990
        $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\">{$this->lang['strargadd']}</span></td></tr></table></td>\n</tr>\n";
991
992
        echo '<script src="'.\SUBFOLDER."/js/functions.js\" type=\"text/javascript\"></script>
993
		<script type=\"text/javascript\">
994
			//<![CDATA[
995
			var g_types_select = '<select name=\"formArgType[]\">{$szTypes}</select>{$szArgReturns}';
996
			var g_modes_select = '{$szModes}';
997
			var g_name = '';
998
			var g_lang_strargremove = '", htmlspecialchars($this->lang['strargremove'], ENT_QUOTES), "';
999
			var g_lang_strargnoargs = '", htmlspecialchars($this->lang['strargnoargs'], ENT_QUOTES), "';
1000
			var g_lang_strargenableargs = '", htmlspecialchars($this->lang['strargenableargs'], ENT_QUOTES), "';
1001
			var g_lang_strargnorowabove = '", htmlspecialchars($this->lang['strargnorowabove'], ENT_QUOTES), "';
1002
			var g_lang_strargnorowbelow = '", htmlspecialchars($this->lang['strargnorowbelow'], ENT_QUOTES), "';
1003
			var g_lang_strargremoveconfirm = '", htmlspecialchars($this->lang['strargremoveconfirm'], ENT_QUOTES), "';
1004
			var g_lang_strargraise = '", htmlspecialchars($this->lang['strargraise'], ENT_QUOTES), "';
1005
			var g_lang_strarglower = '", htmlspecialchars($this->lang['strarglower'], ENT_QUOTES), "';
1006
			//]]>
1007
		</script>
1008
		";
1009
        echo '<form action="'.\SUBFOLDER."/src/views//views/functions\" method=\"post\">\n";
1010
        echo "<table><tbody id=\"args_table\">\n";
1011
        echo "<tr><th class=\"data required\">{$this->lang['strname']}</th>\n";
1012
        echo "<th class=\"data required\" colspan=\"2\">{$this->lang['strreturns']}</th>\n";
1013
        echo "<th class=\"data required\">{$this->lang['strproglanguage']}</th></tr>\n";
1014
        echo "<tr>\n";
1015
        echo "{$szFunctionName}\n";
1016
        echo "{$szReturns}\n";
1017
        echo "{$szLanguage}\n";
1018
        echo "</tr>\n";
1019
        echo "{$szJSArguments}\n";
1020
        echo "<tr>\n";
1021
        echo "<th class=\"data required\">{$this->lang['strargmode']}</th>\n";
1022
        echo "<th class=\"data required\">{$this->lang['strname']}</th>\n";
1023
        echo "<th class=\"data required\" colspan=\"2\">{$this->lang['strargtype']}</th>\n";
1024
        echo "</tr>\n";
1025
        echo "{$szJSAddTR}\n";
1026
1027
        if ('c' == $fnlang) {
1028
            echo "<tr><th class=\"data required\" colspan=\"2\">{$this->lang['strobjectfile']}</th>\n";
1029
            echo "<th class=\"data\" colspan=\"2\">{$this->lang['strlinksymbol']}</th></tr>\n";
1030
            echo '<tr><td class="data1" colspan="2"><input type="text" name="formObjectFile" style="width:100%" value="',
1031
            htmlspecialchars($_POST['formObjectFile']), "\" /></td>\n";
1032
            echo '<td class="data1" colspan="2"><input type="text" name="formLinkSymbol" style="width:100%" value="',
1033
            htmlspecialchars($_POST['formLinkSymbol']), "\" /></td></tr>\n";
1034
        } elseif ('internal' == $fnlang) {
1035
            echo "<tr><th class=\"data\" colspan=\"4\">{$this->lang['strlinksymbol']}</th></tr>\n";
1036
            echo '<tr><td class="data1" colspan="4"><input type="text" name="formLinkSymbol" style="width:100%" value="',
1037
            htmlspecialchars($_POST['formLinkSymbol']), "\" /></td></tr>\n";
1038
        } else {
1039
            echo "<tr><th class=\"data required\" colspan=\"4\">{$this->lang['strdefinition']}</th></tr>\n";
1040
            echo '<tr><td class="data1" colspan="4"><textarea style="width:100%;" rows="20" cols="50" name="formDefinition">',
1041
            htmlspecialchars($_POST['formDefinition']), "</textarea></td></tr>\n";
1042
        }
1043
1044
        // Display function comment
1045
        echo "<tr><th class=\"data\" colspan=\"4\">{$this->lang['strcomment']}</th></tr>\n";
1046
        echo '<tr><td class="data1" colspan="4"><textarea style="width:100%;" name="formComment" rows="3" cols="50">',
1047
        htmlspecialchars($_POST['formComment']), "</textarea></td></tr>\n";
1048
1049
        // Display function cost options
1050
        if ($data->hasFunctionCosting()) {
1051
            echo "<tr><th class=\"data required\" colspan=\"4\">{$this->lang['strfunctioncosting']}</th></tr>\n";
1052
            echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strexecutioncost']}: <input name=\"formCost\" size=\"16\" value=\"".
1053
            htmlspecialchars($_POST['formCost']).'" /></td>';
1054
            echo "<td class=\"data1\" colspan=\"2\">{$this->lang['strresultrows']}: <input name=\"formRows\" size=\"16\" value=\"".
1055
            htmlspecialchars($_POST['formRows']).'" /></td>';
1056
        }
1057
1058
        // Display function properties
1059
        if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) {
1060
            echo "<tr><th class=\"data required\" colspan=\"4\">{$this->lang['strproperties']}</th></tr>\n";
1061
            echo "<tr><td class=\"data1\" colspan=\"4\">\n";
1062
            $i = 0;
1063
            foreach ($data->funcprops as $k => $v) {
1064
                echo "<select name=\"formProperties[{$i}]\">\n";
1065
                foreach ($v as $p) {
1066
                    echo '<option value="', htmlspecialchars($p), '"',
1067
                    ($_POST['formProperties'][$i] == $p) ? ' selected="selected"' : '',
1068
                    '>', $this->misc->printVal($p), "</option>\n";
1069
                }
1070
                echo "</select><br />\n";
1071
                ++$i;
1072
            }
1073
            echo "</td></tr>\n";
1074
        }
1075
        echo "</tbody></table>\n";
1076
        echo $szJSTRArg;
1077
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
1078
        echo $this->misc->form;
1079
        echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n";
1080
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
1081
        echo "</form>\n";
1082
        echo $szJS;
1083
    }
1084
1085
    /**
1086
     * Actually creates the new function in the database.
1087
     */
1088
    public function doSaveCreate()
1089
    {
1090
        $data = $this->misc->getDatabaseAccessor();
1091
1092
        $fnlang = strtolower($_POST['formLanguage']);
1093
1094
        if ('c' == $fnlang) {
1095
            $def = [$_POST['formObjectFile'], $_POST['formLinkSymbol']];
1096
        } elseif ('internal' == $fnlang) {
1097
            $def = $_POST['formLinkSymbol'];
1098
        } else {
1099
            $def = $_POST['formDefinition'];
1100
        }
1101
1102
        $szJS = '';
1103
1104
        echo '<script src="'.\SUBFOLDER.'/js/functions.js" type="text/javascript"></script>';
1105
        echo '<script type="text/javascript">'.$this->buildJSData().'</script>';
1106
        if (!empty($_POST['formArgName'])) {
1107
            $szJS = $this->buildJSRows($this->buildFunctionArguments($_POST));
1108
        } else {
1109
            $szJS = '<script type="text/javascript" src="'.\SUBFOLDER.'/js/functions.js">noArgsRebuild(addArg());</script>';
1110
        }
1111
1112
        $cost = (isset($_POST['formCost'])) ? $_POST['formCost'] : null;
1113
        if ('' == $cost || !is_numeric($cost) || $cost != (int) $cost || $cost < 0) {
1114
            $cost = null;
1115
        }
1116
1117
        $rows = (isset($_POST['formRows'])) ? $_POST['formRows'] : null;
1118
        if ('' == $rows || !is_numeric($rows) || $rows != (int) $rows) {
1119
            $rows = null;
1120
        }
1121
1122
        // Check that they've given a name and a definition
1123
        if ('' == $_POST['formFunction']) {
1124
            $this->doCreate($this->lang['strfunctionneedsname'], $szJS);
1125
        } elseif ('internal' != $fnlang && !$def) {
1126
            $this->doCreate($this->lang['strfunctionneedsdef'], $szJS);
1127
        } else {
1128
            // Append array symbol to type if chosen
1129
            $status = $data->createFunction(
1130
                $_POST['formFunction'],
1131
                empty($_POST['nojs']) ? $this->buildFunctionArguments($_POST) : $_POST['formArguments'],
1132
                $_POST['formReturns'].$_POST['formArray'],
1133
                $def,
1134
                $_POST['formLanguage'],
1135
                $_POST['formProperties'],
1136
                'SETOF' == $_POST['formSetOf'],
1137
                $cost,
1138
                $rows,
1139
                $_POST['formComment'],
1140
                false
1141
            );
1142
            if (0 == $status) {
1143
                $this->doDefault($this->lang['strfunctioncreated']);
1144
            } else {
1145
                $this->doCreate($this->lang['strfunctioncreatedbad'], $szJS);
1146
            }
1147
        }
1148
    }
1149
1150
    /**
1151
     * Build out the function arguments string.
1152
     *
1153
     * @param mixed $arrayVars
1154
     */
1155
    private function buildFunctionArguments($arrayVars)
1 ignored issue
show
Coding Style introduced by
Private method name "FunctionsController::buildFunctionArguments" must be prefixed with an underscore
Loading history...
1156
    {
1157
        if (isset($_POST['formArgName'])) {
1158
            $arrayArgs = [];
1159
            foreach ($arrayVars['formArgName'] as $pK => $pV) {
1160
                $arrayArgs[] = $arrayVars['formArgModes'][$pK].' '.trim($pV).' '.trim($arrayVars['formArgType'][$pK]).$arrayVars['formArgArray'][$pK];
1161
            }
1162
1163
            return implode(',', $arrayArgs);
1164
        }
1165
1166
        return '';
1167
    }
1168
1169
    /**
1170
     * Build out JS to re-create table rows for arguments.
1171
     *
1172
     * @param mixed $szArgs
1173
     */
1174
    private function buildJSRows($szArgs)
1 ignored issue
show
Coding Style introduced by
Private method name "FunctionsController::buildJSRows" must be prefixed with an underscore
Loading history...
1175
    {
1176
        $arrayModes      = ['IN', 'OUT', 'INOUT'];
1177
        $arrayArgs       = explode(',', $szArgs);
1178
        $arrayProperArgs = [];
1179
        $nC              = 0;
1180
        $szReturn        = '';
1181
        foreach ($arrayArgs as $pV) {
1182
            $arrayWords = explode(' ', $pV);
1183
            if (true === in_array($arrayWords[0], $arrayModes, true)) {
1184
                $szMode = $arrayWords[0];
1185
                array_shift($arrayWords);
1186
            }
1187
            $szArgName = array_shift($arrayWords);
1188
            if (false === strpos($arrayWords[count($arrayWords) - 1], '[]')) {
1189
                $szArgType   = implode(' ', $arrayWords);
1190
                $bArgIsArray = 'false';
1191
            } else {
1192
                $szArgType   = str_replace('[]', '', implode(' ', $arrayWords));
1193
                $bArgIsArray = 'true';
1194
            }
1195
            $arrayProperArgs[] = [$szMode, $szArgName, $szArgType, $bArgIsArray];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $szMode does not seem to be defined for all execution paths leading up to this point.
Loading history...
1196
            $szReturn .= "<script type=\"text/javascript\">RebuildArgTR('{$szMode}','{$szArgName}','{$szArgType}',new Boolean({$bArgIsArray}));</script>";
1197
            ++$nC;
1198
        }
1199
1200
        return $szReturn;
1201
    }
1202
1203
    private function buildJSData()
1 ignored issue
show
Coding Style introduced by
Private method name "FunctionsController::buildJSData" must be prefixed with an underscore
Loading history...
1204
    {
1205
        $data = $this->misc->getDatabaseAccessor();
1206
1207
        $arrayModes  = ['IN', 'OUT', 'INOUT'];
1208
        $arrayTypes  = $data->getTypes(true, true, true);
1209
        $arrayPTypes = [];
1210
        $arrayPModes = [];
1211
        $szTypes     = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $szTypes is dead and can be removed.
Loading history...
1212
1213
        while (!$arrayTypes->EOF) {
1214
            $arrayPTypes[] = "'".$arrayTypes->fields['typname']."'";
1215
            $arrayTypes->moveNext();
1216
        }
1217
1218
        foreach ($arrayModes as $pV) {
1219
            $arrayPModes[] = "'{$pV}'";
1220
        }
1221
1222
        $szTypes = 'g_main_types = new Array('.implode(',', $arrayPTypes).');';
1223
        $szModes = 'g_main_modes = new Array('.implode(',', $arrayPModes).');';
1224
1225
        return $szTypes.$szModes;
1226
    }
1227
}
1228