Permalink
Browse files

Create a web interface for interacting with the WTPA data. Split much…

… of the logic for the client into a separate utils file.
  • Loading branch information...
1 parent 0aa1fa7 commit f6fb30ec918230ad04278d36956cde20c55b8ac7 @jeresig committed Mar 8, 2011
Showing with 775 additions and 460 deletions.
  1. +122 −10 index.cgi
  2. +110 −0 main.css
  3. +14 −0 main.js
  4. +476 −0 utils.pl
  5. +53 −450 wtpa-bot.pl
View
132 index.cgi
@@ -6,37 +6,149 @@ use warnings;
use CGI;
+our @events;
+our %places;
+
my $cgi = new CGI();
+my $action = $cgi->param('action') || "";
my $index = join( "", <DATA> );
-if ( $cgi->param('action') eq 'add' ) {
+require "utils.pl";
+
+our $ini = (new Config::Abstract::Ini( 'config.ini' ))->get_all_settings;
+
+init();
+
+if ( $action eq 'add' ) {
+ my $err = addEvent({
+ name => $cgi->param('name'),
+ place => $cgi->param('place'),
+ when => $cgi->param('date') . ' ' . $cgi->param('time') . $cgi->param('ampm')
+ }) || "Event saved.";
+
+ $index =~ s/MSG/$err/g;
+
+} elsif ( $action eq 'update' ) {
+ my $err = updateEvent($cgi->param('old'), "", {
+ name => $cgi->param('name'),
+ place => $cgi->param('place'),
+ when => $cgi->param('date') . ' ' . $cgi->param('time') . $cgi->param('ampm')
+ }) || "Event updated.";
+
+ $index =~ s/MSG/$err/g;
+
+} elsif ( $action eq 'cancel' ) {
+ my $err = cancelEvent($cgi->param('old')) || "Event cancelled.";
+ $index =~ s/MSG/$err/g;
+
+} elsif ( $action eq 'addplace' ) {
+ addPlace( $cgi->param('name'), $cgi->param('address') );
+ $index =~ s/MSG/Place added./g;
+
+} elsif ( $action eq 'updateplace' ) {
+ updatePlace( $cgi->param('old'), $cgi->param('name'), $cgi->param('address') );
+ $index =~ s/MSG/Place updated./g;
+}
+
+my $toUpdate = "";
+
+foreach my $event ( @events ) {
+ my $when = getTime( $event->{when} );
+ my $name = $event->{name};
+ my $place = $event->{place} || "";
+ my $date = $when->strftime("%D");
+ my $time = $when->strftime("%I:%M");
+ my $ampm = $when->strftime("%P");
+ my $nice_date = $when->strftime("%b %e");
+ my $nice_time = ($when->hour > 0 ? " " . $when->strftime(
+ # Don't display the minutes when they're :00
+ $when->minute > 0 ? "%l:%M%P" : "%l%P" ) : "");
+ my $nice_place = $place;
+ my $dbPlace = placeURL( findPlace( $event ), $event );
+ my $namePlace = $nice_place ? "$event->{name} @ $nice_place" : $event->{name};
-} else {
+ $namePlace = $dbPlace ?
+ "<a href='$dbPlace'>$namePlace</a>" :
+ $namePlace;
+ $name =~ s/"/\\"/g;
+ $place =~ s/"/\\"/g;
+
+ # Display the name and location of the event
+ $toUpdate .= qq~<li><span class="date">$nice_date $nice_time:</span> $namePlace
+ <span class='buttons'>
+ <input type='button' value='Update' class="update"/>
+ <form action="" method="POST" class="cance">
+ <input type="hidden" name="action" value="cancel"/>
+ <input type="hidden" name="old" value="' + data.name + '"/>
+ <input type="submit" value="Cancel"/>
+ </form>
+ </span>
+ <form action="" method="POST" class="update">
+ <input type="hidden" name="action" value="update"/>
+ <input type="hidden" name="old" value="$name"/>
+ <label for="name">Name:</label><input type="text" name="name" value="$name"/><br/>
+ <label for="place">Place:</label><input type="text" name="place" value="$place"/><br/>
+ <label for="date">Date:</label><input type="text" name="date" value="$date"/><br/>
+ <label for="time">Time:</label><input type="text" name="time" value="$time"/> <select name="ampm">
+ <option>$ampm</option><option>pm</option><option>am</option></select><br/>
+ <label></label><input type="submit" value="Update Event"/> <input type="reset" value="Cancel"/>
+ </form>
+ </li>~;
+}
+
+my $placeList = "";
+
+foreach my $place ( sort { lc($a) cmp lc($b) } keys %places ) {
+ $placeList .= qq~<li><form action="" method="POST">
+ <input type="hidden" name="action" value="updateplace"/>
+ <input type="hidden" name="old" value="$place"/>
+ <label for="name">Name:</label><input type="text" name="name" value="$place"/>
+ <label for="address">Address:</label><input type="text" name="address" value="$places{$place}"/>
+ <input type="submit" value="Update"/>
+ </form></li>~;
}
-print "Content-type: text/html\n\n";
+$index =~ s/UPCOMING/$toUpdate/g;
+$index =~ s/PLACES/$placeList/g;
+$index =~ s/MSG//g;
+
+#print "Content-type: text/html\n\n";
print $index;
__DATA__
<!DOCTYPE html>
<html>
<head>
+ <meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/>
<title>Update WTPA</title>
+ <link rel="stylesheet" href="main.css"/>
+ <script src="http://code.jquery.com/jquery.js"></script>
+ <script src="main.js"></script>
</head>
<body>
<h1>Where is the Party at?</h1>
- <p>Today: TODAY</p>
- <p>Upcoming: UPCOMING</p>
+ <p style="color:red;">MSG</p>
+ <h2>Events:</h2>
+ <ul id="upcoming">UPCOMING</ul>
<h2>Add New Event:</h2>
<form action="" method="POST">
<input type="hidden" name="action" value="add"/>
- Name: <input type="text" name="name"/><br/>
- Location: <input type="text" name="location"/><br/>
- Date: <input type="text" name="date" value="01/01/2011"/><br/>
- Time: <input type="text" name="time" value="7:00"/> <select name="ampm">
+ <label for="name">Name:</label><input type="text" name="name"/><br/>
+ <label for="place">Place:</label><input type="text" name="place"/><br/>
+ <label for="date">Date:</label><input type="text" name="date" value="01/01/2011"/><br/>
+ <label for="time">Time:</label><input type="text" name="time" value="7:00"/> <select name="ampm">
<option>pm</option><option>am</option></select><br/>
- <input type="submit" value="Add Event"/>
+ <label></label><input type="submit" value="Add Event"/>
+ </form>
+ <h2>Places:</h2>
+ <ul id="places">PLACES</ul>
+ <h2>Add New Place:</h2>
+ <form action="" method="POST">
+ <input type="hidden" name="action" value="addplace"/>
+ <label for="name">Name:</label><input type="text" name="name"/>
+ <label for="address">Address:</label><input type="text" name="address"/>
+ <input type="submit" value="Add Place"/>
</form>
</body>
</html>
View
110 main.css
@@ -0,0 +1,110 @@
+/* http://meyerweb.com/eric/tools/css/reset/
+ v2.0 | 20110126
+ License: none (public domain)
+*/
+
+html, body, div, span, applet, object, iframe,
+h1, h2, h3, h4, h5, h6, p, blockquote, pre,
+a, abbr, acronym, address, big, cite, code,
+del, dfn, em, img, ins, kbd, q, s, samp,
+small, strike, strong, sub, sup, tt, var,
+b, u, i, center,
+dl, dt, dd, ol, ul, li,
+fieldset, form, label, legend,
+table, caption, tbody, tfoot, thead, tr, th, td,
+article, aside, canvas, details, embed,
+figure, figcaption, footer, header, hgroup,
+menu, nav, output, ruby, section, summary,
+time, mark, audio, video {
+ margin: 0;
+ padding: 0;
+ border: 0;
+ font-size: 100%;
+ font: inherit;
+ vertical-align: baseline;
+}
+/* HTML5 display-role reset for older browsers */
+article, aside, details, figcaption, figure,
+footer, header, hgroup, menu, nav, section {
+ display: block;
+}
+body {
+ line-height: 1;
+}
+ol, ul {
+ list-style: none;
+}
+blockquote, q {
+ quotes: none;
+}
+blockquote:before, blockquote:after,
+q:before, q:after {
+ content: '';
+ content: none;
+}
+table {
+ border-collapse: collapse;
+ border-spacing: 0;
+}
+
+html {
+ font-family: Helvetica;
+ font-size: 14px;
+}
+
+h1 {
+ background: darkblue;
+ color: white;
+ padding: 8px;
+ font-size: 30px;
+}
+
+h2 {
+ padding: 4px;
+ margin: 8px 0;
+ font-size: 20px;
+ background: #DDD;
+}
+
+form {
+ display: inline;
+}
+
+form label {
+ display: inline-block;
+ width: 50px;
+ text-align: right;
+ margin-right: 6px;
+}
+
+#upcoming {
+ width: 500px;
+}
+
+#upcoming li {
+ background: #EEE;
+ font-size: 16px;
+ padding: 6px;
+ margin: 4px;
+ position: relative;
+}
+
+#upcoming li span.date {
+ display: inline-block;
+ width: 125px;
+ text-align: right;
+}
+
+#upcoming li .buttons {
+ position: absolute;
+ top: 3px;
+ right: 3px;
+ display: block;
+}
+
+#upcoming li form.update {
+ display: block;
+ background: white;
+ margin-top: 4px;
+ padding: 8px;
+}
View
14 main.js
@@ -0,0 +1,14 @@
+$(function(){
+ $("#upcoming form.update").hide().bind("reset", function() {
+ $(this).hide();
+ return false;
+ });
+
+ $("#upcoming input.update").click(function() {
+ $(this).parents("li").find("form.update").toggle();
+ });
+
+ $("#upcoming form.cancel").submit(function() {
+ return confirm( "Are you sure you wish to cancel this event?" );
+ });
+});
Oops, something went wrong.

0 comments on commit f6fb30e

Please sign in to comment.