package OnTheBeachDB::Messages; use base qw/DBIx::Class/; # Load required DBIC stuff __PACKAGE__->load_components(qw/PK::Auto Core/); # Set the table name __PACKAGE__->table('messages'); # Set columns in table __PACKAGE__->add_columns(qw/message_id parent_id created title name link message/); # Set the primary key for the table __PACKAGE__->set_primary_key(qw/message_id/); ########################################### ## ## THIS WHOLE THING IS TO BECOME A ResultSet CLASS ## ########################################### sub create { ## MAKE ME PLUGINS ##### require HTML::Scrubber; require DateTime; #POSIX; ######################## my ( $self, $c, $name, $title, $message, $parent_id ) = @_; ## hackish right now, needs to go in Forum::Util ## or some such my @lt = localtime(time); my $date = DateTime->now; #POSIX::strftime('%Y-%m-%d %H:%M:%S', @lt); ## create the HTML::Scrubber object to un-taint ## form data and dis/allow HTML my $scrubber = HTML::Scrubber->new( allow => [ qw[ p b i u hr br ] ] ); # cleaninate! my $clean_name = $scrubber->scrub($name); my $clean_title = $scrubber->scrub($title); my $clean_message = $scrubber->scrub($message); # insert into db $c->model('OnTheBeachDB::Messages')->create({ name => $clean_name, title => $clean_title, message => $clean_message, created => $date, parent_id => $parent_id, }); } sub get_thread_ids { my ( $self, $c ) = @_; my @ids; my $rs = $c->model('OnTheBeachDB::Messages')->search({ parent_id => '0' }); while ( my $id = $rs->next ) { push ( @ids, $id->message_id ); } return @ids; } sub get_thread_messages { my ($self, $c, $parent_id, $depth, $replies ) = @_; my $output; ## if no replies, set up the link given if ( !$replies) { $output .= $self->generate_link( $c, $parent_id, $depth ); } ## Get the ids of any children my $rs = $c->model('OnTheBeachDB::Messages')->search( { parent_id => $parent_id }, { group_by => 'created', } ); ## Loop on each child found, if it has children, ## recurse on this function, if now just generate it's link while ( my $id = $rs->next ) { ## Let's see if this child message has children of it's own if ( $self->has_children( $c, $id->message_id ) ) { $output .= $self->get_thread_messages( $c, $id->message_id, $depth + 2 ); } else { $output .= $self->generate_link( $c, $id->message_id, $depth + 2 ); } } return $output; } ## See if this ID has any children sub has_children { my ( $self, $c, $id ) = @_; my $count = $c->model('OnTheBeachDB::Messages')->search( { message_id => $id, } )->count(); if ( $count > 0 ) { return 1; } else { return 0; } } ## Count how many children this thread has sub number_of_children { my ( $self, $c, $id ) = @_; my $count = $c->model('OnTheBeachDB::Messages')->search( { parent_id => $id, } )->count(); return $count; } ## This is what you will see on the actual page ## Generate a link, with username, thread title, date ## Add    per $depth size sub generate_link { my ( $self, $c, $id, $depth ) = @_; my $output; my $rs = $c->model('OnTheBeachDB::Messages')->find($id); my $title = $rs->title; my $name = $rs->name; my $created = $rs->created; my $thread_text = $rs->message; ##### DEFINITELY want to get this into a template somehow. Todo list. ## ## Indendation multiplied by the supplied $depth number my $spaces = '  ' x $depth; $output .= $spaces . qq!! . qq!$title! . qq! by $name! . qq! on $created
\n! . $spaces . qq!$thread_text
\n! . $spaces . qq!comment on ! . $title . qq!

!; ################## } =head1 NAME OnTheBeachDB::Message - A model object representing a forum message. =head1 DESCRIPTION =cut 1;