displayBox()   F
last analyzed

Complexity

Conditions 22
Paths > 20000

Size

Total Lines 153
Code Lines 104

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 104
nc 38912
nop 3
dl 0
loc 153
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
/* Copyright (C) phpBSM
4
 * Copyright (C) 2005-2010 Laurent Destailleur  <[email protected]>
5
 * Copyright (C) 2005-2007 Regis Houssin        <[email protected]>
6
 * Copyright (C) 2014	   Juanjo Menent        <[email protected]>
7
 * Copyright (C) 2024		MDW							<[email protected]>
8
 *
9
 * This file is a modified version of datepicker.php from phpBSM to fix some
10
 * bugs, to add new features and to dramatically increase speed.
11
 * Copyright (C) 2024       Rafael San José             <[email protected]>
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 3 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25
 */
26
27
use Dolibarr\Lib\ViewMain;
28
29
/**
30
 *       \file       htdocs/core/datepicker.php
31
 *       \brief      File to manage popup date selector
32
 */
33
34
if (!defined('NOREQUIREUSER')) {
35
    define('NOREQUIREUSER', '1'); // disabled
36
}
37
//if (! defined('NOREQUIREDB'))   define('NOREQUIREDB','1');    // Not disabled cause need to load personalized language
38
if (!defined('NOREQUIRESOC')) {
39
    define('NOREQUIRESOC', '1');
40
}
41
//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');  // Not disabled cause need to do translations
42
if (!defined('NOCSRFCHECK')) {
43
    define('NOCSRFCHECK', 1);
44
}
45
if (!defined('NOTOKENRENEWAL')) {
46
    define('NOTOKENRENEWAL', 1);
47
}
48
if (!defined('NOLOGIN')) {
49
    define('NOLOGIN', 1); // disabled
50
}
51
if (!defined('NOREQUIREMENU')) {
52
    define('NOREQUIREMENU', 1);
53
}
54
if (!defined('NOREQUIREHTML')) {
55
    define('NOREQUIREHTML', 1);
56
}
57
58
require_once constant('DOL_DOCUMENT_ROOT') . '/main.inc.php';
59
require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/date.lib.php';
60
61
if (GETPOST('lang', 'aZ09')) {
62
    $langs->setDefaultLang(GETPOST('lang', 'aZ09')); // If language was forced on URL by the main.inc.php
63
}
64
65
// Load translation files required by the page
66
$langs->loadLangs(array("main", "agenda"));
67
68
$right = ($langs->trans("DIRECTION") == 'rtl' ? 'left' : 'right');
69
$left = ($langs->trans("DIRECTION") == 'rtl' ? 'right' : 'left');
70
71
//var_dump($langs->defaultlang);
72
//var_dump($conf->format_date_short_java);
73
//var_dump($langs->trans("FormatDateShortJava"));
74
75
76
// URL http://mydolibarr/core/datepicker.php?mode=test&m=10&y=2038 can be used for tests
77
print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">' . "\n";
78
print '<html>' . "\n";
79
print '<head>' . "\n";
80
if (GETPOST('mode') && GETPOST('mode') == 'test') {
81
    print '<script nonce="' . getNonce() . '" type="text/javascript" src="' . constant('BASE_URL') . '/core/js/lib_head.js.php"></script>' . "\n";
82
} else {
83
    print '<title>' . $langs->trans("Calendar") . '</title>';
84
}
85
86
// Define tradMonths javascript array (we define this in datapicker AND in parent page to avoid errors with IE8)
87
$tradTemp = array(
88
    $langs->trans("January"),
89
    $langs->trans("February"),
90
    $langs->trans("March"),
91
    $langs->trans("April"),
92
    $langs->trans("May"),
93
    $langs->trans("June"),
94
    $langs->trans("July"),
95
    $langs->trans("August"),
96
    $langs->trans("September"),
97
    $langs->trans("October"),
98
    $langs->trans("November"),
99
    $langs->trans("December")
100
);
101
print '<script nonce="' . getNonce() . '" type="text/javascript">';
102
print 'var tradMonths = [';
103
foreach ($tradTemp as $val) {
104
    print '"' . addslashes($val) . '",';
105
}
106
print '""];';
107
print '</script>' . "\n";
108
print '</head>' . "\n";
109
110
print '<body>' . "\n";
111
112
113
$qualified = true;
114
115
// TODO Replace with GETPOST
116
if (!isset($_GET["sd"])) {
117
    $_GET["sd"] = "00000000";
118
}
119
if (!isset($_GET["m"]) || !isset($_GET["y"])) {
120
    $qualified = false;
121
}
122
if (isset($_GET["m"]) && isset($_GET["y"])) {
123
    if ($_GET["m"] < 1 || $_GET["m"] > 12) {
124
        $qualified = false;
125
    }
126
    if ($_GET["y"] < 0 || $_GET["y"] > 9999) {
127
        $qualified = false;
128
    }
129
}
130
131
// If parameters provided, we show calendar
132
if ($qualified) {
133
    displayBox(GETPOSTINT("sd"), GETPOSTINT("m"), GETPOSTINT("y"));
134
} else {
135
    dol_print_error(null, 'ErrorBadParameters');
136
}
137
138
139
print '</body></html>' . "\n";
140
141
/**
142
 *  Convert date to timestamp
143
 *
144
 * @param string $mysqldate Date YYYMMDD
145
 * @return integer                 Timestamp
146
 */
147
function xyzToUnixTimestamp($mysqldate)
148
{
149
    $year = substr($mysqldate, 0, 4);
150
    $month = substr($mysqldate, 4, 2);
151
    $day = substr($mysqldate, 6, 2);
152
    $unixtimestamp = dol_mktime(12, 0, 0, $month, $day, $year);
153
    return $unixtimestamp;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $unixtimestamp also could return the type string which is incompatible with the documented return type integer.
Loading history...
154
}
155
156
/**
157
 * Show box
158
 *
159
 * @param string $selectedDate Date YYYMMDD
160
 * @param int $month Month
161
 * @param int $year Year
162
 * @return  void
163
 */
164
function displayBox($selectedDate, $month, $year)
165
{
166
    global $langs, $conf;
167
168
    //print "$selectedDate,$month,$year";
169
    $thedate = dol_mktime(12, 0, 0, $month, 1, $year);
170
    //print "thedate=$thedate";
171
    $today = dol_now();
172
    $todayArray = dol_getdate($today);
173
    if ($selectedDate != "00000000") {
174
        $selDate = xyzToUnixTimestamp($selectedDate);
175
        $xyz = dol_print_date($selDate, "%Y%m%d");
176
    } else {
177
        $selDate = 0;
178
        $xyz = 0;
179
    } ?>
180
    <table class="dp">
181
        <tr>
182
            <td colspan="6" class="dpHead"><?php
183
                $selectMonth = dol_print_date($thedate, '%m');
184
                $selectYear = dol_print_date($thedate, '%Y');
185
                echo $langs->trans("Month" . $selectMonth) . ", " . $selectYear; ?></td>
186
            <td class="dpHead">
187
                <button type="button" class="dpInvisibleButtons" id="DPCancel"
188
                        onClick="closeDPBox();">X
189
                </button>
190
            </td>
191
        </tr>
192
        <tr>
193
            <td class="dpButtons"
194
                onClick="loadMonth('<?php echo constant('BASE_URL') . '/core/' ?>','<?php echo $month ?>','<?php echo $year - 1 ?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">
195
                &lt;&lt;
196
            </td>
197
            <td class="dpButtons"
198
                onClick="loadMonth('<?php echo constant('BASE_URL') . '/core/' ?>','<?php if ($month == 1) {
199
                    echo "12";
200
                } else {
201
                    echo $month - 1;
202
                } ?>','<?php if ($month == 1) {
203
                    echo $year - 1;
204
                } else {
205
                    echo $year;
206
                } ?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">&lt;
207
            </td>
208
            <td colspan="3" class="dpButtons"
209
                onClick="loadMonth('<?php echo constant('BASE_URL') . '/core/' ?>','<?php echo (int)dol_print_date($today, '%m') ?>','<?php echo $todayArray["year"] ?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')"><?php echo '-' ?></td>
210
            <td class="dpButtons"
211
                onClick="loadMonth('<?php echo constant('BASE_URL') . '/core/' ?>','<?php if ($month == 12) {
212
                    echo "1";
213
                } else {
214
                    echo $month + 1;
215
                } ?>','<?php if ($month == 12) {
216
                    echo $year + 1;
217
                } else {
218
                    echo $year;
219
                } ?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">&gt;
220
            </td>
221
            <td class="dpButtons"
222
                onClick="loadMonth('<?php echo constant('BASE_URL') . '/core/' ?>','<?php echo $month ?>','<?php echo $year + 1 ?>','<?php echo $xyz ?>','<?php echo $langs->defaultlang ?>')">
223
                &gt;&gt;
224
            </td>
225
        </tr>
226
        <tr class="dpDayNames">
227
            <?php
228
            $startday = isset($conf->global->MAIN_START_WEEK) ? $conf->global->MAIN_START_WEEK : 1;
229
            $day_names = array('ShortSunday', 'ShortMonday', 'ShortTuesday', 'ShortWednesday', 'ShortThursday', 'ShortFriday', 'ShortSaturday');
230
            for ($i = 0; $i < 7; $i++) {
231
                echo '<td width="', (int)(($i + 1) * 100 / 7) - (int)($i * 100 / 7), '%">', $langs->trans($day_names[($i + $startday) % 7]), '</td>', "\n";
232
            }
233
            print '</tr>';
234
            //print "x ".$thedate." y";         // $thedate = first day of month
235
            $firstdate = dol_getdate($thedate);
236
            //var_dump($firstdateofweek);
237
            $mydate = dol_get_first_day_week(1, $month, $year, true); // mydate = cursor date
238
239
            // Loop on each day of month
240
            $stoploop = 0;
241
            $day = 1;
242
            $cols = 0;
243
            while (!$stoploop) {
244
                //print_r($mydate);
245
                if ($mydate < $firstdate) { // At first run
246
                    echo "<tr class=\"dpWeek\">";
247
                    //echo $conf->global->MAIN_START_WEEK.' '.$firstdate["wday"].' '.$startday;
248
                    $cols = 0;
249
                    for ($i = 0; $i < 7; $i++) {
250
                        $w = ($i + $startday) % 7;
251
                        if ($w == $firstdate["wday"]) {
252
                            $mydate = $firstdate;
253
                            break;
254
                        }
255
                        echo "<td>&nbsp;</td>";
256
                        $cols++;
257
                    }
258
                } else {
259
                    if ($mydate["wday"] == $startday) {
260
                        echo "<tr class=\"dpWeek\">";
261
                        $cols = 0;
262
                    }
263
                }
264
265
                $dayclass = "dpReg";
266
                if ($thedate == $selDate) {
267
                    $dayclass = "dpSelected";
268
                } elseif ($thedate == $today) {
269
                    $dayclass = "dpToday";
270
                }
271
272
                if ($langs->trans("FormatDateShortJavaInput") == "FormatDateShortJavaInput") {
273
                    print "ERROR FormatDateShortJavaInput not defined for language " . $langs->defaultlang;
274
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
275
                }
276
277
                // Sur click dans calendrier, appelle fonction dpClickDay
278
                echo "<td class=\"" . $dayclass . "\"";
279
                echo " onMouseOver=\"dpHighlightDay(" . $mydate["year"] . ",parseInt('" . dol_print_date($thedate, "%m") . "',10)," . $mydate["mday"] . ",tradMonths)\"";
280
                echo " onClick=\"dpClickDay(" . $mydate["year"] . ",parseInt('" . dol_print_date($thedate, "%m") . "',10)," . $mydate["mday"] . ",'" . $langs->trans("FormatDateShortJavaInput") . "')\"";
281
                echo ">" . sprintf("%02d", $mydate["mday"]) . "</td>";
282
                $cols++;
283
284
                if (($mydate["wday"] + 1) % 7 == $startday) {
285
                    echo "</TR>\n";
286
                }
287
288
                //$thedate=strtotime("tomorrow",$thedate);
289
                $day++;
290
                $thedate = dol_mktime(12, 0, 0, $month, $day, $year);
291
                if ($thedate == '') {
292
                    $stoploop = 1;
293
                } else {
294
                    $mydate = dol_getdate($thedate);
295
                    if ($firstdate["month"] != $mydate["month"]) {
296
                        $stoploop = 1;
297
                    }
298
                }
299
            }
300
301
            if ($cols < 7) {
302
                for ($i = 6; $i >= $cols; $i--) {
303
                    echo "<td>&nbsp;</td>";
304
                }
305
                echo "</tr>\n";
306
            } ?>
307
        <tr>
308
            <td id="dpExp" class="dpExplanation" colspan="7"><?php
309
                if ($selDate) {
310
                    $tempDate = dol_getdate($selDate);
311
                    print $langs->trans("Month" . $selectMonth) . " ";
312
                    print sprintf("%02d", $tempDate["mday"]);
313
                    print ", " . $selectYear;
314
                } else {
315
                    print "Click a Date";
316
                } ?></td>
317
        </tr>
318
    </table>
319
    <?php
320
}//end function
321