package WebGUI::Asset::Wobject::Shoutbox; $VERSION = "1.0.0"; #------------------------------------------------------------------- # WebGUI is Copyright 2001-2006 Plain Black Corporation. #------------------------------------------------------------------- # Please read the legal notices (docs/legal.txt) and the license # (docs/license.txt) that came with this distribution before using # this software. #------------------------------------------------------------------- # http://www.plainblack.com info@plainblack.com #------------------------------------------------------------------- =head1 NAME WebGUI::Asset::Wobject::Shoutbox - Real-time chat on your website =head1 DESCRIPTION TODO =head1 INSTALL / UNINSTALL TODO =head1 METHODS =cut use strict; use Tie::IxHash; use WebGUI::International; use WebGUI::Utility; use JSON; use base 'WebGUI::Asset::Wobject'; #------------------------------------------------------------------- =head2 definition ( ) =cut sub definition { my $class = shift; my $session = shift; my $definition = shift; tie my %properties, 'Tie::IxHash', ( templateIdView => { fieldType => "template", namespace => "Shoutbox", tab => "display", label => "Template to View", hoverHelp => "Template for normal view.", }, ); push @{$definition}, { assetName => 'Shoutbox', icon => 'newWobject.gif', autoGenerateForms => 1, tableName => 'Shoutbox', className => 'WebGUI::Asset::Wobject::Shoutbox', properties => \%properties, }; return $class->SUPER::definition($session, $definition); } #------------------------------------------------------------------- =head2 getTemplateVars ( ) Gets a hashref of template vars about this asset. =cut sub getTemplateVars { my $self = shift; my $var = $self->get; # Get a friendly URL $var->{ url } = $self->getUrl; return $var; } #------------------------------------------------------------------- =head2 prepareView ( ) See WebGUI::Asset::prepareView() for details. =cut sub prepareView { my $self = shift; $self->SUPER::prepareView(); my $template = WebGUI::Asset::Template->new($self->session, $self->get("templateIdView")); $template->prepare; $self->{_viewTemplate} = $template; } #------------------------------------------------------------------- =head2 view ( ) View the shoutbox asset. View the form. URL Parameters: bodyText - Post some text to the shoutbox from - (Visitors only) say who you are =cut sub view { my $self = shift; my $session = $self->session; my $form = $session->form; my $db = $session->db; my $var = $self->getTemplateVars; # If we have text to add, add it if ($form->get("bodyText")) { my $sql = q{ INSERT INTO `Shoutbox_chat` (`assetId`, `userId`, `timestamp`, `bodyText`, `from`) VALUES (?, ?, ?, ?, ?) }; my $placeholders = [ $self->getId, # assetId $session->user->userId, # userId time, # timestamp $form->get("bodyText"), # bodyText ]; # Set "from" column if ($session->user->isInGroup("2")) { # Registered users push @$placeholders, $session->user->profileField("alias") || $session->user->username; } else { # Visitors push @$placeholders, $form->get("from") || "Anonymous"; } $db->write($sql, $placeholders); } # Show the data for this shoutbox $var->{ chat } = $db->buildArrayRefOfHashRefs( "SELECT * FROM Shoutbox_chat WHERE assetId=? ORDER BY timestamp DESC", [$self->getId] ); # Build a form to add data to the shoutbox $var->{ form_start } = WebGUI::Form::formHeader($session, { action => $self->getUrl }); $var->{ form_from } = $session->user->isInGroup("2") ? $session->user->profileField("alias") || $session->user->username : WebGUI::Form::text($session, { name => "from" }); $var->{ form_bodyText } = WebGUI::Form::text($session, { name => "bodyText" }); $var->{ form_submit } = WebGUI::Form::submit($session, { name => "submit" }); $var->{ form_end } = WebGUI::Form::formFooter(); return $self->processTemplate($var, undef, $self->{_viewTemplate}); } #------------------------------------------------------------------- =head2 www_json Returns the chat in JSON format for AJAX =cut sub www_json { my $self = shift; my $session = $self->session; my $db = $session->db; my $var = $self->getTemplateVars; $var->{ chat } = $db->buildArrayRefOfHashRefs( "SELECT * FROM Shoutbox_chat WHERE assetId=? ORDER BY timestamp DESC", [$self->getId] ); $session->http->setMimeType('application/json'); return objToJson($var); } #------------------------------------------------------------------- # Everything below here is to make it easier to install your custom # wobject, but has nothing to do with wobjects in general #------------------------------------------------------------------- # cd /data/WebGUI/lib # perl -MWebGUI::Asset::Wobject::Shoutbox -e install www.example.com.conf [ /path/to/WebGUI ] # - or - # perl -MWebGUI::Asset::Wobject::Shoutbox -e uninstall www.example.com.conf [ /path/to/WebGUI ] #------------------------------------------------------------------- use base 'Exporter'; our @EXPORT = qw(install uninstall); use WebGUI::Session; #------------------------------------------------------------------- sub install { my $config = $ARGV[0]; my $home = $ARGV[1] || "/data/WebGUI"; die "usage: perl -MWebGUI::Asset::Wobject::Shoutbox -e install www.example.com.conf\n" unless ($home && $config); print "Installing asset.\n"; my $session = WebGUI::Session->open($home, $config); my $db = $session->db; $session->config->addToArray("assets","WebGUI::Asset::Wobject::Shoutbox"); $db->write(q{ CREATE TABLE Shoutbox ( assetId VARCHAR(22) BINARY NOT NULL, revisionDate BIGINT NOT NULL, templateIdView VARCHAR(22) BINARY, PRIMARY KEY (assetId, revisionDate) ) }); # Install collateral table $db->write(q{ CREATE TABLE `Shoutbox_chat` ( `assetId` VARCHAR(22) BINARY NOT NULL, `timestamp` DATETIME, `userId` VARCHAR(22) BINARY NOT NULL, `from` VARCHAR(30), `bodyText` LONGTEXT, INDEX (assetId) ) }); $session->var->end; $session->close; print "Done. Please restart Apache.\n"; } #------------------------------------------------------------------- sub uninstall { my $config = $ARGV[0]; my $home = $ARGV[1] || "/data/WebGUI"; die "usage: perl -MWebGUI::Asset::Wobject::Shoutbox -e uninstall www.example.com.conf\n" unless ($home && $config); print "Uninstalling asset.\n"; my $session = WebGUI::Session->open($home, $config); $session->config->deleteFromArray("assets","WebGUI::Asset::Wobject::Shoutbox"); my $rs = $session->db->read("select assetId from asset where className='WebGUI::Asset::Wobject::Shoutbox'"); while (my ($id) = $rs->array) { my $asset = WebGUI::Asset->new($session, $id, "WebGUI::Asset::Wobject::Shoutbox"); $asset->purge if defined $asset; } $session->db->write("drop table Shoutbox"); $session->db->write("DROP TABLE `Shoutbox_chat`"); $session->var->end; $session->close; print "Done. Please restart Apache.\n"; } 1;