#!/usr/bin/perl -w package Recipe; use strict; use base qw(CGI::Ex::App); use CGI::Ex::Dump qw(debug); use DBI; Recipe->navigate; ###------------------------------------------### sub handle_error { my ($self, $error) = @_; debug $error, $self->dump_history; die $error; } sub post_navigate { # show what happened during development debug shift->dump_history; } sub base_dir_abs { '/var/www/templates' } sub base_dir_rel { 'content' } sub db_conn { ['dbi:mysql:my_db', 'my_user', 'my_pass'] } sub dbh { my $self = shift; if (! $self->{'dbh'}) { my $conn = $self->db_conn; $self->{'dbh'} = DBI->connect(@$conn, {RaiseError => 1}); $self->create_tables if ! grep {/^`?recipe`?$/} ($self->{'dbh'}->tables); } return $self->{'dbh'}; } sub create_tables { my $self = shift; $self->dbh->do("CREATE TABLE recipe ( id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT, title VARCHAR(50) NOT NULL, ingredients VARCHAR(255) NOT NULL, directions VARCHAR(255) NOT NULL, date_added DATETIME NOT NULL )"); } ###----------------------------------------------------------------### sub main_info_complete { 0 } sub main_hash_swap { my $self = shift; my $s = "SELECT id, title, date_added FROM recipe ORDER BY date_added"; my $data = $self->dbh->selectall_arrayref($s); my @data = map {my %h; @h{qw(id title date_added)} = @$_; \%h} @$data; return { recipies => \@data, }; } ###----------------------------------------------------------------### sub add_file_print { shift->edit_file_print('edit') } sub add_hash_validation { return { 'group order' => [qw(title ingredients directions)], title => { required => 1, max_len => 30, }, ingredients => { required => 1, max_len => 255, }, directions => { required => 1, max_len => 255, }, }; } sub add_finalize { my $self = shift; my $form = $self->form; my $s = "SELECT COUNT(*) FROM recipe WHERE title = ?"; my ($count) = $self->dbh->selectrow_array($s, {}, $form->{'title'}); if ($count) { $self->add_errors(title => 'A recipe by this title already exists'); return 0; } $s = "INSERT INTO recipe (title, ingredients, directions, date_added) VALUES (?, ?, ?, NOW())"; $self->dbh->do($s, {}, $form->{'title'}, $form->{'ingredients'}, $form->{'directions'}); $self->add_to_form(success => "Recipe added to the database"); return 1; } ###----------------------------------------------------------------### sub edit_skip { shift->form->{'id'} ? 0 : 1 } sub edit_hash_common { my $self = shift; return {} if $self->ready_validate; my $sth = $self->dbh->prepare("SELECT * FROM recipe WHERE id = ?"); $sth->execute($self->form->{'id'}); my $hash = $sth->fetchrow_hashref; return $hash; } sub edit_hash_validation { my $hash = shift->add_hash_validation(@_); $hash->{'id'} = {required => 1, match => 'm/^\d+$/'}; # check to make sure id is valid return $hash; } sub edit_finalize { my $self = shift; my $form = $self->form; my $s = "SELECT COUNT(*) FROM recipe WHERE title = ? AND id != ?"; my ($count) = $self->dbh->selectrow_array($s, {}, $form->{'title'}, $form->{'id'}); if ($count) { $self->add_errors(title => 'A recipe by this title already exists'); return 0; } $s = "UPDATE recipe SET title = ?, ingredients = ?, directions = ? WHERE id = ?"; $self->dbh->do($s, {}, $form->{'title'}, $form->{'ingredients'}, $form->{'directions'}, $form->{'id'}); $self->add_to_form(success => "Recipe updated in the database"); return 1; } ###----------------------------------------------------------------### sub view_skip { shift->edit_skip(@_) } sub view_hash_common { shift->edit_hash_common(@_) } ###----------------------------------------------------------------### sub delete_skip { shift->edit_skip(@_) } sub delete_info_complete { 1 } sub delete_finalize { my $self = shift; $self->dbh->do("DELETE FROM recipe WHERE id = ?", {}, $self->form->{'id'}); $self->add_to_form(success => "Recipe deleted from the database"); return 1; } #File: /var/www/templates/content/recipe/main.html ### -------------------------------------------- sub main_file_print { return \ q{
# | Title | Date Added |
---|---|---|
[% loop.count %]. | [% row.title %] (Edit) | [% row.date_added %] |
Add new recipe |