Completed
Push — master ( 8b9728...8dad3b )
by Michael
10s
created

XoopspollFormDateTimePicker::render()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 34
nc 6
nop 0
dl 0
loc 55
rs 9.078
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Xoopspoll form timepicker
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright ::   &copy; {@link http://xoops.org/ XOOPS Project}
13
 * @license   ::     {@link http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU GPL 2}
14
 * @package   ::     xoopspoll
15
 * @subpackage::  class
16
 * @since     ::       1.40
17
 * @author    ::      TXMod Xoops (aka timgno) {@link http://www.txmodxoops.org/ TXMod Xoops}
18
 * @author    ::      zyspec <[email protected]>
19
 * @credits::     {@link http://www.trentrichardson.com Trent Richardson}
20
 */
21
22
// 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...
23
24
/**
25
 * Creates a text field with jquery ui calendar & time select popup
26
 */
27
class XoopspollFormDateTimePicker extends XoopsFormText
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...
28
{
29
    /**
30
     *
31
     * Contains the maximum field size
32
     */
33
    const MAXSIZE = 25;
34
35
    /**
36
     *
37
     * Constructor to build FormDateTimePicker object
38
     * @param mixed $caption HTML description to display for the element
39
     * @param mixed $name    HTML element name (ie. name='$name')
40
     * @param mixed $size    size of field to display
41
     * @param mixed $value   timestamp of date/time to show
42
     */
43
    public function __construct($caption, $name, $size, $value)
44
    {
45
        $value = (!is_numeric($value) || (0 === (int)$value)) ? time($value) : (int)$value;
46
        $size  = (int)$size;
47
        $size  = ($size > 0 && $size <= self::MAXSIZE) ? $size : self::MAXSIZE;
48
        parent::__construct($caption, $name, $size, self::MAXSIZE, $value);
49
    }
50
51
    /**
52
     *
53
     * Generate the HTML <input> to display the date/time field
54
     * @return string HTML code used to display in a form
55
     */
56
    public function render()
0 ignored issues
show
Coding Style introduced by
render uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
57
    {
58
        static $included = false;
59
60
        $ele_name  = $this->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
61
        $ele_value = $this->getValue(true);
62
        //        if (is_string($ele_value)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
63
        if (!is_numeric($ele_value)) {
64
            $display_value = $ele_value;
65
            $ele_value     = time();
0 ignored issues
show
Unused Code introduced by
$ele_value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
        } else {
67
            //            $display_value = '';
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...
68
            //            $display_value = formatTimestamp($ele_value, 'm');
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
69
            $display_value = ucfirst(date(_MEDIUMDATESTRING, $ele_value));
70
        }
71
72
        if (is_object($GLOBALS['xoTheme'])) {
73
            $modHandler    = xoops_getHandler('module');
74
            $sys_module     = $modHandler->getByDirname('system');
75
            $configHandler = xoops_getHandler('config');
76
            $moduleConfig   = $configHandler->getConfigsByCat(0, $sys_module->getVar('mid'));
77
            $jq_theme_dir   = $moduleConfig['jquery_theme'];
78
79
            $GLOBALS['xoTheme']->addStylesheet($GLOBALS['xoops']->url("modules/system/css/ui/{$jq_theme_dir}/ui.all.css"));
80
            $GLOBALS['xoTheme']->addScript('browse.php?Frameworks/jquery/jquery.js');
81
            $GLOBALS['xoTheme']->addScript('browse.php?Frameworks/jquery/plugins/jquery.ui.js');
82
            $GLOBALS['xoTheme']->addScript('browse.php?modules/xoopspoll/assets/js/jquery-ui-timepicker-addon.js');
83
            $GLOBALS['xoTheme']->addScript('browse.php?modules/xoopspoll/assets/js/jquery-ui-sliderAccess.js');
84
            $GLOBALS['xoTheme']->addStylesheet($GLOBALS['xoops']->url('modules/xoopspoll/assets/css/datetimepicker.css'));
85
86
            if (!$included) {
87
                $included = true;
88
                xoops_loadLanguage('admin', 'xoopspoll');
89
                // setup regional date variables
90
                $reg_values = "closeText: '" . _AM_XOOPSPOLL_DTP_CLOSETEXT . "'," . "prevText: '" . _AM_XOOPSPOLL_DTP_PREVTEXT . "'," . "nextText: '" . _AM_XOOPSPOLL_DTP_NEXTTEXT . "'," . "currentText: '" . _AM_XOOPSPOLL_DTP_CURRENTTEXT . "',"
91
                              . 'monthNames: [' . _AM_XOOPSPOLL_DTP_MONTHNAMES . '],' . 'monthNamesShort: [' . _AM_XOOPSPOLL_DTP_MONTHNAMESSHORT . '],' . 'dayNames: [' . _AM_XOOPSPOLL_DTP_DAYNAMES . '],' . 'dayNamesShort: ['
92
                              . _AM_XOOPSPOLL_DTP_DAYNAMESSHORT . '],' . 'dayNamesMin: [' . _AM_XOOPSPOLL_DTP_DAYNAMESMIN . '],' . "weekHeader: '" . _AM_XOOPSPOLL_DTP_WEEKHEADER . "'," . "dateFormat: '" . _AM_XOOPSPOLL_DTP_DATEFORMAT . "',"
93
                              . "firstDay: '" . _AM_XOOPSPOLL_DTP_FIRSTDAY . "'," . 'isRTL: ' . _AM_XOOPSPOLL_DTP_ISRTL . ',' . 'showMonthAfterYear: ' . _AM_XOOPSPOLL_DTP_SHOWMONTHAFTERYEAR . ',' . "yearSuffix: '" . _AM_XOOPSPOLL_DTP_YEARSUFFIX
94
                              . "',";
95
                // set regional time variables
96
                $reg_values .= "timeOnlyTitle: '" . _AM_XOOPSPOLL_DTP_TIMEONLYTITLE . "'," . "timeText: '" . _AM_XOOPSPOLL_DTP_TIMETEXT . "'," . "hourText: '" . _AM_XOOPSPOLL_DTP_HOURTEXT . "'," . "minuteText: '" . _AM_XOOPSPOLL_DTP_MINUTETEXT . "',"
97
                               . "secondText: '" . _AM_XOOPSPOLL_DTP_SECONDTEXT . "'," . "millisecText: '" . _AM_XOOPSPOLL_DTP_MILLISECTEXT . "'," . "timeFormat: '" . _AM_XOOPSPOLL_DTP_TIMEFORMAT . "'," . 'ampm: false,' . 'stepMinute: 5';
98
99
                $GLOBALS['xoTheme']->addScript('', '', '
100
                  $(function() {
101
                      $( ".datetimepicker" ).datetimepicker({
102
                          ' . $reg_values . '
103
                      });
104
                  });
105
        ');
106
            }
107
        }
108
109
        return "<input type='text' name='{$ele_name}' id='{$ele_name}' class='datetimepicker' size='" . $this->getSize() . "' maxlength='" . $this->getMaxlength() . "' value='{$display_value}'" . $this->getExtra() . ' />';
110
    }
111
}
112