SequencesController::doSaveSetval()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
14
class SequencesController extends BaseController
15
{
16
    public $controller_title = 'strsequences';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        if ('tree' === $this->action) {
24
            return $this->doTree();
25
        }
26
27
        // Print header
28
29
        $this->printHeader();
30
        $this->printBody();
31
32
        switch ($this->action) {
33
            case 'create':
34
                $this->doCreateSequence();
35
36
                break;
37
            case 'save_create_sequence':
38
                if (null !== $this->getPostParam('create')) {
39
                    $this->doSaveCreateSequence();
40
                } else {
41
                    $this->doDefault();
42
                }
43
44
                break;
45
            case 'properties':
46
                $this->doProperties();
47
48
                break;
49
            case 'drop':
50
                if (null !== $this->getPostParam('drop')) {
51
                    $this->doDrop(false);
52
                } else {
53
                    $this->doDefault();
54
                }
55
56
                break;
57
            case 'confirm_drop':
58
                $this->doDrop(true);
59
60
                break;
61
            case 'restart':
62
                $this->doRestart();
63
64
                break;
65
            case 'reset':
66
                $this->doReset();
67
68
                break;
69
            case 'nextval':
70
                $this->doNextval();
71
72
                break;
73
            case 'setval':
74
                if (isset($_POST['setval'])) {
75
                    $this->doSaveSetval();
76
                } else {
77
                    $this->doDefault();
78
                }
79
80
                break;
81
            case 'confirm_setval':
82
                $this->doSetval();
83
84
                break;
85
            case 'alter':
86
                if (null !== $this->getPostParam('alter')) {
87
                    $this->doSaveAlter();
88
                } else {
89
                    $this->doDefault();
90
                }
91
92
                break;
93
            case 'confirm_alter':
94
                $this->doAlter();
95
96
                break;
97
98
            default:
99
                $this->doDefault();
100
101
                break;
102
        }
103
104
        // Print footer
105
        return $this->printFooter();
106
    }
107
108
    /**
109
     * Display list of all sequences in the database/schema.
110
     *
111
     * @param mixed $msg
112
     */
113
    public function doDefault($msg = ''): void
114
    {
115
        $data = $this->misc->getDatabaseAccessor();
116
117
        $this->printTrail('schema');
118
        $this->printTabs('schema', 'sequences');
119
        $this->printMsg($msg);
120
121
        // Get all sequences
122
        $sequences = $data->getSequences();
123
124
        $columns = [
125
            'sequence' => [
126
                'title' => $this->lang['strsequence'],
127
                'field' => Decorator::field('seqname'),
128
                'url' => "sequences?action=properties&amp;{$this->misc->href}&amp;",
129
                'vars' => ['sequence' => 'seqname'],
130
            ],
131
            'owner' => [
132
                'title' => $this->lang['strowner'],
133
                'field' => Decorator::field('seqowner'),
134
            ],
135
            'actions' => [
136
                'title' => $this->lang['stractions'],
137
            ],
138
            'comment' => [
139
                'title' => $this->lang['strcomment'],
140
                'field' => Decorator::field('seqcomment'),
141
            ],
142
        ];
143
144
        $actions = [
145
            'multiactions' => [
146
                'keycols' => ['sequence' => 'seqname'],
147
                'url' => 'sequences',
148
            ],
149
            'alter' => [
150
                'content' => $this->lang['stralter'],
151
                'attr' => [
152
                    'href' => [
153
                        'url' => 'sequences',
154
                        'urlvars' => [
155
                            'action' => 'confirm_alter',
156
                            'subject' => 'sequence',
157
                            'sequence' => Decorator::field('seqname'),
158
                        ],
159
                    ],
160
                ],
161
            ],
162
            'drop' => [
163
                'content' => $this->lang['strdrop'],
164
                'attr' => [
165
                    'href' => [
166
                        'url' => 'sequences',
167
                        'urlvars' => [
168
                            'action' => 'confirm_drop',
169
                            'sequence' => Decorator::field('seqname'),
170
                        ],
171
                    ],
172
                ],
173
                'multiaction' => 'confirm_drop',
174
            ],
175
            'privileges' => [
176
                'content' => $this->lang['strprivileges'],
177
                'attr' => [
178
                    'href' => [
179
                        'url' => 'privileges',
180
                        'urlvars' => [
181
                            'subject' => 'sequence',
182
                            'sequence' => Decorator::field('seqname'),
183
                        ],
184
                    ],
185
                ],
186
            ],
187
        ];
188
189
        echo $this->printTable($sequences, $columns, $actions, 'sequences-sequences', $this->lang['strnosequences']);
0 ignored issues
show
Bug introduced by
It seems like $sequences can also be of type integer; however, parameter $tabledata of PHPPgAdmin\Controller\BaseController::printTable() does only seem to accept ADORecordSet|PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

189
        echo $this->printTable(/** @scrutinizer ignore-type */ $sequences, $columns, $actions, 'sequences-sequences', $this->lang['strnosequences']);
Loading history...
190
191
        $this->printNavLinks(['create' => [
192
            'attr' => [
193
                'href' => [
194
                    'url' => 'sequences',
195
                    'urlvars' => [
196
                        'action' => 'create',
197
                        'server' => $_REQUEST['server'],
198
                        'database' => $_REQUEST['database'],
199
                        'schema' => $_REQUEST['schema'],
200
                    ],
201
                ],
202
            ],
203
            'content' => $this->lang['strcreatesequence'],
204
        ]], 'sequences-sequences', \get_defined_vars());
205
    }
206
207
    /**
208
     * Generate XML for the browser tree.
209
     */
210
    public function doTree()
211
    {
212
        $data = $this->misc->getDatabaseAccessor();
213
214
        $sequences = $data->getSequences();
215
        $reqvars = $this->misc->getRequestVars('sequence');
216
217
        $attrs = [
218
            'text' => Decorator::field('seqname'),
219
            'icon' => 'Sequence',
220
            'toolTip' => Decorator::field('seqcomment'),
221
            'iconAction' => Decorator::url('sequences', $reqvars, ['action' => 'properties', 'sequence' => Decorator::field('seqname')]),
222
            'action' => Decorator::url('sequences', $reqvars, ['action' => 'properties', 'sequence' => Decorator::field('seqname')]),
223
        ];
224
225
        return $this->printTree($sequences, $attrs, 'sequences');
0 ignored issues
show
Bug introduced by
It seems like $sequences can also be of type integer; however, parameter $_treedata of PHPPgAdmin\Controller\BaseController::printTree() does only seem to accept PHPPgAdmin\ADORecordSet|PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

225
        return $this->printTree(/** @scrutinizer ignore-type */ $sequences, $attrs, 'sequences');
Loading history...
226
    }
227
228
    /**
229
     * Display the properties of a sequence.
230
     *
231
     * @param mixed $msg
232
     */
233
    public function doProperties($msg = ''): void
234
    {
235
        $data = $this->misc->getDatabaseAccessor();
236
        $this->printTrail('sequence');
237
        $this->printTitle($this->lang['strproperties'], 'pg.sequence');
238
        $this->printMsg($msg);
239
240
        // Fetch the sequence information
241
        $sequence = $data->getSequence($_REQUEST['sequence']);
242
243
        if (\is_object($sequence) && 0 < $sequence->recordCount()) {
244
            $sequence->fields['is_cycled'] = $data->phpBool($sequence->fields['is_cycled']);
245
            $sequence->fields['is_called'] = $data->phpBool($sequence->fields['is_called']);
246
247
            // Show comment if any
248
            if (null !== $sequence->fields['seqcomment']) {
249
                echo '<p class="comment">', $this->misc->printVal($sequence->fields['seqcomment']), '</p>' . \PHP_EOL;
250
            }
251
252
            echo '<table border="0">';
253
            echo "<tr><th class=\"data\">{$this->lang['strname']}</th>";
254
255
            if ($data->hasAlterSequenceStart()) {
256
                echo "<th class=\"data\">{$this->lang['strstartvalue']}</th>";
257
            }
258
            echo "<th class=\"data\">{$this->lang['strlastvalue']}</th>";
259
            echo "<th class=\"data\">{$this->lang['strincrementby']}</th>";
260
            echo "<th class=\"data\">{$this->lang['strmaxvalue']}</th>";
261
            echo "<th class=\"data\">{$this->lang['strminvalue']}</th>";
262
            echo "<th class=\"data\">{$this->lang['strcachevalue']}</th>";
263
            echo "<th class=\"data\">{$this->lang['strlogcount']}</th>";
264
            echo "<th class=\"data\">{$this->lang['strcancycle']}</th>";
265
            echo "<th class=\"data\">{$this->lang['striscalled']}</th></tr>";
266
            echo '<tr>';
267
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['seqname']), '</td>';
268
269
            if ($data->hasAlterSequenceStart()) {
270
                echo '<td class="data1">', $this->misc->printVal($sequence->fields['start_value']), '</td>';
271
            }
272
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['last_value']), '</td>';
273
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['increment_by']), '</td>';
274
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['max_value']), '</td>';
275
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['min_value']), '</td>';
276
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['cache_value']), '</td>';
277
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['log_cnt']), '</td>';
278
            echo '<td class="data1">', ($sequence->fields['is_cycled'] ? $this->lang['stryes'] : $this->lang['strno']), '</td>';
279
            echo '<td class="data1">', ($sequence->fields['is_called'] ? $this->lang['stryes'] : $this->lang['strno']), '</td>';
280
            echo '</tr>';
281
            echo '</table>';
282
283
            $navlinks = [
284
                'alter' => [
285
                    'attr' => [
286
                        'href' => [
287
                            'url' => 'sequences',
288
                            'urlvars' => [
289
                                'action' => 'confirm_alter',
290
                                'server' => $_REQUEST['server'],
291
                                'database' => $_REQUEST['database'],
292
                                'schema' => $_REQUEST['schema'],
293
                                'sequence' => $sequence->fields['seqname'],
294
                            ],
295
                        ],
296
                    ],
297
                    'content' => $this->lang['stralter'],
298
                ],
299
                'setval' => [
300
                    'attr' => [
301
                        'href' => [
302
                            'url' => 'sequences',
303
                            'urlvars' => [
304
                                'action' => 'confirm_setval',
305
                                'server' => $_REQUEST['server'],
306
                                'database' => $_REQUEST['database'],
307
                                'schema' => $_REQUEST['schema'],
308
                                'sequence' => $sequence->fields['seqname'],
309
                            ],
310
                        ],
311
                    ],
312
                    'content' => $this->lang['strsetval'],
313
                ],
314
                'nextval' => [
315
                    'attr' => [
316
                        'href' => [
317
                            'url' => 'sequences',
318
                            'urlvars' => [
319
                                'action' => 'nextval',
320
                                'server' => $_REQUEST['server'],
321
                                'database' => $_REQUEST['database'],
322
                                'schema' => $_REQUEST['schema'],
323
                                'sequence' => $sequence->fields['seqname'],
324
                            ],
325
                        ],
326
                    ],
327
                    'content' => $this->lang['strnextval'],
328
                ],
329
                'restart' => [
330
                    'attr' => [
331
                        'href' => [
332
                            'url' => 'sequences',
333
                            'urlvars' => [
334
                                'action' => 'restart',
335
                                'server' => $_REQUEST['server'],
336
                                'database' => $_REQUEST['database'],
337
                                'schema' => $_REQUEST['schema'],
338
                                'sequence' => $sequence->fields['seqname'],
339
                            ],
340
                        ],
341
                    ],
342
                    'content' => $this->lang['strrestart'],
343
                ],
344
                'reset' => [
345
                    'attr' => [
346
                        'href' => [
347
                            'url' => 'sequences',
348
                            'urlvars' => [
349
                                'action' => 'reset',
350
                                'server' => $_REQUEST['server'],
351
                                'database' => $_REQUEST['database'],
352
                                'schema' => $_REQUEST['schema'],
353
                                'sequence' => $sequence->fields['seqname'],
354
                            ],
355
                        ],
356
                    ],
357
                    'content' => $this->lang['strreset'],
358
                ],
359
                'showall' => [
360
                    'attr' => [
361
                        'href' => [
362
                            'url' => 'sequences',
363
                            'urlvars' => [
364
                                'server' => $_REQUEST['server'],
365
                                'database' => $_REQUEST['database'],
366
                                'schema' => $_REQUEST['schema'],
367
                            ],
368
                        ],
369
                    ],
370
                    'content' => $this->lang['strshowallsequences'],
371
                ],
372
            ];
373
374
            if (!$data->hasAlterSequenceStart()) {
375
                unset($navlinks['restart']);
376
            }
377
378
            $this->printNavLinks($navlinks, 'sequences-properties', \get_defined_vars());
379
        } else {
380
            echo "<p>{$this->lang['strnodata']}</p>" . \PHP_EOL;
381
        }
382
    }
383
384
    /**
385
     * Drop a sequence.
386
     *
387
     * @param mixed $confirm
388
     * @param mixed $msg
389
     */
390
    public function doDrop($confirm, $msg = '')
391
    {
392
        $data = $this->misc->getDatabaseAccessor();
393
394
        if (empty($_REQUEST['sequence']) && empty($_REQUEST['ma'])) {
395
            return $this->doDefault($this->lang['strspecifysequencetodrop']);
396
        }
397
398
        if ($confirm) {
399
            $this->printTrail('sequence');
400
            $this->printTitle($this->lang['strdrop'], 'pg.sequence.drop');
401
            $this->printMsg($msg);
402
403
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/sequences" method="post">' . \PHP_EOL;
404
405
            //If multi drop
406
            if (isset($_REQUEST['ma'])) {
407
                foreach ($_REQUEST['ma'] as $v) {
408
                    $a = \unserialize(\htmlspecialchars_decode($v, \ENT_QUOTES));
409
                    echo '<p>', \sprintf($this->lang['strconfdropsequence'], $this->misc->printVal($a['sequence'])), '</p>' . \PHP_EOL;
410
                    \printf('<input type="hidden" name="sequence[]" value="%s" />', \htmlspecialchars($a['sequence']));
411
                }
412
            } else {
413
                echo '<p>', \sprintf($this->lang['strconfdropsequence'], $this->misc->printVal($_REQUEST['sequence'])), '</p>' . \PHP_EOL;
414
                echo '<input type="hidden" name="sequence" value="', \htmlspecialchars($_REQUEST['sequence']), '" />' . \PHP_EOL;
415
            }
416
417
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label></p>" . \PHP_EOL;
418
            echo '<p><input type="hidden" name="action" value="drop" />' . \PHP_EOL;
419
            echo $this->view->form;
420
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />" . \PHP_EOL;
421
            echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
422
            echo '</form>' . \PHP_EOL;
423
        } else {
424
            if (\is_array($_POST['sequence'])) {
425
                $msg = '';
426
                $status = $data->beginTransaction();
427
428
                if (0 === $status) {
429
                    foreach ($_POST['sequence'] as $s) {
430
                        $status = $data->dropSequence($s, isset($_POST['cascade']));
431
432
                        if (0 === $status) {
433
                            $msg .= \sprintf(
434
                                '%s: %s<br />',
435
                                \htmlentities($s, \ENT_QUOTES, 'UTF-8'),
436
                                $this->lang['strsequencedropped']
437
                            );
438
                        } else {
439
                            $data->endTransaction();
440
                            $this->doDefault(\sprintf(
441
                                '%s%s: %s<br />',
442
                                $msg,
443
                                \htmlentities($s, \ENT_QUOTES, 'UTF-8'),
444
                                $this->lang['strsequencedroppedbad']
445
                            ));
446
447
                            return;
448
                        }
449
                    }
450
                }
451
452
                if (0 === $data->endTransaction()) {
453
                    // Everything went fine, back to the Default page....
454
                    $this->view->setReloadBrowser(true);
455
                    $this->doDefault($msg);
456
                } else {
457
                    $this->doDefault($this->lang['strsequencedroppedbad']);
458
                }
459
            } else {
460
                $status = $data->dropSequence($_POST['sequence'], isset($_POST['cascade']));
461
462
                if (0 === $status) {
463
                    $this->view->setReloadBrowser(true);
464
                    $this->doDefault($this->lang['strsequencedropped']);
465
                } else {
466
                    $this->doDrop(true, $this->lang['strsequencedroppedbad']);
467
                }
468
            }
469
        }
470
    }
471
472
    /**
473
     * Displays a screen where they can enter a new sequence.
474
     *
475
     * @param mixed $msg
476
     */
477
    public function doCreateSequence($msg = ''): void
478
    {
479
        $data = $this->misc->getDatabaseAccessor();
480
481
        $this->coalesceArr($_POST, 'formSequenceName', '');
482
483
        $this->coalesceArr($_POST, 'formIncrement', '');
484
485
        $this->coalesceArr($_POST, 'formMinValue', '');
486
487
        $this->coalesceArr($_POST, 'formMaxValue', '');
488
489
        $this->coalesceArr($_POST, 'formStartValue', '');
490
491
        $this->coalesceArr($_POST, 'formCacheValue', '');
492
493
        $this->printTrail('schema');
494
        $this->printTitle($this->lang['strcreatesequence'], 'pg.sequence.create');
495
        $this->printMsg($msg);
496
497
        echo '<form action="' . \containerInstance()->subFolder . '/src/views/sequences" method="post">' . \PHP_EOL;
498
        echo '<table>' . \PHP_EOL;
499
500
        echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL;
501
        echo "<td class=\"data1\"><input name=\"formSequenceName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
502
            \htmlspecialchars($_POST['formSequenceName']),
503
            '" /></td></tr>' . \PHP_EOL;
504
505
        echo "<tr><th class=\"data left\">{$this->lang['strincrementby']}</th>" . \PHP_EOL;
506
        echo '<td class="data1"><input name="formIncrement" size="5" value="',
507
            \htmlspecialchars($_POST['formIncrement']),
508
            '" /> </td></tr>' . \PHP_EOL;
509
510
        echo "<tr><th class=\"data left\">{$this->lang['strminvalue']}</th>" . \PHP_EOL;
511
        echo '<td class="data1"><input name="formMinValue" size="5" value="',
512
            \htmlspecialchars($_POST['formMinValue']),
513
            '" /></td></tr>' . \PHP_EOL;
514
515
        echo "<tr><th class=\"data left\">{$this->lang['strmaxvalue']}</th>" . \PHP_EOL;
516
        echo '<td class="data1"><input name="formMaxValue" size="5" value="',
517
            \htmlspecialchars($_POST['formMaxValue']),
518
            '" /></td></tr>' . \PHP_EOL;
519
520
        echo "<tr><th class=\"data left\">{$this->lang['strstartvalue']}</th>" . \PHP_EOL;
521
        echo '<td class="data1"><input name="formStartValue" size="5" value="',
522
            \htmlspecialchars($_POST['formStartValue']),
523
            '" /></td></tr>' . \PHP_EOL;
524
525
        echo "<tr><th class=\"data left\">{$this->lang['strcachevalue']}</th>" . \PHP_EOL;
526
        echo '<td class="data1"><input name="formCacheValue" size="5" value="',
527
            \htmlspecialchars($_POST['formCacheValue']),
528
            '" /></td></tr>' . \PHP_EOL;
529
530
        echo "<tr><th class=\"data left\"><label for=\"formCycledValue\">{$this->lang['strcancycle']}</label></th>" . \PHP_EOL;
531
        echo '<td class="data1"><input type="checkbox" id="formCycledValue" name="formCycledValue" ',
532
            (isset($_POST['formCycledValue']) ? ' checked="checked"' : ''),
533
            ' /></td></tr>' . \PHP_EOL;
534
535
        echo '</table>' . \PHP_EOL;
536
        echo '<p><input type="hidden" name="action" value="save_create_sequence" />' . \PHP_EOL;
537
        echo $this->view->form;
538
        echo "<input type=\"submit\" name=\"create\" value=\"{$this->lang['strcreate']}\" />" . \PHP_EOL;
539
        echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
540
        echo '</form>' . \PHP_EOL;
541
    }
542
543
    /**
544
     * Actually creates the new sequence in the database.
545
     */
546
    public function doSaveCreateSequence(): void
547
    {
548
        $data = $this->misc->getDatabaseAccessor();
549
550
        // Check that they've given a name and at least one column
551
        if ('' === $_POST['formSequenceName']) {
552
            $this->doCreateSequence($this->lang['strsequenceneedsname']);
553
        } else {
554
            $status = $data->createSequence(
555
                $_POST['formSequenceName'],
556
                $_POST['formIncrement'],
557
                $_POST['formMinValue'],
558
                $_POST['formMaxValue'],
559
                $_POST['formStartValue'],
560
                $_POST['formCacheValue'],
561
                isset($_POST['formCycledValue'])
562
            );
563
564
            if (0 === $status) {
565
                $this->doDefault($this->lang['strsequencecreated']);
566
            } else {
567
                $this->doCreateSequence($this->lang['strsequencecreatedbad']);
568
            }
569
        }
570
    }
571
572
    /**
573
     * Restarts a sequence.
574
     */
575
    public function doRestart(): void
576
    {
577
        $data = $this->misc->getDatabaseAccessor();
578
579
        $status = $data->restartSequence($_REQUEST['sequence']);
580
581
        if (0 === $status) {
582
            $this->doProperties($this->lang['strsequencerestart']);
583
        } else {
584
            $this->doProperties($this->lang['strsequencerestartbad']);
585
        }
586
    }
587
588
    /**
589
     * Resets a sequence.
590
     */
591
    public function doReset(): void
592
    {
593
        $data = $this->misc->getDatabaseAccessor();
594
595
        $status = $data->resetSequence($_REQUEST['sequence']);
596
597
        if (0 === $status) {
598
            $this->doProperties($this->lang['strsequencereset']);
599
        } else {
600
            $this->doProperties($this->lang['strsequenceresetbad']);
601
        }
602
    }
603
604
    /**
605
     * Set Nextval of a sequence.
606
     */
607
    public function doNextval(): void
608
    {
609
        $data = $this->misc->getDatabaseAccessor();
610
611
        $status = $data->nextvalSequence($_REQUEST['sequence']);
612
613
        if (0 === $status) {
614
            $this->doProperties($this->lang['strsequencenextval']);
615
        } else {
616
            $this->doProperties($this->lang['strsequencenextvalbad']);
617
        }
618
    }
619
620
    /**
621
     * Function to save after 'setval'ing a sequence.
622
     */
623
    public function doSaveSetval(): void
624
    {
625
        $data = $this->misc->getDatabaseAccessor();
626
627
        $status = $data->setvalSequence($_POST['sequence'], $_POST['nextvalue']);
628
629
        if (0 === $status) {
630
            $this->doProperties($this->lang['strsequencesetval']);
631
        } else {
632
            $this->doProperties($this->lang['strsequencesetvalbad']);
633
        }
634
    }
635
636
    /**
637
     * Function to allow 'setval'ing of a sequence.
638
     *
639
     * @param mixed $msg
640
     */
641
    public function doSetval($msg = ''): void
642
    {
643
        $data = $this->misc->getDatabaseAccessor();
644
645
        $this->printTrail('sequence');
646
        $this->printTitle($this->lang['strsetval'], 'pg.sequence');
647
        $this->printMsg($msg);
648
649
        // Fetch the sequence information
650
        $sequence = $data->getSequence($_REQUEST['sequence']);
651
652
        if (\is_object($sequence) && 0 < $sequence->recordCount()) {
653
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/sequences" method="post">' . \PHP_EOL;
654
            echo '<table border="0">';
655
            echo "<tr><th class=\"data left required\">{$this->lang['strlastvalue']}</th>" . \PHP_EOL;
656
            echo '<td class="data1">';
657
            echo "<input name=\"nextvalue\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
658
                $this->misc->printVal($sequence->fields['last_value']),
659
                '" /></td></tr>' . \PHP_EOL;
660
            echo '</table>' . \PHP_EOL;
661
            echo '<p><input type="hidden" name="action" value="setval" />' . \PHP_EOL;
662
            echo '<input type="hidden" name="sequence" value="', \htmlspecialchars($_REQUEST['sequence']), '" />' . \PHP_EOL;
663
            echo $this->view->form;
664
            echo "<input type=\"submit\" name=\"setval\" value=\"{$this->lang['strsetval']}\" />" . \PHP_EOL;
665
            echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
666
            echo '</form>' . \PHP_EOL;
667
        } else {
668
            echo "<p>{$this->lang['strnodata']}</p>" . \PHP_EOL;
669
        }
670
    }
671
672
    /**
673
     * Function to save after altering a sequence.
674
     */
675
    public function doSaveAlter(): void
676
    {
677
        $data = $this->misc->getDatabaseAccessor();
678
679
        $this->coalesceArr($_POST, 'owner', null);
680
681
        $this->coalesceArr($_POST, 'newschema', null);
682
683
        $this->coalesceArr($_POST, 'formIncrement', null);
684
685
        $this->coalesceArr($_POST, 'formMinValue', null);
686
687
        $this->coalesceArr($_POST, 'formMaxValue', null);
688
689
        $this->coalesceArr($_POST, 'formStartValue', null);
690
691
        $this->coalesceArr($_POST, 'formRestartValue', null);
692
693
        $this->coalesceArr($_POST, 'formCacheValue', null);
694
695
        $this->coalesceArr($_POST, 'formCycledValue', null);
696
697
        $status = $data->alterSequence(
698
            $_POST['sequence'],
699
            $_POST['name'],
700
            $_POST['comment'],
701
            $_POST['owner'],
702
            $_POST['newschema'],
703
            $_POST['formIncrement'],
704
            $_POST['formMinValue'],
705
            $_POST['formMaxValue'],
706
            $_POST['formRestartValue'],
707
            $_POST['formCacheValue'],
708
            isset($_POST['formCycledValue']),
709
            $_POST['formStartValue']
710
        );
711
712
        if (0 === $status) {
713
            if ($_POST['sequence'] !== $_POST['name']) {
714
                // Jump them to the new view name
715
                $_REQUEST['sequence'] = $_POST['name'];
716
                // Force a browser reload
717
                $this->view->setReloadBrowser(true);
718
            }
719
720
            if (!empty($_POST['newschema']) && ($_POST['newschema'] !== $data->_schema)) {
721
                // Jump them to the new sequence schema
722
                $this->misc->setCurrentSchema($_POST['newschema']);
723
                $this->view->setReloadBrowser(true);
724
            }
725
            $this->doProperties($this->lang['strsequencealtered']);
726
        } else {
727
            $this->doProperties($this->lang['strsequencealteredbad']);
728
        }
729
    }
730
731
    /**
732
     * Function to allow altering of a sequence.
733
     *
734
     * @param mixed $msg
735
     */
736
    public function doAlter($msg = ''): void
737
    {
738
        $data = $this->misc->getDatabaseAccessor();
739
740
        $this->printTrail('sequence');
741
        $this->printTitle($this->lang['stralter'], 'pg.sequence.alter');
742
        $this->printMsg($msg);
743
744
        // Fetch the sequence information
745
        $sequence = $data->getSequence($_REQUEST['sequence']);
746
747
        if (\is_object($sequence) && 0 < $sequence->recordCount()) {
748
            $this->coalesceArr($_POST, 'name', $_REQUEST['sequence']);
749
750
            $this->coalesceArr($_POST, 'comment', $sequence->fields['seqcomment']);
751
752
            $this->coalesceArr($_POST, 'owner', $sequence->fields['seqowner']);
753
754
            $this->coalesceArr($_POST, 'newschema', $sequence->fields['nspname']);
755
756
            // Handle Checkbox Value
757
            $sequence->fields['is_cycled'] = $data->phpBool($sequence->fields['is_cycled']);
758
759
            if ($sequence->fields['is_cycled']) {
760
                $_POST['formCycledValue'] = 'on';
761
            }
762
763
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/sequences" method="post">' . \PHP_EOL;
764
            echo '<table>' . \PHP_EOL;
765
766
            echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL;
767
            echo '<td class="data1">';
768
            echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
769
                \htmlspecialchars($_POST['name']),
770
                '" /></td></tr>' . \PHP_EOL;
771
772
            if ($data->isSuperUser()) {
773
                // Fetch all users
774
                $users = $data->getUsers();
775
776
                echo "<tr><th class=\"data left required\">{$this->lang['strowner']}</th>" . \PHP_EOL;
777
                echo '<td class="data1"><select name="owner">';
778
779
                while (!$users->EOF) {
780
                    $uname = $users->fields['usename'];
781
                    echo '<option value="', \htmlspecialchars($uname), '"',
782
                        ($uname === $_POST['owner']) ? ' selected="selected"' : '',
783
                        '>',
784
                        \htmlspecialchars($uname),
785
                        '</option>' . \PHP_EOL;
786
                    $users->moveNext();
787
                }
788
                echo '</select></td></tr>' . \PHP_EOL;
789
            }
790
791
            if ($data->hasAlterSequenceSchema()) {
792
                $schemas = $data->getSchemas();
793
                echo "<tr><th class=\"data left required\">{$this->lang['strschema']}</th>" . \PHP_EOL;
794
                echo '<td class="data1"><select name="newschema">';
795
796
                while (!$schemas->EOF) {
797
                    $schema = $schemas->fields['nspname'];
798
                    echo '<option value="', \htmlspecialchars($schema), '"',
799
                        ($schema === $_POST['newschema']) ? ' selected="selected"' : '',
800
                        '>',
801
                        \htmlspecialchars($schema),
802
                        '</option>' . \PHP_EOL;
803
                    $schemas->moveNext();
804
                }
805
                echo '</select></td></tr>' . \PHP_EOL;
806
            }
807
808
            echo "<tr><th class=\"data left\">{$this->lang['strcomment']}</th>" . \PHP_EOL;
809
            echo '<td class="data1">';
810
            echo '<textarea rows="3" cols="32" name="comment">',
811
                \htmlspecialchars($_POST['comment']),
812
                '</textarea></td></tr>' . \PHP_EOL;
813
814
            if ($data->hasAlterSequenceStart()) {
815
                echo "<tr><th class=\"data left\">{$this->lang['strstartvalue']}</th>" . \PHP_EOL;
816
                echo '<td class="data1"><input name="formStartValue" size="5" value="',
817
                    \htmlspecialchars($sequence->fields['start_value']),
818
                    '" /></td></tr>' . \PHP_EOL;
819
            }
820
821
            echo "<tr><th class=\"data left\">{$this->lang['strrestartvalue']}</th>" . \PHP_EOL;
822
            echo '<td class="data1"><input name="formRestartValue" size="5" value="',
823
                \htmlspecialchars($sequence->fields['last_value']),
824
                '" /></td></tr>' . \PHP_EOL;
825
826
            echo "<tr><th class=\"data left\">{$this->lang['strincrementby']}</th>" . \PHP_EOL;
827
            echo '<td class="data1"><input name="formIncrement" size="5" value="',
828
                \htmlspecialchars($sequence->fields['increment_by']),
829
                '" /> </td></tr>' . \PHP_EOL;
830
831
            echo "<tr><th class=\"data left\">{$this->lang['strmaxvalue']}</th>" . \PHP_EOL;
832
            echo '<td class="data1"><input name="formMaxValue" size="5" value="',
833
                \htmlspecialchars($sequence->fields['max_value']),
834
                '" /></td></tr>' . \PHP_EOL;
835
836
            echo "<tr><th class=\"data left\">{$this->lang['strminvalue']}</th>" . \PHP_EOL;
837
            echo '<td class="data1"><input name="formMinValue" size="5" value="',
838
                \htmlspecialchars($sequence->fields['min_value']),
839
                '" /></td></tr>' . \PHP_EOL;
840
841
            echo "<tr><th class=\"data left\">{$this->lang['strcachevalue']}</th>" . \PHP_EOL;
842
            echo '<td class="data1"><input name="formCacheValue" size="5" value="',
843
                \htmlspecialchars($sequence->fields['cache_value']),
844
                '" /></td></tr>' . \PHP_EOL;
845
846
            echo "<tr><th class=\"data left\"><label for=\"formCycledValue\">{$this->lang['strcancycle']}</label></th>" . \PHP_EOL;
847
            echo '<td class="data1"><input type="checkbox" id="formCycledValue" name="formCycledValue" ',
848
                (isset($_POST['formCycledValue']) ? ' checked="checked"' : ''),
849
                ' /></td></tr>' . \PHP_EOL;
850
851
            echo '</table>' . \PHP_EOL;
852
            echo '<p><input type="hidden" name="action" value="alter" />' . \PHP_EOL;
853
            echo $this->view->form;
854
            echo '<input type="hidden" name="sequence" value="', \htmlspecialchars($_REQUEST['sequence']), '" />' . \PHP_EOL;
855
            echo "<input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />" . \PHP_EOL;
856
            echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
857
            echo '</form>' . \PHP_EOL;
858
        } else {
859
            echo "<p>{$this->lang['strnodata']}</p>" . \PHP_EOL;
860
        }
861
    }
862
}
863