Completed
Push — master ( 4076e7...56163a )
by Daniel
02:39
created

updateDivTitleName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
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 DomCssAndJavascriptByDanielGP
37
{
38
39
    use \danielgp\browser_agent_info\BrowserAgentInfosByDanielGP,
40
        DomBasicComponentsByDanielGP,
41
        DomCssAndJavascriptByDanielGPwithCDN;
42
43
    /**
44
     * Creates a mask to differentiate between Mandatory and Optional fields
45
     *
46
     * @param array $details
47
     * @return string
48
     */
49
    protected function getFieldCompletionType($details)
50
    {
51
        $inputFeatures = ['display' => '***', 'ftrs' => ['title' => 'Mandatory', 'class' => 'inputMandatory']];
52
        if ($details['IS_NULLABLE'] == 'YES') {
53
            $inputFeatures = ['display' => '~', 'ftrs' => ['title' => 'Optional', 'class' => 'inputOptional']];
54
        }
55
        return $this->setStringIntoTag($inputFeatures['display'], 'span', $inputFeatures['ftrs']);
56
    }
57
58
    /**
59
     * Returns a Text field 2 use in a form
60
     *
61
     * @param string $fieldType
62
     * @param array $value
63
     * @param array $iar
64
     * @return string
65
     */
66
    protected function getFieldOutputTextLarge($fieldType, $value, $iar = [])
67
    {
68
        if (!in_array($fieldType, ['blob', 'text'])) {
69
            return '';
70
        }
71
        $inAdtnl = [
72
            'name' => $value['COLUMN_NAME'],
73
            'id'   => $value['COLUMN_NAME'],
74
            'rows' => 4,
75
            'cols' => 55,
76
        ];
77
        if ($iar !== []) {
78
            $inAdtnl = array_merge($inAdtnl, $iar);
79
        }
80
        return $this->setStringIntoTag($this->getFieldValue($value), 'textarea', $inAdtnl);
0 ignored issues
show
Bug introduced by
It seems like getFieldValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
81
    }
82
83
    /**
84
     * Returns css codes
85
     *
86
     * @param string $cssContent
87
     * @param array $optionalFlags
88
     * @return string
89
     */
90
    protected function setCssContent($cssContent, $optionalFlags = null)
91
    {
92
        $attr = [];
93
        if (is_null($optionalFlags)) {
94
            $attr['media'] = 'all';
95
        } else {
96
            $knownAttributes = ['media'];
97
            foreach ($knownAttributes as $value) {
98
                if (array_key_exists($value, $optionalFlags)) {
99
                    $attr[$value] = $optionalFlags[$value];
100
                }
101
            }
102
        }
103
        return '<style type="text/css" media="' . $attr['media'] . '">'
104
                . $cssContent . '</style>';
105
    }
106
107
    /**
108
     * Returns css link to a given file
109
     *
110
     * @param string $cssFileName
111
     * @return string
112
     */
113
    protected function setCssFile($cssFileName, $hostsWithoutCDNrq = null)
114
    {
115
        if (is_null($hostsWithoutCDNrq)) {
116
            $hostsWithoutCDNrq = [];
117
        }
118
        if (in_array($this->getClientRealIpAddress(), $hostsWithoutCDNrq)) {
119
            return '<link rel="stylesheet" type="text/css" href="'
120
                    . filter_var($cssFileName, FILTER_SANITIZE_STRING) . '" />';
121
        }
122
        $patternFound = $this->setCssFileCDN($cssFileName);
123
        return '<link rel="stylesheet" type="text/css" href="'
124
                . filter_var($patternFound[1], FILTER_SANITIZE_STRING) . '" />';
125
    }
126
127
    /**
128
     * Builds javascript to avoid multiple form submission
129
     *
130
     * @param string $frmId
131
     * @return string
132
     */
133
    protected function setFormJavascriptFinal($frmId)
134
    {
135
        $cnt = implode(PHP_EOL, [
136
            '$(document).ready(function(){',
137
            '$("form#' . $frmId . '").submit(function(){',
138
            '$("form#' . $frmId . ' input[type=checkbox]").attr("readonly", true);',
139
            '$("form#' . $frmId . ' input[type=password]").attr("readonly", true);',
140
            '$("form#' . $frmId . ' input[type=radio]").attr("readonly", true);',
141
            '$("form#' . $frmId . ' input[type=text]").attr("readonly", true);',
142
            '$("form#' . $frmId . ' textarea").attr("readonly", true);',
143
            '$("form#' . $frmId . ' select").attr("readonly", true);',
144
            '$("input[type=submit]").attr("disabled", "disabled");',
145
            '$("input[type=submit]").attr("value", "' . $this->lclMsgCmn('i18n_Form_ButtonSaving') . '");',
146
            '});',
147
            '});',
148
        ]);
149
        return $this->setJavascriptContent(PHP_EOL . $cnt . PHP_EOL);
150
    }
151
152
    /**
153
     * Returns javascript function to support Add or Edit through Ajax
154
     *
155
     * @return string
156
     */
157
    protected function setJavascriptAddEditByAjax($tabName = 'tabStandard')
158
    {
159
        return $this->setJavascriptContent(implode('', [
160
                    'function loadAE(action) {',
161
                    'document.getElementById("' . $tabName . '").tabber.tabShow(1);',
162
                    '$("#DynamicAddEditSpacer").load(action',
163
                    '+"&specialHook[]=noHeader"',
164
                    '+"&specialHook[]=noMenu"',
165
                    '+"&specialHook[]=noContainer"',
166
                    '+"&specialHook[]=noFooter"',
167
                    ');',
168
                    '}',
169
        ]));
170
    }
171
172
    /**
173
     * Returns javascript codes
174
     *
175
     * @param string $javascriptContent
176
     * @return string
177
     */
178
    protected function setJavascriptContent($javascriptContent)
179
    {
180
        return '<script type="text/javascript">' . $javascriptContent . '</script>';
181
    }
182
183
    /**
184
     * Builds up a confirmation dialog and return delection if Yes
185
     *
186
     * @return string
187
     */
188
    protected function setJavascriptDeleteWithConfirmation()
189
    {
190
        return $this->setJavascriptContent('function setQuest(a, b) { '
191
                        . 'c = a.indexOf("_"); switch(a.slice(0, c)) { '
192
                        . 'case \'delete\': '
193
                        . 'if (confirm(\'' . $this->lclMsgCmn('i18n_ActionDelete_ConfirmationQuestion') . '\')) { '
194
                        . 'window.location = document.location.protocol + "//" + '
195
                        . 'document.location.host + document.location.pathname + '
196
                        . '"?view=" + a + "&" + b; } break; } }');
197
    }
198
199
    /**
200
     * Returns javascript link to a given file
201
     *
202
     * @param string $jsFileName
203
     * @return string
204
     */
205
    protected function setJavascriptFile($jsFileName, $hostsWithoutCDNrq = null)
206
    {
207
        if (is_null($hostsWithoutCDNrq)) {
208
            $hostsWithoutCDNrq = [];
209
        }
210
        if (in_array($this->getClientRealIpAddress(), $hostsWithoutCDNrq)) {
211
            return '<script type="text/javascript" src="' . $jsFileName . '"></script>';
212
        }
213
        $patternFound = $this->setJavascriptFileCDN($jsFileName);
214
        return '<script type="text/javascript" src="' . $patternFound[1] . '"></script>' . $patternFound[2];
215
    }
216
217
    /**
218
     * Returns javascript codes from given file
219
     *
220
     * @param string $jsFileName
221
     * @return string
222
     */
223
    protected function setJavascriptFileContent($jsFileName)
224
    {
225
        return '<script type="text/javascript">' . file_get_contents($jsFileName, true) . '</script>';
226
    }
227
228
    protected function updateDivTitleName($rememberGroupVal, $groupCounter)
229
    {
230
        $jsContent = '$(document).ready(function() { $("#tab_'
231
                . $this->cleanStringForId($rememberGroupVal) . '").attr("title", "'
232
                . $rememberGroupVal . ' (' . $groupCounter . ')"); });';
233
        return $this->setJavascriptContent($jsContent);
234
    }
235
}
236