Completed
Push — master ( 8c2a67...711ae7 )
by Daniel
02:48
created

DomBasicComponentsByDanielGP::setClearBoth1px()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
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
 * 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
     * Capatalize 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 type $urlString
83
     * @return type
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->setFeedbackStyleTitle($sType)]);
129
        return implode('', [
130
            ($skipBr ? '' : '<br/>'),
131
            $this->setStringIntoTag($legend . $sMsg, 'fieldset', ['style' => $this->setFeedbackStyleMessage($sType)]),
132
        ]);
133
    }
134
135 View Code Duplication
    private function setFeedbackStyleTitle($sType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $formatTitle = 'margin-top:-5px;margin-right:20px;padding:5px;';
138
        $styleByType = [
139
            'alert' => 'border:medium solid orange;background-color:orange;color:navy;',
140
            'check' => 'border:medium solid green;background-color:green;color:white;',
141
            'error' => 'border:medium solid red;background-color:red;color:white;',
142
            'info'  => 'border:medium solid black;background-color:black;color:white;font-weight:bold;',
143
        ];
144
        return $formatTitle . $styleByType[$sType];
145
    }
146
147 View Code Duplication
    private function setFeedbackStyleMessage($sType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
    {
149
        $formatMessage = 'display:inline;padding-right:5px;padding-bottom:5px;';
150
        $styleByType   = [
151
            'alert' => 'background-color:navy;color:orange;border:medium solid orange;',
152
            'check' => 'background-color:yellow;color:green;border:medium solid green;',
153
            'error' => 'background-color:yellow;color:red;border:medium solid red;',
154
            'info'  => 'background-color: white; color: black;border:medium solid black;',
155
        ];
156
        return $formatMessage . $styleByType[$sType];
157
    }
158
159
    /**
160
     * Sets the gzip footer for HTML
161
     */
162
    protected function setFooterGZiped()
163
    {
164
        if (extension_loaded('zlib')) {
165
            return $this->setGZipedUnsafe('Footer');
166
        }
167
        return '';
168
    }
169
170
    private function setGZipedUnsafe($outputType)
171
    {
172
        $this->initializeSprGlbAndSession();
173
        if (!is_null($this->tCmnRequest->server->get('HTTP_ACCEPT_ENCODING'))) {
174
            return '';
175
        } elseif (strstr($this->tCmnRequest->server->get('HTTP_ACCEPT_ENCODING'), 'gzip')) {
176
            switch ($outputType) {
177
                case 'Footer':
178
                    $gzipCntntOriginal = ob_get_contents();
179
                    ob_end_clean();
180
                    $gzipCntnt         = gzcompress($gzipCntntOriginal, 9);
181
                    echo "\x1f\x8b\x08\x00\x00\x00\x00\x00" . substr($gzipCntnt, 0, strlen($gzipCntnt) - 4)
182
                    . pack('V', crc32($gzipCntntOriginal)) . pack('V', strlen($gzipCntntOriginal));
183
                    break;
184
                case 'Header':
185
                    ob_start();
186
                    ob_implicit_flush(0);
187
                    header('Content-Encoding: gzip');
188
                    break;
189
            }
190
        }
191
    }
192
193
    /**
194
     * Sets the gzip header for HTML
195
     */
196
    protected function setHeaderGZiped()
197
    {
198
        if (extension_loaded('zlib')) {
199
            return $this->setGZipedUnsafe('Header');
200
        }
201
        return '';
202
    }
203
204
    /**
205
     * Sets the no-cache header
206
     */
207
    protected function setHeaderNoCache($contentType = 'application/json')
208
    {
209
        header("Content-Type: " . $contentType . "; charset=utf-8");
210
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
211
        header("Cache-Control: no-store, no-cache, must-revalidate");
212
        header("Cache-Control: post-check=0, pre-check=0", false);
213
        header("Pragma: no-cache");
214
    }
215
216
    /**
217
     * Puts a given string into a specific short tag
218
     *
219
     * @param string $sTag
220
     * @param array $features
221
     * @return string
222
     */
223
    protected function setStringIntoShortTag($sTag, $features = null)
224
    {
225
        return '<' . $sTag . $this->buildAttributesForTag($features)
226
                . (isset($features['dont_close']) ? '' : '/') . '>';
227
    }
228
229
    /**
230
     * Puts a given string into a specific tag
231
     *
232
     * @param string $sString
233
     * @param string $sTag
234
     * @param array $features
235
     * @return string
236
     */
237
    protected function setStringIntoTag($sString, $sTag, $features = null)
238
    {
239
        return '<' . $sTag . $this->buildAttributesForTag($features) . '>' . $sString . '</' . $sTag . '>';
240
    }
241
242
    protected function setViewModernLinkAdd($identifier, $ftrs = null)
243
    {
244
        $btnText     = '<i class="fa fa-plus-square">&nbsp;</i>' . '&nbsp;' . $this->lclMsgCmn('i18n_AddNewRecord');
245
        $tagFeatures = [
246
            'href'  => $this->setViewModernLinkAddUrl($identifier, $ftrs),
247
            'style' => 'margin: 5px 0px 10px 0px; display: inline-block;',
248
        ];
249
        return $this->setStringIntoTag($btnText, 'a', $tagFeatures);
250
    }
251
252
    protected function setViewModernLinkAddInjectedArguments($ftrs = null)
253
    {
254
        $sArgmnts = '';
255
        if (isset($ftrs['injectAddArguments'])) {
256
            foreach ($ftrs['injectAddArguments'] as $key => $value) {
257
                $sArgmnts .= '&amp;' . $key . '=' . $value;
258
            }
259
        }
260
        return $sArgmnts;
261
    }
262
263
    protected function setViewModernLinkAddUrl($identifier, $ftrs = null)
264
    {
265
        $sArgmnts  = $this->setViewModernLinkAddInjectedArguments($ftrs);
266
        $this->initializeSprGlbAndSession();
267
        $addingUrl = $this->tCmnRequest->server->get('PHP_SELF') . '?view=add_' . $identifier . $sArgmnts;
268
        if (!isset($ftrs['NoAjax'])) {
269
            $addingUrl = 'javascript:loadAE(\'' . $addingUrl . '\');';
270
        }
271
        return $addingUrl;
272
    }
273
}
274