Completed
Push — master ( b21864...9a3b79 )
by Michael
01:24
created

MarqueeUtility::javascriptLinkConfirm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * ****************************************************************************
4
 * marquee - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard (http://www.herve-thouzard.com)
6
 *
7
 * You may not change or alter any portion of this comment or credits
8
 * of supporting developers from this source code or any supporting source code
9
 * which is considered copyrighted (c) material of the original comment or credit authors.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 *
14
 * @copyright         Hervé Thouzard (http://www.herve-thouzard.com)
15
 * @license           http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @package           marquee
17
 * @author            Hervé Thouzard (http://www.herve-thouzard.com)
18
 *
19
 * Version :
20
 * ****************************************************************************
21
 */
22
23
/**
24
 * A set of useful and common functions
25
 *
26
 * @package       references
27
 * @author        Hervé Thouzard (http://www.herve-thouzard.com)
28
 * @copyright (c) Hervé Thouzard
29
 *
30
 * Note: You should be able to use it without the need to instanciate it.
31
 *
32
 */
33
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
35
class MarqueeUtility
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
36
{
37
    const MODULE_NAME = 'marquee';
38
39
    /**
40
     * Access the only instance of this class
41
     *
42
     * @return object
43
     *
44
     * @static
45
     * @staticvar   object
46
     */
47
    public function getInstance()
48
    {
49
        static $instance;
50
        if (null === $instance) {
51
            $instance = new static();
52
        }
53
54
        return $instance;
55
    }
56
57
    /**
58
     * Returns a module's option (with cache)
59
     *
60
     * @param string  $option    module option's name
61
     * @param boolean $withCache Do we have to use some cache ?
62
     *
63
     * @return mixed option's value
64
     */
65
    public function getModuleOption($option, $withCache = true)
66
    {
67
        global $xoopsModuleConfig, $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
68
        $repmodule = self::MODULE_NAME;
69
        static $options = array();
70
        if (is_array($options) && array_key_exists($option, $options) && $withCache) {
71
            return $options[$option];
72
        }
73
74
        $retval = false;
75 View Code Duplication
        if (null !== $xoopsModuleConfig
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
76
            && (is_object($xoopsModule) && $xoopsModule->getVar('dirname') == $repmodule
77
                && $xoopsModule->getVar('isactive'))) {
78
            if (isset($xoopsModuleConfig[$option])) {
79
                $retval = $xoopsModuleConfig[$option];
80
            }
81
        } else {
82
            /** @var XoopsModuleHandler $moduleHandler */
83
            $moduleHandler = xoops_getHandler('module');
84
            $module        = $moduleHandler->getByDirname($repmodule);
85
            $configHandler = xoops_getHandler('config');
86
            if ($module) {
87
                $moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
88
                if (isset($moduleConfig[$option])) {
89
                    $retval = $moduleConfig[$option];
90
                }
91
            }
92
        }
93
        $options[$option] = $retval;
94
95
        return $retval;
96
    }
97
98
    /**
99
     * Is Xoops 2.3.x ?
100
     *
101
     * @return boolean need to say it ?
102
     */
103
    //    function isX23()
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
    //    {
105
    //        $x23 = false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106
    //        $xv  = str_replace('XOOPS ', '', XOOPS_VERSION);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
107
    //        if ((int)(substr($xv, 2, 1)) >= 3) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
108
    //            $x23 = true;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
109
    //        }
110
    //
111
    //        return $x23;
112
    //    }
113
114
    /**
115
     * Retreive an editor according to the module's option "form_options"
116
     *
117
     * @param string $caption Caption to give to the editor
118
     * @param string $name    Editor's name
119
     * @param string $value   Editor's value
120
     * @param string $width   Editor's width
121
     * @param string $height  Editor's height
122
     * @param string $supplemental
123
     *
124
     * @return object The editor to use
125
     */
126
    public static function getWysiwygForm(
127
        $caption,
128
        $name,
129
        $value = '',
130
        $width = '100%',
0 ignored issues
show
Unused Code introduced by
The parameter $width is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
        $height = '400px',
0 ignored issues
show
Unused Code introduced by
The parameter $height is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
        $supplemental = ''
0 ignored issues
show
Unused Code introduced by
The parameter $supplemental is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
    ) {
134
        global $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
135
        if (class_exists('XoopsFormEditor')) {
136
            $options['name']   = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
137
            $options['value']  = $value;
138
            $options['rows']   = 35;
139
            $options['cols']   = '100%';
140
            $options['width']  = '100%';
141
            $options['height'] = '400px';
142
            $editor            = new XoopsFormEditor($caption, $xoopsModuleConfig['form_options'], $options, $nohtml = false, $onfailure = 'textarea');
143
        } else {
144
            $editor = new XoopsFormDhtmlTextArea($caption, $name, $value, '100%', '100%');
145
        }
146
147
        return $editor;
148
    }
149
150
    /**
151
     * Create (in a link) a javascript confirmation's box
152
     *
153
     * @param string  $message Message to display
154
     * @param boolean $form    Is this a confirmation for a form ?
155
     *
156
     * @return string the javascript code to insert in the link (or in the form)
157
     */
158
    public function javascriptLinkConfirm($message, $form = false)
159
    {
160
        if (!$form) {
161
            return "onclick=\"javascript:return confirm('" . str_replace("'", ' ', $message) . "')\"";
162
        } else {
163
            return "onSubmit=\"javascript:return confirm('" . str_replace("'", ' ', $message) . "')\"";
164
        }
165
    }
166
167
    /**
168
     * Redirect user with a message
169
     *
170
     * @param string $message message to display
171
     * @param string $url     The place where to go
172
     * @param        integer  timeout Time to wait before to redirect
173
     */
174
    public function redirect($message = '', $url = 'index.php', $time = 2)
175
    {
176
        redirect_header($url, $time, $message);
177
    }
178
179
    /**
180
     * Internal function used to get the handler of the current module
181
     *
182
     * @return object The module
183
     */
184
    protected static function getModule()
185
    {
186
        static $mymodule;
187
        if (null === $mymodule) {
188
            global $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
189
            if (null !== $xoopsModule && is_object($xoopsModule)
190
                && $xoopsModule->getVar('dirname') == REFERENCES_DIRNAME) {
191
                $mymodule =& $xoopsModule;
192
            } else {
193
                $hModule  = xoops_getHandler('module');
194
                $mymodule = $hModule->getByDirname(REFERENCES_DIRNAME);
195
            }
196
        }
197
198
        return $mymodule;
199
    }
200
201
    /**
202
     * Returns the module's name (as defined by the user in the module manager) with cache
203
     *
204
     * @return string Module's name
205
     */
206
    public function getModuleName()
207
    {
208
        static $moduleName;
209
        if (!isset($moduleName)) {
210
            $mymodule   = self::getModule();
211
            $moduleName = $mymodule->getVar('name');
212
        }
213
214
        return $moduleName;
215
    }
216
217
    /**
218
     * This function indicates if the current Xoops version needs to add asterisks to required fields in forms
219
     *
220
     * @return boolean Yes = we need to add them, false = no
221
     */
222
    public static function needsAsterisk()
223
    {
224
        if (self::isX23()) {
0 ignored issues
show
Bug introduced by
The method isX23() does not seem to exist on object<MarqueeUtility>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
225
            return false;
226
        }
227
        if (strpos(strtolower(XOOPS_VERSION), 'impresscms') !== false) {
228
            return false;
229
        }
230
        if (strpos(strtolower(XOOPS_VERSION), 'legacy') === false) {
231
            $xv = xoops_trim(str_replace('XOOPS ', '', XOOPS_VERSION));
232
            if ((int)substr($xv, 4, 2) >= 17) {
233
                return false;
234
            }
235
        }
236
237
        return true;
238
    }
239
240
    /**
241
     * Mark the mandatory fields of a form with a star
242
     *
243
     * @param object $sform The form to modify
244
     *
245
     * @internal param string $caracter The character to use to mark fields
246
     * @return object The modified form
247
     */
248
    public function &formMarkRequiredFields(&$sform)
249
    {
250
        if (self::needsAsterisk()) {
251
            $required = array();
252
            foreach ($sform->getRequired() as $item) {
253
                $required[] = $item->_name;
254
            }
255
            $elements = array();
256
            $elements =& $sform->getElements();
257
            $cnt      = count($elements);
258
            for ($i = 0; $i < $cnt; ++$i) {
259
                if (is_object($elements[$i]) && in_array($elements[$i]->_name, $required)) {
260
                    $elements[$i]->_caption .= ' *';
261
                }
262
            }
263
        }
264
265
        return $sform;
266
    }
267
}
268