Lines 15-726
package C4::Calendar;
Link Here
|
15 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
15 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
16 |
# Suite 330, Boston, MA 02111-1307 USA |
16 |
# Suite 330, Boston, MA 02111-1307 USA |
17 |
|
17 |
|
18 |
use strict; |
18 |
use Modern::Perl; |
19 |
use warnings; |
|
|
20 |
use vars qw($VERSION @EXPORT); |
19 |
use vars qw($VERSION @EXPORT); |
21 |
|
20 |
|
22 |
use Carp; |
21 |
use Carp; |
23 |
use Date::Calc qw( Date_to_Days Today); |
|
|
24 |
|
22 |
|
25 |
use C4::Context; |
23 |
use C4::Context; |
26 |
|
24 |
|
|
|
25 |
our ( @ISA, @EXPORT ); |
26 |
|
27 |
BEGIN { |
28 |
@ISA = qw( Exporter ); |
29 |
@EXPORT = qw( |
30 |
GetSingleEvents |
31 |
GetWeeklyEvents |
32 |
GetYearlyEvents |
33 |
ModSingleEvent |
34 |
ModRepeatingEvent |
35 |
DelSingleEvent |
36 |
DelRepeatingEvent |
37 |
CopyAllEvents |
38 |
); |
39 |
} |
40 |
|
27 |
use constant ISO_DATE_FORMAT => "%04d-%02d-%02d"; |
41 |
use constant ISO_DATE_FORMAT => "%04d-%02d-%02d"; |
28 |
=head1 NAME |
42 |
=head1 NAME |
29 |
|
43 |
|
30 |
C4::Calendar::Calendar - Koha module dealing with holidays. |
44 |
C4::Calendar - Koha module dealing with holidays. |
31 |
|
45 |
|
32 |
=head1 SYNOPSIS |
46 |
=head1 SYNOPSIS |
33 |
|
47 |
|
34 |
use C4::Calendar::Calendar; |
48 |
use C4::Calendar; |
35 |
|
49 |
|
36 |
=head1 DESCRIPTION |
50 |
=head1 DESCRIPTION |
37 |
|
51 |
|
38 |
This package is used to deal with holidays. Through this package, you can set |
52 |
This package is used to deal with hours and holidays; |
39 |
all kind of holidays for the library. |
|
|
40 |
|
53 |
|
41 |
=head1 FUNCTIONS |
54 |
=head1 FUNCTIONS |
42 |
|
55 |
|
43 |
=head2 new |
56 |
=head2 GetSingleEvents |
44 |
|
57 |
|
45 |
$calendar = C4::Calendar->new(branchcode => $branchcode); |
58 |
\@events = GetSingleEvents( $branchcode ) |
46 |
|
59 |
|
47 |
Each library branch has its own Calendar. |
60 |
Get the non-repeating events for the given library. |
48 |
C<$branchcode> specifies which Calendar you want. |
|
|
49 |
|
61 |
|
50 |
=cut |
62 |
=cut |
51 |
|
63 |
|
52 |
sub new { |
64 |
sub GetSingleEvents { |
53 |
my $classname = shift @_; |
65 |
my ( $branchcode ) = @_; |
54 |
my %options = @_; |
66 |
|
55 |
my $self = bless({}, $classname); |
67 |
return C4::Context->dbh->selectall_arrayref( q{ |
56 |
foreach my $optionName (keys %options) { |
68 |
SELECT |
57 |
$self->{lc($optionName)} = $options{$optionName}; |
69 |
CONCAT(LPAD(year, 4, '0'), '-', LPAD(month, 2, '0'), '-', LPAD(day, 2, '0')) as event_date, |
58 |
} |
70 |
0 as open_hour, 0 as open_minute, IF(isexception, 24, 0) as close_hour, |
59 |
defined($self->{branchcode}) or croak "No branchcode argument to new. Should be C4::Calendar->new(branchcode => \$branchcode)"; |
71 |
0 as close_minute, title, description, IF(isexception, 0, 1) as closed |
60 |
$self->_init($self->{branchcode}); |
72 |
FROM special_holidays |
61 |
return $self; |
73 |
WHERE branchcode = ? |
62 |
} |
74 |
}, { Slice => {} }, $branchcode ); |
63 |
|
|
|
64 |
sub _init { |
65 |
my $self = shift @_; |
66 |
my $branch = shift; |
67 |
defined($branch) or die "No branchcode sent to _init"; # must test for defined here and above to allow "" |
68 |
my $dbh = C4::Context->dbh(); |
69 |
my $repeatable = $dbh->prepare( 'SELECT * |
70 |
FROM repeatable_holidays |
71 |
WHERE ( branchcode = ? ) |
72 |
AND (ISNULL(weekday) = ?)' ); |
73 |
$repeatable->execute($branch,0); |
74 |
my %week_days_holidays; |
75 |
while (my $row = $repeatable->fetchrow_hashref) { |
76 |
my $key = $row->{weekday}; |
77 |
$week_days_holidays{$key}{title} = $row->{title}; |
78 |
$week_days_holidays{$key}{description} = $row->{description}; |
79 |
} |
80 |
$self->{'week_days_holidays'} = \%week_days_holidays; |
81 |
|
82 |
$repeatable->execute($branch,1); |
83 |
my %day_month_holidays; |
84 |
while (my $row = $repeatable->fetchrow_hashref) { |
85 |
my $key = $row->{month} . "/" . $row->{day}; |
86 |
$day_month_holidays{$key}{title} = $row->{title}; |
87 |
$day_month_holidays{$key}{description} = $row->{description}; |
88 |
$day_month_holidays{$key}{day} = sprintf("%02d", $row->{day}); |
89 |
$day_month_holidays{$key}{month} = sprintf("%02d", $row->{month}); |
90 |
} |
91 |
$self->{'day_month_holidays'} = \%day_month_holidays; |
92 |
|
93 |
my $special = $dbh->prepare( 'SELECT day, month, year, title, description |
94 |
FROM special_holidays |
95 |
WHERE ( branchcode = ? ) |
96 |
AND (isexception = ?)' ); |
97 |
$special->execute($branch,1); |
98 |
my %exception_holidays; |
99 |
while (my ($day, $month, $year, $title, $description) = $special->fetchrow) { |
100 |
$exception_holidays{"$year/$month/$day"}{title} = $title; |
101 |
$exception_holidays{"$year/$month/$day"}{description} = $description; |
102 |
$exception_holidays{"$year/$month/$day"}{date} = |
103 |
sprintf(ISO_DATE_FORMAT, $year, $month, $day); |
104 |
} |
105 |
$self->{'exception_holidays'} = \%exception_holidays; |
106 |
|
107 |
$special->execute($branch,0); |
108 |
my %single_holidays; |
109 |
while (my ($day, $month, $year, $title, $description) = $special->fetchrow) { |
110 |
$single_holidays{"$year/$month/$day"}{title} = $title; |
111 |
$single_holidays{"$year/$month/$day"}{description} = $description; |
112 |
$single_holidays{"$year/$month/$day"}{date} = |
113 |
sprintf(ISO_DATE_FORMAT, $year, $month, $day); |
114 |
} |
115 |
$self->{'single_holidays'} = \%single_holidays; |
116 |
return $self; |
117 |
} |
75 |
} |
118 |
|
76 |
|
119 |
=head2 get_week_days_holidays |
77 |
=head2 GetWeeklyEvents |
120 |
|
78 |
|
121 |
$week_days_holidays = $calendar->get_week_days_holidays(); |
79 |
\@events = GetWeeklyEvents( $branchcode ) |
122 |
|
80 |
|
123 |
Returns a hash reference to week days holidays. |
81 |
Get the weekly-repeating events for the given library. |
124 |
|
82 |
|
125 |
=cut |
83 |
=cut |
126 |
|
84 |
|
127 |
sub get_week_days_holidays { |
85 |
sub GetWeeklyEvents { |
128 |
my $self = shift @_; |
86 |
my ( $branchcode ) = @_; |
129 |
my $week_days_holidays = $self->{'week_days_holidays'}; |
|
|
130 |
return $week_days_holidays; |
131 |
} |
132 |
|
133 |
=head2 get_day_month_holidays |
134 |
|
135 |
$day_month_holidays = $calendar->get_day_month_holidays(); |
136 |
|
137 |
Returns a hash reference to day month holidays. |
138 |
|
139 |
=cut |
140 |
|
87 |
|
141 |
sub get_day_month_holidays { |
88 |
return C4::Context->dbh->selectall_arrayref( q{ |
142 |
my $self = shift @_; |
89 |
SELECT |
143 |
my $day_month_holidays = $self->{'day_month_holidays'}; |
90 |
weekday, 0 as open_hour, 0 as open_minute, 0 as close_hour, |
144 |
return $day_month_holidays; |
91 |
0 as close_minute, title, description, 1 as closed |
|
|
92 |
FROM repeatable_holidays |
93 |
WHERE branchcode = ? AND weekday IS NOT NULL |
94 |
}, { Slice => {} }, $branchcode ); |
145 |
} |
95 |
} |
146 |
|
96 |
|
147 |
=head2 get_exception_holidays |
97 |
=head2 GetYearlyEvents |
148 |
|
98 |
|
149 |
$exception_holidays = $calendar->exception_holidays(); |
99 |
\@events = GetYearlyEvents( $branchcode ) |
150 |
|
100 |
|
151 |
Returns a hash reference to exception holidays. This kind of days are those |
101 |
Get the yearly-repeating events for the given library. |
152 |
which stands for a holiday, but you wanted to make an exception for this particular |
|
|
153 |
date. |
154 |
|
102 |
|
155 |
=cut |
103 |
=cut |
156 |
|
104 |
|
157 |
sub get_exception_holidays { |
105 |
sub GetYearlyEvents { |
158 |
my $self = shift @_; |
106 |
my ( $branchcode ) = @_; |
159 |
my $exception_holidays = $self->{'exception_holidays'}; |
|
|
160 |
return $exception_holidays; |
161 |
} |
162 |
|
163 |
=head2 get_single_holidays |
164 |
|
165 |
$single_holidays = $calendar->get_single_holidays(); |
166 |
|
107 |
|
167 |
Returns a hash reference to single holidays. This kind of holidays are those which |
108 |
return C4::Context->dbh->selectall_arrayref( q{ |
168 |
happend just one time. |
109 |
SELECT |
169 |
|
110 |
month, day, 0 as open_hour, 0 as open_minute, 0 as close_hour, |
170 |
=cut |
111 |
0 as close_minute, title, description, 1 as closed |
171 |
|
112 |
FROM repeatable_holidays |
172 |
sub get_single_holidays { |
113 |
WHERE branchcode = ? AND weekday IS NULL |
173 |
my $self = shift @_; |
114 |
}, { Slice => {} }, $branchcode ); |
174 |
my $single_holidays = $self->{'single_holidays'}; |
|
|
175 |
return $single_holidays; |
176 |
} |
115 |
} |
177 |
|
116 |
|
178 |
=head2 insert_week_day_holiday |
117 |
=head2 ModSingleEvent |
179 |
|
|
|
180 |
insert_week_day_holiday(weekday => $weekday, |
181 |
title => $title, |
182 |
description => $description); |
183 |
|
184 |
Inserts a new week day for $self->{branchcode}. |
185 |
|
118 |
|
186 |
C<$day> Is the week day to make holiday. |
119 |
ModSingleEvent( $branchcode, $date, \%info ) |
187 |
|
120 |
|
188 |
C<$title> Is the title to store for the holiday formed by $year/$month/$day. |
121 |
Creates or updates an event for a single date. $date should be an ISO-formatted |
189 |
|
122 |
date string, and \%info should contain the following keys: open_hour, |
190 |
C<$description> Is the description to store for the holiday formed by $year/$month/$day. |
123 |
open_minute, close_hour, close_minute, title and description. |
191 |
|
124 |
|
192 |
=cut |
125 |
=cut |
193 |
|
126 |
|
194 |
sub insert_week_day_holiday { |
127 |
sub ModSingleEvent { |
195 |
my $self = shift @_; |
128 |
my ( $branchcode, $date, $info ) = @_; |
196 |
my %options = @_; |
|
|
197 |
|
198 |
my $weekday = $options{weekday}; |
199 |
croak "Invalid weekday $weekday" unless $weekday =~ m/^[0-6]$/; |
200 |
|
201 |
my $dbh = C4::Context->dbh(); |
202 |
my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )"); |
203 |
$insertHoliday->execute( $self->{branchcode}, $weekday, $options{title}, $options{description}); |
204 |
$self->{'week_days_holidays'}->{$weekday}{title} = $options{title}; |
205 |
$self->{'week_days_holidays'}->{$weekday}{description} = $options{description}; |
206 |
return $self; |
207 |
} |
208 |
|
209 |
=head2 insert_day_month_holiday |
210 |
|
211 |
insert_day_month_holiday(day => $day, |
212 |
month => $month, |
213 |
title => $title, |
214 |
description => $description); |
215 |
|
216 |
Inserts a new day month holiday for $self->{branchcode}. |
217 |
|
129 |
|
218 |
C<$day> Is the day month to make the date to insert. |
130 |
my ( $year, $month, $day ) = ( $date =~ /(\d+)-(\d+)-(\d+)/ ); |
|
|
131 |
return unless ( $year && $month && $day ); |
219 |
|
132 |
|
220 |
C<$month> Is month to make the date to insert. |
133 |
my $dbh = C4::Context->dbh; |
|
|
134 |
my @args = ( ( map { $info->{$_} } qw(title description) ), $info->{close_hour} != 0, $branchcode, $year, $month, $day ); |
221 |
|
135 |
|
222 |
C<$title> Is the title to store for the holiday formed by $year/$month/$day. |
136 |
# The code below relies on $dbh->do returning 0 when the update affects no rows |
|
|
137 |
my $affected = $dbh->do( q{ |
138 |
UPDATE special_holidays |
139 |
SET |
140 |
title = ?, description = ?, isexception = ? |
141 |
WHERE branchcode = ? AND year = ? AND month = ? AND day = ? |
142 |
}, {}, @args ); |
223 |
|
143 |
|
224 |
C<$description> Is the description to store for the holiday formed by $year/$month/$day. |
144 |
$dbh->do( q{ |
225 |
|
145 |
INSERT |
226 |
=cut |
146 |
INTO special_holidays(title, description, isexception, branchcode, year, month, day) |
227 |
|
147 |
VALUES (?, ?, ?, ?, ?, ?, ?) |
228 |
sub insert_day_month_holiday { |
148 |
}, {}, @args ) unless ( $affected > 0 ); |
229 |
my $self = shift @_; |
|
|
230 |
my %options = @_; |
231 |
|
232 |
my $dbh = C4::Context->dbh(); |
233 |
my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )"); |
234 |
$insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description}); |
235 |
$self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title}; |
236 |
$self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description}; |
237 |
return $self; |
238 |
} |
149 |
} |
239 |
|
150 |
|
240 |
=head2 insert_single_holiday |
151 |
=head2 ModRepeatingEvent |
241 |
|
|
|
242 |
insert_single_holiday(day => $day, |
243 |
month => $month, |
244 |
year => $year, |
245 |
title => $title, |
246 |
description => $description); |
247 |
|
248 |
Inserts a new single holiday for $self->{branchcode}. |
249 |
|
250 |
C<$day> Is the day month to make the date to insert. |
251 |
|
252 |
C<$month> Is month to make the date to insert. |
253 |
|
254 |
C<$year> Is year to make the date to insert. |
255 |
|
152 |
|
256 |
C<$title> Is the title to store for the holiday formed by $year/$month/$day. |
153 |
ModRepeatingEvent( $branchcode, $weekday, $month, $day, \%info ) |
257 |
|
154 |
|
258 |
C<$description> Is the description to store for the holiday formed by $year/$month/$day. |
155 |
Creates or updates a weekly- or yearly-repeating event. Either $weekday, |
|
|
156 |
or $month and $day should be set, for a weekly or yearly event, respectively. |
259 |
|
157 |
|
260 |
=cut |
158 |
=cut |
261 |
|
159 |
|
262 |
sub insert_single_holiday { |
160 |
sub _get_compare { |
263 |
my $self = shift @_; |
161 |
my ( $colname, $value ) = @_; |
264 |
my %options = @_; |
|
|
265 |
|
266 |
@options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) |
267 |
if $options{date} && !$options{day}; |
268 |
|
269 |
my $dbh = C4::Context->dbh(); |
270 |
my $isexception = 0; |
271 |
my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); |
272 |
$insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description}); |
273 |
$self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; |
274 |
$self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; |
275 |
return $self; |
276 |
} |
277 |
|
278 |
=head2 insert_exception_holiday |
279 |
|
280 |
insert_exception_holiday(day => $day, |
281 |
month => $month, |
282 |
year => $year, |
283 |
title => $title, |
284 |
description => $description); |
285 |
|
286 |
Inserts a new exception holiday for $self->{branchcode}. |
287 |
|
288 |
C<$day> Is the day month to make the date to insert. |
289 |
|
290 |
C<$month> Is month to make the date to insert. |
291 |
|
292 |
C<$year> Is year to make the date to insert. |
293 |
|
294 |
C<$title> Is the title to store for the holiday formed by $year/$month/$day. |
295 |
|
296 |
C<$description> Is the description to store for the holiday formed by $year/$month/$day. |
297 |
|
298 |
=cut |
299 |
|
300 |
sub insert_exception_holiday { |
301 |
my $self = shift @_; |
302 |
my %options = @_; |
303 |
|
304 |
@options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) |
305 |
if $options{date} && !$options{day}; |
306 |
|
307 |
my $dbh = C4::Context->dbh(); |
308 |
my $isexception = 1; |
309 |
my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); |
310 |
$insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description}); |
311 |
$self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; |
312 |
$self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; |
313 |
return $self; |
314 |
} |
315 |
|
316 |
=head2 ModWeekdayholiday |
317 |
|
318 |
ModWeekdayholiday(weekday =>$weekday, |
319 |
title => $title, |
320 |
description => $description) |
321 |
|
322 |
Modifies the title and description of a weekday for $self->{branchcode}. |
323 |
|
324 |
C<$weekday> Is the title to update for the holiday. |
325 |
|
162 |
|
326 |
C<$description> Is the description to update for the holiday. |
163 |
return ' AND ' . $colname . ' ' . ( defined( $value ) ? '=' : 'IS' ) . ' ?'; |
327 |
|
|
|
328 |
=cut |
329 |
|
330 |
sub ModWeekdayholiday { |
331 |
my $self = shift @_; |
332 |
my %options = @_; |
333 |
|
334 |
my $dbh = C4::Context->dbh(); |
335 |
my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE branchcode = ? AND weekday = ?"); |
336 |
$updateHoliday->execute( $options{title},$options{description},$self->{branchcode},$options{weekday}); |
337 |
$self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title}; |
338 |
$self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description}; |
339 |
return $self; |
340 |
} |
164 |
} |
341 |
|
165 |
|
342 |
=head2 ModDaymonthholiday |
166 |
sub ModRepeatingEvent { |
343 |
|
167 |
my ( $branchcode, $weekday, $month, $day, $info ) = @_; |
344 |
ModDaymonthholiday(day => $day, |
|
|
345 |
month => $month, |
346 |
title => $title, |
347 |
description => $description); |
348 |
|
349 |
Modifies the title and description for a day/month holiday for $self->{branchcode}. |
350 |
|
351 |
C<$day> The day of the month for the update. |
352 |
|
353 |
C<$month> The month to be used for the update. |
354 |
|
355 |
C<$title> The title to be updated for the holiday. |
356 |
|
357 |
C<$description> The description to be update for the holiday. |
358 |
|
359 |
=cut |
360 |
|
361 |
sub ModDaymonthholiday { |
362 |
my $self = shift @_; |
363 |
my %options = @_; |
364 |
|
365 |
my $dbh = C4::Context->dbh(); |
366 |
my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE month = ? AND day = ? AND branchcode = ?"); |
367 |
$updateHoliday->execute( $options{title},$options{description},$options{month},$options{day},$self->{branchcode}); |
368 |
$self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title}; |
369 |
$self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description}; |
370 |
return $self; |
371 |
} |
372 |
|
373 |
=head2 ModSingleholiday |
374 |
|
375 |
ModSingleholiday(day => $day, |
376 |
month => $month, |
377 |
year => $year, |
378 |
title => $title, |
379 |
description => $description); |
380 |
|
381 |
Modifies the title and description for a single holiday for $self->{branchcode}. |
382 |
|
383 |
C<$day> Is the day of the month to make the update. |
384 |
|
168 |
|
385 |
C<$month> Is the month to make the update. |
169 |
my $dbh = C4::Context->dbh; |
|
|
170 |
my $open = ( $info->{close_hour} != 0 ); |
386 |
|
171 |
|
387 |
C<$year> Is the year to make the update. |
172 |
if ($open) { |
388 |
|
173 |
$dbh->do( q{ |
389 |
C<$title> Is the title to update for the holiday formed by $year/$month/$day. |
174 |
DELETE FROM repeatable_holidays |
390 |
|
175 |
WHERE branchcode = ? |
391 |
C<$description> Is the description to update for the holiday formed by $year/$month/$day. |
176 |
} . _get_compare( 'weekday', $weekday ) . _get_compare( 'month', $month ) . _get_compare( 'day', $day ), {}, $branchcode, $weekday, $month, $day ); |
392 |
|
|
|
393 |
=cut |
394 |
|
395 |
sub ModSingleholiday { |
396 |
my $self = shift @_; |
397 |
my %options = @_; |
398 |
|
399 |
my $dbh = C4::Context->dbh(); |
400 |
my $isexception = 0; |
401 |
my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?"); |
402 |
$updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception); |
403 |
$self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; |
404 |
$self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; |
405 |
return $self; |
406 |
} |
407 |
|
408 |
=head2 ModExceptionholiday |
409 |
|
410 |
ModExceptionholiday(day => $day, |
411 |
month => $month, |
412 |
year => $year, |
413 |
title => $title, |
414 |
description => $description); |
415 |
|
416 |
Modifies the title and description for an exception holiday for $self->{branchcode}. |
417 |
|
418 |
C<$day> Is the day of the month for the holiday. |
419 |
|
420 |
C<$month> Is the month for the holiday. |
421 |
|
422 |
C<$year> Is the year for the holiday. |
423 |
|
424 |
C<$title> Is the title to be modified for the holiday formed by $year/$month/$day. |
425 |
|
426 |
C<$description> Is the description to be modified for the holiday formed by $year/$month/$day. |
427 |
|
428 |
=cut |
429 |
|
430 |
sub ModExceptionholiday { |
431 |
my $self = shift @_; |
432 |
my %options = @_; |
433 |
|
434 |
my $dbh = C4::Context->dbh(); |
435 |
my $isexception = 1; |
436 |
my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?"); |
437 |
$updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception); |
438 |
$self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; |
439 |
$self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; |
440 |
return $self; |
441 |
} |
442 |
|
443 |
=head2 delete_holiday |
444 |
|
445 |
delete_holiday(weekday => $weekday |
446 |
day => $day, |
447 |
month => $month, |
448 |
year => $year); |
449 |
|
450 |
Delete a holiday for $self->{branchcode}. |
451 |
|
452 |
C<$weekday> Is the week day to delete. |
453 |
|
454 |
C<$day> Is the day month to make the date to delete. |
455 |
|
456 |
C<$month> Is month to make the date to delete. |
457 |
|
458 |
C<$year> Is year to make the date to delete. |
459 |
|
460 |
=cut |
461 |
|
462 |
sub delete_holiday { |
463 |
my $self = shift @_; |
464 |
my %options = @_; |
465 |
|
466 |
# Verify what kind of holiday that day is. For example, if it is |
467 |
# a repeatable holiday, this should check if there are some exception |
468 |
# for that holiday rule. Otherwise, if it is a regular holiday, it´s |
469 |
# ok just deleting it. |
470 |
|
471 |
my $dbh = C4::Context->dbh(); |
472 |
my $isSingleHoliday = $dbh->prepare("SELECT id FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)"); |
473 |
$isSingleHoliday->execute($self->{branchcode}, $options{day}, $options{month}, $options{year}); |
474 |
if ($isSingleHoliday->rows) { |
475 |
my $id = $isSingleHoliday->fetchrow; |
476 |
$isSingleHoliday->finish; # Close the last query |
477 |
|
478 |
my $deleteHoliday = $dbh->prepare("DELETE FROM special_holidays WHERE id = ?"); |
479 |
$deleteHoliday->execute($id); |
480 |
delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}); |
481 |
} else { |
177 |
} else { |
482 |
$isSingleHoliday->finish; # Close the last query |
178 |
my @args = ( ( map { $info->{$_} } qw(title description) ), $branchcode, $weekday, $month, $day ); |
483 |
|
179 |
|
484 |
my $isWeekdayHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE branchcode = ? AND weekday = ?"); |
180 |
# The code below relies on $dbh->do returning 0 when the update affects no rows |
485 |
$isWeekdayHoliday->execute($self->{branchcode}, $options{weekday}); |
181 |
my $affected = $dbh->do( q{ |
486 |
if ($isWeekdayHoliday->rows) { |
182 |
UPDATE repeatable_holidays |
487 |
my $id = $isWeekdayHoliday->fetchrow; |
183 |
SET |
488 |
$isWeekdayHoliday->finish; # Close the last query |
184 |
title = ?, description = ? |
489 |
|
185 |
WHERE branchcode = ? |
490 |
my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = ?) AND (branchcode = ?)"); |
186 |
} . _get_compare( 'weekday', $weekday ) . _get_compare( 'month', $month ) . _get_compare( 'day', $day ), {}, @args ); |
491 |
$updateExceptions->execute($options{weekday}, $self->{branchcode}); |
187 |
|
492 |
$updateExceptions->finish; # Close the last query |
188 |
$dbh->do( q{ |
493 |
|
189 |
INSERT |
494 |
my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE id = ?"); |
190 |
INTO repeatable_holidays(title, description, branchcode, weekday, month, day) |
495 |
$deleteHoliday->execute($id); |
191 |
VALUES (?, ?, ?, ?, ?, ?) |
496 |
delete($self->{'week_days_holidays'}->{$options{weekday}}); |
192 |
}, {}, @args ) unless ( $affected > 0 ); |
497 |
} else { |
|
|
498 |
$isWeekdayHoliday->finish; # Close the last query |
499 |
|
500 |
my $isDayMonthHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)"); |
501 |
$isDayMonthHoliday->execute($self->{branchcode}, $options{day}, $options{month}); |
502 |
if ($isDayMonthHoliday->rows) { |
503 |
my $id = $isDayMonthHoliday->fetchrow; |
504 |
$isDayMonthHoliday->finish; |
505 |
my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (special_holidays.branchcode = ?) AND (special_holidays.day = ?) and (special_holidays.month = ?)"); |
506 |
$updateExceptions->execute($self->{branchcode}, $options{day}, $options{month}); |
507 |
$updateExceptions->finish; # Close the last query |
508 |
|
509 |
my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (id = ?)"); |
510 |
$deleteHoliday->execute($id); |
511 |
delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"}); |
512 |
} |
513 |
} |
514 |
} |
193 |
} |
515 |
return $self; |
|
|
516 |
} |
517 |
=head2 delete_holiday_range |
518 |
|
519 |
delete_holiday_range(day => $day, |
520 |
month => $month, |
521 |
year => $year); |
522 |
|
523 |
Delete a holiday range of dates for $self->{branchcode}. |
524 |
|
525 |
C<$day> Is the day month to make the date to delete. |
526 |
|
527 |
C<$month> Is month to make the date to delete. |
528 |
|
529 |
C<$year> Is year to make the date to delete. |
530 |
|
531 |
=cut |
532 |
|
533 |
sub delete_holiday_range { |
534 |
my $self = shift; |
535 |
my %options = @_; |
536 |
|
537 |
my $dbh = C4::Context->dbh(); |
538 |
my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)"); |
539 |
$sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year}); |
540 |
} |
194 |
} |
541 |
|
195 |
|
542 |
=head2 delete_holiday_range_repeatable |
196 |
=head2 DelSingleEvent |
543 |
|
197 |
|
544 |
delete_holiday_range_repeatable(day => $day, |
198 |
DelSingleEvent( $branchcode, $date, \%info ) |
545 |
month => $month); |
|
|
546 |
|
199 |
|
547 |
Delete a holiday for $self->{branchcode}. |
200 |
Deletes an event for a single date. $date should be an ISO-formatted date string. |
548 |
|
|
|
549 |
C<$day> Is the day month to make the date to delete. |
550 |
|
551 |
C<$month> Is month to make the date to delete. |
552 |
|
201 |
|
553 |
=cut |
202 |
=cut |
554 |
|
203 |
|
555 |
sub delete_holiday_range_repeatable { |
204 |
sub DelSingleEvent { |
556 |
my $self = shift; |
205 |
my ( $branchcode, $date ) = @_; |
557 |
my %options = @_; |
|
|
558 |
|
559 |
my $dbh = C4::Context->dbh(); |
560 |
my $sth = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)"); |
561 |
$sth->execute($self->{branchcode}, $options{day}, $options{month}); |
562 |
} |
563 |
|
564 |
=head2 delete_exception_holiday_range |
565 |
|
206 |
|
566 |
delete_exception_holiday_range(weekday => $weekday |
207 |
my ( $year, $month, $day ) = ( $date =~ /(\d+)-(\d+)-(\d+)/ ); |
567 |
day => $day, |
208 |
return unless ( $year && $month && $day ); |
568 |
month => $month, |
|
|
569 |
year => $year); |
570 |
|
209 |
|
571 |
Delete a holiday for $self->{branchcode}. |
210 |
C4::Context->dbh->do( q{ |
572 |
|
211 |
DELETE FROM special_holidays |
573 |
C<$day> Is the day month to make the date to delete. |
212 |
WHERE branchcode = ? AND year = ? AND month = ? AND day = ? |
574 |
|
213 |
}, {}, $branchcode, $year, $month, $day ); |
575 |
C<$month> Is month to make the date to delete. |
|
|
576 |
|
577 |
C<$year> Is year to make the date to delete. |
578 |
|
579 |
=cut |
580 |
|
581 |
sub delete_exception_holiday_range { |
582 |
my $self = shift; |
583 |
my %options = @_; |
584 |
|
585 |
my $dbh = C4::Context->dbh(); |
586 |
my $sth = $dbh->prepare("DELETE FROM special_holidays WHERE (branchcode = ?) AND (isexception = 1) AND (day = ?) AND (month = ?) AND (year = ?)"); |
587 |
$sth->execute($self->{branchcode}, $options{day}, $options{month}, $options{year}); |
588 |
} |
214 |
} |
589 |
|
215 |
|
590 |
=head2 isHoliday |
216 |
=head2 DelRepeatingEvent |
591 |
|
|
|
592 |
$isHoliday = isHoliday($day, $month $year); |
593 |
|
594 |
C<$day> Is the day to check whether if is a holiday or not. |
595 |
|
596 |
C<$month> Is the month to check whether if is a holiday or not. |
597 |
|
598 |
C<$year> Is the year to check whether if is a holiday or not. |
599 |
|
600 |
=cut |
601 |
|
602 |
sub isHoliday { |
603 |
my ($self, $day, $month, $year) = @_; |
604 |
# FIXME - date strings are stored in non-padded metric format. should change to iso. |
605 |
# FIXME - should change arguments to accept C4::Dates object |
606 |
$month=$month+0; |
607 |
$year=$year+0; |
608 |
$day=$day+0; |
609 |
my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7; |
610 |
my $weekDays = $self->get_week_days_holidays(); |
611 |
my $dayMonths = $self->get_day_month_holidays(); |
612 |
my $exceptions = $self->get_exception_holidays(); |
613 |
my $singles = $self->get_single_holidays(); |
614 |
if (defined($exceptions->{"$year/$month/$day"})) { |
615 |
return 0; |
616 |
} else { |
617 |
if ((exists($weekDays->{$weekday})) || |
618 |
(exists($dayMonths->{"$month/$day"})) || |
619 |
(exists($singles->{"$year/$month/$day"}))) { |
620 |
return 1; |
621 |
} else { |
622 |
return 0; |
623 |
} |
624 |
} |
625 |
|
217 |
|
626 |
} |
218 |
DelRepeatingEvent( $branchcode, $weekday, $month, $day ) |
627 |
|
219 |
|
628 |
=head2 copy_to_branch |
220 |
Deletes a weekly- or yearly-repeating event. Either $weekday, or $month and |
629 |
|
221 |
$day should be set, for a weekly or yearly event, respectively. |
630 |
$calendar->copy_to_branch($target_branch) |
|
|
631 |
|
222 |
|
632 |
=cut |
223 |
=cut |
633 |
|
224 |
|
634 |
sub copy_to_branch { |
225 |
sub DelRepeatingEvent { |
635 |
my ($self, $target_branch) = @_; |
226 |
my ( $branchcode, $weekday, $month, $day ) = @_; |
636 |
|
|
|
637 |
croak "No target_branch" unless $target_branch; |
638 |
|
227 |
|
639 |
my $target_calendar = C4::Calendar->new(branchcode => $target_branch); |
228 |
C4::Context->dbh->do( q{ |
640 |
|
229 |
DELETE FROM repeatable_holidays |
641 |
my ($y, $m, $d) = Today(); |
230 |
WHERE branchcode = ? |
642 |
my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d; |
231 |
} . _get_compare( 'weekday', $weekday ) . _get_compare( 'month', $month ) . _get_compare( 'day', $day ), {}, $branchcode, $weekday, $month, $day ); |
643 |
|
|
|
644 |
my $wdh = $self->get_week_days_holidays; |
645 |
$target_calendar->insert_week_day_holiday( weekday => $_, %{ $wdh->{$_} } ) |
646 |
foreach keys %$wdh; |
647 |
$target_calendar->insert_day_month_holiday(%$_) |
648 |
foreach values %{ $self->get_day_month_holidays }; |
649 |
$target_calendar->insert_exception_holiday(%$_) |
650 |
foreach grep { $_->{date} gt $today } values %{ $self->get_exception_holidays }; |
651 |
$target_calendar->insert_single_holiday(%$_) |
652 |
foreach grep { $_->{date} gt $today } values %{ $self->get_single_holidays }; |
653 |
|
654 |
return 1; |
655 |
} |
232 |
} |
656 |
|
233 |
|
657 |
=head2 addDate |
234 |
=head2 CopyAllEvents |
658 |
|
235 |
|
659 |
my ($day, $month, $year) = $calendar->addDate($date, $offset) |
236 |
CopyAllEvents( $from_branchcode, $to_branchcode ) |
660 |
|
237 |
|
661 |
C<$date> is a C4::Dates object representing the starting date of the interval. |
238 |
Copies all events from one branch to another. |
662 |
|
|
|
663 |
C<$offset> Is the number of days that this function has to count from $date. |
664 |
|
239 |
|
665 |
=cut |
240 |
=cut |
666 |
|
241 |
|
667 |
sub addDate { |
242 |
sub CopyAllEvents { |
668 |
my ($self, $startdate, $offset) = @_; |
243 |
my ( $from_branchcode, $to_branchcode ) = @_; |
669 |
my ($year,$month,$day) = split("-",$startdate->output('iso')); |
244 |
|
670 |
my $daystep = 1; |
245 |
C4::Context->dbh->do( q{ |
671 |
if ($offset < 0) { # In case $offset is negative |
246 |
INSERT IGNORE INTO special_holidays(branchcode, year, month, day, isexception, title, description) |
672 |
# $offset = $offset*(-1); |
247 |
SELECT ?, year, month, day, isexception, title, description |
673 |
$daystep = -1; |
248 |
FROM special_holidays |
674 |
} |
249 |
WHERE branchcode = ? |
675 |
my $daysMode = C4::Context->preference('useDaysMode'); |
250 |
}, {}, $to_branchcode, $from_branchcode ); |
676 |
if ($daysMode eq 'Datedue') { |
251 |
|
677 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); |
252 |
C4::Context->dbh->do( q{ |
678 |
while ($self->isHoliday($day, $month, $year)) { |
253 |
INSERT IGNORE INTO repeatable_holidays(branchcode, weekday, month, day, title, description) |
679 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep); |
254 |
SELECT ?, weekday, month, day, title, description |
680 |
} |
255 |
FROM repeatable_holidays |
681 |
} elsif($daysMode eq 'Calendar') { |
256 |
WHERE branchcode = ? |
682 |
while ($offset != 0) { |
257 |
}, {}, $to_branchcode, $from_branchcode ); |
683 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep); |
|
|
684 |
if (!($self->isHoliday($day, $month, $year))) { |
685 |
$offset = $offset - $daystep; |
686 |
} |
687 |
} |
688 |
} else { ## ($daysMode eq 'Days') |
689 |
($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); |
690 |
} |
691 |
return(C4::Dates->new( sprintf(ISO_DATE_FORMAT,$year,$month,$day),'iso')); |
692 |
} |
258 |
} |
693 |
|
259 |
|
694 |
=head2 daysBetween |
|
|
695 |
|
696 |
my $daysBetween = $calendar->daysBetween($startdate, $enddate) |
697 |
|
698 |
C<$startdate> and C<$enddate> are C4::Dates objects that define the interval. |
699 |
|
700 |
Returns the number of non-holiday days in the interval. |
701 |
useDaysMode syspref has no effect here. |
702 |
=cut |
703 |
|
704 |
sub daysBetween { |
705 |
my $self = shift or return; |
706 |
my $startdate = shift or return; |
707 |
my $enddate = shift or return; |
708 |
my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso')); |
709 |
my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso')); |
710 |
if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) { |
711 |
return 0; |
712 |
# we don't go backwards ( FIXME - handle this error better ) |
713 |
} |
714 |
my $count = 0; |
715 |
while (1) { |
716 |
($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day |
717 |
unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) { |
718 |
$count++; |
719 |
} |
720 |
($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1); |
721 |
} |
722 |
return($count); |
723 |
} |
724 |
|
260 |
|
725 |
1; |
261 |
1; |
726 |
|
262 |
|
Lines 729-733
__END__
Link Here
|
729 |
=head1 AUTHOR |
265 |
=head1 AUTHOR |
730 |
|
266 |
|
731 |
Koha Physics Library UNLP <matias_veleda@hotmail.com> |
267 |
Koha Physics Library UNLP <matias_veleda@hotmail.com> |
|
|
268 |
Jesse Weaver <jweaver@bywatersolutions.com> |
732 |
|
269 |
|
733 |
=cut |
270 |
=cut |