Forced block name/id set in module loader.

This commit is contained in:
Chris 2012-06-22 12:13:18 +01:00
parent a316c58217
commit 0b46c2fa83
2 changed files with 35 additions and 28 deletions

View File

@ -68,17 +68,6 @@ sub new {
@_); @_);
return undef if(!$self); return undef if(!$self);
# Work out which block we're being invoked with
$self -> {"block"} = is_defined_numeric($self -> {"cgi"}, "block");
# block id is not set, check whether the block name is
if(!defined($self -> {"block"})) {
my $block = $self -> {"cgi"} -> param("block");
$self -> {"block"} = $block if($block && $block =~ /^\w+$/);
$self -> {"block"} = $self -> {"settings"} -> {"config"} -> {"default_block"} if(!$self -> {"block"});
}
# Set up the logger, if needed (Application usually does this long before the Block constructor # Set up the logger, if needed (Application usually does this long before the Block constructor
# gets called, but even if it has, doing it again won't hurt anything). # gets called, but even if it has, doing it again won't hurt anything).
$self -> {"logger"} -> init_database_log($self -> {"dbh"}, $self -> {"logtable"}) $self -> {"logger"} -> init_database_log($self -> {"dbh"}, $self -> {"logtable"})

View File

@ -178,6 +178,8 @@ sub new_module {
# If we have a block row, return an instance of the module for it # If we have a block row, return an instance of the module for it
return $self -> new_module_byid($modrow -> {"module_id"}, return $self -> new_module_byid($modrow -> {"module_id"},
$modrow -> {"id"},
$modrow -> {"name"},
$modrow -> {"args"}) $modrow -> {"args"})
if($modrow); if($modrow);
@ -205,6 +207,8 @@ sub new_module_byblockid {
# If we have a block row, return an instance of the module for it # If we have a block row, return an instance of the module for it
return $self -> new_module_byid($modrow -> {"module_id"}, return $self -> new_module_byid($modrow -> {"module_id"},
$modrow -> {"id"},
$modrow -> {"name"},
$modrow -> {"args"}) $modrow -> {"args"})
if($modrow); if($modrow);
@ -212,7 +216,7 @@ sub new_module_byblockid {
} }
## @method $ new_module_byname($modname, $argument) ## @method $ new_module_byname($modname, $block_id, $block_name, $argument)
# Load a module based on its name in the modules table. This allows direct creation # Load a module based on its name in the modules table. This allows direct creation
# of a module from the modules table rather than the normal indirect route via # of a module from the modules table rather than the normal indirect route via
# new_module() or new_module_byblockid(). # new_module() or new_module_byblockid().
@ -220,21 +224,27 @@ sub new_module_byblockid {
# @note In most cases, you do not want to use this function - you want to use # @note In most cases, you do not want to use this function - you want to use
# new_module() instead. Only use this if you know what you are doing. # new_module() instead. Only use this if you know what you are doing.
# #
# @param modname The name of the module to load. # @param modname The name of the module to load.
# @param argument Argument to pass to the module constructor. # @param block_id The ID of the block to associate with the loaded module. May be undef.
# @param block_name The name of the block to associate with the loaded module. May be undef.
# @param argument Argument to pass to the module constructor.
# @return An instance of the module, or undef on error. # @return An instance of the module, or undef on error.
sub new_module_byname { sub new_module_byname {
my $self = shift; my $self = shift;
my $modname = shift; my $modname = shift;
my $argument = shift; my $block_id = shift;
my $block_name = shift;
my $argument = shift;
return $self -> _new_module_internal("WHERE name LIKE ?", return $self -> _new_module_internal("WHERE name LIKE ?",
$modname, $modname,
$block_id,
$block_name,
$argument); $argument);
} }
## @method $ new_module_byid($modid, $argument) ## @method $ new_module_byid($modid, $block_id, $block_name, $argument)
# Load a module based on its id in the modules table. This allows direct creation # Load a module based on its id in the modules table. This allows direct creation
# of a module from the modules table rather than the normal indirect route via # of a module from the modules table rather than the normal indirect route via
# new_module() or new_module_byblockid(). # new_module() or new_module_byblockid().
@ -242,16 +252,22 @@ sub new_module_byname {
# @note In most cases, you do not want to use this function - you want to use # @note In most cases, you do not want to use this function - you want to use
# new_module_byblockid() instead. Only use this if you know what you are doing. # new_module_byblockid() instead. Only use this if you know what you are doing.
# #
# @param modid The id of the module to load. # @param modid The id of the module to load.
# @param argument Argument to pass to the module constructor. # @param block_id The ID of the block to associate with the loaded module. May be undef.
# @param block_name The name of the block to associate with the loaded module. May be undef.
# @param argument Argument to pass to the module constructor.
# @return An instance of the module, or undef on error. # @return An instance of the module, or undef on error.
sub new_module_byid { sub new_module_byid {
my $self = shift; my $self = shift;
my $modid = shift; my $modid = shift;
my $argument = shift; my $block_id = shift;
my $block_name = shift;
my $argument = shift;
return $self -> _new_module_internal("WHERE module_id = ?", return $self -> _new_module_internal("WHERE module_id = ?",
$modid, $modid,
$block_id,
$block_name,
$argument); $argument);
} }
@ -265,10 +281,12 @@ sub new_module_byid {
# @param modargs The argument to pass to the module. # @param modargs The argument to pass to the module.
# @return A new object, or undef if a problem occured or the module is disabled. # @return A new object, or undef if a problem occured or the module is disabled.
sub _new_module_internal { sub _new_module_internal {
my $self = shift; my $self = shift;
my $where = shift; my $where = shift;
my $argument = shift; my $argument = shift;
my $modarg = shift; my $block_id = shift;
my $block_name = shift;
my $modarg = shift;
my $modh = $self -> {"dbh"} -> prepare("SELECT * FROM ".$self -> {"settings"} -> {"database"} -> {"modules"}." $where"); my $modh = $self -> {"dbh"} -> prepare("SELECT * FROM ".$self -> {"settings"} -> {"database"} -> {"modules"}." $where");
$modh -> execute($argument) $modh -> execute($argument)
@ -281,7 +299,7 @@ sub _new_module_internal {
my $name = $modrow -> {"perl_module"}; my $name = $modrow -> {"perl_module"};
return $self -> load_module($name, id => $modrow -> {"module_id"}, args => $modarg); return $self -> load_module($name, id => $modrow -> {"module_id"}, block => $block_name, block_id => $block_id, args => $modarg);
} }