Completed
Push — master ( 2eb9e0...6fec5f )
by Daniel
02:30
created

DomCssAndJavascriptByDanielGP   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 2
cbo 3
dl 0
loc 160
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setCssContent() 0 16 4
A setCssFile() 0 13 3
A setFormJavascriptFinal() 0 18 1
A setJavascriptAddEditByAjax() 0 14 1
A setJavascriptContent() 0 4 1
A setJavascriptDeleteWithConfirmation() 0 10 1
A setJavascriptFile() 0 11 3
A setJavascriptFileContent() 0 4 1
A updateDivTitleName() 0 7 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 DomCssAndJavascriptByDanielGP
37
{
38
39
    use \danielgp\browser_agent_info\BrowserAgentInfosByDanielGP,
40
        DomBasicComponentsByDanielGP,
41
        DomCssAndJavascriptByDanielGPwithCDN;
42
43
    /**
44
     * Returns css codes
45
     *
46
     * @param string $cssContent
47
     * @param array $optionalFlags
48
     * @return string
49
     */
50
    protected function setCssContent($cssContent, $optionalFlags = null)
51
    {
52
        $attr = [];
53
        if (is_null($optionalFlags)) {
54
            $attr['media'] = 'all';
55
        } else {
56
            $knownAttributes = ['media'];
57
            foreach ($knownAttributes as $value) {
58
                if (array_key_exists($value, $optionalFlags)) {
59
                    $attr[$value] = $optionalFlags[$value];
60
                }
61
            }
62
        }
63
        return '<style type="text/css" media="' . $attr['media'] . '">'
64
                . $cssContent . '</style>';
65
    }
66
67
    /**
68
     * Returns css link to a given file
69
     *
70
     * @param string $cssFileName
71
     * @return string
72
     */
73
    protected function setCssFile($cssFileName, $hostsWithoutCDNrq = null)
74
    {
75
        if (is_null($hostsWithoutCDNrq)) {
76
            $hostsWithoutCDNrq = [];
77
        }
78
        if (in_array($this->getClientRealIpAddress(), $hostsWithoutCDNrq)) {
79
            return '<link rel="stylesheet" type="text/css" href="'
80
                    . filter_var($cssFileName, FILTER_SANITIZE_STRING) . '" />';
81
        }
82
        $patternFound = $this->setCssFileCDN($cssFileName);
83
        return '<link rel="stylesheet" type="text/css" href="'
84
                . filter_var($patternFound[1], FILTER_SANITIZE_STRING) . '" />';
85
    }
86
87
    /**
88
     * Builds javascript to avoid multiple form submission
89
     *
90
     * @param string $frmId
91
     * @return string
92
     */
93
    protected function setFormJavascriptFinal($frmId)
94
    {
95
        $cnt = implode(PHP_EOL, [
96
            '$(document).ready(function(){',
97
            '$("form#' . $frmId . '").submit(function(){',
98
            '$("form#' . $frmId . ' input[type=checkbox]").attr("readonly", true);',
99
            '$("form#' . $frmId . ' input[type=password]").attr("readonly", true);',
100
            '$("form#' . $frmId . ' input[type=radio]").attr("readonly", true);',
101
            '$("form#' . $frmId . ' input[type=text]").attr("readonly", true);',
102
            '$("form#' . $frmId . ' textarea").attr("readonly", true);',
103
            '$("form#' . $frmId . ' select").attr("readonly", true);',
104
            '$("input[type=submit]").attr("disabled", "disabled");',
105
            '$("input[type=submit]").attr("value", "' . $this->lclMsgCmn('i18n_Form_ButtonSaving') . '");',
106
            '});',
107
            '});',
108
        ]);
109
        return $this->setJavascriptContent(PHP_EOL . $cnt . PHP_EOL);
110
    }
111
112
    /**
113
     * Returns javascript function to support Add or Edit through Ajax
114
     *
115
     * @return string
116
     */
117
    protected function setJavascriptAddEditByAjax($tabName = 'tabStandard')
118
    {
119
        return $this->setJavascriptContent(implode('', [
120
                    'function loadAE(action) {',
121
                    'document.getElementById("' . $tabName . '").tabber.tabShow(1);',
122
                    '$("#DynamicAddEditSpacer").load(action',
123
                    '+"&specialHook[]=noHeader"',
124
                    '+"&specialHook[]=noMenu"',
125
                    '+"&specialHook[]=noContainer"',
126
                    '+"&specialHook[]=noFooter"',
127
                    ');',
128
                    '}',
129
        ]));
130
    }
131
132
    /**
133
     * Returns javascript codes
134
     *
135
     * @param string $javascriptContent
136
     * @return string
137
     */
138
    protected function setJavascriptContent($javascriptContent)
139
    {
140
        return '<script type="text/javascript">' . $javascriptContent . '</script>';
141
    }
142
143
    /**
144
     * Builds up a confirmation dialog and return delection if Yes
145
     *
146
     * @return string
147
     */
148
    protected function setJavascriptDeleteWithConfirmation()
149
    {
150
        return $this->setJavascriptContent('function setQuest(a, b) { '
151
                        . 'c = a.indexOf("_"); switch(a.slice(0, c)) { '
152
                        . 'case \'delete\': '
153
                        . 'if (confirm(\'' . $this->lclMsgCmn('i18n_ActionDelete_ConfirmationQuestion') . '\')) { '
154
                        . 'window.location = document.location.protocol + "//" + '
155
                        . 'document.location.host + document.location.pathname + '
156
                        . '"?view=" + a + "&" + b; } break; } }');
157
    }
158
159
    /**
160
     * Returns javascript link to a given file
161
     *
162
     * @param string $jsFileName
163
     * @return string
164
     */
165
    protected function setJavascriptFile($jsFileName, $hostsWithoutCDNrq = null)
166
    {
167
        if (is_null($hostsWithoutCDNrq)) {
168
            $hostsWithoutCDNrq = [];
169
        }
170
        if (in_array($this->getClientRealIpAddress(), $hostsWithoutCDNrq)) {
171
            return '<script type="text/javascript" src="' . $jsFileName . '"></script>';
172
        }
173
        $patternFound = $this->setJavascriptFileCDN($jsFileName);
174
        return '<script type="text/javascript" src="' . $patternFound[1] . '"></script>' . $patternFound[2];
175
    }
176
177
    /**
178
     * Returns javascript codes from given file
179
     *
180
     * @param string $jsFileName
181
     * @return string
182
     */
183
    protected function setJavascriptFileContent($jsFileName)
184
    {
185
        return '<script type="text/javascript">' . file_get_contents($jsFileName, true) . '</script>';
186
    }
187
188
    protected function updateDivTitleName($rememberGroupVal, $groupCounter)
189
    {
190
        $jsContent = '$(document).ready(function() { $("#tab_'
191
                . $this->cleanStringForId($rememberGroupVal) . '").attr("title", "'
192
                . $rememberGroupVal . ' (' . $groupCounter . ')"); });';
193
        return $this->setJavascriptContent($jsContent);
194
    }
195
}
196