1 package C4::Calendar::Calendar;
3 # This file is part of Koha.
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
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.
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
20 use vars qw($VERSION @EXPORT);
26 # set the version for version checking
31 C4::Calendar::Calendar - Koha module dealing with holidays.
35 use C4::Calendar::Calendar;
39 This package is used to deal with holidays. Through this package, you can set all kind of holidays for the library.
49 &get_week_days_holidays
50 &get_day_month_holidays
51 &get_exception_holidays
53 &insert_week_day_holiday
54 &insert_day_month_holiday
55 &insert_single_holiday
56 &insert_exception_holiday
64 $calendar = C4::Calendar::Calendar->new(branchcode => $branchcode);
66 C<$branchcode> Is the branch code wich you want to use calendar.
71 my $classname = shift @_;
75 my $self = bless(\%hash, $classname);
77 foreach my $optionName (keys %options) {
78 $self->{lc($optionName)} = $options{$optionName};
89 my $dbh = C4::Context->dbh();
90 my $week_days_sql = $dbh->prepare("select weekday, title, description from repeatable_holidays where ('$self->{branchcode}' = branchcode) and (NOT(ISNULL(weekday)))");
91 $week_days_sql->execute;
92 my %week_days_holidays;
93 while (my ($weekday, $title, $description) = $week_days_sql->fetchrow) {
94 $week_days_holidays{$weekday}{title} = $title;
95 $week_days_holidays{$weekday}{description} = $description;
97 $week_days_sql->finish;
98 $self->{'week_days_holidays'} = \%week_days_holidays;
100 my $day_month_sql = $dbh->prepare("select day, month, title, description from repeatable_holidays where ('$self->{branchcode}' = branchcode) and ISNULL(weekday)");
101 $day_month_sql->execute;
102 my %day_month_holidays;
103 while (my ($day, $month, $title, $description) = $day_month_sql->fetchrow) {
104 $day_month_holidays{"$month/$day"}{title} = $title;
105 $day_month_holidays{"$month/$day"}{description} = $description;
107 $day_month_sql->finish;
108 $self->{'day_month_holidays'} = \%day_month_holidays;
110 my $exception_holidays_sql = $dbh->prepare("select day, month, year, title, description from special_holidays where ('$self->{branchcode}' = branchcode) and (isexception = 1)");
111 $exception_holidays_sql->execute;
112 my %exception_holidays;
113 while (my ($day, $month, $year, $title, $description) = $exception_holidays_sql->fetchrow) {
114 $exception_holidays{"$year/$month/$day"}{title} = $title;
115 $exception_holidays{"$year/$month/$day"}{description} = $description;
117 $exception_holidays_sql->finish;
118 $self->{'exception_holidays'} = \%exception_holidays;
120 my $holidays_sql = $dbh->prepare("select day, month, year, title, description from special_holidays where ('$self->{branchcode}' = branchcode) and (isexception = 0)");
121 $holidays_sql->execute;
123 while (my ($day, $month, $year, $title, $description) = $holidays_sql->fetchrow) {
124 $single_holidays{"$year/$month/$day"}{title} = $title;
125 $single_holidays{"$year/$month/$day"}{description} = $description;
127 $holidays_sql->finish;
128 $self->{'single_holidays'} = \%single_holidays;
131 =item change_branchcode
133 $calendar->change_branchcode(branchcode => $branchcode)
135 Change the calendar branch code. This means to change the holidays structure.
137 C<$branchcode> Is the branch code wich you want to use calendar.
141 sub change_branchcode {
142 my ($self, $branchcode) = @_;
145 foreach my $optionName (keys %options) {
146 $self->{lc($optionName)} = $options{$optionName};
153 =item get_week_days_holidays
155 $week_days_holidays = $calendar->get_week_days_holidays();
157 Returns a hash reference to week days holidays.
161 sub get_week_days_holidays {
163 my $week_days_holidays = $self->{'week_days_holidays'};
164 return $week_days_holidays;
167 =item get_day_month_holidays
169 $day_month_holidays = $calendar->get_day_month_holidays();
171 Returns a hash reference to day month holidays.
175 sub get_day_month_holidays {
177 my $day_month_holidays = $self->{'day_month_holidays'};
178 return $day_month_holidays;
181 =item get_exception_holidays
183 $exception_holidays = $calendar->exception_holidays();
185 Returns a hash reference to exception holidays. This kind of days are those
186 which stands for a holiday, but you wanted to make an exception for this particular
191 sub get_exception_holidays {
193 my $exception_holidays = $self->{'exception_holidays'};
194 return $exception_holidays;
197 =item get_single_holidays
199 $single_holidays = $calendar->get_single_holidays();
201 Returns a hash reference to single holidays. This kind of holidays are those which
202 happend just one time.
206 sub get_single_holidays {
208 my $single_holidays = $self->{'single_holidays'};
209 return $single_holidays;
212 =item insert_week_day_holiday
214 insert_week_day_holiday(weekday => $weekday,
216 description => $description);
218 Inserts a new week day for $self->{branchcode}.
220 C<$day> Is the week day to make holiday.
222 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
224 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
228 sub insert_week_day_holiday {
232 my $dbh = C4::Context->dbh();
233 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', '$self->{branchcode}', $options{weekday}, NULL, NULL, '$options{title}', '$options{description}')");
234 $insertHoliday->execute;
235 $insertHoliday->finish;
237 $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};
238 $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};
242 =item insert_day_month_holiday
244 insert_day_month_holiday(day => $day,
247 description => $description);
249 Inserts a new day month holiday for $self->{branchcode}.
251 C<$day> Is the day month to make the date to insert.
253 C<$month> Is month to make the date to insert.
255 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
257 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
261 sub insert_day_month_holiday {
265 my $dbh = C4::Context->dbh();
266 my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ('', '$self->{branchcode}', NULL, $options{day}, $options{month}, '$options{title}', '$options{description}')");
267 $insertHoliday->execute;
268 $insertHoliday->finish;
270 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};
271 $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};
275 =item insert_single_holiday
277 insert_single_holiday(day => $day,
281 description => $description);
283 Inserts a new single holiday for $self->{branchcode}.
285 C<$day> Is the day month to make the date to insert.
287 C<$month> Is month to make the date to insert.
289 C<$year> Is year to make the date to insert.
291 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
293 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
297 sub insert_single_holiday {
301 my $dbh = C4::Context->dbh();
303 my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', '$self->{branchcode}', $options{day}, $options{month}, $options{year}, $isexception, '$options{title}', '$options{description}')");
304 $insertHoliday->execute;
305 $insertHoliday->finish;
307 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
308 $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
312 =item insert_exception_holiday
314 insert_exception_holiday(day => $day,
318 description => $description);
320 Inserts a new exception holiday for $self->{branchcode}.
322 C<$day> Is the day month to make the date to insert.
324 C<$month> Is month to make the date to insert.
326 C<$year> Is year to make the date to insert.
328 C<$title> Is the title to store for the holiday formed by $year/$month/$day.
330 C<$description> Is the description to store for the holiday formed by $year/$month/$day.
334 sub insert_exception_holiday {
338 my $dbh = C4::Context->dbh();
340 my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', '$self->{branchcode}', $options{day}, $options{month}, $options{year}, $isexception, '$options{title}', '$options{description}')");
341 $insertException->execute;
342 $insertException->finish;
344 $self->{'exceptions_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};
345 $self->{'exceptions_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};
351 delete_holiday(weekday => $weekday
356 Delete a holiday for $self->{branchcode}.
358 C<$weekday> Is the week day to delete.
360 C<$day> Is the day month to make the date to delete.
362 C<$month> Is month to make the date to delete.
364 C<$year> Is year to make the date to delete.
372 # Verify what kind of holiday that day is. For example, if it is
373 # a repeatable holiday, this should check if there are some exception
374 # for that holiday rule. Otherwise, if it is a regular holiday, it´s
375 # ok just deleting it.
377 my $dbh = C4::Context->dbh();
378 my $isSingleHoliday = $dbh->prepare("select id from special_holidays where (branchcode = '$self->{branchcode}') and (day = $options{day}) and (month = $options{month}) and (year = $options{year})");
379 $isSingleHoliday->execute;
380 if ($isSingleHoliday->rows) {
381 my $id = $isSingleHoliday->fetchrow;
382 $isSingleHoliday->finish; # Close the last query
384 my $deleteHoliday = $dbh->prepare("delete from special_holidays where (id = $id)");
385 $deleteHoliday->execute;
386 $deleteHoliday->finish; # Close the last query
387 delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});
389 $isSingleHoliday->finish; # Close the last query
391 my $isWeekdayHoliday = $dbh->prepare("select id from repeatable_holidays where (branchcode = '$self->{branchcode}') and (weekday = $options{weekday})");
392 $isWeekdayHoliday->execute;
393 if ($isWeekdayHoliday->rows) {
394 my $id = $isWeekdayHoliday->fetchrow;
395 $isWeekdayHoliday->finish; # Close the last query
397 my $updateExceptions = $dbh->prepare("update special_holidays set isexception = 0 where (WEEKDAY(CONCAT(special_holidays.year,'-',special_holidays.month,'-',special_holidays.day)) = $options{weekday}) and (branchcode = '$self->{branchcode}')");
398 $updateExceptions->execute;
399 $updateExceptions->finish; # Close the last query
401 my $deleteHoliday = $dbh->prepare("delete from repeatable_holidays where (id = $id)");
402 $deleteHoliday->execute;
403 $deleteHoliday->finish;
404 delete($self->{'week_days_holidays'}->{$options{weekday}});
406 $isWeekdayHoliday->finish; # Close the last query
408 my $isDayMonthHoliday = $dbh->prepare("select id from repeatable_holidays where (branchcode = '$self->{branchcode}') (day = $options{day}) and (month = $options{month})");
409 $isDayMonthHoliday->execute;
410 if ($isDayMonthHoliday->rows) {
411 my $id = $isDayMonthHoliday->fetchrow;
412 $isDayMonthHoliday->finish;
413 my $updateExceptions = $dbh->prepare("update special_holidays set isexception = 0 where (special_holidays.branchcode = '$self->{branchcode}') and (special_holidays.day = $options{day}) and (special_holidays.month = $options{month})");
414 $updateExceptions->execute;
415 $updateExceptions->finish; # Close the last query
417 my $deleteHoliday = $dbh->prepare("delete from repeatable_holidays where (id = $id)");
418 $deleteHoliday->execute;
419 $deleteHoliday->finish; # Close the last query
420 $isDayMonthHoliday->finish; # Close the last query
421 delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});
430 $isHoliday = isHoliday($day, $month $year);
433 C<$day> Is the day to check wether if is a holiday or not.
435 C<$month> Is the month to check wether its a holiday or not.
437 C<$year> Is the year to check wether if its a holiday or not.
442 my ($self, $day, $month, $year) = @_;
444 my $weekday = Date_DayOfWeek($month, $day, $year) % 7;
445 my $weekDays = $self->get_week_days_holidays();
446 my $dayMonths = $self->get_day_month_holidays();
447 my $exceptions = $self->get_exception_holidays();
448 my $singles = $self->get_single_holidays();
450 if (defined($exceptions->{"$year/$month/$day"})) {
453 if ((exists($weekDays->{$weekday})) ||
454 (exists($dayMonths->{"$month/$day"})) ||
455 (exists($singles->{"$year/$month/$day"}))) {
466 my ($day, $month, $year) = $calendar->addDate($day, $month, $year, $offset)
468 C<$day> Is the starting day of the interval.
470 C<$month> Is the starting month of the interval.
472 C<$year> Is the starting year of the interval.
474 C<$offset> Is the number of days that this function has to count from $date.
479 my ($self, $day, $month, $year, $offset) = @_;
480 if ($offset < 0) { # In case $offset is negative
481 $offset = $offset*(-1);
484 my $daysMode = C4::Context->preference('useDaysMode');
485 if ($daysMode eq 'normal') {
486 ($year, $month, $day) = Add_Delta_Days($year, $month, $day, ($offset - 1));
488 while ($offset > 0) {
489 if (!($self->isHoliday($day, $month, $year))) {
490 $offset = $offset - 1;
493 ($year, $month, $day) = Add_Delta_Days($year, $month, $day, 1);
497 return($day, $month, $year);
502 my $daysBetween = $calendar->daysBetween($dayFrom, $monthFrom, $yearFrom,
503 $dayTo, $monthTo, $yearTo)
505 C<$dayFrom> Is the starting day of the interval.
507 C<$monthFrom> Is the starting month of the interval.
509 C<$yearFrom> Is the starting year of the interval.
511 C<$dayTo> Is the ending day of the interval.
513 C<$monthTo> Is the ending month of the interval.
515 C<$yearTo> Is the ending year of the interval.
520 my ($self, $dayFrom, $monthFrom, $yearFrom, $dayTo, $monthTo, $yearTo) = @_;
522 my $daysMode = C4::Context->preference('useDaysMode');
525 if ($daysMode eq 'normal') {
527 if (($yearFrom != $yearTo) || ($monthFrom != $monthTo) || ($dayFrom != $dayTo)) {
528 ($yearFrom, $monthFrom, $dayFrom) = Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
536 if (($yearFrom != $yearTo) || ($monthFrom != $monthTo) || ($dayFrom != $dayTo)) {
537 if (!($self->isHoliday($dayFrom, $monthFrom, $yearFrom))) {
540 ($yearFrom, $monthFrom, $dayFrom) = Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);
550 my ($month, $day, $year)=@_;
551 my $date=$year."-".$month."-".$day;
552 my $dbh=C4::Context->dbh;
553 my $sth=$dbh->prepare("SELECT DAYOFWEEK(?)");
554 $sth->execute($date);
555 my $dayofweek=$sth->fetchrow;
560 my ($year, $month, $day, $offset)=@_;
561 my $date=$year."-".$month."-".$day;
562 my $dbh=C4::Context->dbh;
563 my $sth=$dbh->prepare(" SELECT DATE_ADD(?, INTERVAL ? DAY)");
564 $sth->execute($date,$offset);
565 $date=$sth->fetchrow;
566 ($year, $month, $day)=split /-/,$date;
567 return ($year, $month, $day);
580 Koha Physics Library UNLP <matias_veleda@hotmail.com>
581 Modified by Tumer Garip NUE Grand Library --No more Date::Manip