Completed
Push — master ( 56163a...9d8405 )
by Daniel
02:39
created

CommonViews::setFormGenericSingleRecord()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 12
nc 4
nop 3
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 the name of a field for displaying
82
     *
83
     * @param array $details
84
     * @return string
85
     */
86
    private function getFieldNameForDisplay($details)
87
    {
88
        $tableUniqueId = $details['TABLE_SCHEMA'] . '.' . $details['TABLE_NAME'];
89
        if ($details['COLUMN_COMMENT'] != '') {
90
            return $details['COLUMN_COMMENT'];
91
        } elseif (isset($this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']])) {
92
            return $this->advCache['tableStructureLocales'][$tableUniqueId][$details['COLUMN_NAME']];
93
        }
94
        return $details['COLUMN_NAME'];
95
    }
96
97
    /**
98
     * Returns a generic form based on a given table
99
     *
100
     * @param string $tblSrc
101
     * @param array $feat
102
     * @param array $hdnInf
103
     *
104
     * @return string Form to add/modify detail for a single row within a table
105
     */
106
    protected function setFormGenericSingleRecord($tblSrc, $feat, $hdnInf = [])
107
    {
108
        echo $this->setStringIntoTag('', 'div', ['id' => 'loading']);
109
        $this->setTableCache($tblSrc);
110
        if (strpos($tblSrc, '.') !== false) {
111
            $tblSrc = explode('.', str_replace('`', '', $tblSrc))[1];
112
        }
113
        $sReturn = [];
114
        if (count($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc]) != 0) {
115
            foreach ($this->advCache['tableStructureCache'][$this->advCache['workingDatabase']][$tblSrc] as $value) {
116
                $sReturn[] = $this->setNeededField($tblSrc, $value, $feat);
117
            }
118
        }
119
        $frmFtrs = ['id' => $feat['id'], 'action' => $feat['action'], 'method' => $feat['method']];
120
        return $this->setStringIntoTag(implode('', $sReturn) . $this->setFormButtons($feat, $hdnInf), 'form', $frmFtrs)
121
                . $this->setFormJavascriptFinal($feat['id']);
122
    }
123
124
    /**
125
     * Analyse the field and returns the proper line 2 use in forms
126
     *
127
     * @param string $tableSource
128
     * @param array $details
129
     * @param array $features
130
     * @return string|array
131
     */
132
    private function setNeededField($tableSource, $details, $features)
133
    {
134
        if (isset($features['hidden'])) {
135
            if (in_array($details['COLUMN_NAME'], $features['hidden'])) {
136
                return null;
137
            }
138
        }
139
        $fieldLabel = $this->getFieldNameForDisplay($details);
140
        if ($fieldLabel == 'hidden') {
141
            return null;
142
        }
143
        return $this->setNeededFieldFinal($tableSource, $details, $features, $fieldLabel);
144
    }
145
146
    /**
147
     * Analyse the field type and returns the proper lines 2 use in forms
148
     *
149
     * @param string $tblName
150
     * @param array $dtls
151
     * @param array $features
152
     * @return string|array
153
     */
154
    private function setNeededFieldByType($tblName, $dtls, $features)
155
    {
156
        if (isset($features['special']) && isset($features['special'][$dtls['COLUMN_NAME']])) {
157
            $sOpt = $this->setMySQLquery2Server($features['special'][$dtls['COLUMN_NAME']], 'array_key_value');
158
            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 157 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...
159
        }
160
        return $this->setNeededFieldKnown($tblName, $dtls, $features);
161
    }
162
163
    private function setNeededFieldFinal($tableSource, $details, $features, $fieldLabel)
164
    {
165
        $sReturn = $this->setField($tableSource, $details, $features, $fieldLabel);
166
        $lmts    = $this->setFieldNumbers($details);
167
        return '<div>' . $sReturn['label']
168
                . $this->setStringIntoTag($sReturn['input'], 'span', ['class' => 'labell'])
169
                . '<span style="font-size:x-small;font-style:italic;">&nbsp;(max. '
170
                . $lmts['M'] . (isset($lmts['d']) ? ' w. ' . $lmts['d'] . ' decimals' : '') . ')</span>'
171
                . '</div>';
172
    }
173
174
    protected function setTableLocaleFields($localizationStrings)
175
    {
176
        $this->advCache['tableStructureLocales'] = $localizationStrings;
177
    }
178
179
    private function setViewDeleteFeedbacks()
180
    {
181
        return [
182
            'Confirmation' => $this->lclMsgCmn('i18n_Action_Confirmation'),
183
            'Failed'       => $this->lclMsgCmn('i18n_ActionDelete_Failed'),
184
            'Impossible'   => $this->lclMsgCmn('i18n_ActionDelete_Impossible'),
185
            'Success'      => $this->lclMsgCmn('i18n_ActionDelete_Success'),
186
        ];
187
    }
188
189
    private function setViewDeletePackedFinal($sReturn)
190
    {
191
        $finalJavascript = $this->setJavascriptContent(implode('', [
192
            '$("#DeleteFeedback").fadeOut(4000, function() {',
193
            '$(this).remove();',
194
            '});',
195
        ]));
196
        return '<div id="DeleteFeedback">' . $sReturn . '</div>' . $finalJavascript;
197
    }
198
199
    /**
200
     * Automatic handler for Record deletion
201
     *
202
     * @param string $tbl
203
     * @param string $idn
204
     * @return string
205
     */
206
    protected function setViewModernDelete($tbl, $idn)
207
    {
208
        $tMsg = $this->setViewDeleteFeedbacks();
209
        if ($tbl == '') {
210
            $sReturn = $this->setFeedbackModern('error', $tMsg['Confirmation'], $tMsg['Impossible']);
211
        } else {
212
            $idFldVal = $this->tCmnRequest->request->get($idn);
213
            $this->setMySQLquery2Server($this->sQueryToDeleteSingleIdentifier([$tbl, $idn, $idFldVal]));
214
            $sReturn  = $this->setFeedbackModern('error', $tMsg['Confirmation'], $tMsg['Failed'])
215
                    . '(' . $this->mySQLconnection->error . ')';
216
            if ($this->mySQLconnection->affected_rows > 0) {
217
                $sReturn = $this->setFeedbackModern('check', $tMsg['Confirmation'], $tMsg['Success']);
218
            }
219
        }
220
        return $this->setViewDeletePackedFinal($sReturn);
221
    }
222
}
223