MulticolumnsThemeForm::setTitles()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Wfdownloads;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * WfdownloadsMulticolumnsThemeForm Class
17
 *
18
 * @copyright   XOOPS Project (https://xoops.org)
19
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
20
 * @author      lucio <[email protected]>
21
 * @package     Wfdownloads
22
 * @since       3.23
23
 */
24
25
use XoopsModules\Wfdownloads;
26
27
\xoops_load('XoopsForm');
28
29
/**
30
 * Form that will output formatted as a HTML table
31
 *
32
 * No styles and no JavaScript to check for required fields.
33
 */
34
class MulticolumnsThemeForm extends \XoopsForm
35
{
36
    /**
37
     * number of two-dimensional array (matrix) rows
38
     *
39
     * @var int
40
     */
41
    public $_rows = 0;
42
43
    /**
44
     * return number of rows
45
     *
46
     * @return int
47
     */
48
    public function getCountRows()
49
    {
50
        return $this->_rows;
51
    }
52
53
    /**
54
     * number of two-dimensional array (matrix) columns
55
     *
56
     * @var int
57
     */
58
    public $_columns = 0;
59
60
    /**
61
     * return number of columns
62
     *
63
     * @return int
64
     */
65
    public function getCountColumns()
66
    {
67
        return $this->_columns;
68
    }
69
70
    /**
71
     * two-dimensional array (matrix) of {@link XoopsFormElement} objects
72
     *
73
     * @var array
74
     */
75
    //    public $_elements = [];
76
77
    /**
78
     * mono-dimensional array of column titles
79
     *
80
     * @var array
81
     */
82
    public $_titles = [];
83
84
    /**
85
     * Add an element to the form
86
     *
87
     * @param string|\XoopsFormElement|\XoopsForm $formElement
88
     * @param bool                                $required is this a "required" element?
89
     * @param int                                 $row      two-dimensional array (matrix) row (0 first key)
90
     * @param int                                 $column   two-dimensional array (matrix) column (0 first key)
91
     */
92
    public function addElement($formElement, $required = false, $row = null, $column = null)
93
    {
94
        if (null === $row) {
95
            $row = $this->_rows;
96
        }
97
        if (null === $column) {
98
            $column = (0 == $this->_columns) ? $this->_columns : $this->_columns - 1;
99
        } // add new element as new row of the last column
100
        if (\is_string($formElement)) {
101
            $this->_elements[$row][$column] = $formElement;
102
            if ($row >= $this->_rows) {
103
                $this->_rows = $row + 1;
104
            }
105
            if ($column >= $this->_columns) {
106
                $this->_columns = $column + 1;
107
            }
108
        } elseif (\is_subclass_of($formElement, 'xoopsformelement')) {
109
            $this->_elements[$row][$column] = &$formElement;
110
            if ($row >= $this->_rows) {
111
                $this->_rows = $row + 1;
112
            }
113
            if ($column >= $this->_columns) {
114
                $this->_columns = $column + 1;
115
            }
116
            if (!$formElement->isContainer()) {
0 ignored issues
show
Bug introduced by
The method isContainer() does not exist on XoopsForm. ( Ignorable by Annotation )

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

116
            if (!$formElement->/** @scrutinizer ignore-call */ isContainer()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
                if ($required) {
118
                    $formElement->_required = true;
0 ignored issues
show
Documentation Bug introduced by
It seems like true of type true is incompatible with the declared type array of property $_required.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
119
                    $this->_required[]      = &$formElement;
120
                }
121
            } else {
122
                $required_elements = &$formElement->getRequired();
0 ignored issues
show
Bug introduced by
The method getRequired() does not exist on XoopsFormElement. It seems like you code against a sub-type of XoopsFormElement such as XoopsFormElementTray. ( Ignorable by Annotation )

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

122
                $required_elements = &$formElement->/** @scrutinizer ignore-call */ getRequired();
Loading history...
123
                foreach ($required_elements as $i => $iValue) {
124
                    $this->_required[] = &$required_elements[$i];
125
                }
126
            }
127
        }
128
    }
129
130
    /**
131
     * @param $elements
132
     */
133
    public function addRow($elements)
134
    {
135
        foreach ($elements as $key => $element) {
136
            $this->addElement($element, false, $this->_rows, $key);
137
        }
138
    }
139
140
    /**
141
     * @param $elements
142
     */
143
    public function addColumn($elements)
144
    {
145
        foreach ($elements as $key => $element) {
146
            $this->addElement($element, false, $key, $this->_columns);
147
        }
148
    }
149
150
    /**
151
     * @param $form
152
     */
153
    public function addForm($form)
154
    {
155
        foreach ($form->getElements() as  $key => $element) {
156
            $this->addElement($element, $element->isRequired(), $key, $this->_columns);
157
        }
158
    }
159
160
    /**
161
     * @param $titles
162
     */
163
    public function setTitles($titles)
164
    {
165
        if (\is_array($titles)) {
166
            foreach ($titles as $key => $title) {
167
                $this->_titles[$key] = $title;
168
            }
169
        } else {
170
            $this->_title = $titles;
171
        }
172
    }
173
174
    /**
175
     * create HTML to output the form as a theme-enabled table with validation.
176
     *
177
     * YOU SHOULD AVOID TO USE THE FOLLOWING Nocolspan METHOD, IT WILL BE REMOVED
178
     *
179
     * To use the noColspan simply use the following example:
180
     *
181
     * $colspan = new \XoopsFormDhtmlTextArea( '', 'key', $value, '100%', '100%' );
182
     * $colspan->setNocolspan();
183
     * $form->addElement( $colspan );
184
     *
185
     * @return string
186
     */
187
    public function render()
188
    {
189
        $ele_name = $this->getName();
190
        $ret      = '';
191
        $ret      .= "<form name='{$ele_name}' id='{$ele_name}' action='{$this->getAction()}' method='{$this->getMethod()}' onsubmit='return xoopsFormValidate_{$ele_name}();' {$this->getExtra()} >" . NWLINE;
192
        $ret      .= "<table width='100%' class='outer' cellspacing='1'>" . NWLINE;
193
        $ret      .= "<tr><th colspan='{$this->_columns}'>{$this->getTitle()}</th></tr>" . NWLINE;
194
        if (\count($this->_titles) > 0) {
195
            $ret .= '<tr>';
196
            for ($column = 0; $column < $this->_columns; ++$column) {
197
                $ret .= '<th>';
198
                $ret .= isset($this->_titles[$column]) ? (string)$this->_titles[$column] : '&nbsp;';
199
                $ret .= '</th>' . NWLINE;
200
            }
201
            $ret .= '</tr>';
202
        }
203
        $hidden = '';
204
        $class  = 'even';
205
        for ($row = 0; $row < $this->_rows; ++$row) {
206
            $ret .= '<tr>';
207
            for ($column = 0; $column < $this->_columns; ++$column) {
208
                $ret .= "<td class='{$class}'>";
209
                $ele = $this->_elements[$row][$column] ?? '&nbsp;';
210
                if (!\is_object($ele)) {
211
                    $ret .= $ele;
212
                } elseif (!$ele->isHidden()) {
213
                    if (!$ele->getNocolspan()) {
214
                        //$ret .= '<tr valign="top" align="left"><td class="head">';
215
                        if ('' !== ($caption = $ele->getCaption())) {
216
                            $ret .= "<div class='xoops-form-element-caption" . ($ele->isRequired() ? '-required' : '') . "'>";
217
                            $ret .= "<span class='caption-text'>{$caption}</span>";
218
                            $ret .= "<span class='caption-marker'>*</span>";
219
                            $ret .= '</div>';
220
                        }
221
                        if ('' !== ($desc = $ele->getDescription())) {
222
                            $ret .= "<div class='xoops-form-element-help'>{$desc}</div>";
223
                        }
224
                        //$ret .= '</td><td class="' . $class . '">';
225
                        $ret .= $ele->render();
226
                        //$ret .= '</td></tr>' . NWLINE;
227
                    } else {
228
                        //$ret .= '<tr valign="top" align="left"><td class="head" colspan="2">';
229
                        if ('' !== ($caption = $ele->getCaption())) {
230
                            $ret .= "<div class='xoops-form-element-caption" . ($ele->isRequired() ? '-required' : '') . "'>";
231
                            $ret .= "<span class='caption-text'>{$caption}</span>";
232
                            $ret .= "<span class='caption-marker'>*</span>";
233
                            $ret .= '</div>';
234
                        }
235
                        //$ret .= '</td></tr>' . NWLINE;
236
                        //$ret .= '<tr valign="top" align="left"><td class="' . $class . '" colspan="' . $this->_columns . '">';
237
                        $ret .= $ele->render();
238
                        //$ret .= '</td></tr>' . NWLINE;
239
                    }
240
                } else {
241
                    $hidden .= $ele->render();
242
                }
243
                $ret .= '</td>';
244
            }
245
            $ret .= '</tr>';
246
        }
247
        $ret .= '</table>' . NWLINE;
248
        $ret .= $hidden . NWLINE;
249
        $ret .= '</form>' . NWLINE;
250
        $ret .= $this->renderValidationJS(true);
251
252
        return $ret;
253
    }
254
}
255