Passed
Push — main ( f05cfd...29a9c1 )
by Stefan
02:38
created

FormButtonBox::getCustomButton()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Button-Box with standrad buttons to control the form.
8
 * 
9
 * Supports the most used 'control' buttons for a form like
10
 * [OK] [Save] [Cancel] ...
11
 * Custom defined buttons can also be added.
12
 * Language can be configured through the config file.
13
 * 
14
 * #### History
15
 * - *2021-01-22*   initial version
16
 *
17
 * @package Formgenerator
18
 * @version 1.1.0
19
 * @author Stefanius <[email protected]>
20
 * @copyright MIT License - see the LICENSE file for details
21
 */
22
class FormButtonBox extends FormElement
23
{
24
    public const NONE       = 0;
25
    public const FIRST      = 0;
26
    public const LAST       = -1;
27
    public const OK	        = 0x0001;   // An "OK" button for submit.
28
    public const OPEN	    = 0x0002;   // An "Open" button for submit.
29
    public const SAVE       = 0x0004;   // A "Save" button for submit.
30
    public const YES	    = 0x0008;   // A "Yes" button for submit.
31
    public const NO         = 0x0010;   // A "No" button
32
    public const CANCEL     = 0x0020;   // A "Cancel" button
33
    public const CLOSE      = 0x0040;   // A "Close" button
34
    public const DISCARD    = 0x0080;   // A "Discard" button
35
    public const APPLY      = 0x0100;   // An "Apply" button for submit.
36
    public const RESET      = 0x0200;   // A "Reset" button
37
    public const RETRY      = 0x0400;   // A "Retry" button for submit.
38
    public const IGNORE     = 0x0800;   // An "Ignore" button
39
    public const BACK       = 0x1000;   // A "Back" button
40
    
41
    public const YES_NO_CANCEL = self::YES | self::NO | self::CANCEL;
42
    public const SAVE_CANCEL = self::SAVE | self::CANCEL;
43
    
44
    /** @var integer Buttons, the box containing     */
45
    protected int $iBtns = 0;
46
    /** @var array user defined button(s)     */
47
    protected array $aCustomButtons = [];
48
49
    /**
50
     * @param int $iBtns
51
     */
52
    public function __construct(int $iBtns, int $wFlags = 0)
53
    {
54
        $this->iBtns = $iBtns;
55
        parent::__construct($wFlags);
56
    }
57
    
58
    /**
59
     * Add custom button to the buttonbox.
60
     * Position of the button inside the box can be specified with the param $iAfterBtn: <ul>
61
     * <li> self::FIRST </li>
62
     * <li> self::LAST </li>
63
     * <li> other valid Button: the custom Button appears after this button </li></ul>
64
     * @param string $strText
65
     * @param string $strID
66
     * @param int $iAfterBtn
67
     * @param bool $bSubmit
68
     */
69
    public function addButton(string $strText, string $strID, int $iAfterBtn = self::LAST, bool $bSubmit = false) : void
70
    {
71
        $this->aCustomButtons[$iAfterBtn] = ['text' => $strText, 'id' => $strID, 'type' => ($bSubmit ? 'submit' : 'button') ]; 
72
    }
73
74
    /**
75
     * (non-PHPdoc)
76
     *
77
     * @see \SKien\Formgenerator\FormElement::getHTML()
78
     */
79
    public function getHTML() : string
80
    {
81
        if ($this->iBtns === 0) {
82
            return '';
83
        }
84
        if ($this->oFlags->isSet(FormFlags::ALIGN_CENTER)) {
85
            $this->addStyle('text-align', 'center');
86
        } else if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
87
            $this->addStyle('text-align', 'right');
88
        }
89
        
90
        $aButtonDef = $this->loadButtonDef();
91
        
92
        $strHTML = '<div id=buttonbox' . $this->buildStyle() . '>' . PHP_EOL;
93
        $iBtn = 0x0001;
94
        $strHTML .= $this->getCustomButton(self::FIRST);
95
        while ($iBtn < 0xffff) {
96
            if (($this->iBtns & $iBtn) != 0) {
97
                $strHTML .= $this->getButton($aButtonDef[$iBtn]);
98
            }
99
            $strHTML .= $this->getCustomButton($iBtn);
100
            $iBtn = $iBtn << 1;
101
        }
102
        $strHTML .= $this->getCustomButton(self::LAST);
103
        $strHTML .= '</div>' . PHP_EOL;
104
        
105
        return $strHTML;
106
    }
107
    
108
    /**
109
     * Set the tab index of first button.
110
     * Method is called from the PageGenerator after an element is added to the form.
111
     * @param int $iTabindex
112
     * @return int the count of buttons (-> number tabindexes 'needed')
113
     */
114
    public function setTabindex(int $iTabindex) : int
115
    {
116
        $this->iTabindex = $iTabindex;
117
        return $this->getButtonCount();
118
    }
119
    
120
    /**
121
     * Build the markup for the button.
122
     * @param array $aBtn
123
     * @return string
124
     */
125
    protected function getButton(array $aBtn) : string
126
    {
127
        $strHTML = '  <input id="' . $aBtn['id'] . '"';
128
        $strHTML .= ' type="' . $aBtn['type'] . '"';
129
        $strHTML .= ' tabindex="' . $this->iTabindex++ . '"';
130
        if ($this->oFlags->isSet(FormFlags::READ_ONLY | FormFlags::DISABLED)) {
131
            if ($aBtn['type'] == 'submit') {
132
                $strHTML .= ' disabled';
133
            }
134
        } else {
135
            $strHTML .= ' onclick="' . $aBtn['id'] . 'Clicked();"';
136
        }
137
        $strHTML .= ' value="' . $aBtn['text'] . '"';
138
        $strHTML .= '>' . PHP_EOL;
139
        
140
        return $strHTML;
141
    }
142
143
    /**
144
     * Build custom button, if defined for the requested position.
145
     * @param int $iAfterBtn
146
     * @return string
147
     */
148
    protected function getCustomButton(int $iAfterBtn) : string
149
    {
150
        if (!isset($this->aCustomButtons[$iAfterBtn])) {
151
            return '';
152
        }
153
        return $this->getButton($this->aCustomButtons[$iAfterBtn]);
154
    }
155
156
    /**
157
     * Get the number of buttons the box contains.
158
     * @return int
159
     */
160
    protected function getButtonCount() : int
161
    {
162
        $iCount = 0;
163
        $iBtns = $this->iBtns;
164
        while($iBtns) {
165
            $iCount += ($iBtns & 1);
166
            $iBtns >>= 1;
167
        }
168
        return $iCount + count($this->aCustomButtons);
169
    }
170
    
171
    /**
172
     * Get Textlabels for all buttons.
173
     * Default they are initialized with the englisch Text.
174
     * Configuration can contain localization.
175
     * @return array
176
     */
177
    protected function loadButtonDef() : array
178
    {
179
        $aButtonDef = [
180
            self::OK => ['text' => 'OK', 'id' => 'btnOK', 'type' => 'submit' ],
181
            self::OPEN => ['text' => 'Open', 'id' => 'btnOpen', 'type' => 'button' ],
182
            self::SAVE => ['text' => 'Save', 'id' => 'btnSave', 'type' => 'submit' ],
183
            self::YES => ['text' => 'Yes', 'id' => 'btnYes', 'type' => 'submit' ],
184
            self::NO => ['text' => 'No', 'id' => 'btnNo', 'type' => 'button' ],
185
            self::CANCEL => ['text' => 'Cancel', 'id' => 'btnCancel', 'type' => 'button' ],
186
            self::CLOSE => ['text' => 'Close', 'id' => 'btnClose', 'type' => 'button' ],
187
            self::DISCARD => ['text' => 'Discard', 'id' => 'btnDiscard', 'type' => 'button' ],
188
            self::APPLY => ['text' => 'Apply', 'id' => 'btnApply', 'type' => 'submit' ],
189
            self::RESET => ['text' => 'Reset', 'id' => 'btnReset', 'type' => 'button' ],
190
            self::RETRY => ['text' => 'Retry', 'id' => 'btnRetry', 'type' => 'submit' ],
191
            self::IGNORE => ['text' => 'Ignore', 'id' => 'btnIgnore', 'type' => 'button' ],
192
            self::BACK => ['text' => 'Back', 'id' => 'btnBack', 'type' => 'button' ],
193
        ];
194
        
195
        $aConfig = $this->oFG->getConfig()->getArray('ButtonBox.ButtonText');
196
        // To make it easier to read, the configuration contains the names of the constants 
197
        // as keys. So we have to convert the names into the values and assign the texts 
198
        // accordingly.
199
        foreach ($aConfig as $strName => $strText) {
200
            $iBtn = constant('self::' . $strName);
201
            $aButtonDef[$iBtn]['text'] = $strText;
202
        }
203
        return $aButtonDef;
204
    }
205
}
206
207