Skip to content

Commit b26236c

Browse files
authored
Merge pull request #50 from brlumen/fix-current-week
Fixing date error for new year
2 parents 61d11a4 + 21d0bab commit b26236c

File tree

6 files changed

+70
-28
lines changed

6 files changed

+70
-28
lines changed

Calendar/Calendar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function register() {
138138
$this->description = plugin_lang_get( 'description' );
139139
$this->page = 'config_page';
140140

141-
$this->version = '2.6.0';
141+
$this->version = '2.6.1';
142142

143143
$this->requires = array(
144144
'MantisCore' => '2.14.0',

Calendar/core/calendar_date_api.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,35 @@
1515
# along with Customer management plugin for MantisBT.
1616
# If not, see <http://www.gnu.org/licenses/>.
1717

18-
function days_of_number_week( $p_start_step_days, $p_count_step_days, $t_week_days_name, $p_week = null ) {
18+
function timestamp_next_week_get( $p_week, $p_year ) {
19+
return strtotime( $p_year . 'W' . str_pad( $p_week, 2, 0, STR_PAD_LEFT ) . ' + 1 weeks' );
20+
}
21+
22+
function timestamp_previous_week_get( $p_week, $p_year ) {
23+
return strtotime( $p_year . 'W' . str_pad( $p_week, 2, 0, STR_PAD_LEFT ) . ' - 1 weeks' );
24+
}
25+
26+
function days_of_number_week( $p_start_step_days, $p_count_step_days, $t_week_days_name, $p_week = null, $p_year = null ) {
1927

2028
if( $p_week == null ) {
21-
$t_week = date( "e" );
29+
$t_week = date( "W" );
2230
}
31+
32+
$t_week = str_pad( $p_week, 2, 0, STR_PAD_LEFT );
33+
34+
if( $p_year == null ) {
35+
$p_year = date( "Y" );
36+
}
2337

24-
$t_week = $p_week - 1;
25-
$t_curent_years = date( "Y" );
2638
$t_days_week = array();
2739

2840
for( $i = $p_start_step_days; $i < $p_start_step_days + $p_count_step_days; $i++ ) {
2941

30-
$t_day_cheks = date( "D", strtotime( 'last monday + ' . $i . ' days 1/1/' . $t_curent_years . ' + ' . $t_week . ' weeks' ) );
42+
$t_process_time = strtotime( $p_year . 'W' . $t_week . ' + ' . $i . ' days' );
43+
$t_day_cheks = date( "D", $t_process_time );
3144

3245
if( $t_week_days_name[$t_day_cheks] == ON ) {
33-
$t_days_week[$i] = date( strtotime( 'last monday + ' . $i . ' days 1/1/' . $t_curent_years . ' + ' . $t_week . ' weeks' ) );
46+
$t_days_week[$i] = date( $t_process_time );
3447
}
3548
}
3649
return $t_days_week;

Calendar/core/classes/ViewWeekCalendar.class.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
class ViewWeekCalendar extends WeekCalendar {
1414
protected $week;
1515
protected $for_user;
16+
protected $year;
1617

1718
//put your code here
18-
public function __construct( $p_week, $p_user, $p_is_full_time, $p_days_events, $p_link_options ) {
19+
public function __construct( $p_week, $p_user, $p_is_full_time, $p_days_events, $p_link_options, $p_year ) {
1920
parent::__construct($p_days_events, $p_link_options, $p_is_full_time);
2021

2122
$this->week = $p_week;
2223
$this->for_user = (int)$p_user;
24+
$this->year = $p_year;
2325
}
2426

2527
protected function print_spacer_top() {
@@ -53,21 +55,21 @@ protected function print_menu_top() {
5355

5456
echo '<div class="btn-group pull-left">';
5557
if( self::$full_time_is == FALSE ) {
56-
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . $this->week . "&full_time=TRUE", "0-24" );
58+
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . $this->week . "&year=" . $this->year . "&full_time=TRUE", "0-24" );
5759
} else {
58-
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . $this->week, gmdate( "H", plugin_config_get( 'time_day_start' ) ) . "-" . gmdate( "H", plugin_config_get( 'time_day_finish' ) ) );
60+
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . $this->week . "&year=" . $this->year, gmdate( "H", plugin_config_get( 'time_day_start' ) ) . "-" . gmdate( "H", plugin_config_get( 'time_day_finish' ) ) );
5961
}
6062
echo '</div>';
6163

6264
echo '<div class="btn-group pull-right">';
6365
if( self::$full_time_is == FALSE ) {
64-
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . ($this->week - 1), plugin_lang_get( 'previous_period' ) );
66+
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . date( "W", timestamp_previous_week_get( $this->week, $this->year ) ) . "&year=" . date( "Y", timestamp_previous_week_get( $this->week, $this->year ) ), plugin_lang_get( 'previous_period' ) );
6567
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . (int)date( "W" ), plugin_lang_get( 'week' ) );
66-
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . ($this->week + 1), plugin_lang_get( 'next_period' ) );
68+
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . date( "W", timestamp_next_week_get( $this->week, $this->year ) ) . "&year=" . date( "Y", timestamp_next_week_get( $this->week, $this->year ) ), plugin_lang_get( 'next_period' ) );
6769
} else {
68-
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . ($this->week - 1) . "&full_time=TRUE", plugin_lang_get( 'previous_period' ) );
70+
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . date( "W", timestamp_previous_week_get( $this->week, $this->year ) ) . "&year=" . date( "Y", timestamp_previous_week_get( $this->week, $this->year ) ) . "&full_time=TRUE", plugin_lang_get( 'previous_period' ) );
6971
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . (int)date( "W" ) . "&full_time=TRUE", plugin_lang_get( 'week' ) );
70-
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . ($this->week + 1) . "&full_time=TRUE", plugin_lang_get( 'next_period' ) );
72+
print_small_button( plugin_page( 'calendar_user_page' ) . "&for_user=" . $this->for_user . "&week=" . date( "W", timestamp_next_week_get( $this->week, $this->year ) ) . "&year=" . date( "Y", timestamp_next_week_get( $this->week, $this->year ) ) . "&full_time=TRUE", plugin_lang_get( 'next_period' ) );
7173
}
7274
echo '</div>';
7375

@@ -96,6 +98,7 @@ protected function print_menu_bottom() {
9698
# CSRF protection not required here - form does not result in modifications
9799
echo '<input type="hidden" name="page" value="Calendar/calendar_user_page" />';
98100
echo '<input type="hidden" name="week" value="' . $this->week . '" />';
101+
echo '<input type="hidden" name="year" value="' . $this->year . '" />';
99102
echo '<input type="hidden" name="full_time" value="' . (int)self::$full_time_is . '" />';
100103

101104
echo '<label class="inline">' . plugin_lang_get( 'filter_text' ) . '</label>';

Calendar/core/classes/ViewWeekSelect.class.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
class ViewWeekSelect extends ViewWeekCalendar {
1414
private $bug_id;
1515

16-
public function __construct( $p_week, $p_user, $p_is_full_time, $p_days_events, $p_bug_id ) {
17-
parent::__construct( $p_week, $p_user, $p_is_full_time, $p_days_events, plugin_page( 'event_insert' ) . '&bug_id=' . $p_bug_id . htmlspecialchars( form_security_param( 'event_insert' ) ) );
16+
public function __construct( $p_week, $p_user, $p_is_full_time, $p_days_events, $p_bug_id, $p_year ) {
17+
parent::__construct( $p_week, $p_user, $p_is_full_time, $p_days_events, plugin_page( 'event_insert' ) . '&bug_id=' . $p_bug_id . htmlspecialchars( form_security_param( 'event_insert' ) ), $p_year );
1818
$this->bug_id = (int)$p_bug_id;
1919
}
2020

@@ -45,21 +45,21 @@ protected function print_menu_top() {
4545

4646
echo '<div class="btn-group pull-left">';
4747
if( WeekCalendar::$full_time_is == FALSE ) {
48-
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . $this->week . "&full_time=TRUE" . '&id=' . $this->bug_id, "0-24" );
48+
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . $this->week . "&year=" . $this->year . "&full_time=TRUE" . '&id=' . $this->bug_id, "0-24" );
4949
} else {
50-
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . $this->week . '&id=' . $this->bug_id, gmdate( "H", plugin_config_get( 'time_day_start' ) ) . "-" . gmdate( "H", plugin_config_get( 'time_day_finish' ) ) );
50+
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . $this->week . "&year=" . $this->year . '&id=' . $this->bug_id, gmdate( "H", plugin_config_get( 'time_day_start' ) ) . "-" . gmdate( "H", plugin_config_get( 'time_day_finish' ) ) );
5151
}
5252
echo '</div>';
5353

5454
echo '<div class="btn-group pull-right">';
5555
if( WeekCalendar::$full_time_is == FALSE ) {
56-
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . ($this->week - 1) . '&id=' . $this->bug_id, plugin_lang_get( 'previous_period' ) );
56+
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . date( "W", timestamp_previous_week_get( $this->week, $this->year ) ) . "&year=" . date( "Y", timestamp_previous_week_get( $this->week, $this->year ) ) . '&id=' . $this->bug_id, plugin_lang_get( 'previous_period' ) );
5757
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . (int)date( "W" ) . '&id=' . $this->bug_id, plugin_lang_get( 'week' ) );
58-
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . ($this->week + 1) . '&id=' . $this->bug_id, plugin_lang_get( 'next_period' ) );
58+
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . date( "W", timestamp_next_week_get( $this->week, $this->year ) ) . "&year=" . date( "Y", timestamp_next_week_get( $this->week, $this->year ) ) . '&id=' . $this->bug_id, plugin_lang_get( 'next_period' ) );
5959
} else {
60-
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . ($this->week - 1) . "&full_time=TRUE" . '&id=' . $this->bug_id, plugin_lang_get( 'previous_period' ) );
60+
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . date( "W", timestamp_previous_week_get( $this->week, $this->year ) ) . "&year=" . date( "Y", timestamp_previous_week_get( $this->week, $this->year ) ) . "&full_time=TRUE" . '&id=' . $this->bug_id, plugin_lang_get( 'previous_period' ) );
6161
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . (int)date( "W" ) . "&full_time=TRUE" . '&id=' . $this->bug_id, plugin_lang_get( 'week' ) );
62-
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . ($this->week + 1) . "&full_time=TRUE" . '&id=' . $this->bug_id, plugin_lang_get( 'next_period' ) );
62+
print_small_button( plugin_page( 'event_insert_page' ) . "&for_user=" . $this->for_user . "&week=" . date( "W", timestamp_next_week_get( $this->week, $this->year ) ) . "&year=" . date( "Y", timestamp_next_week_get( $this->week, $this->year ) ) . "&full_time=TRUE" . '&id=' . $this->bug_id, plugin_lang_get( 'next_period' ) );
6363
}
6464
echo '</div>';
6565

@@ -88,6 +88,7 @@ protected function print_menu_bottom() {
8888
# CSRF protection not required here - form does not result in modifications
8989
echo '<input type="hidden" name="page" value="Calendar/event_insert_page" />';
9090
echo '<input type="hidden" name="week" value="' . $this->week . '" />';
91+
echo '<input type="hidden" name="year" value="' . $this->year . '" />';
9192
echo '<input type="hidden" name="full_time" value="' . (int)self::$full_time_is . '" />';
9293
echo '<input type="hidden" name="id" value="' . $this->bug_id . '" />';
9394

Calendar/pages/calendar_user_page.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,34 @@
3838
# don't index view issues pages
3939
html_robots_noindex();
4040

41+
$t_current_week = date( "W" );
42+
$t_current_year = date( "Y" );
43+
$t_year_start_of_current_week = date( "Y", strtotime( 'this week' ) );
4144

42-
$f_week = gpc_get_int( "week", date( "W" ) );
45+
if( $t_current_year != $t_year_start_of_current_week ) {
46+
$t_current_year = $t_year_start_of_current_week;
47+
}
48+
49+
$f_week = gpc_get_int( "week", $t_current_week );
50+
$f_year = gpc_get_int( "year", $t_current_year );
4351
$f_is_fulltime = gpc_get_bool( "full_time" );
4452
$f_for_user = gpc_get_int( "for_user", auth_get_current_user_id() );
4553
//$t_access_level_current_user = access_get_project_level();
4654
//$t_access_level_global_current_user = access_get_global_level();
4755

56+
if( strtotime( $f_year . 'W' . str_pad( $f_week, 2, 0, STR_PAD_LEFT ) ) == false ) {
57+
error_parameters( plugin_lang_get( 'date_event' ) );
58+
plugin_error( 'ERROR_RANGE_TIME', ERROR );
59+
}
60+
4861
$t_start_day_of_the_week = plugin_config_get( "startStepDays" );
4962
$t_step_days_count = plugin_config_get( "countStepDays" );
5063
$t_arWeekdaysName = plugin_config_get( "arWeekdaysName" );
5164

52-
$t_days = days_of_number_week( $t_start_day_of_the_week, $t_step_days_count, $t_arWeekdaysName, $f_week );
65+
$t_days = days_of_number_week( $t_start_day_of_the_week, $t_step_days_count, $t_arWeekdaysName, $f_week, $f_year );
5366
$t_days_events = get_days_object( $t_days, helper_get_current_project(), $f_for_user );
5467

55-
$t_calendar_week = new ViewWeekCalendar( $f_week, $f_for_user, $f_is_fulltime, $t_days_events, plugin_page( 'view' ) );
68+
$t_calendar_week = new ViewWeekCalendar( $f_week, $f_for_user, $f_is_fulltime, $t_days_events, plugin_page( 'view' ), $f_year );
5669

5770
$t_calendar_week->print_html();
5871

Calendar/pages/event_insert_page.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,33 @@
4242
# don't index view issues pages
4343
html_robots_noindex();
4444

45+
$t_current_week = date( "W" );
46+
$t_current_year = date( "Y" );
47+
$t_year_start_of_current_week = date( "Y", strtotime( 'this week' ) );
4548

49+
if( $t_current_year != $t_year_start_of_current_week ) {
50+
$t_current_year = $t_year_start_of_current_week;
51+
}
4652

47-
$f_week = gpc_get_int( "week", date( "W" ) );
53+
$f_week = gpc_get_int( "week", $t_current_week );
54+
$f_year = gpc_get_int( "year", $t_current_year );
4855
$f_is_fulltime = gpc_get_bool( "full_time" );
4956
$f_for_user = gpc_get_int( "for_user", auth_get_current_user_id() );
5057

58+
if( strtotime( $f_year . 'W' . str_pad( $f_week, 2, 0, STR_PAD_LEFT ) ) == false ) {
59+
error_parameters( plugin_lang_get( 'date_event' ) );
60+
plugin_error( 'ERROR_RANGE_TIME', ERROR );
61+
}
62+
5163
$t_start_day_of_the_week = plugin_config_get( "startStepDays" );
5264
$t_step_days_count = plugin_config_get( "countStepDays" );
5365
$t_arWeekdaysName = plugin_config_get( "arWeekdaysName" );
5466

5567
$t_excluded_events = get_events_id_from_bug_id( $f_bug_id );
56-
$t_days = days_of_number_week( $t_start_day_of_the_week, $t_step_days_count, $t_arWeekdaysName, $f_week );
68+
$t_days = days_of_number_week( $t_start_day_of_the_week, $t_step_days_count, $t_arWeekdaysName, $f_week, $f_year );
5769
$t_days_events = get_days_object( $t_days, helper_get_current_project(), $f_for_user, $t_excluded_events );
5870

59-
$t_calendar_week = new ViewWeekSelect( $f_week, $f_for_user, $f_is_fulltime, $t_days_events, $f_bug_id );
71+
$t_calendar_week = new ViewWeekSelect( $f_week, $f_for_user, $f_is_fulltime, $t_days_events, $f_bug_id, $f_year );
6072

6173
$t_calendar_week->print_html();
6274

0 commit comments

Comments
 (0)