Completed
Push — master ( 9fc4e6...f0af25 )
by Daniel
02:54
created

DomCssAndJavascriptByDanielGP   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 135
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setCssContent() 0 16 4
A setCssFile() 0 13 3
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 (in_array($value, array_keys($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 $cssFile
0 ignored issues
show
Documentation introduced by
There is no parameter named $cssFile. Did you maybe mean $cssFileName?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
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
     * Returns javascript function to support Add or Edit through Ajax
89
     *
90
     * @return string
91
     */
92
    protected function setJavascriptAddEditByAjax($tabName = 'tabStandard')
93
    {
94
        return $this->setJavascriptContent(implode('', [
95
                    'function loadAE(action) {',
96
                    'document.getElementById("' . $tabName . '").tabber.tabShow(1);',
97
                    '$("#DynamicAddEditSpacer").load(action',
98
                    '+"&specialHook[]=noHeader"',
99
                    '+"&specialHook[]=noMenu"',
100
                    '+"&specialHook[]=noContainer"',
101
                    '+"&specialHook[]=noFooter"',
102
                    ');',
103
                    '}',
104
        ]));
105
    }
106
107
    /**
108
     * Returns javascript codes
109
     *
110
     * @param string $javascriptContent
111
     * @return string
112
     */
113
    protected function setJavascriptContent($javascriptContent)
114
    {
115
        return '<script type="text/javascript">' . $javascriptContent . '</script>';
116
    }
117
118
    /**
119
     * Builds up a confirmation dialog and return delection if Yes
120
     *
121
     * @return string
122
     */
123
    protected function setJavascriptDeleteWithConfirmation()
124
    {
125
        return $this->setJavascriptContent('function setQuest(a, b) { '
126
                        . 'c = a.indexOf("_"); switch(a.slice(0, c)) { '
127
                        . 'case \'delete\': '
128
                        . 'if (confirm(\'' . $this->lclMsgCmn('i18n_ActionDelete_ConfirmationQuestion') . '\')) { '
129
                        . 'window.location = document.location.protocol + "//" + '
130
                        . 'document.location.host + document.location.pathname + '
131
                        . '"?view=" + a + "&" + b; } break; } }');
132
    }
133
134
    /**
135
     * Returns javascript link to a given file
136
     *
137
     * @param string $jsFileName
138
     * @return string
139
     */
140
    protected function setJavascriptFile($jsFileName, $hostsWithoutCDNrq = null)
141
    {
142
        if (is_null($hostsWithoutCDNrq)) {
143
            $hostsWithoutCDNrq = [];
144
        }
145
        if (in_array($this->getClientRealIpAddress(), $hostsWithoutCDNrq)) {
146
            return '<script type="text/javascript" src="' . $jsFileName . '"></script>';
147
        }
148
        $patternFound = $this->setJavascriptFileCDN($jsFileName);
149
        return '<script type="text/javascript" src="' . $patternFound[1] . '"></script>' . $patternFound[2];
150
    }
151
152
    /**
153
     * Returns javascript codes from given file
154
     *
155
     * @param string $jsFileName
156
     * @return string
157
     */
158
    protected function setJavascriptFileContent($jsFileName)
159
    {
160
        return '<script type="text/javascript">' . file_get_contents($jsFileName, true) . '</script>';
161
    }
162
163
    protected function updateDivTitleName($rememberGroupVal, $groupCounter)
164
    {
165
        $jsContent = '$(document).ready(function() { $("#tab_'
166
                . $this->cleanStringForId($rememberGroupVal) . '").attr("title", "'
167
                . $rememberGroupVal . ' (' . $groupCounter . ')"); });';
168
        return $this->setJavascriptContent($jsContent);
169
    }
170
}
171