Completed
Push — master ( 9d8405...8db55c )
by Daniel
02:33
created

CommonViews::getFieldNameForDisplay()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\common_lib;
30
31
/**
32
 * usefull functions to get quick results
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait CommonViews
37
{
38
39
    use MySQLiAdvancedOutput;
40
41
    /**
42
     * Builds field output w. special column name
43
     *
44
     * @param string $tableSource
45
     * @param array $dtl
46
     * @param array $features
47
     * @param string $fieldLabel
48
     * @return array
49
     */
50
    private function setField($tableSource, $dtl, $features, $fieldLabel)
51
    {
52
        if ($dtl['COLUMN_NAME'] == 'host') {
53
            $inVl = gethostbyaddr($this->tCmnRequest->server->get('REMOTE_ADDR'));
54
            return [
55
                'label' => '<label for="' . $dtl['COLUMN_NAME'] . '">Numele calculatorului</label>',
56
                'input' => '<input type="text" name="host" size="15" readonly value="' . $inVl . '" />',
57
            ];
58
        }
59
        $result = $this->setFieldInput($tableSource, $dtl, $features);
60
        return ['label' => $this->setFieldLabel($dtl, $features, $fieldLabel), 'input' => $result];
61
    }
62
63
    /**
64
     * Builds field output w. another special column name
65
     *
66
     * @param string $tableSource
67
     * @param array $dtl
68
     * @param array $features
69
     * @return string
70
     */
71
    private function setFieldInput($tableSource, $dtl, $features)
72
    {
73
        if ($dtl['COLUMN_NAME'] == 'ChoiceId') {
74
            return '<input type="text" name="ChoiceId" value="'
75
                    . $this->tCmnRequest->request->get($dtl['COLUMN_NAME']) . '" />';
76
        }
77
        return $this->setNeededFieldByType($tableSource, $dtl, $features);
78
    }
79
80
    /**
81
     * Returns a generic form based on a given table
82
     *
83
     * @param string $tblSrc
84
     * @param array $feat
85
     * @param array $hdnInf
86
     *
87
     * @return string Form to add/modify detail for a single row within a table
88
     */
89
    protected function setFormGenericSingleRecord($tblSrc, $feat, $hdnInf = [])
90
    {
91
        echo $this->setStringIntoTag('', 'div', ['id' => 'loading']);
92
        $this->setTableCache($tblSrc);
93
        if (strpos($tblSrc, '.') !== false) {
94
            $tblSrc = explode('.', str_replace('`', '', $tblSrc))[1];
95
        }
96
        $sReturn = [];
97
        if (count($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc]) != 0) {
98
            foreach ($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc] as $value) {
99
                $sReturn[] = $this->setNeededField($tblSrc, $value, $feat);
100
            }
101
        }
102
        $frmFtrs = ['id' => $feat['id'], 'action' => $feat['action'], 'method' => $feat['method']];
103
        return $this->setStringIntoTag(implode('', $sReturn) . $this->setFormButtons($feat, $hdnInf), 'form', $frmFtrs)
104
                . $this->setFormJavascriptFinal($feat['id']);
105
    }
106
107
    /**
108
     * Analyse the field and returns the proper line 2 use in forms
109
     *
110
     * @param string $tableSource
111
     * @param array $details
112
     * @param array $features
113
     * @return string|array
114
     */
115
    private function setNeededField($tableSource, $details, $features)
116
    {
117
        if (isset($features['hidden'])) {
118
            if (in_array($details['COLUMN_NAME'], $features['hidden'])) {
119
                return null;
120
            }
121
        }
122
        $fieldLabel = $this->getFieldNameForDisplay($details);
123
        if ($fieldLabel == 'hidden') {
124
            return null;
125
        }
126
        return $this->setNeededFieldFinal($tableSource, $details, $features, $fieldLabel);
127
    }
128
129
    /**
130
     * Analyse the field type and returns the proper lines 2 use in forms
131
     *
132
     * @param string $tblName
133
     * @param array $dtls
134
     * @param array $features
135
     * @return string|array
136
     */
137
    private function setNeededFieldByType($tblName, $dtls, $features)
138
    {
139
        if (isset($features['special']) && isset($features['special'][$dtls['COLUMN_NAME']])) {
140
            $sOpt = $this->setMySQLquery2Server($features['special'][$dtls['COLUMN_NAME']], 'array_key_value');
141
            return $this->setArrayToSelect($sOpt, $this->getFieldValue($dtls), $dtls['COLUMN_NAME'], ['size' => 1]);
0 ignored issues
show
Bug introduced by
It seems like $sOpt defined by $this->setMySQLquery2Ser...']], 'array_key_value') on line 140 can also be of type string; however, danielgp\common_lib\DomC...lGP::setArrayToSelect() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
142
        }
143
        return $this->setNeededFieldKnown($tblName, $dtls, $features);
144
    }
145
146
    private function setNeededFieldFinal($tableSource, $details, $features, $fieldLabel)
147
    {
148
        $sReturn = $this->setField($tableSource, $details, $features, $fieldLabel);
149
        $lmts    = $this->setFieldNumbers($details);
150
        return '<div>' . $sReturn['label']
151
                . $this->setStringIntoTag($sReturn['input'], 'span', ['class' => 'labell'])
152
                . '<span style="font-size:x-small;font-style:italic;">&nbsp;(max. '
153
                . $lmts['M'] . (isset($lmts['d']) ? ' w. ' . $lmts['d'] . ' decimals' : '') . ')</span>'
154
                . '</div>';
155
    }
156
157
    protected function setTableLocaleFields($localizationStrings)
158
    {
159
        $this->advCache['tableStructureLocales'] = $localizationStrings;
160
    }
161
162
    private function setViewDeleteFeedbacks()
163
    {
164
        return [
165
            'Confirmation' => $this->lclMsgCmn('i18n_Action_Confirmation'),
166
            'Failed'       => $this->lclMsgCmn('i18n_ActionDelete_Failed'),
167
            'Impossible'   => $this->lclMsgCmn('i18n_ActionDelete_Impossible'),
168
            'Success'      => $this->lclMsgCmn('i18n_ActionDelete_Success'),
169
        ];
170
    }
171
172
    private function setViewDeletePackedFinal($sReturn)
173
    {
174
        $finalJavascript = $this->setJavascriptContent(implode('', [
175
            '$("#DeleteFeedback").fadeOut(4000, function() {',
176
            '$(this).remove();',
177
            '});',
178
        ]));
179
        return '<div id="DeleteFeedback">' . $sReturn . '</div>' . $finalJavascript;
180
    }
181
182
    /**
183
     * Automatic handler for Record deletion
184
     *
185
     * @param string $tbl
186
     * @param string $idn
187
     * @return string
188
     */
189
    protected function setViewModernDelete($tbl, $idn)
190
    {
191
        $tMsg = $this->setViewDeleteFeedbacks();
192
        if ($tbl == '') {
193
            $sReturn = $this->setFeedbackModern('error', $tMsg['Confirmation'], $tMsg['Impossible']);
194
        } else {
195
            $idFldVal = $this->tCmnRequest->request->get($idn);
196
            $this->setMySQLquery2Server($this->sQueryToDeleteSingleIdentifier([$tbl, $idn, $idFldVal]));
197
            $sReturn  = $this->setFeedbackModern('error', $tMsg['Confirmation'], $tMsg['Failed'])
198
                    . '(' . $this->mySQLconnection->error . ')';
199
            if ($this->mySQLconnection->affected_rows > 0) {
200
                $sReturn = $this->setFeedbackModern('check', $tMsg['Confirmation'], $tMsg['Success']);
201
            }
202
        }
203
        return $this->setViewDeletePackedFinal($sReturn);
204
    }
205
}
206