commit for holidays and news management.
authortipaul <tipaul>
Mon, 6 Mar 2006 12:43:36 +0000 (12:43 +0000)
committertipaul <tipaul>
Mon, 6 Mar 2006 12:43:36 +0000 (12:43 +0000)
(some forgotten files)

C4/Calendar.pm [new file with mode: 0644]
C4/NewsChannels.pm [new file with mode: 0644]

diff --git a/C4/Calendar.pm b/C4/Calendar.pm
new file mode 100644 (file)
index 0000000..73fe549
--- /dev/null
@@ -0,0 +1,562 @@
+package C4::Calendar;\r
+\r
+# This file is part of Koha.\r
+#\r
+# Koha is free software; you can redistribute it and/or modify it under the\r
+# terms of the GNU General Public License as published by the Free Software\r
+# Foundation; either version 2 of the License, or (at your option) any later\r
+# version.\r
+#\r
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY\r
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR\r
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.\r
+#\r
+# You should have received a copy of the GNU General Public License along with\r
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,\r
+# Suite 330, Boston, MA  02111-1307 USA\r
+\r
+use strict;\r
+require Exporter;\r
+use vars qw($VERSION @EXPORT);\r
+\r
+use C4::Database;\r
+use Date::Manip;\r
+# use Date::Calc;\r
+\r
+# set the version for version checking\r
+$VERSION = 0.01;\r
+\r
+=head1 NAME\r
+\r
+C4::Calendar::Calendar - Koha module dealing with holidays.\r
+\r
+=head1 SYNOPSIS\r
+\r
+       use C4::Calendar::Calendar;\r
+\r
+=head1 DESCRIPTION\r
+\r
+This package is used to deal with holidays. Through this package, you can set all kind of holidays for the library.\r
+\r
+=head1 FUNCTIONS\r
+\r
+=over 2\r
+\r
+=cut\r
+\r
+@EXPORT = qw(&new \r
+             &change_branchcode \r
+                        &get_week_days_holidays \r
+                        &get_day_month_holidays \r
+             &get_exception_holidays \r
+                        &get_single_holidays \r
+                        &insert_week_day_holiday \r
+                        &insert_day_month_holiday \r
+                        &insert_single_holiday \r
+                        &insert_exception_holiday\r
+                        &delete_holiday \r
+                        &isHoliday \r
+                        &addDate\r
+                        &daysBetween);\r
+\r
+=item new\r
+\r
+       $calendar = C4::Calendar::Calendar->new(branchcode => $branchcode);\r
+\r
+C<$branchcode> Is the branch code wich you want to use calendar.\r
+\r
+=cut\r
+\r
+sub new {\r
+       my $classname = shift @_;\r
+       my %options = @_;\r
+\r
+       my %hash;\r
+       my $self = bless(\%hash, $classname);\r
+\r
+       foreach my $optionName (keys %options) {\r
+               $self->{lc($optionName)} = $options{$optionName};\r
+       }\r
+\r
+       $self->_init;\r
+\r
+       return $self;\r
+}\r
+\r
+sub _init {\r
+       my $self = shift @_;\r
+\r
+       my $dbh = C4::Context->dbh();\r
+       my $week_days_sql = $dbh->prepare("select weekday, title, description from repeatable_holidays where ('$self->{branchcode}' = branchcode) and (NOT(ISNULL(weekday)))");\r
+       $week_days_sql->execute;\r
+       my %week_days_holidays;\r
+       while (my ($weekday, $title, $description) = $week_days_sql->fetchrow) {\r
+               $week_days_holidays{$weekday}{title} = $title;\r
+               $week_days_holidays{$weekday}{description} = $description;\r
+       }\r
+       $week_days_sql->finish;\r
+       $self->{'week_days_holidays'} = \%week_days_holidays;\r
+\r
+       my $day_month_sql = $dbh->prepare("select day, month, title, description from repeatable_holidays where ('$self->{branchcode}' = branchcode) and ISNULL(weekday)");\r
+       $day_month_sql->execute;\r
+       my %day_month_holidays;\r
+       while (my ($day, $month, $title, $description) = $day_month_sql->fetchrow) {\r
+               $day_month_holidays{"$month/$day"}{title} = $title;\r
+               $day_month_holidays{"$month/$day"}{description} = $description;\r
+       }\r
+       $day_month_sql->finish;\r
+       $self->{'day_month_holidays'} = \%day_month_holidays;\r
+\r
+       my $exception_holidays_sql = $dbh->prepare("select day, month, year, title, description from special_holidays where ('$self->{branchcode}' = branchcode) and (isexception = 1)");\r
+       $exception_holidays_sql->execute;\r
+       my %exception_holidays;\r
+       while (my ($day, $month, $year, $title, $description) = $exception_holidays_sql->fetchrow) {\r
+               $exception_holidays{"$year/$month/$day"}{title} = $title;\r
+               $exception_holidays{"$year/$month/$day"}{description} = $description;\r
+       }\r
+       $exception_holidays_sql->finish;\r
+       $self->{'exception_holidays'} = \%exception_holidays;\r
+\r
+       my $holidays_sql = $dbh->prepare("select day, month, year, title, description from special_holidays where ('$self->{branchcode}' = branchcode) and (isexception = 0)");\r
+       $holidays_sql->execute;\r
+       my %single_holidays;\r
+       while (my ($day, $month, $year, $title, $description) = $holidays_sql->fetchrow) {\r
+               $single_holidays{"$year/$month/$day"}{title} = $title;\r
+               $single_holidays{"$year/$month/$day"}{description} = $description;\r
+       }\r
+       $holidays_sql->finish;\r
+       $self->{'single_holidays'} = \%single_holidays;\r
+}\r
+\r
+=item change_branchcode\r
+\r
+       $calendar->change_branchcode(branchcode => $branchcode)\r
+\r
+Change the calendar branch code. This means to change the holidays structure.\r
+\r
+C<$branchcode> Is the branch code wich you want to use calendar.\r
+\r
+=cut\r
+\r
+sub change_branchcode {\r
+       my ($self, $branchcode) = @_;\r
+       my %options = @_;\r
+\r
+       foreach my $optionName (keys %options) {\r
+               $self->{lc($optionName)} = $options{$optionName};\r
+       }\r
+       $self->_init;\r
+\r
+       return $self;\r
+}\r
+\r
+=item get_week_days_holidays\r
+\r
+       $week_days_holidays = $calendar->get_week_days_holidays();\r
+\r
+Returns a hash reference to week days holidays.\r
+\r
+=cut\r
+\r
+sub get_week_days_holidays {\r
+       my $self = shift @_;\r
+       my $week_days_holidays = $self->{'week_days_holidays'};\r
+       return $week_days_holidays;\r
+}\r
+\r
+=item get_day_month_holidays\r
+       \r
+       $day_month_holidays = $calendar->get_day_month_holidays();\r
+\r
+Returns a hash reference to day month holidays.\r
+\r
+=cut\r
+\r
+sub get_day_month_holidays {\r
+       my $self = shift @_;\r
+       my $day_month_holidays = $self->{'day_month_holidays'};\r
+       return $day_month_holidays;\r
+}\r
+\r
+=item get_exception_holidays\r
+       \r
+       $exception_holidays = $calendar->exception_holidays();\r
+\r
+Returns a hash reference to exception holidays. This kind of days are those\r
+which stands for a holiday, but you wanted to make an exception for this particular\r
+date.\r
+\r
+=cut\r
+\r
+sub get_exception_holidays {\r
+       my $self = shift @_;\r
+       my $exception_holidays = $self->{'exception_holidays'};\r
+       return $exception_holidays;\r
+}\r
+\r
+=item get_single_holidays\r
+       \r
+       $single_holidays = $calendar->get_single_holidays();\r
+\r
+Returns a hash reference to single holidays. This kind of holidays are those which\r
+happend just one time.\r
+\r
+=cut\r
+\r
+sub get_single_holidays {\r
+       my $self = shift @_;\r
+       my $single_holidays = $self->{'single_holidays'};\r
+       return $single_holidays;\r
+}\r
+\r
+=item insert_week_day_holiday\r
+\r
+       insert_week_day_holiday(weekday => $weekday,\r
+                                                       title => $title,\r
+                                                       description => $description);\r
+\r
+Inserts a new week day for $self->{branchcode}.\r
+\r
+C<$day> Is the week day to make holiday.\r
+\r
+C<$title> Is the title to store for the holiday formed by $year/$month/$day.\r
+\r
+C<$description> Is the description to store for the holiday formed by $year/$month/$day.\r
+\r
+=cut\r
+\r
+sub insert_week_day_holiday {\r
+       my $self = shift @_;\r
+       my %options = @_;\r
+\r
+       my $dbh = C4::Context->dbh();\r
+       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}')");\r
+       $insertHoliday->execute;\r
+       $insertHoliday->finish;\r
+\r
+       $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title};\r
+       $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description};\r
+       return $self;\r
+}\r
+\r
+=item insert_day_month_holiday\r
+\r
+       insert_day_month_holiday(day => $day,\r
+                                month => $month,\r
+                                                        title => $title,\r
+                                                        description => $description);\r
+\r
+Inserts a new day month holiday for $self->{branchcode}.\r
+\r
+C<$day> Is the day month to make the date to insert.\r
+\r
+C<$month> Is month to make the date to insert.\r
+\r
+C<$title> Is the title to store for the holiday formed by $year/$month/$day.\r
+\r
+C<$description> Is the description to store for the holiday formed by $year/$month/$day.\r
+\r
+=cut\r
+\r
+sub insert_day_month_holiday {\r
+       my $self = shift @_;\r
+       my %options = @_;\r
+\r
+       my $dbh = C4::Context->dbh();\r
+       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}')");\r
+       $insertHoliday->execute;\r
+       $insertHoliday->finish;\r
+\r
+       $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title};\r
+       $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description};\r
+       return $self;\r
+}\r
+\r
+=item insert_single_holiday\r
+\r
+       insert_single_holiday(day => $day,\r
+                             month => $month,\r
+                                                 year => $year,\r
+                                                 title => $title,\r
+                                                 description => $description);\r
+\r
+Inserts a new single holiday for $self->{branchcode}.\r
+\r
+C<$day> Is the day month to make the date to insert.\r
+\r
+C<$month> Is month to make the date to insert.\r
+\r
+C<$year> Is year to make the date to insert.\r
+\r
+C<$title> Is the title to store for the holiday formed by $year/$month/$day.\r
+\r
+C<$description> Is the description to store for the holiday formed by $year/$month/$day.\r
+\r
+=cut\r
+\r
+sub insert_single_holiday {\r
+       my $self = shift @_;\r
+       my %options = @_;\r
+\r
+       my $dbh = C4::Context->dbh();\r
+       my $isexception = 0;\r
+       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}')");\r
+       $insertHoliday->execute;\r
+       $insertHoliday->finish;\r
+\r
+       $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};\r
+       $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};\r
+       return $self;\r
+}\r
+\r
+=item insert_exception_holiday\r
+\r
+       insert_exception_holiday(day => $day,\r
+                                month => $month,\r
+                                                    year => $year,\r
+                                                    title => $title,\r
+                                                    description => $description);\r
+\r
+Inserts a new exception holiday for $self->{branchcode}.\r
+\r
+C<$day> Is the day month to make the date to insert.\r
+\r
+C<$month> Is month to make the date to insert.\r
+\r
+C<$year> Is year to make the date to insert.\r
+\r
+C<$title> Is the title to store for the holiday formed by $year/$month/$day.\r
+\r
+C<$description> Is the description to store for the holiday formed by $year/$month/$day.\r
+\r
+=cut\r
+\r
+sub insert_exception_holiday {\r
+       my $self = shift @_;\r
+       my %options = @_;\r
+\r
+       my $dbh = C4::Context->dbh();\r
+       my $isexception = 1;\r
+       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}')");\r
+       $insertException->execute;\r
+       $insertException->finish;\r
+\r
+       $self->{'exceptions_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title};\r
+       $self->{'exceptions_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description};\r
+       return $self;\r
+}\r
+\r
+=item delete_holiday\r
+\r
+       delete_holiday(weekday => $weekday\r
+                      day => $day,\r
+                      month => $month,\r
+                                  year => $year);\r
+\r
+Delete a holiday for $self->{branchcode}.\r
+\r
+C<$weekday> Is the week day to delete.\r
+\r
+C<$day> Is the day month to make the date to delete.\r
+\r
+C<$month> Is month to make the date to delete.\r
+\r
+C<$year> Is year to make the date to delete.\r
+\r
+=cut\r
+\r
+sub delete_holiday {\r
+       my $self = shift @_;\r
+       my %options = @_;\r
+\r
+       # Verify what kind of holiday that day is. For example, if it is\r
+       # a repeatable holiday, this should check if there are some exception\r
+       # for that holiday rule. Otherwise, if it is a regular holiday, it´s \r
+       # ok just deleting it.\r
+\r
+       my $dbh = C4::Context->dbh();\r
+       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})");\r
+       $isSingleHoliday->execute;\r
+       if ($isSingleHoliday->rows) {\r
+               my $id = $isSingleHoliday->fetchrow;\r
+               $isSingleHoliday->finish; # Close the last query\r
+\r
+               my $deleteHoliday = $dbh->prepare("delete from special_holidays where (id = $id)");\r
+               $deleteHoliday->execute;\r
+               $deleteHoliday->finish; # Close the last query\r
+               delete($self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"});\r
+       } else {        \r
+               $isSingleHoliday->finish; # Close the last query\r
+\r
+               my $isWeekdayHoliday = $dbh->prepare("select id from repeatable_holidays where (branchcode = '$self->{branchcode}') and (weekday = $options{weekday})");\r
+               $isWeekdayHoliday->execute;\r
+               if ($isWeekdayHoliday->rows) {\r
+                       my $id = $isWeekdayHoliday->fetchrow;\r
+                       $isWeekdayHoliday->finish; # Close the last query\r
+\r
+                       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}')");\r
+                       $updateExceptions->execute;\r
+                       $updateExceptions->finish; # Close the last query\r
+\r
+                       my $deleteHoliday = $dbh->prepare("delete from repeatable_holidays where (id = $id)");\r
+                       $deleteHoliday->execute;\r
+                       $deleteHoliday->finish;\r
+                       delete($self->{'week_days_holidays'}->{$options{weekday}});\r
+               } else {\r
+                       $isWeekdayHoliday->finish; # Close the last query\r
+\r
+                       my $isDayMonthHoliday = $dbh->prepare("select id from repeatable_holidays where (branchcode = '$self->{branchcode}') (day = $options{day}) and (month = $options{month})");\r
+                       $isDayMonthHoliday->execute;\r
+                       if ($isDayMonthHoliday->rows) {\r
+                               my $id = $isDayMonthHoliday->fetchrow;\r
+                               $isDayMonthHoliday->finish;\r
+                               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})");\r
+                               $updateExceptions->execute;\r
+                               $updateExceptions->finish; # Close the last query\r
+\r
+                               my $deleteHoliday = $dbh->prepare("delete from repeatable_holidays where (id = $id)");\r
+                               $deleteHoliday->execute;\r
+                               $deleteHoliday->finish; # Close the last query\r
+                               $isDayMonthHoliday->finish; # Close the last query\r
+                               delete($self->{'day_month_holidays'}->{"$options{month}/$options{day}"});\r
+                       }\r
+               }\r
+       }       \r
+       return $self;\r
+}\r
+\r
+=item isHoliday\r
+       \r
+       $isHoliday = isHoliday($day, $month $year);\r
+\r
+\r
+C<$day> Is the day to check wether if is a holiday or not.\r
+\r
+C<$month> Is the month to check wether if is a holiday or not.\r
+\r
+C<$year> Is the year to check wether if is a holiday or not.\r
+\r
+=cut\r
+\r
+sub isHoliday {\r
+       my ($self, $day, $month, $year) = @_;\r
+\r
+       my $weekday = Date_DayOfWeek($month, $day, $year) % 7;\r
+       \r
+       my $weekDays = $self->get_week_days_holidays();\r
+       my $dayMonths = $self->get_day_month_holidays();\r
+       my $exceptions = $self->get_exception_holidays();\r
+       my $singles = $self->get_single_holidays();\r
+\r
+       if (defined($exceptions->{"$year/$month/$day"})) {\r
+               return 0;\r
+       } else {                \r
+               if ((exists($weekDays->{$weekday})) || \r
+                       (exists($dayMonths->{"$month/$day"})) || \r
+                       (exists($singles->{"$year/$month/$day"}))) {                    \r
+                       return 1;\r
+               } else {\r
+                       return 0;\r
+               }\r
+       }\r
+\r
+}\r
+\r
+=item addDate\r
+\r
+       my ($day, $month, $year) = $calendar->addDate($day, $month, $year, $offset)\r
+\r
+C<$day> Is the starting day of the interval.\r
+\r
+C<$month> Is the starting month of the interval.\r
+\r
+C<$year> Is the starting year of the interval.\r
+\r
+C<$offset> Is the number of days that this function has to count from $date.\r
+\r
+=cut\r
+\r
+sub addDate {\r
+       my ($self, $day, $month, $year, $offset) = @_;\r
+        \r
+       if ($offset < 0) { # In case $offset is negative\r
+               $offset = $offset*(-1);\r
+       }\r
+\r
+       my $daysMode = C4::Context->preference('useDaysMode');\r
+       if ($daysMode eq 'normal') {\r
+               ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, ($offset - 1));\r
+       } else {\r
+               while ($offset > 0) {                                                           \r
+                       if (!($self->isHoliday($day, $month, $year))) {\r
+                               $offset = $offset - 1;                                  \r
+                       }                               \r
+                       if ($offset > 0) {\r
+                               ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, 1);\r
+                       }                               \r
+               }\r
+       }\r
+\r
+       return($day, $month, $year);    \r
+}\r
+\r
+=item daysBetween\r
+\r
+       my $daysBetween = $calendar->daysBetween($dayFrom, $monthFrom, $yearFrom,\r
+                                                $dayTo, $monthTo, $yearTo)\r
+\r
+C<$dayFrom> Is the starting day of the interval.\r
+\r
+C<$monthFrom> Is the starting month of the interval.\r
+\r
+C<$yearFrom> Is the starting year of the interval.\r
+\r
+C<$dayTo> Is the ending day of the interval.\r
+\r
+C<$monthTo> Is the ending month of the interval.\r
+\r
+C<$yearTo> Is the ending year of the interval.\r
+\r
+=cut\r
+\r
+sub daysBetween {\r
+       my ($self, $dayFrom, $monthFrom, $yearFrom, $dayTo, $monthTo, $yearTo) = @_;\r
+        \r
+       my $daysMode = C4::Context->preference('useDaysMode');\r
+       my $count = 1;\r
+       my $continue = 1;\r
+       if ($daysMode eq 'normal') {\r
+               while ($continue) {\r
+                       if (($yearFrom != $yearTo) || ($monthFrom != $monthTo) || ($dayFrom != $dayTo)) {\r
+                               ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);\r
+                               $count++;\r
+                       } else {\r
+                               $continue = 0;  \r
+                       }\r
+               }               \r
+       } else {\r
+               while ($continue) {\r
+                       if (($yearFrom != $yearTo) || ($monthFrom != $monthTo) || ($dayFrom != $dayTo)) {\r
+                               if (!($self->isHoliday($dayFrom, $monthFrom, $yearFrom))) {\r
+                                       $count++;\r
+                               }       \r
+                               ($yearFrom, $monthFrom, $dayFrom) = &Date::Calc::Add_Delta_Days($yearFrom, $monthFrom, $dayFrom, 1);                            \r
+                       } else {\r
+                               $continue = 0;  \r
+                       }\r
+               }               \r
+       }\r
+       return($count); \r
+}\r
+\r
+1;\r
+\r
+__END__\r
+\r
+=back\r
+\r
+=head1 AUTHOR\r
+\r
+Koha Physics Library UNLP <matias_veleda@hotmail.com>\r
+\r
+=cut
\ No newline at end of file
diff --git a/C4/NewsChannels.pm b/C4/NewsChannels.pm
new file mode 100644 (file)
index 0000000..ab78c18
--- /dev/null
@@ -0,0 +1,387 @@
+package C4::NewsChannels;\r
+\r
+# Copyright 2000-2002 Katipo Communications\r
+#\r
+# This file is part of Koha.\r
+#\r
+# Koha is free software; you can redistribute it and/or modify it under the\r
+# terms of the GNU General Public License as published by the Free Software\r
+# Foundation; either version 2 of the License, or (at your option) any later\r
+# version.\r
+#\r
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY\r
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR\r
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.\r
+#\r
+# You should have received a copy of the GNU General Public License along with\r
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,\r
+# Suite 330, Boston, MA  02111-1307 USA\r
+\r
+use strict;\r
+\r
+use C4::Context;\r
+use C4::Date;\r
+\r
+use vars qw($VERSION @ISA @EXPORT);\r
+\r
+# set the version for version checking\r
+$VERSION = 0.01;\r
+\r
+=head1 NAME\r
+\r
+C4::NewsChannels - Functions to manage the news channels and its categories\r
+\r
+=head1 DESCRIPTION\r
+\r
+This module provides the functions needed to admin the news channels and its categories\r
+\r
+=head1 FUNCTIONS\r
+\r
+=over 2\r
+\r
+=cut\r
+\r
+\r
+@ISA = qw(Exporter);\r
+@EXPORT = qw(\r
+  &news_channels &get_new_channel &del_channels &add_channel &update_channel\r
+  &news_channels_categories &get_new_channel_category &del_channels_categories\r
+  &add_channel_category &update_channel_category &news_channels_by_category\r
+&add_opac_new &upd_opac_new &del_opac_new &get_opac_new &get_opac_news\r
+  &add_opac_electronic &upd_opac_electronic &del_opac_electronic &get_opac_electronic &get_opac_electronics\r
+);\r
+\r
+\r
+=item news_channels\r
+\r
+  ($count, @channels) = &news_channels($channel_name, $id_category, $unclassified);\r
+\r
+Looks up news channels by name or category.\r
+\r
+C<$channel_name> is the channel name to search.\r
+\r
+C<$id_category> is the channel category code to search.\r
+\r
+C<$$unclassified> if it is set and $channel_name and $id_category search for the news channels without a category\r
+\r
+if none of the params are set C<&news_channels> returns all the news channels.\r
+\r
+C<&news_channels> returns two values: an integer giving the number of\r
+news channels found and a reference to an array\r
+of references to hash, which has the news_channels and news_channels_categories fields.\r
+\r
+=cut\r
+\r
+sub news_channels {\r
+       my ($channel_name, $id_category, $unclassified) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my @channels;\r
+       my $query = "SELECT * FROM news_channels LEFT JOIN news_channels_categories ON news_channels.id_category = news_channels_categories.id_category";\r
+       if ( ($channel_name ne '') && ($id_category ne '') ) {\r
+               $query.= " WHERE channel_name like '" . $channel_name . "%' AND news_channels.id_category = " . $id_category;\r
+       } elsif ($channel_name ne '')  {\r
+               $query.= " WHERE channel_name like '" . $channel_name . "%'";\r
+       } elsif ($id_category ne '') {\r
+               $query.= " WHERE news_channels.id_category = " . $id_category;\r
+       } elsif ($unclassified) {\r
+               $query.= " WHERE news_channels.id_category IS NULL ";\r
+       }\r
+       my $sth = $dbh->prepare($query);\r
+       $sth->execute();\r
+       while (my $row = $sth->fetchrow_hashref) {\r
+               push @channels, $row;\r
+       }\r
+       $sth->finish;\r
+       return (scalar(@channels), @channels);\r
+}\r
+\r
+=item news_channels_by_category\r
+\r
+  ($count, @results) = &news_channels_by_category();\r
+\r
+Looks up news channels grouped by category.\r
+\r
+C<&news_channels_by_category> returns two values: an integer giving the number of\r
+categories found and a reference to an array\r
+of references to hash, which the following keys: \r
+\r
+=over 4\r
+\r
+=item C<channels_count>\r
+\r
+The number of news channels in that category\r
+\r
+=item C<channels>\r
+\r
+A reference to an array of references to hash which keys are the new_channels fields. \r
+\r
+Additionally the last index of results has a reference to all the news channels which don't have a category \r
+\r
+=cut\r
+\r
+sub news_channels_by_category {\r
+       \r
+       my ($categories_count, @results) = &news_channels_categories();\r
+       foreach my $row (@results) {\r
+\r
+               my ($channels_count, @channels) = &news_channels('', $row->{'id_category'});\r
+               $row->{'channels_count'} = $channels_count;\r
+               $row->{'channels'} = \@channels;\r
+       }\r
+\r
+       my ($channels_count, @channels) = &news_channels('', '', 1);\r
+       my %row;\r
+       $row{'id_category'} = -1;\r
+       $row{'unclassified'} = 1;\r
+       $row{'channels_count'} = $channels_count;\r
+       $row{'channels'} = \@channels;\r
+       push @results, \%row;\r
+\r
+       return (scalar(@results), @results);\r
+}\r
+\r
+sub get_new_channel {\r
+       my ($id) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("SELECT * FROM news_channels WHERE id = ?");\r
+       $sth->execute($id);\r
+       my $channel = $sth->fetchrow_hashref;\r
+       $sth->finish;\r
+       return $channel;\r
+}\r
+\r
+sub del_channels {\r
+       my ($ids) = @_;\r
+       if ($ids ne '') {\r
+               my $dbh = C4::Context->dbh;\r
+               my $sth = $dbh->prepare("DELETE FROM news_channels WHERE id IN ($ids) ");\r
+               $sth->execute();\r
+               $sth->finish;\r
+               return $ids;\r
+       }\r
+       return 0;\r
+}\r
+\r
+sub add_channel {\r
+       my ($name, $url, $id_category, $notes) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("INSERT INTO news_channels (channel_name, url, id_category, notes) VALUES (?,?,?,?)");\r
+       $sth->execute($name, $url, $id_category, $notes);\r
+       $sth->finish;\r
+       return 1;\r
+}\r
+\r
+sub update_channel {\r
+       my ($id, $name, $url, $id_category, $notes) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("UPDATE news_channels SET channel_name = ?,  url = ?, id_category = ?, notes = ? WHERE id = ?");\r
+       $sth->execute($name, $url, $id_category, $notes, $id);\r
+       $sth->finish;\r
+       return 1;\r
+}\r
+\r
+sub news_channels_categories {\r
+       my $dbh = C4::Context->dbh;\r
+       my @categories;\r
+       my $query = "SELECT * FROM news_channels_categories";\r
+       my $sth = $dbh->prepare($query);\r
+       $sth->execute();\r
+       while (my $row = $sth->fetchrow_hashref) {\r
+               push @categories, $row;\r
+       }\r
+       $sth->finish;\r
+       return (scalar(@categories), @categories);\r
+\r
+}\r
+\r
+sub get_new_channel_category {\r
+       my ($id) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("SELECT * FROM news_channels_categories WHERE id_category = ?");\r
+       $sth->execute($id);\r
+       my $category = $sth->fetchrow_hashref;\r
+       $sth->finish;\r
+       return $category;\r
+}\r
+\r
+sub del_channels_categories {\r
+       my ($ids) = @_;\r
+       if ($ids ne '') {\r
+               my $dbh = C4::Context->dbh;\r
+               my $sth = $dbh->prepare("UPDATE news_channels SET id_category = NULL WHERE id_category IN ($ids) "); \r
+               $sth->execute();\r
+               $sth = $dbh->prepare("DELETE FROM news_channels_categories WHERE id_category IN ($ids) ");\r
+               $sth->execute();\r
+               $sth->finish;\r
+               return $ids;\r
+       }\r
+       return 0;\r
+}\r
+\r
+sub add_channel_category {\r
+       my ($name) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("INSERT INTO news_channels_categories (category_name) VALUES (?)");\r
+       $sth->execute($name);\r
+       $sth->finish;\r
+       return 1;\r
+}\r
+\r
+sub update_channel_category {\r
+       my ($id, $name) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("UPDATE news_channels_categories SET category_name = ? WHERE id_category = ?");\r
+       $sth->execute($name, $id);\r
+       $sth->finish;\r
+       return 1;\r
+}\r
+\r
+\r
+sub add_opac_new {\r
+       my ($title, $new, $lang) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("INSERT INTO opac_news (title, new, lang) VALUES (?,?,?)");\r
+       $sth->execute($title, $new, $lang);\r
+       $sth->finish;\r
+       return 1;\r
+}\r
+\r
+sub upd_opac_new {\r
+       my ($idnew, $title, $new, $lang) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("UPDATE opac_news SET title = ?, new = ?, lang = ? WHERE idnew = ?");\r
+       $sth->execute($title, $new, $lang, $idnew);\r
+       $sth->finish;\r
+       return 1;\r
+}\r
+\r
+sub del_opac_new {\r
+       my ($ids) = @_;\r
+       if ($ids) {\r
+               my $dbh = C4::Context->dbh;\r
+               my $sth = $dbh->prepare("DELETE FROM opac_news WHERE idnew IN ($ids)");\r
+               $sth->execute();\r
+               $sth->finish;\r
+               return 1;\r
+       } else {\r
+               return 0;\r
+       }\r
+}\r
+\r
+sub get_opac_new {\r
+       my ($idnew) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("SELECT * FROM opac_news WHERE idnew = ?");\r
+       $sth->execute($idnew);\r
+       my $data = $sth->fetchrow_hashref;\r
+       $data->{$data->{'lang'}} = 1;\r
+       $sth->finish;\r
+       return $data;\r
+}\r
+\r
+sub get_opac_news {\r
+       my ($limit, $lang) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $query = "SELECT *, DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate FROM opac_news";\r
+       if ($lang) {\r
+               $query.= " WHERE lang = '" .$lang ."' ";\r
+       }\r
+       $query.= " ORDER BY timestamp DESC ";\r
+       #if ($limit) {\r
+       #       $query.= "LIMIT 0, " . $limit;\r
+       #}\r
+       my $sth = $dbh->prepare($query);\r
+       $sth->execute();\r
+       my @opac_news;\r
+       my $count = 0;\r
+       while (my $row = $sth->fetchrow_hashref) {\r
+               if ((($limit) && ($count < $limit)) || (!$limit)) {\r
+                       $row->{'newdate'} = format_date($row->{'newdate'});\r
+                       push @opac_news, $row;  \r
+               }\r
+               $count++;\r
+       }\r
+       return ($count, \@opac_news);\r
+}\r
+\r
+### get electronic databases\r
+\r
+sub add_opac_electronic {\r
+       my ($title, $edata, $lang,$image,$href,$section) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("INSERT INTO opac_electronic (title, edata, lang,image,href,section) VALUES (?,?,?,?,?,?)");\r
+       $sth->execute($title, $edata, $lang,$image,$href,$section);\r
+       $sth->finish;\r
+       return 1;\r
+}\r
+\r
+sub upd_opac_electronic {\r
+       my ($idelectronic, $title, $edata, $lang, $image, $href,$section) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("UPDATE opac_electronic SET title = ?, edata = ?, lang = ? , image=?, href=? ,section=? WHERE idelectronic = ?");\r
+       $sth->execute($title, $edata, $lang, $image,$href ,$section, $idelectronic);\r
+       $sth->finish;\r
+       return 1;\r
+}\r
+\r
+sub del_opac_electronic {\r
+       my ($ids) = @_;\r
+       if ($ids) {\r
+               my $dbh = C4::Context->dbh;\r
+               my $sth = $dbh->prepare("DELETE FROM opac_electronic WHERE idelectronic IN ($ids)");\r
+               $sth->execute();\r
+               $sth->finish;\r
+               return 1;\r
+       } else {\r
+               return 0;\r
+       }\r
+}\r
+\r
+sub get_opac_electronic {\r
+       my ($idelectronic) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $sth = $dbh->prepare("SELECT * FROM opac_electronic WHERE idelectronic = ?");\r
+       $sth->execute($idelectronic);\r
+       my $data = $sth->fetchrow_hashref;\r
+       $data->{$data->{'lang'}} = 1;\r
+       $data->{$data->{'section'}} = 1;\r
+       $sth->finish;\r
+       return $data;\r
+}\r
+\r
+sub get_opac_electronics {\r
+       my ($section, $lang) = @_;\r
+       my $dbh = C4::Context->dbh;\r
+       my $query = "SELECT *, DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate FROM opac_electronic";\r
+       if ($lang) {\r
+               $query.= " WHERE lang = '" .$lang ."' ";\r
+       }\r
+       if ($section) {\r
+               $query.= " and section= '" . $section."' ";\r
+       }\r
+       $query.= " ORDER BY title ";\r
+       \r
+       my $sth = $dbh->prepare($query);\r
+       $sth->execute();\r
+       my @opac_electronic;\r
+       my $count = 0;\r
+       while (my $row = $sth->fetchrow_hashref) {\r
+                       push @opac_electronic, $row;    \r
+\r
+               \r
+               $count++;\r
+       }\r
+\r
+       return ($count,\@opac_electronic);\r
+}\r
+END { }    # module clean-up code here (global destructor)\r
+\r
+=back\r
+\r
+=head1 AUTHOR\r
+\r
+TG\r
+\r
+=cut\r
+\r
+\r