Bug 6627 [Signed Off] - Update to improve security in specified log files - bug 6627
[koha.git] / C4 / Calendar.pm
1 package C4::Calendar;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19 use warnings;
20 use vars qw($VERSION @EXPORT);
21
22 use Carp;
23 use Date::Calc qw( Date_to_Days );
24
25 use C4::Context;
26
27 BEGIN {
28     # set the version for version checking
29     $VERSION = 3.01;
30     require Exporter;
31     @EXPORT = qw(
32         &get_week_days_holidays
33         &get_day_month_holidays
34         &get_exception_holidays 
35         &get_single_holidays
36         &insert_week_day_holiday
37         &insert_day_month_holiday
38         &insert_single_holiday
39         &insert_exception_holiday
40         &ModWeekdayholiday
41         &ModDaymonthholiday
42         &ModSingleholiday
43         &ModExceptionholiday
44         &delete_holiday
45         &isHoliday
46         &addDate
47         &daysBetween
48     );
49 }
50
51 =head1 NAME
52
53 C4::Calendar::Calendar - Koha module dealing with holidays.
54
55 =head1 SYNOPSIS
56
57     use C4::Calendar::Calendar;
58
59 =head1 DESCRIPTION
60
61 This package is used to deal with holidays. Through this package, you can set 
62 all kind of holidays for the library.
63
64 =head1 FUNCTIONS
65
66 =head2 new
67
68   $calendar = C4::Calendar->new(branchcode => $branchcode);
69
70 Each library branch has its own Calendar.  
71 C<$branchcode> specifies which Calendar you want.
72
73 =cut
74
75 sub new {
76     my $classname = shift @_;
77     my %options = @_;
78     my $self = bless({}, $classname);
79     foreach my $optionName (keys %options) {
80         $self->{lc($optionName)} = $options{$optionName};
81     }
82     defined($self->{branchcode}) or croak "No branchcode argument to new.  Should be C4::Calendar->new(branchcode => \$branchcode)";
83     $self->_init($self->{branchcode});
84     return $self;
85 }
86
87 sub _init {
88     my $self = shift @_;
89     my $branch = shift;
90     defined($branch) or die "No branchcode sent to _init";  # must test for defined here and above to allow ""
91     my $dbh = C4::Context->dbh();
92     my $repeatable = $dbh->prepare( 'SELECT *
93                                        FROM repeatable_holidays
94                                       WHERE ( branchcode = ? )
95                                         AND (ISNULL(weekday) = ?)' );
96     $repeatable->execute($branch,0);
97     my %week_days_holidays;
98     while (my $row = $repeatable->fetchrow_hashref) {
99         my $key = $row->{weekday};
100         $week_days_holidays{$key}{title}       = $row->{title};
101         $week_days_holidays{$key}{description} = $row->{description};
102     }
103     $self->{'week_days_holidays'} = \%week_days_holidays;
104
105     $repeatable->execute($branch,1);
106     my %day_month_holidays;
107     while (my $row = $repeatable->fetchrow_hashref) {
108         my $key = $row->{month} . "/" . $row->{day};
109         $day_month_holidays{$key}{title}       = $row->{title};
110         $day_month_holidays{$key}{description} = $row->{description};
111         $day_month_holidays{$key}{day} = sprintf("%02d", $row->{day});
112         $day_month_holidays{$key}{month} = sprintf("%02d", $row->{month});
113     }
114     $self->{'day_month_holidays'} = \%day_month_holidays;
115
116     my $special = $dbh->prepare( 'SELECT day, month, year, title, description
117                                     FROM special_holidays
118                                    WHERE ( branchcode = ? )
119                                      AND (isexception = ?)' );
120     $special->execute($branch,1);
121     my %exception_holidays;
122     while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
123         $exception_holidays{"$year/$month/$day"}{title} = $title;
124         $exception_holidays{"$year/$month/$day"}{description} = $description;
125         $exception_holidays{"$year/$month/$day"}{date} = 
126                 sprintf("%04d-%02d-%02d", $year, $month, $day);
127     }
128     $self->{'exception_holidays'} = \%exception_holidays;
129
130     $special->execute($branch,0);
131     my %single_holidays;
132     while (my ($day, $month, $year, $title, $description) = $special->fetchrow) {
133         $single_holidays{"$year/$month/$day"}{title} = $title;
134         $single_holidays{"$year/$month/$day"}{description} = $description;
135         $single_holidays{"$year/$month/$day"}{date} = 
136                 sprintf("%04d-%02d-%02d", $year, $month, $day);
137     }
138     $self->{'single_holidays'} = \%single_holidays;
139     return $self;
140 }
141
142 =head2 get_week_days_holidays
143
144    $week_days_holidays = $calendar->get_week_days_holidays();
145
146 Returns a hash reference to week days holidays.
147
148 =cut
149
150 sub get_week_days_holidays {
151     my $self = shift @_;
152     my $week_days_holidays = $self->{'week_days_holidays'};
153     return $week_days_holidays;
154 }
155
156 =head2 get_day_month_holidays
157
158    $day_month_holidays = $calendar->get_day_month_holidays();
159
160 Returns a hash reference to day month holidays.
161
162 =cut
163
164 sub get_day_month_holidays {
165     my $self = shift @_;
166     my $day_month_holidays = $self->{'day_month_holidays'};
167     return $day_month_holidays;
168 }
169
170 =head2 get_exception_holidays
171
172     $exception_holidays = $calendar->exception_holidays();
173
174 Returns a hash reference to exception holidays. This kind of days are those
175 which stands for a holiday, but you wanted to make an exception for this particular
176 date.
177
178 =cut
179
180 sub get_exception_holidays {
181     my $self = shift @_;
182     my $exception_holidays = $self->{'exception_holidays'};
183     return $exception_holidays;
184 }
185
186 =head2 get_single_holidays
187
188     $single_holidays = $calendar->get_single_holidays();
189
190 Returns a hash reference to single holidays. This kind of holidays are those which
191 happend just one time.
192
193 =cut
194
195 sub get_single_holidays {
196     my $self = shift @_;
197     my $single_holidays = $self->{'single_holidays'};
198     return $single_holidays;
199 }
200
201 =head2 insert_week_day_holiday
202
203     insert_week_day_holiday(weekday => $weekday,
204                             title => $title,
205                             description => $description);
206
207 Inserts a new week day for $self->{branchcode}.
208
209 C<$day> Is the week day to make holiday.
210
211 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
212
213 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
214
215 =cut
216
217 sub insert_week_day_holiday {
218     my $self = shift @_;
219     my %options = @_;
220
221     my $dbh = C4::Context->dbh();
222     my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )"); 
223         $insertHoliday->execute( $self->{branchcode}, $options{weekday},$options{title}, $options{description});
224     $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
225     $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
226     return $self;
227 }
228
229 =head2 insert_day_month_holiday
230
231     insert_day_month_holiday(day => $day,
232                              month => $month,
233                              title => $title,
234                              description => $description);
235
236 Inserts a new day month holiday for $self->{branchcode}.
237
238 C<$day> Is the day month to make the date to insert.
239
240 C<$month> Is month to make the date to insert.
241
242 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
243
244 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
245
246 =cut
247
248 sub insert_day_month_holiday {
249     my $self = shift @_;
250     my %options = @_;
251
252     my $dbh = C4::Context->dbh();
253     my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', ?, NULL, ?, ?, ?,? )");
254         $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{title}, $options{description});
255     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
256     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
257     return $self;
258 }
259
260 =head2 insert_single_holiday
261
262     insert_single_holiday(day => $day,
263                           month => $month,
264                           year => $year,
265                           title => $title,
266                           description => $description);
267
268 Inserts a new single holiday for $self->{branchcode}.
269
270 C<$day> Is the day month to make the date to insert.
271
272 C<$month> Is month to make the date to insert.
273
274 C<$year> Is year to make the date to insert.
275
276 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
277
278 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
279
280 =cut
281
282 sub insert_single_holiday {
283     my $self = shift @_;
284     my %options = @_;
285     
286         my $dbh = C4::Context->dbh();
287     my $isexception = 0;
288     my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
289         $insertHoliday->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
290     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
291     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
292     return $self;
293 }
294
295 =head2 insert_exception_holiday
296
297     insert_exception_holiday(day => $day,
298                              month => $month,
299                              year => $year,
300                              title => $title,
301                              description => $description);
302
303 Inserts a new exception holiday for $self->{branchcode}.
304
305 C<$day> Is the day month to make the date to insert.
306
307 C<$month> Is month to make the date to insert.
308
309 C<$year> Is year to make the date to insert.
310
311 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
312
313 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
314
315 =cut
316
317 sub insert_exception_holiday {
318     my $self = shift @_;
319     my %options = @_;
320
321     my $dbh = C4::Context->dbh();
322     my $isexception = 1;
323     my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)");
324         $insertException->execute( $self->{branchcode}, $options{day},$options{month},$options{year}, $isexception, $options{title}, $options{description});
325     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
326     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
327     return $self;
328 }
329
330 =head2 ModWeekdayholiday
331
332     ModWeekdayholiday(weekday =>$weekday,
333                       title => $title,
334                       description => $description)
335
336 Modifies the title and description of a weekday for $self->{branchcode}.
337
338 C<$weekday> Is the title to update for the holiday.
339
340 C<$description> Is the description to update for the holiday.
341
342 =cut
343
344 sub ModWeekdayholiday {
345     my $self = shift @_;
346     my %options = @_;
347
348     my $dbh = C4::Context->dbh();
349     my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE branchcode = ? AND weekday = ?");
350     $updateHoliday->execute( $options{title},$options{description},$self->{branchcode},$options{weekday}); 
351     $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
352     $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
353     return $self;
354 }
355
356 =head2 ModDaymonthholiday
357
358     ModDaymonthholiday(day => $day,
359                        month => $month,
360                        title => $title,
361                        description => $description);
362
363 Modifies the title and description for a day/month holiday for $self->{branchcode}.
364
365 C<$day> The day of the month for the update.
366
367 C<$month> The month to be used for the update.
368
369 C<$title> The title to be updated for the holiday.
370
371 C<$description> The description to be update for the holiday.
372
373 =cut
374
375 sub ModDaymonthholiday {
376     my $self = shift @_;
377     my %options = @_;
378
379     my $dbh = C4::Context->dbh();
380     my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE month = ? AND day = ? AND branchcode = ?");
381        $updateHoliday->execute( $options{title},$options{description},$options{month},$options{day},$self->{branchcode}); 
382     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
383     $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
384     return $self;
385 }
386
387 =head2 ModSingleholiday
388
389     ModSingleholiday(day => $day,
390                      month => $month,
391                      year => $year,
392                      title => $title,
393                      description => $description);
394
395 Modifies the title and description for a single holiday for $self->{branchcode}.
396
397 C<$day> Is the day of the month to make the update.
398
399 C<$month> Is the month to make the update.
400
401 C<$year> Is the year to make the update.
402
403 C<$title> Is the title to update for the holiday formed by $year/$month/$day.
404
405 C<$description> Is the description to update for the holiday formed by $year/$month/$day.
406
407 =cut
408
409 sub ModSingleholiday {
410     my $self = shift @_;
411     my %options = @_;
412
413     my $dbh = C4::Context->dbh();
414     my $isexception = 0;
415     my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
416       $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception);    
417     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
418     $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
419     return $self;
420 }
421
422 =head2 ModExceptionholiday
423
424     ModExceptionholiday(day => $day,
425                         month => $month,
426                         year => $year,
427                         title => $title,
428                         description => $description);
429
430 Modifies the title and description for an exception holiday for $self->{branchcode}.
431
432 C<$day> Is the day of the month for the holiday.
433
434 C<$month> Is the month for the holiday.
435
436 C<$year> Is the year for the holiday.
437
438 C<$title> Is the title to be modified for the holiday formed by $year/$month/$day.
439
440 C<$description> Is the description to be modified for the holiday formed by $year/$month/$day.
441
442 =cut
443
444 sub ModExceptionholiday {
445     my $self = shift @_;
446     my %options = @_;
447
448     my $dbh = C4::Context->dbh();
449     my $isexception = 1;
450     my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?");
451     $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception);    
452     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
453     $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
454     return $self;
455 }
456
457 =head2 delete_holiday
458
459     delete_holiday(weekday => $weekday
460                    day => $day,
461                    month => $month,
462                    year => $year);
463
464 Delete a holiday for $self->{branchcode}.
465
466 C<$weekday> Is the week day to delete.
467
468 C<$day> Is the day month to make the date to delete.
469
470 C<$month> Is month to make the date to delete.
471
472 C<$year> Is year to make the date to delete.
473
474 =cut
475
476 sub delete_holiday {
477     my $self = shift @_;
478     my %options = @_;
479
480     # Verify what kind of holiday that day is. For example, if it is
481     # a repeatable holiday, this should check if there are some exception
482         # for that holiday rule. Otherwise, if it is a regular holiday, it´s 
483     # ok just deleting it.
484
485     my $dbh = C4::Context->dbh();
486     my $isSingleHoliday = $dbh->prepare("SELECT id FROM special_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?) AND (year = ?)");
487     $isSingleHoliday->execute($self->{branchcode}, $options{day}, $options{month}, $options{year});
488     if ($isSingleHoliday->rows) {
489         my $id = $isSingleHoliday->fetchrow;
490         $isSingleHoliday->finish; # Close the last query
491
492         my $deleteHoliday = $dbh->prepare("DELETE FROM special_holidays WHERE id = ?");
493         $deleteHoliday->execute($id);
494         delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
495     } else {
496         $isSingleHoliday->finish; # Close the last query
497
498         my $isWeekdayHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE branchcode = ? AND weekday = ?");
499         $isWeekdayHoliday->execute($self->{branchcode}, $options{weekday});
500         if ($isWeekdayHoliday->rows) {
501             my $id = $isWeekdayHoliday->fetchrow;
502             $isWeekdayHoliday->finish; # Close the last query
503
504             my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = ?) AND (branchcode = ?)");
505             $updateExceptions->execute($options{weekday}, $self->{branchcode});
506             $updateExceptions->finish; # Close the last query
507
508             my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE id = ?");
509             $deleteHoliday->execute($id);
510             delete($self->{'week_days_holidays'}->{$options{weekday}});
511         } else {
512             $isWeekdayHoliday->finish; # Close the last query
513
514             my $isDayMonthHoliday = $dbh->prepare("SELECT id FROM repeatable_holidays WHERE (branchcode = ?) AND (day = ?) AND (month = ?)");
515             $isDayMonthHoliday->execute($self->{branchcode}, $options{day}, $options{month});
516             if ($isDayMonthHoliday->rows) {
517                 my $id = $isDayMonthHoliday->fetchrow;
518                 $isDayMonthHoliday->finish;
519                 my $updateExceptions = $dbh->prepare("UPDATE special_holidays SET isexception = 0 WHERE (special_holidays.branchcode = ?) AND (special_holidays.day = ?) and (special_holidays.month = ?)");
520                 $updateExceptions->execute($self->{branchcode}, $options{day}, $options{month});
521                 $updateExceptions->finish; # Close the last query
522
523                 my $deleteHoliday = $dbh->prepare("DELETE FROM repeatable_holidays WHERE (id = ?)");
524                 $deleteHoliday->execute($id);
525                 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
526             }
527         }
528     }
529     return $self;
530 }
531
532 =head2 isHoliday
533
534     $isHoliday = isHoliday($day, $month $year);
535
536 C<$day> Is the day to check whether if is a holiday or not.
537
538 C<$month> Is the month to check whether if is a holiday or not.
539
540 C<$year> Is the year to check whether if is a holiday or not.
541
542 =cut
543
544 sub isHoliday {
545     my ($self, $day, $month, $year) = @_;
546         # FIXME - date strings are stored in non-padded metric format. should change to iso.
547         # FIXME - should change arguments to accept C4::Dates object
548         $month=$month+0;
549         $year=$year+0;
550         $day=$day+0;
551     my $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7; 
552     my $weekDays   = $self->get_week_days_holidays();
553     my $dayMonths  = $self->get_day_month_holidays();
554     my $exceptions = $self->get_exception_holidays();
555     my $singles    = $self->get_single_holidays();
556     if (defined($exceptions->{"$year/$month/$day"})) {
557         return 0;
558     } else {
559         if ((exists($weekDays->{$weekday})) ||
560             (exists($dayMonths->{"$month/$day"})) ||
561             (exists($singles->{"$year/$month/$day"}))) {
562                         return 1;
563         } else {
564             return 0;
565         }
566     }
567
568 }
569
570 =head2 addDate
571
572     my ($day, $month, $year) = $calendar->addDate($date, $offset)
573
574 C<$date> is a C4::Dates object representing the starting date of the interval.
575
576 C<$offset> Is the number of days that this function has to count from $date.
577
578 =cut
579
580 sub addDate {
581     my ($self, $startdate, $offset) = @_;
582     my ($year,$month,$day) = split("-",$startdate->output('iso'));
583         my $daystep = 1;
584         if ($offset < 0) { # In case $offset is negative
585        # $offset = $offset*(-1);
586                 $daystep = -1;
587     }
588         my $daysMode = C4::Context->preference('useDaysMode');
589     if ($daysMode eq 'Datedue') {
590         ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
591                 while ($self->isHoliday($day, $month, $year)) {
592             ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
593         }
594     } elsif($daysMode eq 'Calendar') {
595         while ($offset !=  0) {
596             ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $daystep);
597             if (!($self->isHoliday($day, $month, $year))) {
598                 $offset = $offset - $daystep;
599                         }
600         }
601         } else { ## ($daysMode eq 'Days') 
602         ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset );
603     }
604     return(C4::Dates->new( sprintf("%04d-%02d-%02d",$year,$month,$day),'iso'));
605 }
606
607 =head2 daysBetween
608
609     my $daysBetween = $calendar->daysBetween($startdate, $enddate)
610
611 C<$startdate> and C<$enddate> are C4::Dates objects that define the interval.
612
613 Returns the number of non-holiday days in the interval.
614 useDaysMode syspref has no effect here.
615 =cut
616
617 sub daysBetween ($$$) {
618     my $self      = shift or return undef;
619     my $startdate = shift or return undef;
620     my $enddate   = shift or return undef;
621         my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso'));
622         my ($yearTo,  $monthTo,  $dayTo  ) = split("-",  $enddate->output('iso'));
623         if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) {
624                 return 0;
625                 # we don't go backwards  ( FIXME - handle this error better )
626         }
627     my $count = 0;
628     while (1) {
629         ($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day
630         unless ($self->isHoliday($dayFrom, $monthFrom, $yearFrom)) {
631             $count++;
632         }
633         ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
634     }
635     return($count);
636 }
637
638 1;
639
640 __END__
641
642 =head1 AUTHOR
643
644 Koha Physics Library UNLP <matias_veleda@hotmail.com>
645
646 =cut