Update support code to handle editing properly
This commit is contained in:
parent
b82ea79d35
commit
269e901ca2
@ -135,14 +135,15 @@ sub create {
|
|||||||
unless($args -> {"created"});
|
unless($args -> {"created"});
|
||||||
|
|
||||||
# We need a metadata context for the recipe
|
# We need a metadata context for the recipe
|
||||||
my $metadataid = $self -> _create_recipe_metadata($args -> {"origid"});
|
$args -> {"metadataid"} = $self -> _create_recipe_metadata()
|
||||||
|
unless($args -> {"metadataid"});
|
||||||
|
|
||||||
# Do the insert, and fetch the ID of the new row
|
# Do the insert, and fetch the ID of the new row
|
||||||
my $newh = $self -> {"dbh"} -> prepare("INSERT INTO `".$self -> {"settings"} -> {"database"} -> {"recipes"}."`
|
my $newh = $self -> {"dbh"} -> prepare("INSERT INTO `".$self -> {"settings"} -> {"database"} -> {"recipes"}."`
|
||||||
(`id`, `metadata_id`, `original_id`, `name`, `source`, `prepinfo`, `preptime`, `cooktime`, `yield`, `temp`, `temptype`, `method`, `notes`, `type_id`, `status_id`, `creator_id`, `created`, `updater_id`, `updated`)
|
(`id`, `metadata_id`, `original_id`, `name`, `source`, `prepinfo`, `preptime`, `cooktime`, `yield`, `temp`, `temptype`, `method`, `notes`, `type_id`, `status_id`, `creator_id`, `created`, `updater_id`, `updated`)
|
||||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,?)");
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,?)");
|
||||||
my $result = $newh -> execute($args -> {"id"},
|
my $result = $newh -> execute($args -> {"id"},
|
||||||
$metadataid,
|
$args -> {"metadataid"},
|
||||||
$args -> {"origid"},
|
$args -> {"origid"},
|
||||||
$args -> {"name"},
|
$args -> {"name"},
|
||||||
$args -> {"source"},
|
$args -> {"source"},
|
||||||
@ -173,7 +174,7 @@ sub create {
|
|||||||
if(!$newid);
|
if(!$newid);
|
||||||
|
|
||||||
# Attach to the metadata context as it's in use now
|
# Attach to the metadata context as it's in use now
|
||||||
$self -> {"metadata"} -> attach($metadataid)
|
$self -> {"metadata"} -> attach($args -> {"metadataid"})
|
||||||
or return $self -> self_error("Error in metadata system: ".$self -> {"metadata"} -> errstr());
|
or return $self -> self_error("Error in metadata system: ".$self -> {"metadata"} -> errstr());
|
||||||
|
|
||||||
# Add the ingredients for the recipe
|
# Add the ingredients for the recipe
|
||||||
@ -211,8 +212,14 @@ sub edit {
|
|||||||
# We want the new recipe to get the same ID as the old one, so record it
|
# We want the new recipe to get the same ID as the old one, so record it
|
||||||
$args -> {"id"} = $args -> {"origid"};
|
$args -> {"id"} = $args -> {"origid"};
|
||||||
|
|
||||||
|
# And get the source metadata context
|
||||||
|
$args -> {"metadataid"} = $self -> get_recipe_metadata($args -> {"id"});
|
||||||
|
|
||||||
# Now move the old recipe out of the way so the new one can use its ID
|
# Now move the old recipe out of the way so the new one can use its ID
|
||||||
my $renumbered = $self -> _renumber_recipe($args -> {"id"});
|
my $renumbered = $self -> _renumber_recipe($args -> {"id"}, $args -> {"metadataid"})
|
||||||
|
or return undef;
|
||||||
|
|
||||||
|
$self -> {"logger"} -> log("recipe.edit", $args -> {"creator_id"}, "unknown", "Renumbered ".$args -> {"id"}." as $renumbered");
|
||||||
|
|
||||||
# Create the new recipe at the old ID
|
# Create the new recipe at the old ID
|
||||||
$self -> create($args)
|
$self -> create($args)
|
||||||
@ -224,8 +231,6 @@ sub edit {
|
|||||||
$args -> {"updaterid"})
|
$args -> {"updaterid"})
|
||||||
or return undef;
|
or return undef;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return $args -> {"id"};
|
return $args -> {"id"};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -802,7 +807,7 @@ sub _get_tags {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
## @method private $ _renumber_recipe($sourceid)
|
## @method private $ _renumber_recipe($sourceid, $contextid)
|
||||||
# Given a recipe ID, move the recipe to a new ID at the end of the recipe
|
# Given a recipe ID, move the recipe to a new ID at the end of the recipe
|
||||||
# table. This will move the recipe and all relations involving it, to
|
# table. This will move the recipe and all relations involving it, to
|
||||||
# a new ID at the end of the table, leaving the source ID available for
|
# a new ID at the end of the table, leaving the source ID available for
|
||||||
@ -810,11 +815,13 @@ sub _get_tags {
|
|||||||
# an autoincrement, reusing the ID will require explicit specification
|
# an autoincrement, reusing the ID will require explicit specification
|
||||||
# of the ID in the insert.
|
# of the ID in the insert.
|
||||||
#
|
#
|
||||||
# @param sourceid The ID of the recipe to move.
|
# @param sourceid The ID of the recipe to move.
|
||||||
|
# @param contextid The metadata context ID of the source recipe.
|
||||||
# @return The new ID of the recipe on success, undef on error.
|
# @return The new ID of the recipe on success, undef on error.
|
||||||
sub _renumber_recipe {
|
sub _renumber_recipe {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $sourceid = shift;
|
my $sourceid = shift;
|
||||||
|
my $contextid = shift;
|
||||||
|
|
||||||
$self -> clear_error();
|
$self -> clear_error();
|
||||||
|
|
||||||
@ -833,7 +840,7 @@ sub _renumber_recipe {
|
|||||||
or return $self -> self_error("Unable to obtain id for new recipe");
|
or return $self -> self_error("Unable to obtain id for new recipe");
|
||||||
|
|
||||||
# Move all the old ingredient and tage relations to the copy we've just made
|
# Move all the old ingredient and tage relations to the copy we've just made
|
||||||
$self -> _fix_recipe_relations($sourceid, $newid)
|
$self -> _fix_recipe_relations($sourceid, $newid, $contextid)
|
||||||
or return undef;
|
or return undef;
|
||||||
|
|
||||||
# Nuke the old recipe
|
# Nuke the old recipe
|
||||||
@ -848,18 +855,20 @@ sub _renumber_recipe {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
## @method private $ _fix_recipe_relations($sourceid, $destid)
|
## @method private $ _fix_recipe_relations($sourceid, $destid, $contextid)
|
||||||
# Correct all relations to the source recipe so that they refer to the
|
# Correct all relations to the source recipe so that they refer to the
|
||||||
# destination. This is used as part of the renumbering process to
|
# destination. This is used as part of the renumbering process to
|
||||||
# fix up any relations that use the old recipe Id to use the new one.
|
# fix up any relations that use the old recipe Id to use the new one.
|
||||||
#
|
#
|
||||||
# @param sourceid The ID of the old recipe.
|
# @param sourceid The ID of the old recipe.
|
||||||
# @param destid The ID of the new recipe.
|
# @param destid The ID of the new recipe.
|
||||||
|
# @param contextid The metadata context ID of the source recipe.
|
||||||
# @return true on success, undef on error.
|
# @return true on success, undef on error.
|
||||||
sub _fix_recipe_relations {
|
sub _fix_recipe_relations {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $sourceid = shift;
|
my $sourceid = shift;
|
||||||
my $destid = shift;
|
my $destid = shift;
|
||||||
|
my $contextid = shift;
|
||||||
|
|
||||||
$self -> clear_error();
|
$self -> clear_error();
|
||||||
|
|
||||||
@ -877,11 +886,13 @@ sub _fix_recipe_relations {
|
|||||||
$moveh -> execute($destid, $sourceid)
|
$moveh -> execute($destid, $sourceid)
|
||||||
or return $self -> self_error("Ingredient relation fixup failed: ".$self -> {"dbh"} -> errstr());
|
or return $self -> self_error("Ingredient relation fixup failed: ".$self -> {"dbh"} -> errstr());
|
||||||
|
|
||||||
|
my $metadataid = $self -> _create_recipe_metadata($contextid);
|
||||||
|
|
||||||
# And set the original ID in the renumbered recipe
|
# And set the original ID in the renumbered recipe
|
||||||
my $origh = $self -> {"dbh"} -> prepare("UPDATE `".$self -> {"settings"} -> {"database"} -> {"recipes"}."`
|
my $origh = $self -> {"dbh"} -> prepare("UPDATE `".$self -> {"settings"} -> {"database"} -> {"recipes"}."`
|
||||||
SET `original_id` = ?
|
SET `original_id` = ?, `metadata_id` = ?
|
||||||
WHERE `id` = ?");
|
WHERE `id` = ?");
|
||||||
$origh -> execute($sourceid, $destid)
|
$origh -> execute($sourceid, $metadataid, $destid)
|
||||||
or return $self -> self_error("Ingredient origin fixup failed: ".$self -> {"dbh"} -> errstr());
|
or return $self -> self_error("Ingredient origin fixup failed: ".$self -> {"dbh"} -> errstr());
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@ -921,7 +932,7 @@ sub get_recipe_metadata {
|
|||||||
# as a child of the metadata context for the specific previous recipe, if one
|
# as a child of the metadata context for the specific previous recipe, if one
|
||||||
# is provided, to allow cascading permissions.
|
# is provided, to allow cascading permissions.
|
||||||
#
|
#
|
||||||
# @param previd Optional ID of the previous recipe. This should be set when
|
# @param previd Optional metadata ID of the previous recipe. This should be set when
|
||||||
# editing a recipe; for new recipes, leave this as undef or 0.
|
# editing a recipe; for new recipes, leave this as undef or 0.
|
||||||
# @return The ID of a new metadata context to attach the recipe to on success,
|
# @return The ID of a new metadata context to attach the recipe to on success,
|
||||||
# undef on error.
|
# undef on error.
|
||||||
@ -929,12 +940,8 @@ sub _create_recipe_metadata {
|
|||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $previd = shift;
|
my $previd = shift;
|
||||||
|
|
||||||
if($previd) {
|
return $self -> {"metadata"} -> create($previd)
|
||||||
my $parentid = $self -> get_recipe_metadata($previd)
|
if($previd);
|
||||||
or return undef;
|
|
||||||
|
|
||||||
return $self -> {"metadata"} -> create($parentid);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $self -> {"metadata"} -> create($self -> {"settings"} -> {"config"} -> {"Recipe:base_metadata"} // 1);
|
return $self -> {"metadata"} -> create($self -> {"settings"} -> {"config"} -> {"Recipe:base_metadata"} // 1);
|
||||||
}
|
}
|
||||||
|
@ -140,5 +140,5 @@ $(function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Build the ingredient list before submitting
|
// Build the ingredient list before submitting
|
||||||
$('#newrecipe').on('submit', function() { build_ingdata(); return true });
|
$('#recipeform').on('submit', function() { build_ingdata(); return true });
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
%(errors)s
|
%(errors)s
|
||||||
<div class="small-8 small-offset-2 cell">
|
<div class="small-8 small-offset-2 cell">
|
||||||
<form class="nomargin" method="POST" id="newrecipe">
|
<form class="nomargin" method="POST" id="recipeform">
|
||||||
<h4 class="underscore">{L_NEW_TITLE}</h4>
|
<h4 class="underscore">{L_NEW_TITLE}</h4>
|
||||||
<div>
|
<div>
|
||||||
<label>{L_RECIPE_NAME}
|
<label>{L_RECIPE_NAME}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<li class="separator">
|
<li class="separator">
|
||||||
<div class="grid-x">
|
<div class="grid-x">
|
||||||
<div class="small-11 cell">
|
<div class="small-11 cell">
|
||||||
<input type="text" placeholder="{L_RECIPE_ING_SEP_PH}" value="%(name)s" />
|
<input type="text" class="separator" placeholder="{L_RECIPE_ING_SEP_PH}" value="%(name)s" />
|
||||||
</div>
|
</div>
|
||||||
<div class="small-1 cell">
|
<div class="small-1 cell">
|
||||||
<button class="button alert deletectrl" type="button" title="{L_RECIPE_ING_DELETE}"><i class="fa fa-trash" aria-hidden="true"></i></button>
|
<button class="button alert deletectrl" type="button" title="{L_RECIPE_ING_DELETE}"><i class="fa fa-trash" aria-hidden="true"></i></button>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user