DomHeaderFooterByDanielGP   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 200
rs 10
c 0
b 0
f 0
wmc 27

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setHeaderLanguage() 0 7 2
A setCalendarControl() 0 8 1
A setCalendarControlWithTime() 0 8 1
A setHeaderCssOrJavascript() 0 12 3
A setFooterCommonInjected() 0 10 3
A getSanitizedUrl() 0 3 1
A setHeaderCommon() 0 26 5
A setUpperRightBoxLanguages() 0 11 1
A setFooterCommon() 0 7 3
A setHeaderFeatures() 0 9 3
A setUpperRightVisibleOnHoverLanguages() 0 16 4
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2018 Daniel Popiniuc
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace danielgp\common_lib;
28
29
trait DomHeaderFooterByDanielGP
30
{
31
32
    use DomCssAndJavascriptByDanielGP;
33
34
    /**
35
     * Set a control to a user-friendly calendar
36
     *
37
     * @param string $controlName
38
     * @param string $additionalStyle
39
     * @return string
40
     */
41
    public function setCalendarControl($controlName, $additionalStyle = '')
42
    {
43
        return $this->setStringIntoTag('&nbsp;', 'span', [
44
                'onclick' => 'javascript:NewCssCal(\'' . $controlName
45
                . '\',\'yyyyMMdd\',\'dropdown\',false,\'24\',false);',
46
                'class'   => 'fa fa-calendar',
47
                'id'      => $controlName . '_picker',
48
                'style'   => 'cursor:pointer;' . $additionalStyle,
49
        ]);
50
    }
51
52
    /**
53
     * Set a control to a user-friendly calendar with time included
54
     *
55
     * @param string $controlName
56
     * @param string $additionalStyle
57
     * @return string
58
     */
59
    public function setCalendarControlWithTime($controlName, $additionalStyle = '')
60
    {
61
        return $this->setStringIntoTag('&nbsp;', 'span', [
62
                'onclick' => 'javascript:NewCssCal(\'' . $controlName
63
                . '\',\'yyyyMMdd\',\'dropdown\',true,\'24\',true);',
64
                'class'   => 'fa fa-calendar',
65
                'id'      => $controlName . '_picker',
66
                'style'   => 'cursor:pointer;' . $additionalStyle,
67
        ]);
68
    }
69
70
    /**
71
     * Outputs an HTML footer
72
     *
73
     * @param array $footerInjected
74
     * @return string
75
     */
76
    protected function setFooterCommon($footerInjected = null)
77
    {
78
        $sHK = $this->tCmnSuperGlobals->get('specialHook');
79
        if (!is_null($sHK) && (in_array('noHeader', $sHK))) {
80
            return '';
81
        }
82
        return $this->setFooterCommonInjected($footerInjected) . '</body></html>';
83
    }
84
85
    protected function setFooterCommonInjected($footerInjected = null)
86
    {
87
        $sReturn = '';
88
        if (!is_null($footerInjected)) {
89
            $sReturn = $footerInjected;
90
            if (is_array($footerInjected)) {
91
                $sReturn = implode('', $footerInjected);
92
            }
93
        }
94
        return $sReturn;
95
    }
96
97
    /**
98
     * Outputs an HTML header
99
     *
100
     * @param array $headerFeatures
101
     * @return string
102
     */
103
    protected function setHeaderCommon($headerFeatures = [])
104
    {
105
        $sReturn = [];
106
        $this->initializeSprGlbAndSession();
107
        $sHK     = $this->tCmnSuperGlobals->get('specialHook');
108
        if (!is_null($sHK) && (in_array('noHeader', $sHK))) {
109
            return ''; // no Header
110
        }
111
        $fixedHeaderElements = [
112
            'start'    => '<!DOCTYPE html>',
113
            'lang'     => $this->setHeaderLanguage($headerFeatures),
114
            'head'     => '<head>',
115
            'charset'  => '<meta charset="utf-8" />',
116
            'viewport' => '<meta name="viewport" content="width=device-width height=device-height initial-scale=1" />',
117
        ];
118
        if ($headerFeatures !== []) {
119
            $aFeatures = [];
120
            foreach ($headerFeatures as $key => $value) {
121
                $aFeatures[] = $this->setHeaderFeatures($key, $value);
122
            }
123
            return implode('', $fixedHeaderElements) . implode('', $aFeatures) . '</head>' . '<body>';
124
        }
125
        $sReturn[] = implode('', $fixedHeaderElements) . '</head>' . '<body>'
126
            . '<p style="background-color:red;color:#FFF;">The parameter sent to '
127
            . __FUNCTION__ . ' must be a non-empty array</p>' . $this->setFooterCommon();
128
        throw new \Exception(implode('', $sReturn));
129
    }
130
131
    /**
132
     *
133
     * @param string|array $value
134
     * @param string $sCssOrJavascript
135
     * @return string
136
     */
137
    private function setHeaderCssOrJavascript($value, $sCssOrJavascript)
138
    {
139
        $strFnToCall = (string) 'set' . ucwords($sCssOrJavascript) . 'File';
140
        if (is_array($value)) {
141
            $aFeatures = [];
142
            foreach ($value as $value2) {
143
                $fnResult    = call_user_func_array([$this, $strFnToCall], [$this->getSanitizedUrl($value2), null]);
144
                $aFeatures[] = $fnResult;
145
            }
146
            return implode('', $aFeatures);
147
        }
148
        return call_user_func_array([$this, $strFnToCall], [$this->getSanitizedUrl($value), null]);
149
    }
150
151
    /**
152
     *
153
     * @param string $key
154
     * @param string|array $value
155
     * @return string
156
     */
157
    private function setHeaderFeatures($key, $value)
158
    {
159
        $sReturn = '';
160
        if (in_array($key, ['css', 'javascript'])) {
161
            $sReturn = $this->setHeaderCssOrJavascript($value, $key);
162
        } elseif ($key == 'title') {
163
            $sReturn = '<title>' . filter_var($value, FILTER_SANITIZE_STRING) . '</title>';
164
        }
165
        return $sReturn;
166
    }
167
168
    /**
169
     *
170
     * @param array $headerFeatures
171
     * @return string
172
     */
173
    private function setHeaderLanguage($headerFeatures = [])
174
    {
175
        $sReturn = '<html lang="en-US">';
176
        if (array_key_exists('lang', $headerFeatures)) {
177
            $sReturn = str_replace('en-US', filter_var($headerFeatures['lang'], FILTER_SANITIZE_STRING), $sReturn);
178
        }
179
        return $sReturn;
180
    }
181
182
    protected function getSanitizedUrl($strInputUrl)
183
    {
184
        return filter_var($strInputUrl, FILTER_SANITIZE_URL);
185
    }
186
187
    /**
188
     * Create an upper right box with choices for languages
189
     * (requires flag-icon.min.css to be loaded)
190
     * (makes usage of custom class "upperRightBox" and id = "visibleOnHover", provided here as scss file)
191
     *
192
     * @param array $aAvailableLanguages
193
     * @return string
194
     */
195
    public function setUpperRightBoxLanguages($aAvailableLanguages)
196
    {
197
        $this->handleLanguageIntoSession();
198
        return '<div class="upperRightBox">'
199
            . '<div style="text-align:right;">'
200
            . '<span class="flag-icon flag-icon-' . strtolower(substr($this->tCmnSession->get('lang'), -2))
201
            . '" style="margin-right:2px;">&nbsp;</span>'
202
            . $aAvailableLanguages[$this->tCmnSession->get('lang')]
203
            . '</div><!-- default Language -->'
204
            . $this->setUpperRightVisibleOnHoverLanguages($aAvailableLanguages)
205
            . '</div><!-- upperRightBox end -->';
206
    }
207
208
    /**
209
     *
210
     * @param array $aAvailableLanguages
211
     * @return string
212
     */
213
    private function setUpperRightVisibleOnHoverLanguages($aAvailableLanguages)
214
    {
215
        $linkWithoutLanguage = '';
216
        $alR                 = $this->tCmnSuperGlobals->query->all();
217
        if (count($alR) > 0) {
218
            $linkWithoutLanguage = $this->setArrayToStringForUrl('&amp;', $alR, ['lang']) . '&amp;';
219
        }
220
        $sReturn = [];
221
        foreach ($aAvailableLanguages as $key => $value) {
222
            if ($this->tCmnSession->get('lang') !== $key) {
223
                $sReturn[] = '<a href="?' . $linkWithoutLanguage . 'lang=' . $key . '" style="display:block;">'
224
                    . '<span class="flag-icon flag-icon-' . strtolower(substr($key, -2))
225
                    . '" style="margin-right:2px;">&nbsp;</span>' . $value . '</a>';
226
            }
227
        }
228
        return '<div id="visibleOnHover">' . implode('', $sReturn) . '</div><!-- visibleOnHover end -->';
229
    }
230
}
231