#!/bin/env perl # # The ticker (client & admin) loosely follows an MVC architecture. The model # is over in TickerAd.pm (it does all the DB work). ticker.cgi is the view for # the client, while this is the view+controller for the admin. # use strict; use warnings; use lib '/home/slowtwitch/forum.slowtwitch.com/cgi-bin/admin'; use GForum qw($IN); GForum::init('/home/slowtwitch/forum.slowtwitch.com/cgi-bin/admin'); use TickerAd; use Error qw(:try); use constant { STATE_NORMAL => 0, STATE_UPDATE => 1, STATE_REDIRECT => 2, ADMIN_URL => "http://forum.slowtwitch.com/cgi-bin/tickerad/admin/admin.cgi" }; # prototypes. sub controller(); sub view($); # And call the controller. controller(); # # This is the controller. This is where work gets done. # sub controller() { my %viewstate = (state => STATE_NORMAL); # if we have an action parameter (that's non-empty), then do work: if(defined $IN->param('action') && length($IN->param('action')) > 0) { $viewstate{state} = STATE_REDIRECT; try { my $p = $IN->get_hash(); if($p->{action} =~ /create/) { TickerAd::create_ticker($p->{msg}, $p->{link}); } elsif($p->{action} =~ /show_update/) { $viewstate{state} = STATE_UPDATE; $viewstate{id} = $p->{'id'}; } elsif($p->{action} =~ /do_update/) { TickerAd::update_ticker($p->{'id'}, $p->{'msg'}, $p->{'link'}); } elsif($p->{action} =~ /delete/) { TickerAd::delete_ticker($p->{'id'}); } } catch TickerAd::TickerAdException with { # oops. something bad happened. $viewstate{error} = "Error: " . shift; # reset the viewstate so that we display the error message. $viewstate{state} = STATE_NORMAL; }; } return view(\%viewstate); } # # Build the view, which takes a single hashref describing how the view should # behave. # sub view($) { my ($state) = @_; my %s = %$state; # If the state is redirect, we're done. if($s{state} == STATE_REDIRECT) { print $IN->redirect(ADMIN_URL); return; } # Now let's actually build the view, depending on our current state: print $IN->header(); print qq{
" . $s{error} . "
"; } # What should the top form look like? if($s{state} == STATE_NORMAL) { $s{title} = 'Create a ticker:'; $s{submit} = 'Create ticker'; $s{action} = 'create'; $s{id} = ""; $s{msg} = ""; $s{link} = ""; } elsif($s{state} == STATE_UPDATE) { $s{title} = 'Update a ticker:'; $s{submit} = 'Update ticker'; $s{action} = 'do_update'; } # print the form, which is configured for the type of action we're # performing: print qq{ID | Message | Link | ||
$id | Delete | Update | } . $msg . " | " . qq{} . $link . " |