Passed
Push — develop ( c2019a...2cab77 )
by Felipe
04:50
created

SequencesController::doSaveSetval()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.48
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class SequencesController extends BaseController
17
{
18
    public $controller_title = 'strsequences';
19
20
    /**
21
     * Default method to render the controller according to the action parameter.
22
     */
23
    public function render()
24
    {
25
        if ('tree' == $this->action) {
26
            return $this->doTree();
27
        }
28
29
        // Print header
30
31
        $this->printHeader();
32
        $this->printBody();
33
34
        switch ($this->action) {
35
            case 'create':
36
                $this->doCreateSequence();
37
38
                break;
39
            case 'save_create_sequence':
40
                if (isset($_POST['create'])) {
41
                    $this->doSaveCreateSequence();
42
                } else {
43
                    $this->doDefault();
44
                }
45
46
                break;
47
            case 'properties':
48
                $this->doProperties();
49
50
                break;
51
            case 'drop':
52
                if (isset($_POST['drop'])) {
53
                    $this->doDrop(false);
54
                } else {
55
                    $this->doDefault();
56
                }
57
58
                break;
59
            case 'confirm_drop':
60
                $this->doDrop(true);
61
62
                break;
63
            case 'restart':
64
                $this->doRestart();
65
66
                break;
67
            case 'reset':
68
                $this->doReset();
69
70
                break;
71
            case 'nextval':
72
                $this->doNextval();
73
74
                break;
75
            case 'setval':
76
                if (isset($_POST['setval'])) {
77
                    $this->doSaveSetval();
78
                } else {
79
                    $this->doDefault();
80
                }
81
82
                break;
83
            case 'confirm_setval':
84
                $this->doSetval();
85
86
                break;
87
            case 'alter':
88
                if (isset($_POST['alter'])) {
89
                    $this->doSaveAlter();
90
                } else {
91
                    $this->doDefault();
92
                }
93
94
                break;
95
            case 'confirm_alter':
96
                $this->doAlter();
97
98
                break;
99
            default:
100
                $this->doDefault();
101
102
                break;
103
        }
104
105
        // Print footer
106
        return $this->printFooter();
107
    }
108
109
    /**
110
     * Display list of all sequences in the database/schema.
111
     *
112
     * @param mixed $msg
113
     */
114
    public function doDefault($msg = '')
115
    {
116
        $data = $this->misc->getDatabaseAccessor();
117
118
        $this->printTrail('schema');
119
        $this->printTabs('schema', 'sequences');
120
        $this->printMsg($msg);
121
122
        // Get all sequences
123
        $sequences = $data->getSequences();
124
125
        $columns = [
126
            'sequence' => [
127
                'title' => $this->lang['strsequence'],
128
                'field' => Decorator::field('seqname'),
129
                'url'   => "sequences?action=properties&amp;{$this->misc->href}&amp;",
130
                'vars'  => ['sequence' => 'seqname'],
131
            ],
132
            'owner'    => [
133
                'title' => $this->lang['strowner'],
134
                'field' => Decorator::field('seqowner'),
135
            ],
136
            'actions'  => [
137
                'title' => $this->lang['stractions'],
138
            ],
139
            'comment'  => [
140
                'title' => $this->lang['strcomment'],
141
                'field' => Decorator::field('seqcomment'),
142
            ],
143
        ];
144
145
        $actions = [
146
            'multiactions' => [
147
                'keycols' => ['sequence' => 'seqname'],
148
                'url'     => 'sequences',
149
            ],
150
            'alter'        => [
151
                'content' => $this->lang['stralter'],
152
                'attr'    => [
153
                    'href' => [
154
                        'url'     => 'sequences',
155
                        'urlvars' => [
156
                            'action'   => 'confirm_alter',
157
                            'subject'  => 'sequence',
158
                            'sequence' => Decorator::field('seqname'),
159
                        ],
160
                    ],
161
                ],
162
            ],
163
            'drop'         => [
164
                'content'     => $this->lang['strdrop'],
165
                'attr'        => [
166
                    'href' => [
167
                        'url'     => 'sequences',
168
                        'urlvars' => [
169
                            'action'   => 'confirm_drop',
170
                            'sequence' => Decorator::field('seqname'),
171
                        ],
172
                    ],
173
                ],
174
                'multiaction' => 'confirm_drop',
175
            ],
176
            'privileges'   => [
177
                'content' => $this->lang['strprivileges'],
178
                'attr'    => [
179
                    'href' => [
180
                        'url'     => 'privileges',
181
                        'urlvars' => [
182
                            'subject'  => 'sequence',
183
                            'sequence' => Decorator::field('seqname'),
184
                        ],
185
                    ],
186
                ],
187
            ],
188
        ];
189
190
        echo $this->printTable($sequences, $columns, $actions, 'sequences-sequences', $this->lang['strnosequences']);
191
192
        $this->printNavLinks(['create' => [
193
            'attr'    => [
194
                'href' => [
195
                    'url'     => 'sequences',
196
                    'urlvars' => [
197
                        'action'   => 'create',
198
                        'server'   => $_REQUEST['server'],
199
                        'database' => $_REQUEST['database'],
200
                        'schema'   => $_REQUEST['schema'],
201
                    ],
202
                ],
203
            ],
204
            'content' => $this->lang['strcreatesequence'],
205
        ]], 'sequences-sequences', get_defined_vars());
206
    }
207
208
    /**
209
     * Generate XML for the browser tree.
210
     */
211
    public function doTree()
212
    {
213
        $data = $this->misc->getDatabaseAccessor();
214
215
        $sequences = $data->getSequences();
216
217
        $reqvars = $this->misc->getRequestVars('sequence');
218
219
        $attrs = [
220
            'text'    => Decorator::field('seqname'),
221
            'icon'    => 'Sequence',
222
            'toolTip' => Decorator::field('seqcomment'),
223
            'action'  => Decorator::actionurl(
224
                'sequences',
225
                $reqvars,
226
                [
227
                    'action'   => 'properties',
228
                    'sequence' => Decorator::field('seqname'),
229
                ]
230
            ),
231
        ];
232
233
        return $this->printTree($sequences, $attrs, 'sequences');
234
    }
235
236
    /**
237
     * Display the properties of a sequence.
238
     *
239
     * @param mixed $msg
240
     */
241
    public function doProperties($msg = '')
242
    {
243
        $data = $this->misc->getDatabaseAccessor();
244
        $this->printTrail('sequence');
245
        $this->printTitle($this->lang['strproperties'], 'pg.sequence');
246
        $this->printMsg($msg);
247
248
        // Fetch the sequence information
249
        $sequence = $data->getSequence($_REQUEST['sequence']);
250
251
        if (is_object($sequence) && $sequence->recordCount() > 0) {
252
            $sequence->fields['is_cycled'] = $data->phpBool($sequence->fields['is_cycled']);
253
            $sequence->fields['is_called'] = $data->phpBool($sequence->fields['is_called']);
254
255
            // Show comment if any
256
            if (null !== $sequence->fields['seqcomment']) {
257
                echo '<p class="comment">', $this->misc->printVal($sequence->fields['seqcomment']), '</p>'.PHP_EOL;
258
            }
259
260
            echo '<table border="0">';
261
            echo "<tr><th class=\"data\">{$this->lang['strname']}</th>";
262
            if ($data->hasAlterSequenceStart()) {
263
                echo "<th class=\"data\">{$this->lang['strstartvalue']}</th>";
264
            }
265
            echo "<th class=\"data\">{$this->lang['strlastvalue']}</th>";
266
            echo "<th class=\"data\">{$this->lang['strincrementby']}</th>";
267
            echo "<th class=\"data\">{$this->lang['strmaxvalue']}</th>";
268
            echo "<th class=\"data\">{$this->lang['strminvalue']}</th>";
269
            echo "<th class=\"data\">{$this->lang['strcachevalue']}</th>";
270
            echo "<th class=\"data\">{$this->lang['strlogcount']}</th>";
271
            echo "<th class=\"data\">{$this->lang['strcancycle']}</th>";
272
            echo "<th class=\"data\">{$this->lang['striscalled']}</th></tr>";
273
            echo '<tr>';
274
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['seqname']), '</td>';
275
            if ($data->hasAlterSequenceStart()) {
276
                echo '<td class="data1">', $this->misc->printVal($sequence->fields['start_value']), '</td>';
277
            }
278
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['last_value']), '</td>';
279
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['increment_by']), '</td>';
280
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['max_value']), '</td>';
281
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['min_value']), '</td>';
282
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['cache_value']), '</td>';
283
            echo '<td class="data1">', $this->misc->printVal($sequence->fields['log_cnt']), '</td>';
284
            echo '<td class="data1">', ($sequence->fields['is_cycled'] ? $this->lang['stryes'] : $this->lang['strno']), '</td>';
285
            echo '<td class="data1">', ($sequence->fields['is_called'] ? $this->lang['stryes'] : $this->lang['strno']), '</td>';
286
            echo '</tr>';
287
            echo '</table>';
288
289
            $navlinks = [
290
                'alter'   => [
291
                    'attr'    => [
292
                        'href' => [
293
                            'url'     => 'sequences',
294
                            'urlvars' => [
295
                                'action'   => 'confirm_alter',
296
                                'server'   => $_REQUEST['server'],
297
                                'database' => $_REQUEST['database'],
298
                                'schema'   => $_REQUEST['schema'],
299
                                'sequence' => $sequence->fields['seqname'],
300
                            ],
301
                        ],
302
                    ],
303
                    'content' => $this->lang['stralter'],
304
                ],
305
                'setval'  => [
306
                    'attr'    => [
307
                        'href' => [
308
                            'url'     => 'sequences',
309
                            'urlvars' => [
310
                                'action'   => 'confirm_setval',
311
                                'server'   => $_REQUEST['server'],
312
                                'database' => $_REQUEST['database'],
313
                                'schema'   => $_REQUEST['schema'],
314
                                'sequence' => $sequence->fields['seqname'],
315
                            ],
316
                        ],
317
                    ],
318
                    'content' => $this->lang['strsetval'],
319
                ],
320
                'nextval' => [
321
                    'attr'    => [
322
                        'href' => [
323
                            'url'     => 'sequences',
324
                            'urlvars' => [
325
                                'action'   => 'nextval',
326
                                'server'   => $_REQUEST['server'],
327
                                'database' => $_REQUEST['database'],
328
                                'schema'   => $_REQUEST['schema'],
329
                                'sequence' => $sequence->fields['seqname'],
330
                            ],
331
                        ],
332
                    ],
333
                    'content' => $this->lang['strnextval'],
334
                ],
335
                'restart' => [
336
                    'attr'    => [
337
                        'href' => [
338
                            'url'     => 'sequences',
339
                            'urlvars' => [
340
                                'action'   => 'restart',
341
                                'server'   => $_REQUEST['server'],
342
                                'database' => $_REQUEST['database'],
343
                                'schema'   => $_REQUEST['schema'],
344
                                'sequence' => $sequence->fields['seqname'],
345
                            ],
346
                        ],
347
                    ],
348
                    'content' => $this->lang['strrestart'],
349
                ],
350
                'reset'   => [
351
                    'attr'    => [
352
                        'href' => [
353
                            'url'     => 'sequences',
354
                            'urlvars' => [
355
                                'action'   => 'reset',
356
                                'server'   => $_REQUEST['server'],
357
                                'database' => $_REQUEST['database'],
358
                                'schema'   => $_REQUEST['schema'],
359
                                'sequence' => $sequence->fields['seqname'],
360
                            ],
361
                        ],
362
                    ],
363
                    'content' => $this->lang['strreset'],
364
                ],
365
                'showall' => [
366
                    'attr'    => [
367
                        'href' => [
368
                            'url'     => 'sequences',
369
                            'urlvars' => [
370
                                'server'   => $_REQUEST['server'],
371
                                'database' => $_REQUEST['database'],
372
                                'schema'   => $_REQUEST['schema'],
373
                            ],
374
                        ],
375
                    ],
376
                    'content' => $this->lang['strshowallsequences'],
377
                ],
378
            ];
379
380
            if (!$data->hasAlterSequenceStart()) {
381
                unset($navlinks['restart']);
382
            }
383
384
            $this->printNavLinks($navlinks, 'sequences-properties', get_defined_vars());
385
        } else {
386
            echo "<p>{$this->lang['strnodata']}</p>".PHP_EOL;
387
        }
388
    }
389
390
    /**
391
     * Drop a sequence.
392
     *
393
     * @param mixed $confirm
394
     * @param mixed $msg
395
     */
396
    public function doDrop($confirm, $msg = '')
397
    {
398
        $data = $this->misc->getDatabaseAccessor();
399
400
        if (empty($_REQUEST['sequence']) && empty($_REQUEST['ma'])) {
401
            return $this->doDefault($this->lang['strspecifysequencetodrop']);
402
        }
403
404
        if ($confirm) {
405
            $this->printTrail('sequence');
406
            $this->printTitle($this->lang['strdrop'], 'pg.sequence.drop');
407
            $this->printMsg($msg);
408
409
            echo '<form action="'.\SUBFOLDER.'/src/views/sequences" method="post">'.PHP_EOL;
410
411
            //If multi drop
412
            if (isset($_REQUEST['ma'])) {
413
                foreach ($_REQUEST['ma'] as $v) {
414
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
415
                    echo '<p>', sprintf($this->lang['strconfdropsequence'], $this->misc->printVal($a['sequence'])), '</p>'.PHP_EOL;
416
                    printf('<input type="hidden" name="sequence[]" value="%s" />', htmlspecialchars($a['sequence']));
417
                }
418
            } else {
419
                echo '<p>', sprintf($this->lang['strconfdropsequence'], $this->misc->printVal($_REQUEST['sequence'])), '</p>'.PHP_EOL;
420
                echo '<input type="hidden" name="sequence" value="', htmlspecialchars($_REQUEST['sequence']), '" />'.PHP_EOL;
421
            }
422
423
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label></p>".PHP_EOL;
424
            echo '<p><input type="hidden" name="action" value="drop" />'.PHP_EOL;
425
            echo $this->misc->form;
426
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />".PHP_EOL;
427
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL;
428
            echo '</form>'.PHP_EOL;
429
        } else {
430
            if (is_array($_POST['sequence'])) {
431
                $msg    = '';
432
                $status = $data->beginTransaction();
433
                if (0 == $status) {
434
                    foreach ($_POST['sequence'] as $s) {
435
                        $status = $data->dropSequence($s, isset($_POST['cascade']));
436
                        if (0 == $status) {
437
                            $msg .= sprintf('%s: %s<br />', htmlentities($s, ENT_QUOTES, 'UTF-8'), $this->lang['strsequencedropped']);
438
                        } else {
439
                            $data->endTransaction();
440
                            $this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($s, ENT_QUOTES, 'UTF-8'), $this->lang['strsequencedroppedbad']));
441
442
                            return;
443
                        }
444
                    }
445
                }
446
                if (0 == $data->endTransaction()) {
447
                    // Everything went fine, back to the Default page....
448
                    $this->misc->setReloadBrowser(true);
449
                    $this->doDefault($msg);
450
                } else {
451
                    $this->doDefault($this->lang['strsequencedroppedbad']);
452
                }
453
            } else {
454
                $status = $data->dropSequence($_POST['sequence'], isset($_POST['cascade']));
455
                if (0 == $status) {
456
                    $this->misc->setReloadBrowser(true);
457
                    $this->doDefault($this->lang['strsequencedropped']);
458
                } else {
459
                    $this->doDrop(true, $this->lang['strsequencedroppedbad']);
460
                }
461
            }
462
        }
463
    }
464
465
    /**
466
     * Displays a screen where they can enter a new sequence.
467
     *
468
     * @param mixed $msg
469
     */
470
    public function doCreateSequence($msg = '')
471
    {
472
        $data = $this->misc->getDatabaseAccessor();
473
474
        $this->coalesceArr($_POST, 'formSequenceName', '');
475
476
        $this->coalesceArr($_POST, 'formIncrement', '');
477
478
        $this->coalesceArr($_POST, 'formMinValue', '');
479
480
        $this->coalesceArr($_POST, 'formMaxValue', '');
481
482
        $this->coalesceArr($_POST, 'formStartValue', '');
483
484
        $this->coalesceArr($_POST, 'formCacheValue', '');
485
486
        $this->printTrail('schema');
487
        $this->printTitle($this->lang['strcreatesequence'], 'pg.sequence.create');
488
        $this->printMsg($msg);
489
490
        echo '<form action="'.\SUBFOLDER.'/src/views/sequences" method="post">'.PHP_EOL;
491
        echo '<table>'.PHP_EOL;
492
493
        echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>".PHP_EOL;
494
        echo "<td class=\"data1\"><input name=\"formSequenceName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
495
        htmlspecialchars($_POST['formSequenceName']), '" /></td></tr>'.PHP_EOL;
496
497
        echo "<tr><th class=\"data left\">{$this->lang['strincrementby']}</th>".PHP_EOL;
498
        echo '<td class="data1"><input name="formIncrement" size="5" value="',
499
        htmlspecialchars($_POST['formIncrement']), '" /> </td></tr>'.PHP_EOL;
500
501
        echo "<tr><th class=\"data left\">{$this->lang['strminvalue']}</th>".PHP_EOL;
502
        echo '<td class="data1"><input name="formMinValue" size="5" value="',
503
        htmlspecialchars($_POST['formMinValue']), '" /></td></tr>'.PHP_EOL;
504
505
        echo "<tr><th class=\"data left\">{$this->lang['strmaxvalue']}</th>".PHP_EOL;
506
        echo '<td class="data1"><input name="formMaxValue" size="5" value="',
507
        htmlspecialchars($_POST['formMaxValue']), '" /></td></tr>'.PHP_EOL;
508
509
        echo "<tr><th class=\"data left\">{$this->lang['strstartvalue']}</th>".PHP_EOL;
510
        echo '<td class="data1"><input name="formStartValue" size="5" value="',
511
        htmlspecialchars($_POST['formStartValue']), '" /></td></tr>'.PHP_EOL;
512
513
        echo "<tr><th class=\"data left\">{$this->lang['strcachevalue']}</th>".PHP_EOL;
514
        echo '<td class="data1"><input name="formCacheValue" size="5" value="',
515
        htmlspecialchars($_POST['formCacheValue']), '" /></td></tr>'.PHP_EOL;
516
517
        echo "<tr><th class=\"data left\"><label for=\"formCycledValue\">{$this->lang['strcancycle']}</label></th>".PHP_EOL;
518
        echo '<td class="data1"><input type="checkbox" id="formCycledValue" name="formCycledValue" ',
519
        (isset($_POST['formCycledValue']) ? ' checked="checked"' : ''), ' /></td></tr>'.PHP_EOL;
520
521
        echo '</table>'.PHP_EOL;
522
        echo '<p><input type="hidden" name="action" value="save_create_sequence" />'.PHP_EOL;
523
        echo $this->misc->form;
524
        echo "<input type=\"submit\" name=\"create\" value=\"{$this->lang['strcreate']}\" />".PHP_EOL;
525
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL;
526
        echo '</form>'.PHP_EOL;
527
    }
528
529
    /**
530
     * Actually creates the new sequence in the database.
531
     */
532
    public function doSaveCreateSequence()
533
    {
534
        $data = $this->misc->getDatabaseAccessor();
535
536
        // Check that they've given a name and at least one column
537
        if ('' == $_POST['formSequenceName']) {
538
            $this->doCreateSequence($this->lang['strsequenceneedsname']);
539
        } else {
540
            $status = $data->createSequence(
541
                $_POST['formSequenceName'],
542
                $_POST['formIncrement'],
543
                $_POST['formMinValue'],
544
                $_POST['formMaxValue'],
545
                $_POST['formStartValue'],
546
                $_POST['formCacheValue'],
547
                isset($_POST['formCycledValue'])
548
            );
549
            if (0 == $status) {
550
                $this->doDefault($this->lang['strsequencecreated']);
551
            } else {
552
                $this->doCreateSequence($this->lang['strsequencecreatedbad']);
553
            }
554
        }
555
    }
556
557
    /**
558
     * Restarts a sequence.
559
     */
560
    public function doRestart()
561
    {
562
        $data = $this->misc->getDatabaseAccessor();
563
564
        $status = $data->restartSequence($_REQUEST['sequence']);
565
        if (0 == $status) {
566
            $this->doProperties($this->lang['strsequencerestart']);
567
        } else {
568
            $this->doProperties($this->lang['strsequencerestartbad']);
569
        }
570
    }
571
572
    /**
573
     * Resets a sequence.
574
     */
575
    public function doReset()
576
    {
577
        $data = $this->misc->getDatabaseAccessor();
578
579
        $status = $data->resetSequence($_REQUEST['sequence']);
580
        if (0 == $status) {
581
            $this->doProperties($this->lang['strsequencereset']);
582
        } else {
583
            $this->doProperties($this->lang['strsequenceresetbad']);
584
        }
585
    }
586
587
    /**
588
     * Set Nextval of a sequence.
589
     */
590
    public function doNextval()
591
    {
592
        $data = $this->misc->getDatabaseAccessor();
593
594
        $status = $data->nextvalSequence($_REQUEST['sequence']);
595
        if (0 == $status) {
596
            $this->doProperties($this->lang['strsequencenextval']);
597
        } else {
598
            $this->doProperties($this->lang['strsequencenextvalbad']);
599
        }
600
    }
601
602
    /**
603
     * Function to save after 'setval'ing a sequence.
604
     */
605
    public function doSaveSetval()
606
    {
607
        $data = $this->misc->getDatabaseAccessor();
608
609
        $status = $data->setvalSequence($_POST['sequence'], $_POST['nextvalue']);
610
        if (0 == $status) {
611
            $this->doProperties($this->lang['strsequencesetval']);
612
        } else {
613
            $this->doProperties($this->lang['strsequencesetvalbad']);
614
        }
615
    }
616
617
    /**
618
     * Function to allow 'setval'ing of a sequence.
619
     *
620
     * @param mixed $msg
621
     */
622
    public function doSetval($msg = '')
623
    {
624
        $data = $this->misc->getDatabaseAccessor();
625
626
        $this->printTrail('sequence');
627
        $this->printTitle($this->lang['strsetval'], 'pg.sequence');
628
        $this->printMsg($msg);
629
630
        // Fetch the sequence information
631
        $sequence = $data->getSequence($_REQUEST['sequence']);
632
633
        if (is_object($sequence) && $sequence->recordCount() > 0) {
634
            echo '<form action="'.\SUBFOLDER.'/src/views/sequences" method="post">'.PHP_EOL;
635
            echo '<table border="0">';
636
            echo "<tr><th class=\"data left required\">{$this->lang['strlastvalue']}</th>".PHP_EOL;
637
            echo '<td class="data1">';
638
            echo "<input name=\"nextvalue\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
639
            $this->misc->printVal($sequence->fields['last_value']), '" /></td></tr>'.PHP_EOL;
640
            echo '</table>'.PHP_EOL;
641
            echo '<p><input type="hidden" name="action" value="setval" />'.PHP_EOL;
642
            echo '<input type="hidden" name="sequence" value="', htmlspecialchars($_REQUEST['sequence']), '" />'.PHP_EOL;
643
            echo $this->misc->form;
644
            echo "<input type=\"submit\" name=\"setval\" value=\"{$this->lang['strsetval']}\" />".PHP_EOL;
645
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL;
646
            echo '</form>'.PHP_EOL;
647
        } else {
648
            echo "<p>{$this->lang['strnodata']}</p>".PHP_EOL;
649
        }
650
    }
651
652
    /**
653
     * Function to save after altering a sequence.
654
     */
655
    public function doSaveAlter()
656
    {
657
        $data = $this->misc->getDatabaseAccessor();
658
659
        $this->coalesceArr($_POST, 'owner', null);
660
661
        $this->coalesceArr($_POST, 'newschema', null);
662
663
        $this->coalesceArr($_POST, 'formIncrement', null);
664
665
        $this->coalesceArr($_POST, 'formMinValue', null);
666
667
        $this->coalesceArr($_POST, 'formMaxValue', null);
668
669
        $this->coalesceArr($_POST, 'formStartValue', null);
670
671
        $this->coalesceArr($_POST, 'formRestartValue', null);
672
673
        $this->coalesceArr($_POST, 'formCacheValue', null);
674
675
        $this->coalesceArr($_POST, 'formCycledValue', null);
676
677
        $status = $data->alterSequence(
678
            $_POST['sequence'],
679
            $_POST['name'],
680
            $_POST['comment'],
681
            $_POST['owner'],
682
            $_POST['newschema'],
683
            $_POST['formIncrement'],
684
            $_POST['formMinValue'],
685
            $_POST['formMaxValue'],
686
            $_POST['formRestartValue'],
687
            $_POST['formCacheValue'],
688
            isset($_POST['formCycledValue']),
689
            $_POST['formStartValue']
690
        );
691
692
        if (0 == $status) {
693
            if ($_POST['sequence'] != $_POST['name']) {
694
                // Jump them to the new view name
695
                $_REQUEST['sequence'] = $_POST['name'];
696
                // Force a browser reload
697
                $this->misc->setReloadBrowser(true);
698
            }
699
            if (!empty($_POST['newschema']) && ($_POST['newschema'] != $data->_schema)) {
700
                // Jump them to the new sequence schema
701
                $this->misc->setCurrentSchema($_POST['newschema']);
702
                $this->misc->setReloadBrowser(true);
703
            }
704
            $this->doProperties($this->lang['strsequencealtered']);
705
        } else {
706
            $this->doProperties($this->lang['strsequencealteredbad']);
707
        }
708
    }
709
710
    /**
711
     * Function to allow altering of a sequence.
712
     *
713
     * @param mixed $msg
714
     */
715
    public function doAlter($msg = '')
716
    {
717
        $data = $this->misc->getDatabaseAccessor();
718
719
        $this->printTrail('sequence');
720
        $this->printTitle($this->lang['stralter'], 'pg.sequence.alter');
721
        $this->printMsg($msg);
722
723
        // Fetch the sequence information
724
        $sequence = $data->getSequence($_REQUEST['sequence']);
725
726
        if (is_object($sequence) && $sequence->recordCount() > 0) {
727
            $this->coalesceArr($_POST, 'name', $_REQUEST['sequence']);
728
729
            $this->coalesceArr($_POST, 'comment', $sequence->fields['seqcomment']);
730
731
            $this->coalesceArr($_POST, 'owner', $sequence->fields['seqowner']);
732
733
            $this->coalesceArr($_POST, 'newschema', $sequence->fields['nspname']);
734
735
            // Handle Checkbox Value
736
            $sequence->fields['is_cycled'] = $data->phpBool($sequence->fields['is_cycled']);
737
            if ($sequence->fields['is_cycled']) {
738
                $_POST['formCycledValue'] = 'on';
739
            }
740
741
            echo '<form action="'.\SUBFOLDER.'/src/views/sequences" method="post">'.PHP_EOL;
742
            echo '<table>'.PHP_EOL;
743
744
            echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>".PHP_EOL;
745
            echo '<td class="data1">';
746
            echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
747
            htmlspecialchars($_POST['name']), '" /></td></tr>'.PHP_EOL;
748
749
            if ($data->isSuperUser()) {
750
                // Fetch all users
751
                $users = $data->getUsers();
752
753
                echo "<tr><th class=\"data left required\">{$this->lang['strowner']}</th>".PHP_EOL;
754
                echo '<td class="data1"><select name="owner">';
755
                while (!$users->EOF) {
756
                    $uname = $users->fields['usename'];
757
                    echo '<option value="', htmlspecialchars($uname), '"',
758
                    ($uname == $_POST['owner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), '</option>'.PHP_EOL;
759
                    $users->moveNext();
760
                }
761
                echo '</select></td></tr>'.PHP_EOL;
762
            }
763
764
            if ($data->hasAlterSequenceSchema()) {
765
                $schemas = $data->getSchemas();
766
                echo "<tr><th class=\"data left required\">{$this->lang['strschema']}</th>".PHP_EOL;
767
                echo '<td class="data1"><select name="newschema">';
768
                while (!$schemas->EOF) {
769
                    $schema = $schemas->fields['nspname'];
770
                    echo '<option value="', htmlspecialchars($schema), '"',
771
                    ($schema == $_POST['newschema']) ? ' selected="selected"' : '', '>', htmlspecialchars($schema), '</option>'.PHP_EOL;
772
                    $schemas->moveNext();
773
                }
774
                echo '</select></td></tr>'.PHP_EOL;
775
            }
776
777
            echo "<tr><th class=\"data left\">{$this->lang['strcomment']}</th>".PHP_EOL;
778
            echo '<td class="data1">';
779
            echo '<textarea rows="3" cols="32" name="comment">',
780
            htmlspecialchars($_POST['comment']), '</textarea></td></tr>'.PHP_EOL;
781
782
            if ($data->hasAlterSequenceStart()) {
783
                echo "<tr><th class=\"data left\">{$this->lang['strstartvalue']}</th>".PHP_EOL;
784
                echo '<td class="data1"><input name="formStartValue" size="5" value="',
785
                htmlspecialchars($sequence->fields['start_value']), '" /></td></tr>'.PHP_EOL;
786
            }
787
788
            echo "<tr><th class=\"data left\">{$this->lang['strrestartvalue']}</th>".PHP_EOL;
789
            echo '<td class="data1"><input name="formRestartValue" size="5" value="',
790
            htmlspecialchars($sequence->fields['last_value']), '" /></td></tr>'.PHP_EOL;
791
792
            echo "<tr><th class=\"data left\">{$this->lang['strincrementby']}</th>".PHP_EOL;
793
            echo '<td class="data1"><input name="formIncrement" size="5" value="',
794
            htmlspecialchars($sequence->fields['increment_by']), '" /> </td></tr>'.PHP_EOL;
795
796
            echo "<tr><th class=\"data left\">{$this->lang['strmaxvalue']}</th>".PHP_EOL;
797
            echo '<td class="data1"><input name="formMaxValue" size="5" value="',
798
            htmlspecialchars($sequence->fields['max_value']), '" /></td></tr>'.PHP_EOL;
799
800
            echo "<tr><th class=\"data left\">{$this->lang['strminvalue']}</th>".PHP_EOL;
801
            echo '<td class="data1"><input name="formMinValue" size="5" value="',
802
            htmlspecialchars($sequence->fields['min_value']), '" /></td></tr>'.PHP_EOL;
803
804
            echo "<tr><th class=\"data left\">{$this->lang['strcachevalue']}</th>".PHP_EOL;
805
            echo '<td class="data1"><input name="formCacheValue" size="5" value="',
806
            htmlspecialchars($sequence->fields['cache_value']), '" /></td></tr>'.PHP_EOL;
807
808
            echo "<tr><th class=\"data left\"><label for=\"formCycledValue\">{$this->lang['strcancycle']}</label></th>".PHP_EOL;
809
            echo '<td class="data1"><input type="checkbox" id="formCycledValue" name="formCycledValue" ',
810
            (isset($_POST['formCycledValue']) ? ' checked="checked"' : ''), ' /></td></tr>'.PHP_EOL;
811
812
            echo '</table>'.PHP_EOL;
813
            echo '<p><input type="hidden" name="action" value="alter" />'.PHP_EOL;
814
            echo $this->misc->form;
815
            echo '<input type="hidden" name="sequence" value="', htmlspecialchars($_REQUEST['sequence']), '" />'.PHP_EOL;
816
            echo "<input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />".PHP_EOL;
817
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL;
818
            echo '</form>'.PHP_EOL;
819
        } else {
820
            echo "<p>{$this->lang['strnodata']}</p>".PHP_EOL;
821
        }
822
    }
823
}
824