Completed
Push — master ( cfc16f...6c07bb )
by Daniel
03:06
created

setViewModernLinkAdd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
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
     * Sets the gzip footer for HTML
117
     */
118
    protected function setFooterGZiped()
119
    {
120
        if (extension_loaded('zlib')) {
121
            return $this->setGZipedUnsafe('Footer');
122
        }
123
        return '';
124
    }
125
126
    private function setGZipedUnsafe($outputType)
127
    {
128
        $this->initializeSprGlbAndSession();
129
        if (!is_null($this->tCmnRequest->server->get('HTTP_ACCEPT_ENCODING'))) {
130
            return '';
131
        }
132
        if (strstr($this->tCmnRequest->server->get('HTTP_ACCEPT_ENCODING'), 'gzip')) {
133
            switch ($outputType) {
134
                case 'Footer':
135
                    $gzipCntnt = ob_get_contents();
136
                    ob_end_clean();
137
                    $gzipSize  = strlen($gzipCntnt);
138
                    $gzipCrc   = crc32($gzipCntnt);
139
                    $gzipCntnt = gzcompress($gzipCntnt, 9);
140
                    $gzipCntnt = substr($gzipCntnt, 0, strlen($gzipCntnt) - 4);
141
                    echo "\x1f\x8b\x08\x00\x00\x00\x00\x00" . $gzipCntnt . pack('V', $gzipCrc) . pack('V', $gzipSize);
142
                    break;
143
                case 'Header':
144
                    ob_start();
145
                    ob_implicit_flush(0);
146
                    header('Content-Encoding: gzip');
147
                    break;
148
            }
149
        }
150
    }
151
152
    /**
153
     * Sets the gzip header for HTML
154
     */
155
    protected function setHeaderGZiped()
156
    {
157
        if (extension_loaded('zlib')) {
158
            return $this->setGZipedUnsafe('Header');
159
        }
160
        return '';
161
    }
162
163
    /**
164
     * Sets the no-cache header
165
     */
166
    protected function setHeaderNoCache($contentType = 'application/json')
167
    {
168
        header("Content-Type: " . $contentType . "; charset=utf-8");
169
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
170
        header("Cache-Control: no-store, no-cache, must-revalidate");
171
        header("Cache-Control: post-check=0, pre-check=0", false);
172
        header("Pragma: no-cache");
173
    }
174
175
    /**
176
     * Puts a given string into a specific short tag
177
     *
178
     * @param string $sTag
179
     * @param array $features
180
     * @return string
181
     */
182
    protected function setStringIntoShortTag($sTag, $features = null)
183
    {
184
        return '<' . $sTag . $this->buildAttributesForTag($features)
185
                . (isset($features['dont_close']) ? '' : '/') . '>';
186
    }
187
188
    /**
189
     * Puts a given string into a specific tag
190
     *
191
     * @param string $sString
192
     * @param string $sTag
193
     * @param array $features
194
     * @return string
195
     */
196
    protected function setStringIntoTag($sString, $sTag, $features = null)
197
    {
198
        return '<' . $sTag . $this->buildAttributesForTag($features) . '>' . $sString . '</' . $sTag . '>';
199
    }
200
201
    protected function setViewModernLinkAdd($identifier, $ftrs = null)
202
    {
203
        $btnText     = '<i class="fa fa-plus-square">&nbsp;</i>' . '&nbsp;' . $this->lclMsgCmn('i18n_AddNewRecord');
204
        $tagFeatures = [
205
            'href'  => $this->setViewModernLinkAddUrl($identifier, $ftrs),
206
            'style' => 'margin: 5px 0px 10px 0px; display: inline-block;',
207
        ];
208
        return $this->setStringIntoTag($btnText, 'a', $tagFeatures);
209
    }
210
211
    protected function setViewModernLinkAddInjectedArguments($ftrs = null)
212
    {
213
        $sArgmnts = '';
214
        if (isset($ftrs['injectAddArguments'])) {
215
            foreach ($ftrs['injectAddArguments'] as $key => $value) {
216
                $sArgmnts .= '&amp;' . $key . '=' . $value;
217
            }
218
        }
219
        return $sArgmnts;
220
    }
221
222
    protected function setViewModernLinkAddUrl($identifier, $ftrs = null)
223
    {
224
        $sArgmnts  = $this->setViewModernLinkAddInjectedArguments($ftrs);
225
        $this->initializeSprGlbAndSession();
226
        $addingUrl = $this->tCmnRequest->server->get('PHP_SELF') . '?view=add_' . $identifier . $sArgmnts;
227
        if (!isset($ftrs['NoAjax'])) {
228
            $addingUrl = 'javascript:loadAE(\'' . $addingUrl . '\');';
229
        }
230
        return $addingUrl;
231
    }
232
}
233