Minor improvements
[koha.git] / C4 / Context.pm
index 7932c2e..2d9e9e2 100644 (file)
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
+# $Id$
+
 package C4::Context;
 use strict;
 use DBI;
+use C4::Boolean;
 
 use vars qw($VERSION $AUTOLOAD),
        qw($context),
@@ -167,6 +170,8 @@ sub new
        my $conf_fname = shift;         # Config file to load
        my $self = {};
 
+       # check that the specified config file exists
+       undef $conf_fname unless (defined $conf_fname && -e $conf_fname);
        # Figure out a good config file to load if none was specified.
        if (!defined($conf_fname))
        {
@@ -175,7 +180,6 @@ sub new
                $conf_fname = $ENV{"KOHA_CONF"} ||
                                CONFIG_FNAME;
        }
-
        $self->{"config_file"} = $conf_fname;
 
        # Load the desired config file.
@@ -291,10 +295,53 @@ sub config
        return $context->{"config"}{$var};
 }
 
+=item preference
+
+  $sys_preference = C4::Context->preference("some_variable");
+
+Looks up the value of the given system preference in the
+systempreferences table of the Koha database, and returns it. If the
+variable is not set, or in case of error, returns the undefined value.
+
+=cut
+#'
+# FIXME - The preferences aren't likely to change over the lifetime of
+# the script (and things might break if they did change), so perhaps
+# this function should cache the results it finds.
+sub preference
+{
+       my $self = shift;
+       my $var = shift;                # The system preference to return
+       my $retval;                     # Return value
+       my $dbh = C4::Context->dbh;     # Database handle
+       my $sth;                        # Database query handle
+
+       # Look up systempreferences.variable==$var
+       $retval = $dbh->selectrow_array(<<EOT);
+               SELECT  value
+               FROM    systempreferences
+               WHERE   variable='$var'
+               LIMIT   1
+EOT
+       return $retval;
+}
+
+sub boolean_preference ($) {
+       my $self = shift;
+       my $var = shift;                # The system preference to return
+       my $it = preference($self, $var);
+       return defined($it)? C4::Boolean::true_p($it): undef;
+}
+
 # AUTOLOAD
 # This implements C4::Config->foo, and simply returns
 # C4::Context->config("foo"), as described in the documentation for
 # &config, above.
+
+# FIXME - Perhaps this should be extended to check &config first, and
+# then &preference if that fails. OTOH, AUTOLOAD could lead to crappy
+# code, so it'd probably be best to delete it altogether so as not to
+# encourage people to use it.
 sub AUTOLOAD
 {
        my $self = shift;
@@ -311,11 +358,13 @@ sub AUTOLOAD
 sub _new_dbh
 {
        my $db_driver = $context->{"config"}{"db_scheme"} || "mysql";
+               # FIXME - It should be possible to use "MySQL" instead
+               # of "mysql", "PostgreSQL" instead of "Pg", and so
+               # forth.
        my $db_name   = $context->{"config"}{"database"};
        my $db_host   = $context->{"config"}{"hostname"};
        my $db_user   = $context->{"config"}{"user"};
        my $db_passwd = $context->{"config"}{"pass"};
-
        return DBI->connect("DBI:$db_driver:$db_name:$db_host",
                            $db_user, $db_passwd);
 }
@@ -453,27 +502,20 @@ sub stopwords
 # hash with stopwords
 sub _new_stopwords
 {
-       my $dbh = &dbh;
+       my $dbh = C4::Context->dbh;
        my $stopwordlist;
        my $sth = $dbh->prepare("select word from stopwords");
        $sth->execute;
        while (my $stopword = $sth->fetchrow_array) {
-       my $retval = {};
-       $stopwordlist->{$stopword} = uc($stopword);
+               my $retval = {};
+               $stopwordlist->{$stopword} = uc($stopword);
        }
        return $stopwordlist;
-#      my $db_driver = $context->{"config"}{"db_scheme"} || "mysql";
-#      my $db_name   = $context->{"config"}{"database"};
-#      my $db_host   = $context->{"config"}{"hostname"};
-#      my $db_user   = $context->{"config"}{"user"};
-#      my $db_passwd = $context->{"config"}{"pass"};
-
-#      return DBI->connect("DBI:$db_driver:$db_name:$db_host",
-#                          $db_user, $db_passwd);
 }
 
 1;
 __END__
+
 =back
 
 =head1 ENVIRONMENT
@@ -488,10 +530,10 @@ Specifies the configuration file to read.
 
 =head1 SEE ALSO
 
-L<DBI(3)|DBI>
+DBI(3)
 
 =head1 AUTHOR
 
-Andrew Arensburger
+Andrew Arensburger <arensb at ooblick dot com>
 
 =cut