DomBasicComponentsByDanielGP::setClearBoth1px()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
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
 * DOM component functions
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait DomBasicComponentsByDanielGP
37
{
38
39
    use CommonLibLocale;
40
41
    private function buildAttributesForTag($features)
42
    {
43
        if (!is_array($features)) {
44
            return '';
45
        }
46
        $attributes = '';
47
        foreach ($features as $key => $value) {
48
            $val        = $this->buildAttributesForTagValueArray($value);
49
            $attributes .= ' ' . $key . '="' . $val . '"';
50
        }
51
        return $attributes;
52
    }
53
54
    private function buildAttributesForTagValueArray($value)
55
    {
56
        $val = $value;
57
        if (is_array($value)) {
58
            $valA = [];
59
            foreach ($value as $key2 => $value2) {
60
                $valA[] = $key2 . ':' . $value2;
61
            }
62
            $val = implode(';', $valA) . ';';
63
        }
64
        return $val;
65
    }
66
67
    /**
68
     * Capitalize first letter of each word
69
     * AND filters only letters and numbers
70
     *
71
     * @param string $givenString
72
     * @return string
73
     */
74
    protected function cleanStringForId($givenString)
75
    {
76
        return preg_replace("/[^a-zA-Z0-9]/", '', ucwords($givenString));
77
    }
78
79
    /**
80
     * Cleans a string for certain internal rules
81
     *
82
     * @param string $urlString
83
     * @return string
84
     */
85
    protected function setCleanUrl($urlString)
86
    {
87
        $arrayToReplace = [
88
            '&#038;'    => '&amp;',
89
            '&'         => '&amp;',
90
            '&amp;amp;' => '&amp;',
91
            ' '         => '%20',
92
        ];
93
        $kys            = array_keys($arrayToReplace);
94
        $vls            = array_values($arrayToReplace);
95
        return str_replace($kys, $vls, filter_var($urlString, FILTER_SANITIZE_URL));
96
    }
97
98
    /**
99
     * Returns a div tag that clear any float
100
     *
101
     * @param integer $height
102
     */
103
    protected function setClearBoth1px($height = 1)
104
    {
105
        $divStyle = implode('', [
106
            'height:' . $height . 'px;',
107
            'line-height:' . $height . 'px;',
108
            'float:none;',
109
            'clear:both;',
110
            'margin:0px;'
111
        ]);
112
        return $this->setStringIntoTag('&nbsp;', 'div', ['style' => $divStyle]);
113
    }
114
115
    /**
116
     * Builds a structured modern message
117
     *
118
     * @param string $sType
119
     * @param string $sTitle
120
     * @param string $sMsg
121
     * @param boolean $skipBr
122
     */
123
    protected function setFeedbackModern($sType, $sTitle, $sMsg, $skipBr = false)
124
    {
125
        if ($sTitle == 'light') {
126
            return $sMsg;
127
        }
128
        $legend = $this->setStringIntoTag($sTitle, 'legend', ['style' => $this->setFeedbackStyle($sType)]);
129
        return implode('', [
130
            ($skipBr ? '' : '<br/>'),
131
            $this->setStringIntoTag($legend . $sMsg, 'fieldset', ['style' => $this->setFeedbackStyle($sType)]),
132
        ]);
133
    }
134
135
    private function setFeedbackStyle($sType)
136
    {
137
        $styleKnown = [
138
            'alert' => $this->setFeedbackStyleArray('orange', 'navy'),
139
            'check' => $this->setFeedbackStyleArray('green', 'white'),
140
            'error' => $this->setFeedbackStyleArray('red', 'yellow'),
141
            'info'  => $this->setFeedbackStyleArray('black', 'white'),
142
        ];
143
        return $styleKnown[$sType];
144
    }
145
146
    private function setFeedbackStyleArray($color1, $color2)
147
    {
148
        return [
149
            'Title' => 'margin-top:-5px;margin-right:20px;padding:5px;background-color:' . $color1
150
            . ';color:' . $color2 . 'border:medium solid ' . $color1 . ';',
151
            'Msg'   => 'display:inline;padding-right:5px;padding-bottom:5px;background-color:' . $color2
152
            . ';color:' . $color1 . ';border:medium solid ' . $color1 . ';',
153
        ];
154
    }
155
156
    /**
157
     * Sets the gzip footer for HTML
158
     */
159
    protected function setFooterGZiped()
160
    {
161
        if (extension_loaded('zlib')) {
162
            return $this->setGZipedUnsafe('Footer');
163
        }
164
        return '';
165
    }
166
167
    private function setGZipedUnsafe($outputType)
168
    {
169
        $this->initializeSprGlbAndSession();
170
        if (!is_null($this->tCmnRequest->server->get('HTTP_ACCEPT_ENCODING'))) {
171
            return '';
172
        } elseif (strstr($this->tCmnRequest->server->get('HTTP_ACCEPT_ENCODING'), 'gzip')) {
173
            $this->setGZipedUnsafeWithGzipEnabled($outputType);
174
        }
175
    }
176
177
    private function setGZipedUnsafeWithGzipEnabled($outputType)
178
    {
179
        if ($outputType === 'Footer') {
180
            $gzipCntntOriginal = ob_get_contents();
181
            ob_end_clean();
182
            $gzipCntnt         = gzcompress($gzipCntntOriginal, 9);
183
            echo "\x1f\x8b\x08\x00\x00\x00\x00\x00" . substr($gzipCntnt, 0, strlen($gzipCntnt) - 4)
184
            . pack('V', crc32($gzipCntntOriginal)) . pack('V', strlen($gzipCntntOriginal));
185
        } elseif ($outputType === 'Header') {
186
            ob_start();
187
            ob_implicit_flush(0);
188
            header('Content-Encoding: gzip');
189
        }
190
    }
191
192
    /**
193
     * Sets the gzip header for HTML
194
     */
195
    protected function setHeaderGZiped()
196
    {
197
        if (extension_loaded('zlib')) {
198
            return $this->setGZipedUnsafe('Header');
199
        }
200
        return '';
201
    }
202
203
    /**
204
     * Sets the no-cache header
205
     */
206
    protected function setHeaderNoCache($contentType = 'application/json')
207
    {
208
        header("Content-Type: " . $contentType . "; charset=utf-8");
209
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
210
        header("Cache-Control: no-store, no-cache, must-revalidate");
211
        header("Cache-Control: post-check=0, pre-check=0", false);
212
        header("Pragma: no-cache");
213
    }
214
215
    /**
216
     * Puts a given string into a specific short tag
217
     *
218
     * @param string $sTag
219
     * @param array $features
220
     * @return string
221
     */
222
    protected function setStringIntoShortTag($sTag, $features = null)
223
    {
224
        return '<' . $sTag . $this->buildAttributesForTag($features)
225
            . (isset($features['dont_close']) ? '' : '/') . '>';
226
    }
227
228
    /**
229
     * Puts a given string into a specific tag
230
     *
231
     * @param string $sString
232
     * @param string $sTag
233
     * @param array $features
234
     * @return string
235
     */
236
    protected function setStringIntoTag($sString, $sTag, $features = null)
237
    {
238
        return '<' . $sTag . $this->buildAttributesForTag($features) . '>' . $sString . '</' . $sTag . '>';
239
    }
240
241
    protected function setViewModernLinkAdd($identifier, $ftrs = null)
242
    {
243
        $btnText     = '<i class="fa fa-plus-square">&nbsp;</i>' . '&nbsp;' . $this->lclMsgCmn('i18n_AddNewRecord');
244
        $tagFeatures = [
245
            'href'  => $this->setViewModernLinkAddUrl($identifier, $ftrs),
246
            'style' => 'margin: 5px 0px 10px 0px; display: inline-block;',
247
        ];
248
        return $this->setStringIntoTag($btnText, 'a', $tagFeatures);
249
    }
250
251
    protected function setViewModernLinkAddInjectedArguments($ftrs = null)
252
    {
253
        $sArgmnts = '';
254
        if (isset($ftrs['injectAddArguments'])) {
255
            foreach ($ftrs['injectAddArguments'] as $key => $value) {
256
                $sArgmnts .= '&amp;' . $key . '=' . $value;
257
            }
258
        }
259
        return $sArgmnts;
260
    }
261
262
    protected function setViewModernLinkAddUrl($identifier, $ftrs = null)
263
    {
264
        $sArgmnts  = $this->setViewModernLinkAddInjectedArguments($ftrs);
265
        $this->initializeSprGlbAndSession();
266
        $addingUrl = $this->tCmnRequest->server->get('PHP_SELF') . '?view=add_' . $identifier . $sArgmnts;
267
        if (!isset($ftrs['NoAjax'])) {
268
            $addingUrl = 'javascript:loadAE(\'' . $addingUrl . '\');';
269
        }
270
        return $addingUrl;
271
    }
272
}
273