Build the system-wide recipe and entity objects
This commit is contained in:
parent
25c8ec75f4
commit
536ed85f97
@ -27,7 +27,8 @@ use parent qw(Webperl::System);
|
|||||||
|
|
||||||
use ORB::System::Metadata;
|
use ORB::System::Metadata;
|
||||||
use ORB::System::Roles;
|
use ORB::System::Roles;
|
||||||
|
use ORB::System::Entity;
|
||||||
|
use ORB::System::Recipe;
|
||||||
|
|
||||||
## @method $ init(%args)
|
## @method $ init(%args)
|
||||||
# Initialise the ORB System's references to other system objects. This
|
# Initialise the ORB System's references to other system objects. This
|
||||||
@ -53,6 +54,7 @@ sub init {
|
|||||||
$self -> SUPER::init(@_)
|
$self -> SUPER::init(@_)
|
||||||
or return undef;
|
or return undef;
|
||||||
|
|
||||||
|
# Metadata and permissions handling gubbins
|
||||||
$self -> {"metadata"} = ORB::System::Metadata -> new(dbh => $self -> {"dbh"},
|
$self -> {"metadata"} = ORB::System::Metadata -> new(dbh => $self -> {"dbh"},
|
||||||
settings => $self -> {"settings"},
|
settings => $self -> {"settings"},
|
||||||
logger => $self -> {"logger"})
|
logger => $self -> {"logger"})
|
||||||
@ -64,6 +66,23 @@ sub init {
|
|||||||
metadata => $self -> {"metadata"})
|
metadata => $self -> {"metadata"})
|
||||||
or return $self -> self_error("Roles system init failed: ".$Webperl::SystemModule::errstr);
|
or return $self -> self_error("Roles system init failed: ".$Webperl::SystemModule::errstr);
|
||||||
|
|
||||||
|
# Objects used to track entities
|
||||||
|
foreach my $entity ("ingredients", "prep", "states", "tags", "types", "units") {
|
||||||
|
$self -> {"entities"} -> {$entity} = ORB::System::Entity -> new(dbh => $self -> {"dbh"},
|
||||||
|
settings => $self -> {"settings"},
|
||||||
|
logger => $self -> {"logger"},
|
||||||
|
entity_table => $entity)
|
||||||
|
or return $self -> self_error("'$entity' system init failed: ".$Webperl::SystemModule::errstr);
|
||||||
|
}
|
||||||
|
|
||||||
|
# And we need a recipe object that pulls it all together
|
||||||
|
$self -> {"recipe"} = ORB::System::Recipe -> new(dbh => $self -> {"dbh"},
|
||||||
|
settings => $self -> {"settings"},
|
||||||
|
logger => $self -> {"logger"},
|
||||||
|
metadata => $self -> {"metadata"},
|
||||||
|
entities => $self -> {"entities"})
|
||||||
|
or return $self -> self_error("Recipe model init failed: ".$Webperl::SystemModule::errstr);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ use Webperl::Utils qw(hash_or_hashref array_or_arrayref);
|
|||||||
|
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# Constructor
|
# Constructor and cleanup
|
||||||
|
|
||||||
## @cmethod $ new(%args)
|
## @cmethod $ new(%args)
|
||||||
# Create a new Recipe object to manage recipe creation and management.
|
# Create a new Recipe object to manage recipe creation and management.
|
||||||
@ -86,6 +86,18 @@ sub new {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
## @method void clear()
|
||||||
|
# Delete all references to entity management objects from the current recipe
|
||||||
|
# object. This helps teardown after page generation by making life easier for
|
||||||
|
# perl's destructor code.
|
||||||
|
#
|
||||||
|
sub clear {
|
||||||
|
my $self = shift;
|
||||||
|
|
||||||
|
delete $self -> {"entities"};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# Recipe creation and status modification
|
# Recipe creation and status modification
|
||||||
|
|
||||||
@ -128,11 +140,11 @@ sub create {
|
|||||||
$self -> clear_error();
|
$self -> clear_error();
|
||||||
|
|
||||||
# Get IDs for the type and status
|
# Get IDs for the type and status
|
||||||
$args -> {"typeid"} = $self -> {"system"} -> {"types"} -> get_id($args -> {"type"})
|
$args -> {"typeid"} = $self -> {"entities"} -> {"types"} -> get_id($args -> {"type"})
|
||||||
or return $self -> self_error($self -> {"system"} -> {"types"} -> errstr());
|
or return $self -> self_error($self -> {"entities"} -> {"types"} -> errstr());
|
||||||
|
|
||||||
$args -> {"statusid"} = $self -> {"system"} -> {"states"} -> get_id($args -> {"status"})
|
$args -> {"statusid"} = $self -> {"entities"} -> {"states"} -> get_id($args -> {"status"})
|
||||||
or return $self -> self_error($self -> {"system"} -> {"states"} -> errstr());
|
or return $self -> self_error($self -> {"entities"} -> {"states"} -> errstr());
|
||||||
|
|
||||||
# We need a metadata context for the recipe
|
# We need a metadata context for the recipe
|
||||||
my $metadataid = $self -> _create_recipe_metadata($args -> {"previd"});
|
my $metadataid = $self -> _create_recipe_metadata($args -> {"previd"});
|
||||||
@ -223,8 +235,8 @@ sub set_status {
|
|||||||
|
|
||||||
$self -> clear_error();
|
$self -> clear_error();
|
||||||
|
|
||||||
my $statusid = $self -> {"system"} -> {"states"} -> get_id($status)
|
my $statusid = $self -> {"entities"} -> {"states"} -> get_id($status)
|
||||||
or return $self -> self_error($self -> {"system"} -> {"states"} -> errstr());
|
or return $self -> self_error($self -> {"entities"} -> {"states"} -> errstr());
|
||||||
|
|
||||||
my $stateh = $self -> {"dbh"} -> prepare("UPDATE `".$self -> {"settings"} -> {"database"} -> {"recipes"}."`
|
my $stateh = $self -> {"dbh"} -> prepare("UPDATE `".$self -> {"settings"} -> {"database"} -> {"recipes"}."`
|
||||||
SET `status_id` = ?
|
SET `status_id` = ?
|
||||||
@ -351,15 +363,15 @@ sub find {
|
|||||||
|
|
||||||
# Convert ingredients and tags to IDs for easier query structure
|
# Convert ingredients and tags to IDs for easier query structure
|
||||||
# This will return an empty array if there are no ingredients to search on
|
# This will return an empty array if there are no ingredients to search on
|
||||||
my $ingreds = $self -> {"system"} -> {"ingredients"} -> find_ids($args -> {"ingredients"})
|
my $ingreds = $self -> {"entities"} -> {"ingredients"} -> find_ids($args -> {"ingredients"})
|
||||||
or return $self -> self_error("Ingredient lookup error: ".$self -> {"system"} -> {"ingredients"} -> errstr());
|
or return $self -> self_error("Ingredient lookup error: ".$self -> {"entities"} -> {"ingredients"} -> errstr());
|
||||||
|
|
||||||
# Fix the array returned from find_ids so that we only have the id numbers
|
# Fix the array returned from find_ids so that we only have the id numbers
|
||||||
$args -> {"ingredids"} = $self -> _hashlist_to_list($ingreds, "id");
|
$args -> {"ingredids"} = $self -> _hashlist_to_list($ingreds, "id");
|
||||||
|
|
||||||
# Repeat the process for the recipe tags
|
# Repeat the process for the recipe tags
|
||||||
my $tags = $self -> {"system"} -> {"tags"} -> find_ids($args -> {"tags"})
|
my $tags = $self -> {"entities"} -> {"tags"} -> find_ids($args -> {"tags"})
|
||||||
or return $self -> self_error("Tag lookup error: ".$self -> {"system"} -> {"tags"} -> errstr());
|
or return $self -> self_error("Tag lookup error: ".$self -> {"entities"} -> {"tags"} -> errstr());
|
||||||
$args -> {"tagids"} = $self -> _hashlist_to_list($tags, "id");
|
$args -> {"tagids"} = $self -> _hashlist_to_list($tags, "id");
|
||||||
|
|
||||||
# Fix up default matching modes
|
# Fix up default matching modes
|
||||||
@ -370,9 +382,9 @@ sub find {
|
|||||||
my (@params, $joins, @where) = ((), "", ());
|
my (@params, $joins, @where) = ((), "", ());
|
||||||
|
|
||||||
# Matching all ingredients or tags requires multiple inner joins
|
# Matching all ingredients or tags requires multiple inner joins
|
||||||
$joins .= $self -> _join_fragment($args -> {"ingredids"}, $self -> {"system"} -> {"ingredients"} -> {"entity_table"}, \@params)
|
$joins .= $self -> _join_fragment($args -> {"ingredids"}, $self -> {"entities"} -> {"ingredients"} -> {"entity_table"}, \@params)
|
||||||
if(scalar(@{$args -> {"ingredids"}}) && $args -> {"ingredmatch"} eq "all");
|
if(scalar(@{$args -> {"ingredids"}}) && $args -> {"ingredmatch"} eq "all");
|
||||||
$joins .= $self -> _join_fragment($args -> {"tagids"}, $self -> {"system"} -> {"tags"} -> {"entity_table"}, \@params)
|
$joins .= $self -> _join_fragment($args -> {"tagids"}, $self -> {"entities"} -> {"tags"} -> {"entity_table"}, \@params)
|
||||||
if(scalar(@{$args -> {"tagids"}}) && $args -> {"tagmatch"} eq "all");
|
if(scalar(@{$args -> {"tagids"}}) && $args -> {"tagmatch"} eq "all");
|
||||||
|
|
||||||
# Simple searches on recipe fields
|
# Simple searches on recipe fields
|
||||||
@ -493,23 +505,23 @@ sub _add_ingredients {
|
|||||||
# Otherwise, it's a real ingredient, so we need to do the more complex work
|
# Otherwise, it's a real ingredient, so we need to do the more complex work
|
||||||
} else {
|
} else {
|
||||||
# obtain the IDs of entities referenced by this ingredient relation
|
# obtain the IDs of entities referenced by this ingredient relation
|
||||||
my $ingid = $self -> {"system"} -> {"ingredients"} -> get_id($ingred -> {"name"})
|
my $ingid = $self -> {"entities"} -> {"ingredients"} -> get_id($ingred -> {"name"})
|
||||||
or return $self -> self_error("Unable to get ingreditent ID for '".$ingred -> {"name"}."': ".$self -> {"system"} -> {"ingredient"} -> errstr());
|
or return $self -> self_error("Unable to get ingreditent ID for '".$ingred -> {"name"}."': ".$self -> {"entities"} -> {"ingredients"} -> errstr());
|
||||||
|
|
||||||
my $unitid = $self -> {"system"} -> {"units"} -> get_id($ingred -> {"units"})
|
my $unitid = $self -> {"entities"} -> {"units"} -> get_id($ingred -> {"units"})
|
||||||
or return $self -> self_error("Unable to get unit ID for '".$ingred -> {"units"}."': ".$self -> {"system"} -> {"units"} -> errstr());
|
or return $self -> self_error("Unable to get unit ID for '".$ingred -> {"units"}."': ".$self -> {"entities"} -> {"units"} -> errstr());
|
||||||
|
|
||||||
my $prepid = $self -> {"system"} -> {"prepmethod"} -> get_id($ingred -> {"prep"})
|
my $prepid = $self -> {"entities"} -> {"prep"} -> get_id($ingred -> {"prep"})
|
||||||
or return $self -> self_error("Unable to get preparation method ID for '".$ingred -> {"prep"}."': ".$self -> {"system"} -> {"prepmethod"} -> errstr());
|
or return $self -> self_error("Unable to get preparation method ID for '".$ingred -> {"prep"}."': ".$self -> {"entities"} -> {"prep"} -> errstr());
|
||||||
|
|
||||||
# If we have an ID we can add the ingredient.
|
# If we have an ID we can add the ingredient.
|
||||||
$addh -> execute($recipeid, $position, $unitid, $prepid, $ingid, $ingred -> {"quant"}, $ingred -> {"notes"}, undef)
|
$addh -> execute($recipeid, $position, $unitid, $prepid, $ingid, $ingred -> {"quant"}, $ingred -> {"notes"}, undef)
|
||||||
or return $self -> self_error("Unable to add ingredient '".$ingred -> {"name"}."' to recipe '$recipeid': ".$self -> {"dbh"} -> errstr());
|
or return $self -> self_error("Unable to add ingredient '".$ingred -> {"name"}."' to recipe '$recipeid': ".$self -> {"dbh"} -> errstr());
|
||||||
|
|
||||||
# And increase the entity refcounts
|
# And increase the entity refcounts
|
||||||
$self -> {"system"} -> {"ingredients"} -> increase_refcount($ingid);
|
$self -> {"entities"} -> {"ingredients"} -> increase_refcount($ingid);
|
||||||
$self -> {"system"} -> {"units"} -> increase_refcount($unitid);
|
$self -> {"entities"} -> {"units"} -> increase_refcount($unitid);
|
||||||
$self -> {"system"} -> {"prepmethod"} -> increase_refcount($prepid);
|
$self -> {"entities"} -> {"prep"} -> increase_refcount($prepid);
|
||||||
}
|
}
|
||||||
|
|
||||||
++$position;
|
++$position;
|
||||||
@ -559,13 +571,13 @@ sub _add_tags {
|
|||||||
# Go through each tag, adding it
|
# Go through each tag, adding it
|
||||||
foreach my $tag (@{$tags}) {
|
foreach my $tag (@{$tags}) {
|
||||||
# Try to get the tag id
|
# Try to get the tag id
|
||||||
my $tagid = $self -> {"system"} -> {"tags"} -> get_id($tag)
|
my $tagid = $self -> {"entities"} -> {"tags"} -> get_id($tag)
|
||||||
or return $self -> self_error("Unable to obtain ID for tag '$tag'");
|
or return $self -> self_error("Unable to obtain ID for tag '$tag'");
|
||||||
|
|
||||||
$addh -> execute($recipeid, $tagid)
|
$addh -> execute($recipeid, $tagid)
|
||||||
or return $self -> self_error("Tag association failed: ".$self -> {"dbh"} -> errstr);
|
or return $self -> self_error("Tag association failed: ".$self -> {"dbh"} -> errstr);
|
||||||
|
|
||||||
$self -> {"system"} -> {"tags"} -> increase_refcount($tagid)
|
$self -> {"entities"} -> {"tags"} -> increase_refcount($tagid)
|
||||||
or return $self -> self_error("Tag refcount change failed");
|
or return $self -> self_error("Tag refcount change failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user