Passed
Pull Request — develop (#92)
by Felipe
04:47
created

FunctionsController::doSaveEdit()   C

Complexity

Conditions 9
Paths 18

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 36
nc 18
nop 0
dl 0
loc 51
rs 6.2727
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

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