Konstrukt

 view release on metacpan or  search on metacpan

lib/Konstrukt/Doc/Tutorial/Plugin/Note/DBI.pod  view on Meta::CPAN

	
	<div class="note message success">
		<h1>Note updated!</h1>
		<p>The note has been updated successfully.</p>
	</div>
	
	-- 8< -- textfile: messages/delete_successful.template -- >8 --
	
	<div class="note message success">
		<h1>Note deleted!</h1>
		<p>The note has been deleted successfully.</p>
	</div>

=head2 Backend

	package Konstrukt::Plugin::note::DBI;
	
	use strict;
	use warnings;
	
	use base 'Konstrukt::Plugin';
	
	sub init {
		my ($self) = @_;
		
		my $db_source = $Konstrukt::Settings->get('blog/backend/DBI/source');
		my $db_user   = $Konstrukt::Settings->get('blog/backend/DBI/user');
		my $db_pass   = $Konstrukt::Settings->get('blog/backend/DBI/pass');
		
		$self->{db_settings} = [$db_source, $db_user, $db_pass];
		
		return 1;
	}
	
	sub install {
		my ($self) = @_;
		return $Konstrukt::Lib->plugin_dbi_install_helper($self->{db_settings});
	}
	
	sub get_notes {
		my ($self, $id) = @_;
		
		my $dbh = $Konstrukt::DBI->get_connection(@{$self->{db_settings}}) or return undef;
		my $where = (defined $id ? "WHERE id = " . int($id) : "");
		
		my $rv = $dbh->selectall_arrayref("SELECT id, text FROM note $where ORDER BY id", { Columns => {} });
		$rv = $rv->[0] if defined $id and defined $rv;
		
		return $rv;
	}
	
	sub add_note {
		my ($self, $text) = @_;
		
		my $dbh = $Konstrukt::DBI->get_connection(@{$self->{db_settings}}) or return undef;
		$text = $dbh->quote($text);
		
		return $dbh->do("INSERT INTO note(text) VALUES($text)");
	}
	
	sub update_note {
		my ($self, $id, $text) = @_;
		
		my $dbh = $Konstrukt::DBI->get_connection(@{$self->{db_settings}}) or return undef;
		$id   = $dbh->quote($id);
		$text = $dbh->quote($text);
		
		return $dbh->do("UPDATE note SET text = $text WHERE id = $id");
	}
	
	sub delete_note {
		my ($self, $id) = @_;
		
		my $dbh = $Konstrukt::DBI->get_connection(@{$self->{db_settings}}) or return undef;
		return $dbh->do("DELETE FROM note WHERE id = " . int($id));
	}
	
	
	1;
	
	__DATA__
	
	-- 8< -- dbi: create -- >8 --
	
	CREATE TABLE IF NOT EXISTS note
	(
	  id          INT UNSIGNED     NOT NULL AUTO_INCREMENT,
	  text        TEXT             NOT NULL,
	  PRIMARY KEY(id)
	);

=head2 Page

	<& note / &>
	
	<a href="?note_action=add">[ add note ]</a>
	<br />
	<a href="?">[ all notes ]</a>

=head1 AUTHOR

Copyright 2006 Thomas Wittek (mail at gedankenkonstrukt dot de). All rights reserved. 

This document is free software.
It is distributed under the same terms as Perl itself.

=head1 SEE ALSO

Previous: L<Konstrukt::Doc::Tutorial::Plugin::Note::Template>

Parent: L<Konstrukt::Doc>

See also: L<Konstrukt::SimplePlugin>, L<Konstrukt::Doc::CreatingPlugins>, L<Konstrukt::DBI>, L<DBI>

=cut



( run in 0.826 second using v1.01-cache-2.11-cpan-97f6503c9c8 )