Passed
Push — develop ( ec145b...e1ceac )
by Felipe
06:24
created

FormTrait::getActionTableAndButtons()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 6
nc 1
nop 4
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.48
5
 */
6
7
namespace PHPPgAdmin\Traits;
8
9
/**
10
 * Common trait to print form parts that appear on different controller dialogs.
11
 */
12
trait FormTrait
13
{
14
    public $misc;
15
16
    /**
17
     * Prints inputs for action, table and submit/cancel buttons.
18
     *
19
     * @param string $action value for action input
20
     * @param string $table  value for table input
21
     * @param string $add    text for add button
22
     * @param string $cancel text for cancel button
23
     */
24
    public function getActionTableAndButtons($action, $table, $add, $cancel)
25
    {
26
        $content = $this->misc->form;
27
        $content .= sprintf('<input type="hidden" name="action" value="%s" />%s', $action, "\n");
28
        $content .= sprintf('<input type="hidden" name="table" value="%s" />%s', $table, "\n");
29
        $content .= sprintf('<input type="submit" value="%s" />%s', $add, "\n");
30
        $content .= sprintf('<input type="submit" name="cancel" value="%s" />', $cancel);
31
32
        return $content;
33
    }
34
35
    /**
36
     * Prints inputs for action, table and submit/cancel buttons.
37
     *
38
     * @param array $inputs   array of inputs with their name, type and value
39
     * @param array $buttons  array of buttons with their name, type and value
40
     * @param array $cheboxes array of cheboxes with their name, id, checked state, type and text for label
41
     */
42
    public function getFormInputsAndButtons($inputs, $buttons, $cheboxes = [])
43
    {
44
        $content = $this->misc->form;
45
46
        foreach ($cheboxes as $checkbox) {
47
            $content .= sprintf('<p>%s', "\n");
48
            $content .= sprintf('<input type="%s" name="%s" id="%s" %s />', $checkbox['type'], $checkbox['name'], $checkbox['id'], $checkbox['checked'] ? 'checked="checked"' : '');
49
            $content .= sprintf('<label for="%s">%s</label>', $checkbox['id'], $checkbox['labeltext']);
50
            $content .= sprintf('</p>%s', "\n");
51
        }
52
53
        foreach ($inputs as $input) {
54
            $content .= sprintf('<input type="%s" name="%s" value="%s" />%s', $input['type'], $input['name'], $input['value'], "\n");
55
        }
56
57
        $content .= sprintf('<p>%s', "\n");
58
        foreach ($buttons as $button) {
59
            $content .= sprintf('<input type="%s" name="%s" value="%s" />%s', $button['type'], $button['name'], $button['value'], "\n");
60
        }
61
62
        $content .= sprintf('</p>%s', "\n");
63
64
        return $content;
65
    }
66
}
67