The ENCY controller is responsible for handling requests related to the ENCY model. It includes several subroutines, each of which performs a specific task.
The index subroutine handles requests to the root of the ENCY controller's URL. It sets the template to 'ENCY/index.tt', which is used to render the response.
sub index :Path('/ENCY') :Args(0) {
my ( $self, $c ) = @_;
# The index action will display the 'index.tt' template
$c->stash(template => 'ENCY/index.tt');
}
The botanical_name_view subroutine handles requests to display the Botanical Name View page. It fetches herbal data from the 'DBForager' model and passes this data to the template.
sub index :Path('/ENCY') :Args(0) {
my ( $self, $c ) = @_;
# The index action will display the 'index.tt' template
$c->stash(template => 'ENCY/index.tt');
}
The botanical_name_view subroutine handles requests to display the Botanical Name View page. It fetches herbal data from the 'DBForager' model and passes this data to the template.
sub botanical_name_view :Path('/ENCY/BotanicalNameView') :Args(0) {
my ( $self, $c ) = @_;
# Fetch the herbal data
my $forager_data = $c->model('DBForager')->get_herbal_data();
# Pass the data to the template
my $herbal_data = $forager_data;
$c->stash(herbal_data => $herbal_data, template => 'ENCY/BotanicalNameView.tt');
}
The herb_detail subroutine handles requests to display detailed information about a specific herb. It takes an id as an argument, fetches the corresponding herb from the 'DBForager' model, and sets the herb and the template 'ENCY/HerbDetailView.tt' in the stash.
sub herb_detail :Path('/ENCY/herb_detail') :Args(1) {
my ( $self, $c, $id ) = @_;
my $herb = $c->model('DBForager')->get_herb_by_id($id);
$c->stash(herb => $herb, template => 'ENCY/HerbDetailView.tt');
}
The add_herb subroutine handles requests to add a new herb. It processes the form submission and saves the new herb to the database.
The get_reference_by_id subroutine handles requests to get a reference by its id. It takes an id as an argument, fetches the corresponding reference from the 'ENCY' model, and sets the reference and the template 'ency/get_reference_form.tt' in the stash.
sub get_reference_by_id :Local {
my ( $self, $c, $id ) = @_;
my $reference = $c->model('ENCY')->get_reference_by_id($id);
$c->stash(reference => $reference, template => 'ency/get_reference_form.tt');
}
The create_reference subroutine handles requests to display the form for creating a new reference. It sets the template 'ency/create_reference_form.tt' in the stash.
sub add_herb :Path('/ENCY/add_herb') :Args(0) {
my ( $self, $c ) = @_;
if ($c->request->method eq 'POST') {
# Handle form submission
my $form_data = $c->request->body_parameters;
my $new_herb = {
therapeutic_action => $form_data->{therapeutic_action},
botanical_name => $form_data->{botanical_name},
common_names => $form_data->{common_names},
parts_used => $form_data->{parts_used},
comments => $form_data->{comments},
medical_uses => $form_data->{medical_uses},
homiopathic => $form_data->{homiopathic},
ident_character => $form_data->{ident_character},
image => $form_data->{image},
stem => $form_data->{stem},
nectar => $form_data->{nectar},
pollinator => $form_data->{pollinator},
pollen => $form_data->{pollen},
leaves => $form_data->{leaves},
flowers => $form_data->{flowers},
fruit => $form_data->{fruit},
taste => $form_data->{taste},
odour => $form_data->{odour},
distribution => $form_data->{distribution},
url => $form_data->{url},
root => $form_data->{root},
constituents => $form_data->{constituents},
solvents => $form_data->{solvents},
chinese => $form_data->{chinese},
culinary => $form_data->{culinary},
contra_indications => $form_data->{contra_indications},
dosage => $form_data->{dosage},
administration => $form_data->{administration},
formulas => $form_data->{formulas},
vetrinary => $form_data->{vetrinary},
cultivation => $form_data->{cultivation},
sister_plants => $form_data->{sister_plants},
harvest => $form_data->{harvest},
non_med => $form_data->{non_med},
history => $form_data->{history},
reference => $form_data->{reference},
username_of_poster => $c->session->{username},
group_of_poster => $c->session->{group},
date_time_posted => \'NOW()', # Assuming you want to set this to the current timestamp
share => $form_data->{share} // 0,
should_display => $form_data->{should_display} // 0,
preperation => $form_data->{preperation},
pollennotes => $form_data->{pollennotes},
nectarnotes => $form_data->{nectarnotes},
apis => $form_data->{apis},
};
# Save the new herb using the ENCYModel
$c->model('ENCYModel')->add_herb($new_herb);
# Redirect or display a success message
$c->flash->{success_message} = 'Herb added successfully';
$c->res->redirect($c->uri_for($self->action_for('index')));
} else {
# Display the form
$c->stash(
template => 'ENCY/add_herb_form.tt',
user_role => $c->session->{roles} # Pass user role to the template
);
}
$c->stash(template => 'ency/create_reference_form.tt');
}
The get_category_by_id subroutine handles requests to get a category by its id. It takes an id as an argument, fetches the corresponding category from the 'ENCY' model, and sets the category and the template 'ency/get_category_form.tt' in the stash.
sub get_category_by_id :Local {
my ( $self, $c, $id ) = @_;
my $category = $c->model('ENCY')->get_category_by_id($id);
$c->stash(category => $category, template => 'ency/get_category_form.tt');
}
For more information on how AI can enhance the functionalities of the ENCY module, refer to the AI Integration Plan.
The create_category subroutine handles requests to display the form for creating a new category. It sets the template 'ency/create_category_form.tt' in the stash.
sub create_category :Local {
my ( $self, $c ) = @_;
$c->stash(template => 'ency/create_category_form.tt');
}
The edit_herb subroutine handles requests to edit an existing herb. It processes the form submission and updates the herb in the database.
sub edit_herb :Path('/ENCY/edit_herb') :Args(0) {
my ( $self, $c ) = @_;
my $form_data = $c->request->params;
# Ensure all required fields are captured and logged
$self->logging->log_with_details('info', "Form data: " . join(", ", map { "$_: $form_data->{$_}" } keys %$form_data));
if ($c->request->method eq 'POST') {
my $id = $c->session->{record_id}; # Retrieve the record_id from the session
# Add all specified fields to the updated_herb hash
my $updated_herb = {
botanical_name => $form_data->{botanical_name} // '',
common_names => $form_data->{common_names} // '',
parts_used => $form_data->{parts_used} // '',
comments => $form_data->{comments} // '',
contra_indications => $form_data->{contra_indications} // '',
preparation => $form_data->{preparation} // '',
chinese => $form_data->{chinese} // '',
vetrinary => $form_data->{vetrinary} // '',
homiopathic => $form_data->{homiopathic} // '',
key_name => $form_data->{key_name} // '', # Added key_name
url => $form_data->{url} // '', # Added url
apis => $form_data->{apis} // 0, # Ensure apis is an integer
pollinator => $form_data->{pollinator} // '',
pollen => $form_data->{pollen} // 0, # Ensure pollen is an integer
pollennotes => $form_data->{pollennotes} // '',
parts_used => $form_data->{parts_used} // '', # Added parts_used
};
# Log the updated herb data
$self->logging->log_with_details('info', "Updated herb data: " . join(", ", map { "$_: $updated_herb->{$_}" } keys %$updated_herb));
my ($success, $message) = $c->model('ENCYModel')->update_herb($id, $updated_herb);
if (!$success) {
$self->logging->log_with_details('error', "Failed to update herb: $message");
$c->stash(
error_message => $message,
mode => 'edit',
herb => $form_data,
template => 'ENCY/HerbView.tt'
);
} else {
$c->flash->{success_message} = $message;
$c->res->redirect($c->uri_for($self->action_for('herb_detail'), [$id]));
}
} else {
my $id = $c->request->params->{id}; # Retrieve the id from the request parameters
my $herb = $c->model('DBForager')->get_herb_by_id($id);
$c->session->{record_id} = $id; # Store the id in the session
$c->stash(
mode => 'edit',
herb => $herb,
template => 'ENCY/HerbView.tt'
);
}
}
The add_herb subroutine handles requests to add a new herb. It processes the form submission and saves the new herb to the database.
sub add_herb :Path('/ENCY/add_herb') :Args(0) {
my ( $self, $c ) = @_;
if ($c->request->method eq 'POST') {
# Handle form submission
my $form_data = $c->request->body_parameters;
my $new_herb = {
therapeutic_action => $form_data->{therapeutic_action},
botanical_name => $form_data->{botanical_name},
common_names => $form_data->{common_names},
parts_used => $form_data->{parts_used},
comments => $form_data->{comments},
medical_uses => $form_data->{medical_uses},
homiopathic => $form_data->{homiopathic},
ident_character => $form_data->{ident_character},
image => $form_data->{image},
stem => $form_data->{stem},
nectar => $form_data->{nectar},
pollinator => $form_data->{pollinator},
pollen => $form_data->{pollen},
leaves => $form_data->{leaves},
flowers => $form_data->{flowers},
fruit => $form_data->{fruit},
taste => $form_data->{taste},
odour => $form_data->{odour},
distribution => $form_data->{distribution},
url => $form_data->{url},
root => $form_data->{root},
constituents => $form_data->{constituents},
solvents => $form_data->{solvents},
chinese => $form_data->{chinese},
culinary => $form_data->{culinary},
contra_indications => $form_data->{contra_indications},
dosage => $form_data->{dosage},
administration => $form_data->{administration},
formulas => $form_data->{formulas},
vetrinary => $form_data->{vetrinary},
cultivation => $form_data->{cultivation},
sister_plants => $form_data->{sister_plants},
harvest => $form_data->{harvest},
non_med => $form_data->{non_med},
history => $form_data->{history},
reference => $form_data->{reference},
username_of_poster => $c->session->{username},
group_of_poster => $c->session->{group},
date_time_posted => \'NOW()', # Assuming you want to set this to the current timestamp
share => $form_data->{share} // 0,
should_display => $form_data->{should_display} // 0,
preperation => $form_data->{preperation},
pollennotes => $form_data->{pollennotes},
nectarnotes => $form_data->{nectarnotes},
apis => $form_data->{apis},
};
# Save the new herb using the ENCYModel
$c->model('ENCYModel')->add_herb($new_herb);
# Redirect or display a success message
$c->flash->{success_message} = 'Herb added successfully';
$c->res->redirect($c->uri_for($self->action_for('index')));
} else {
# Display the form
$c->stash(
template => 'ENCY/add_herb_form.tt',
user_role => $c->session->{roles} # Pass user role to the template
);
}
}
The search subroutine handles requests to search for herbs. It processes the search string and fetches the results from the 'DBForager' model.
sub search :Path('/ENCY/search') :Args(0) {
my ($self, $c) = @_;
my $search_string = $c->request->parameters->{search_string};
# Call the searchHerbs method in the DBForager model
my $results = $c->model('DBForager')->searchHerbs($c, $search_string);
# Stash the results for the view
$c->stash(herbal_data => $results); # Changed from 'results' to 'herbal_data'
# Get the referer from the request headers
my $referer = $c->req->headers->referer;
# Extract the template name from the referer
$c->stash(template => 'ENCY/BotanicalNameView.tt');
}
There was an issue with the `HerbView.tt` form where the values were not being saved into the database. The form data was not being correctly passed to the `edit_herb` subroutine, and the existing functionality was inadvertently removed during debugging attempts. The goal was to ensure that the form data is correctly passed and saved without removing any existing functionality.
Each of these subroutines relies on the ENCY model to perform its tasks. The ENCY model is loaded in the ENCY controller with the use Comserv::Model::ENCY; statement. The ENCY model is defined in the 'Comserv::Model::ENCY' package, which is located in the 'lib/Comserv/Model' directory.
To evolve the ENCY application, consider the following enhancements:
The ENCY model is responsible for handling data related to the ENCY controller. It includes several methods, each of which performs a specific task.
The 'get_reference_by_id' method is responsible for fetching a reference by its id. It takes an id as an argument, fetches the corresponding reference from the database, and returns the reference.
The 'create_reference' method is responsible for creating a new reference. It takes the necessary data as arguments, creates a new reference in the database, and returns the new reference.
The 'get_category_by_id' method is responsible for fetching a category by its id. It takes an id as an argument, fetches the corresponding category from the database, and returns the category.
The 'create_category' method is responsible for creating a new category. It takes the necessary data as arguments, creates a new category in the database, and returns the new category.
Each of these methods is used by the ENCY controller to perform its tasks. The ENCY model is defined in the 'Comserv::Model::ENCY' package, which is located in the 'lib/Comserv/Model' directory.
package Comserv::Model::Schema::Forager::Result::Herb;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->load_components('InflateColumn::DateTime', 'TimeStamp', 'EncodedColumn');
__PACKAGE__->table('ency_herb_tb');
__PACKAGE__->add_columns(
'therapeutic_action',
'record_id',
'apis',
'botanical_name',
'common_names',
'key_name',
'parts_used',
'comments',
'medical_uses',
'homiopathic',
'ident_character',
'image',
'stem',
'nectar',
'pollinator',
'pollen',
'leaves',
'flowers',
'fruit',
'taste',
'odour',
'distribution',
'url',
'root',
'constituents',
'solvents',
'chinese',
'culinary',
'contra_indications',
'dosage',
'administration',
'formulas',
'vetrinary',
'cultivation',
'sister_plants',
'harvest',
'non_med',
'history',
'reference',
'username_of_poster',
'group_of_poster',
'date_time_posted',
'share' => { data_type => 'integer', default_value => 0, is_nullable => 0 },
'should_display' => { data_type => 'integer', default_value => 0, is_nullable => 0 },
'preperation' => { data_type => 'varchar', size => 150, is_nullable => 0 },
'pollennotes' => { data_type => 'text', is_nullable => 0 },
'nectarnotes' => { data_type => 'text', is_nullable => 0 },
'apis' => { data_type => 'varchar', size => 100, is_nullable => 0 },
);
__PACKAGE__->set_primary_key('record_id');
1;
Controller
The goal of ENCY is to provide a comprehensive database of herbs and their uses. We will be starting with plants that have medicinal properties, but the database can be expanded to include other types of plants as well. They will be categorized by their botanical names, common names, and uses. The database will also include references to scientific studies and other sources of information about the plants.