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

FunctionsController::buildJSRows()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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