FormCollection::getColWidth()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Base-class for all elements that can have child elements.
8
 *
9
 * @package Formgenerator
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
abstract class FormCollection extends FormElement
14
{
15
    /** @var FormElementInterface[] all direct child elements     */
16
    protected array $aChild = [];
17
    /** @var array<int> the width information for the cols inside this element     */
18
    protected ?array $aColWidth = null;
19
    /** @var string dimension of the width values ('%', 'px', 'em')     */
20
    protected string $strWidthDim = '%';
21
22
    /**
23
     * Create any kind of collection
24
     * @param int $wFlags
25
     */
26
    public function __construct(int $wFlags)
27
    {
28
        parent::__construct($wFlags);
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     * @see \SKien\Formgenerator\FormElement::readAdditionalXML()
34
     * @internal
35
     */
36
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
37
    {
38
        parent::readAdditionalXML($oXMLElement);
39
        $strWidthDim = self::getAttribString($oXMLElement, 'widthdim', '%');
40
        if (self::hasAttrib($oXMLElement, 'colwidth')) {
41
            $aColWidth = self::getAttribIntArray($oXMLElement, 'colwidth');
42
            $this->setColWidth($aColWidth, $strWidthDim);
43
        }
44
    }
45
46
    /**
47
     * Add a child to this element.
48
     * Some properties of the element to add (parent, tabindex, ...) are changed/set
49
     * with the call of this method.
50
     * @param FormElementInterface $oElement element to add
51
     * @return FormElementInterface added element
52
     */
53
    public function add(FormElementInterface $oElement) : FormElementInterface
54
    {
55
        $oElement->setParent($this);
56
        $this->aChild[] = $oElement;
57
        $this->oFG->addElement($oElement);
58
59
        return $oElement;
60
    }
61
62
    /**
63
     * Set width for the cols included in this element.
64
     * > <b>Note:</b><br/>
65
     *   If the colwidth is not set for a collection, the element tries to get
66
     *   these settings from its parent element. If no colwidth is set there,
67
     *   the system continues with the next parent element until the property
68
     *   is set or the form generator element is reached.
69
     * @param array<int> $aColWidth  numeric array of width for each col
70
     * @param string $strDim    dimension of the width values ('%', 'px', 'em')
71
     */
72
    public function setColWidth(array $aColWidth, string $strDim = '%') : void
73
    {
74
        $this->aColWidth = $aColWidth;
75
        $this->strWidthDim = $strDim;
76
    }
77
78
    /**
79
     * Get colwidth for requested element.
80
     * If no width set, we try to get the width throught the parent.
81
     * @param int $iCol  requested col, if -1 current col is used
82
     * @return string       colwidth including dimension
83
     * @internal
84
     */
85
    public function getColWidth(int $iCol = -1) : string
86
    {
87
        $strWidth = '';
88
        if ($iCol < 0) {
89
            $iCol = $this->iCol;
90
        }
91
        if ($this->aColWidth != null && $iCol < count($this->aColWidth) && $this->aColWidth[$iCol] >= 0) {
92
            $strWidth = $this->aColWidth[$iCol] . $this->strWidthDim;
93
        } else if ($this->oParent != null) {
94
            $strWidth = $this->oParent->getColWidth($iCol);
95
        }
96
        return $strWidth;
97
    }
98
99
    /**
100
     * Add a new div as child.
101
     * @see FormDiv
102
     * @param int $iWidth   width of the div in percent
103
     * @param int $iAlign   align (`FormDiv::NONE`, `FormDiv::LEFT`, `FormDiv::RIGHT`, `FormDiv::CLEAR`)
104
     * @param string $strID ID of the div
105
     * @return \SKien\Formgenerator\FormDiv created div element
106
     */
107
    public function addDiv(int $iWidth = 0, int $iAlign = FormDiv::CLEAR, string $strID = '') : FormDiv
108
    {
109
        $oDiv = new FormDiv($iWidth, $iAlign);
110
        $oDiv->SetID($strID);
111
        $this->add($oDiv);
112
113
        return $oDiv;
114
    }
115
116
    /**
117
     * Add a new fieldset to the element.
118
     * @see FormFieldSet
119
     * @param string $strLegend text or image of the legend
120
     * @param string $strID  ID of the fieldset
121
     * @param int $iType type of the legend (FormFieldSet::TEXT or FormFieldSet::IMAGE)
122
     * @return \SKien\Formgenerator\FormFieldSet
123
     */
124
    public function addFieldSet(string $strLegend, string $strID = '', $iType = FormFieldSet::TEXT) : FormFieldSet
125
    {
126
        $oFS = new FormFieldSet($strLegend, $strID, $iType);
127
        $this->add($oFS);
128
129
        return $oFS;
130
    }
131
132
    /**
133
     * Add new line to this fieldset
134
     * @see FormLine
135
     * @param string $strLabel (default: '&nbsp;')
136
     * @return \SKien\Formgenerator\FormLine
137
     */
138
    public function addLine(string $strLabel = '&nbsp;') : FormLine
139
    {
140
        $oFL = new FormLine($strLabel);
141
        $this->add($oFL);
142
143
        return $oFL;
144
    }
145
146
    /**
147
     * Build the HTML-notation for the element and/or all child elements.
148
     * @return string
149
     * @internal
150
     */
151
    public function getHTML() : string
152
    {
153
        $strHTML = '';
154
        $iCnt = count($this->aChild);
155
        for ($i = 0; $i < $iCnt; $i++) {
156
            $strHTML .= $this->aChild[$i]->GetHTML();
157
        }
158
        return $strHTML;
159
    }
160
161
    /**
162
     * Get styles from all child elements.
163
     * This method gives each element the chance to add special styles to the
164
     * current page.
165
     * @return string
166
     * @internal
167
     */
168
    public function getStyle() : string
169
    {
170
        $strStyle = '';
171
        $iCnt = count($this->aChild);
172
        for ($i = 0; $i < $iCnt; $i++) {
173
            $strStyle .= $this->aChild[$i]->getStyle();
174
        }
175
        return $strStyle;
176
    }
177
}
178
179