Konstrukt

 view release on metacpan or  search on metacpan

lib/Konstrukt/Plugin/blog/DBI.pm  view on Meta::CPAN


=over

=item * $title - The title of this entry

=item * $description - A short abstract of this entry

=item * $content - The entry's content (usually wiki "source code")

=item * $author - The entry's author

=item * $private - Is this entry only visible to the author?

=back

=cut
sub add_entry {
	my ($self, $title, $description, $content, $author, $private) = @_;
	
	my $dbh = $Konstrukt::DBI->get_connection(@{$self->{db_settings}}) or return undef;
	
	#quoting
	$title       = $dbh->quote($title       || '');
	$description = $dbh->quote($description || '');
	$content     = $dbh->quote($content     || '');
	$author      = $dbh->quote($author      ||  0);
	$private     = $dbh->quote($private     ||  0);
	
	#insert blog entry
	my $query = "INSERT INTO blog_entry (title, description, content, author, private, date) VALUES ($title, $description, $content, $author, $private, NOW())";
	$dbh->do($query) or return;
	
	#id of added entry
	return $dbh->last_insert_id(undef, undef, undef, undef) || undef;
}
#= /add_entry

=head2 update_entry

Updates an existing blog entry.

B<Parameters>:

=over

=item * $id          - The id of the entry, which should be updated

=item * $title       - The title of this entry

=item * $description - A short abstract of this entry

=item * $content     - The entry's content

=item * $private     - Is this entry only visible to the author?

=item * $update      - Update the publication date to "now"

=back

=cut
sub update_entry {
	my ($self, $id, $title, $description, $content, $private, $update) = @_;
	
	my $dbh = $Konstrukt::DBI->get_connection(@{$self->{db_settings}}) or return undef;
	
	#quoting
	$title       = $dbh->quote($title       || '');
	$description = $dbh->quote($description || '');
	$content     = $dbh->quote($content     || '');
	$private     = $dbh->quote($private     ||  0);
	
	$update = ($update ? ", date = NOW()" : "");
	
	#update blog entry
	my $query = "UPDATE blog_entry SET title = $title, description = $description, content = $content, private = $private $update WHERE id = " . int($id);
	return $dbh->do($query);
}
#= /update_entry

=head2 get_entry

Returns the requested blog entry as an hash reference with the keys id, title,
description, content, author, year, month, day, hour, minute, private,
comment_count and trackback_count.

Returns C<undef> if the entry does not exist.

B<Parameters>:

=over

=item * $id - The id of the entry

=back

=cut
sub get_entry {
	my ($self, $id) = @_;
	
	my $rv = $self->get_entries({id => $id});
	
	return (@{$rv} ? $rv->[0] : undef);
}
#= /get_entry

=head2 get_entries_count

Returns the count of the entries.

=cut
sub get_entries_count {
	my ($self) = @_;
	
	my $dbh = $Konstrukt::DBI->get_connection(@{$self->{db_settings}}) or return -1;
	return $dbh->selectrow_array('SELECT COUNT(id) FROM blog_entry');
}
#= /get_entries_count

=head2 get_entries

Returns the blog entries as an array reference of hash references:



( run in 0.825 second using v1.01-cache-2.11-cpan-bbe5e583499 )