Passed
Push — master ( ab3dee...cb29e2 )
by Daniel
02:51
created

setCalendarControlWithTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
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 . '\',\'yyyyMMdd\',\'dropdown\',false,\'24\',false);',
45
                'class'   => 'fa fa-calendar',
46
                'id'      => $controlName . '_picker',
47
                'style'   => 'cursor:pointer;' . $additionalStyle,
48
        ]);
49
    }
50
51
    /**
52
     * Set a control to a user-friendly calendar with time included
53
     *
54
     * @param string $controlName
55
     * @param string $additionalStyle
56
     * @return string
57
     */
58
    public function setCalendarControlWithTime($controlName, $additionalStyle = '')
59
    {
60
        return $this->setStringIntoTag('&nbsp;', 'span', [
61
                'onclick' => 'javascript:NewCssCal(\'' . $controlName . '\',\'yyyyMMdd\',\'dropdown\',true,\'24\',true);',
62
                'class'   => 'fa fa-calendar',
63
                'id'      => $controlName . '_picker',
64
                'style'   => 'cursor:pointer;' . $additionalStyle,
65
        ]);
66
    }
67
68
    /**
69
     * Outputs an HTML footer
70
     *
71
     * @param array $footerInjected
72
     * @return string
73
     */
74
    protected function setFooterCommon($footerInjected = null)
75
    {
76
        $sHK = $this->tCmnSuperGlobals->get('specialHook');
77
        if (!is_null($sHK) && (in_array('noHeader', $sHK))) {
78
            return '';
79
        }
80
        return $this->setFooterCommonInjected($footerInjected) . '</body></html>';
81
    }
82
83
    protected function setFooterCommonInjected($footerInjected = null)
84
    {
85
        $sReturn = '';
86
        if (!is_null($footerInjected)) {
87
            $sReturn = $footerInjected;
88
            if (is_array($footerInjected)) {
89
                $sReturn = implode('', $footerInjected);
90
            }
91
        }
92
        return $sReturn;
93
    }
94
95
    /**
96
     * Outputs an HTML header
97
     *
98
     * @param array $headerFeatures
99
     * @return string
100
     */
101
    protected function setHeaderCommon($headerFeatures = [])
102
    {
103
        $sReturn = [];
104
        $sHK     = $this->tCmnSuperGlobals->get('specialHook');
105
        if (!is_null($sHK) && (in_array('noHeader', $sHK))) {
106
            return ''; // no Header
107
        }
108
        $fixedHeaderElements = [
109
            'start'    => '<!DOCTYPE html>',
110
            'lang'     => $this->setHeaderLanguage($headerFeatures),
111
            'head'     => '<head>',
112
            'charset'  => '<meta charset="utf-8" />',
113
            'viewport' => '<meta name="viewport" content="width=device-width height=device-height initial-scale=1" />',
114
        ];
115
        if ($headerFeatures !== []) {
116
            $aFeatures = [];
117
            foreach ($headerFeatures as $key => $value) {
118
                $aFeatures[] = $this->setHeaderFeatures($key, $value);
119
            }
120
            return implode('', $fixedHeaderElements) . implode('', $aFeatures) . '</head>' . '<body>';
121
        }
122
        $sReturn[] = implode('', $fixedHeaderElements) . '</head>' . '<body>'
123
            . '<p style="background-color:red;color:#FFF;">The parameter sent to '
124
            . __FUNCTION__ . ' must be a non-empty array</p>' . $this->setFooterCommon();
125
        throw new \Exception(implode('', $sReturn));
126
    }
127
128
    /**
129
     *
130
     * @param string|array $value
131
     * @param string $sCssOrJavascript
132
     * @return string
133
     */
134
    private function setHeaderCssOrJavascript($value, $sCssOrJavascript)
135
    {
136
        $strFunctionToCall = (string) 'set' . ucwords($sCssOrJavascript) . 'File';
137
        if (is_array($value)) {
138
            $aFeatures = [];
139
            foreach ($value as $value2) {
140
                $fnResult    = call_user_func_array([$this, $strFunctionToCall], [$this->getSanitizedUrl($value2), null]);
141
                $aFeatures[] = $fnResult;
142
            }
143
            return implode('', $aFeatures);
144
        }
145
        return call_user_func_array([$this, $strFunctionToCall], [$this->getSanitizedUrl($value), null]);
146
    }
147
148
    /**
149
     *
150
     * @param string $key
151
     * @param string|array $value
152
     * @return string
153
     */
154
    private function setHeaderFeatures($key, $value)
155
    {
156
        $sReturn = '';
157
        if (in_array($key, ['css', 'javascript'])) {
158
            $sReturn = $this->setHeaderCssOrJavascript($value, $key);
159
        } elseif ($key == 'title') {
160
            $sReturn = '<title>' . filter_var($value, FILTER_SANITIZE_STRING) . '</title>';
161
        }
162
        return $sReturn;
163
    }
164
165
    /**
166
     *
167
     * @param array $headerFeatures
168
     * @return string
169
     */
170
    private function setHeaderLanguage($headerFeatures = [])
171
    {
172
        $sReturn = '<html lang="en-US">';
173
        if (array_key_exists('lang', $headerFeatures)) {
174
            $sReturn = str_replace('en-US', filter_var($headerFeatures['lang'], FILTER_SANITIZE_STRING), $sReturn);
175
        }
176
        return $sReturn;
177
    }
178
179
    protected function getSanitizedUrl($strInputUrl)
180
    {
181
        return filter_var($strInputUrl, FILTER_SANITIZE_URL);
182
    }
183
184
    /**
185
     * Create an upper right box with choices for languages
186
     * (requires flag-icon.min.css to be loaded)
187
     * (makes usage of custom class "upperRightBox" and id = "visibleOnHover", provided here as scss file)
188
     *
189
     * @param array $aAvailableLanguages
190
     * @return string
191
     */
192
    protected function setUpperRightBoxLanguages($aAvailableLanguages)
193
    {
194
        $this->handleLanguageIntoSession();
195
        return '<div class="upperRightBox">'
196
            . '<div style="text-align:right;">'
197
            . '<span class="flag-icon flag-icon-' . strtolower(substr($this->tCmnSession->get('lang'), -2))
198
            . '" style="margin-right:2px;">&nbsp;</span>'
199
            . $aAvailableLanguages[$this->tCmnSession->get('lang')]
200
            . '</div><!-- default Language -->'
201
            . $this->setUpperRightVisibleOnHoverLanguages($aAvailableLanguages)
202
            . '</div><!-- upperRightBox end -->';
203
    }
204
205
    /**
206
     *
207
     * @param array $aAvailableLanguages
208
     * @return string
209
     */
210
    private function setUpperRightVisibleOnHoverLanguages($aAvailableLanguages)
211
    {
212
        $linkWithoutLanguage = '';
213
        $alR                 = $this->tCmnSuperGlobals->query->all();
214
        if (count($alR) > 0) {
215
            $linkWithoutLanguage = $this->setArrayToStringForUrl('&amp;', $alR, ['lang']) . '&amp;';
216
        }
217
        $sReturn = [];
218
        foreach ($aAvailableLanguages as $key => $value) {
219
            if ($this->tCmnSession->get('lang') !== $key) {
220
                $sReturn[] = '<a href="?' . $linkWithoutLanguage . 'lang=' . $key . '" style="display:block;">'
221
                    . '<span class="flag-icon flag-icon-' . strtolower(substr($key, -2))
222
                    . '" style="margin-right:2px;">&nbsp;</span>' . $value . '</a>';
223
            }
224
        }
225
        return '<div id="visibleOnHover">' . implode('', $sReturn) . '</div><!-- visibleOnHover end -->';
226
    }
227
228
}
229