#!/usr/bin/perl
#
# Catch22 (llama@osoal.org.nz)
# One Short Of A Llama [http://web.osoal.org.nz]

# create table horoscopes
# (
#    time        int unsigned auto_increment primary key,
#    aries       longtext,
#    taurus      longtext,
#    gemini      longtext,
#    cancer      longtext,
#    leo         longtext,
#    virgo       longtext,
#    libra       longtext,
#    scorpio     longtext,
#    sagittarius longtext,
#    capricorn   longtext,
#    aquarius    longtext,
#    pisces      longtext
# );

#
use strict;
use DBI;
use OneShortOfALlama;

#
my(@zodiac);

@zodiac = (
		   +{
			   sign => 'Aries',
			   date => 'Mar 21 - Apr 20'
			   },
		   +{
			   sign => 'Taurus',
			   date => 'Apr 21 - May 21'
			   },
		   +{
			   sign => 'Gemini',
			   date => 'May 22 - June 21'
			   },
		   +{
			   sign => 'Cancer',
			   date => 'June 22 - July 22'
			   },
		   +{
			   sign => 'Leo',
			   date => 'July 23 - Aug 22'
			   },
		   +{
			   sign => 'Virgo',
			   date => 'Aug 23 - Sept 22'
			   },
		   +{
			   sign => 'Libra',
			   date => 'Sept 23 - Oct 23'
			   },
		   +{
			   sign => 'Scorpio',
			   date => 'Oct 24 - Nov 21'
			   },
		   +{
			   sign => 'Sagittarius',
			   date => 'Nov 22 - Dec 20'
			   },
		   +{
			   sign => 'Capricorn',
			   date => 'Dec 21 - Jan 20'
			   },
		   +{
			   sign => 'Aquarius',
			   date => 'Jan 21 - Feb 19'
			   },
		   +{
			   sign => 'Pisces',
			   date => 'Feb 20 - Mar 20'
			   }
		   );

#
sub print_horoscope
{
    my($dbh, $sth, $i, $id, $height, @horoscopes);

    #
	$dbh = shift(@_);
	$id  = shift(@_);

	$height = scalar(@zodiac) / 2;

	#
	$i   = 0;
    $sth = $dbh->prepare("select aries, taurus, gemini, cancer, leo, virgo,
                                 libra, scorpio, sagittarius, capricorn, aquarius, pisces
                          from osoal.horoscopes where (id = $id)");
    $sth->execute();

    #
    if ($sth->rows())
    {
		@horoscopes = $sth->fetchrow_array();
		print('<table width="100%" border="0" cellspacing="5">', "\n\n");

		while ($i < $height)
		{
			print('<tr>', "\n");
			
			print_starsign($zodiac[$i], $horoscopes[$i]);
			print_starsign($zodiac[$i + 6], $horoscopes[$i + 6]);

			print('</tr>', "\n");

			$i++;
		}

		$sth->finish();
		print('</table>', "\n\n");
    }
}

sub print_starsign
{
    my($horoscope, $zodiac);

    #
	$zodiac    = shift(@_);
    $horoscope = shift(@_);

    #
    print('<td width="295" valign="top">
           <a name="', $zodiac->{'sign'}, '">

           <table cols="3" width="100%" align="center">
           <tr>

           <td align="left" width="32" valign="center">
           <img src="/pics/horoscopes/', lcfirst($zodiac->{'sign'}), '.jpg" vspace="5" alt=""></td>

           <td align="center" valign="center">
           <h3>', $zodiac->{'sign'}, '</h3></td>

           <td align="right" valign="center">
           <nobr><h4><font color="#224488">', $zodiac->{'date'}, '</font></h4></nobr></td>

           </tr>
           </table>

           <table cellpadding="5">
           <tr>
           <td>', $horoscope, '</td>
           </tr>
           </table>

           </td>', "\n\n");
}

sub input_horoscope
{
    my($buf, $param);
	$param  = shift(@_);

	#
    print('<form method="POST" action="', $ENV{'SCRIPT_NAME'}, '">');

	if ($param->{'select'} eq 'Modify')
	{
		print('<input type="hidden" name="id" value="', $param->{'id'}, '">');
	}

	print('<table align="center">', "\n\n");

	#
    foreach $buf (@zodiac)
    {
		print('<tr>
               <td align="left">
                   <div align="center">
                   <img src="/pics/horoscopes/', lcfirst($buf->{'sign'}), '.jpg"
                        border="0" vspace="5" alt=""><br>
                   <br>
                   <b>', ucfirst($buf->{'sign'}), '</b><br>
                   <small><font color="#224488"><nobr>', $buf->{'date'}, '</nobr></font></small>
                   </div>
               </td>

               <td align="left">
                   <textarea name="', $buf->{'sign'}, '" rows="5" cols="50" wrap="virtual">');

		#
		if (@_)
		{
			print(shift(@_));
		}

		print('</textarea>
               </td>
               </tr>', "\n\n");
    }

    #
    print('<tr>
           <td align="left">
              <input type="submit" name="select" value="');

	if ($param->{'select'} eq 'Update')
	{
		print('Submit');
	}
	elsif ($param->{'select'} eq 'Modify')
	{
		print('Apply');
	}

	print('">
           </td>
           <td align="right"><img src="/pics/icons/oneshortofallama.png" alt=""></td>
           </tr>

           </table>
           </form>');
}

sub submit_horoscope
{
    my($buf, $dbh, $sth, $param, @horoscopes);

	#
	$dbh   = shift(@_);
	$param = shift(@_);

	#
	foreach $buf (@zodiac)
    {
		$param->{$buf->{'sign'}} =~ s/\r//g;
		push(@horoscopes, $param->{$buf->{'sign'}});
    }

    $sth = $dbh->prepare('insert into osoal.horoscopes
                                 (time, aries, taurus, gemini, cancer, leo, virgo,
                                 libra, scorpio, sagittarius, capricorn, aquarius, pisces)
                          values (unix_timestamp(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
	$sth->execute(@horoscopes);

	# massage the id to the current horoscope
	$param->{'id'} = $sth->{'mysql_insertid'};
}

sub delete_horoscope
{
	my($dbh, $id);
	
	#
	$dbh = shift(@_);
	$id  = shift(@_);

	#
	$dbh->do("delete from osoal.horoscopes where (id = $id)");
}

sub modify_horoscope
{
	my($param, $dbh, @buf);

	#
	$dbh   = shift(@_);
	$param = shift(@_);

	#
	@buf = $dbh->selectrow_array('select aries, taurus, gemini, cancer, leo, virgo, 
                                  libra, scorpio, sagittarius, capricorn, aquarius, pisces
                                  from osoal.horoscopes where (id = ' . $param->{'id'} . ')');

	#
	input_horoscope($param, @buf);
}

sub apply_horoscope
{
    my($dbh, $buf, $param, $id, @horoscopes);

	#
	$dbh   = shift(@_);
	$param = shift(@_);

	#
	foreach $buf (@zodiac)
    {
		$param->{$buf->{'sign'}} =~ s/\r//g;
		push(@horoscopes, $buf->{'sign'} . ' = ' . $dbh->quote($param->{$buf->{'sign'}}));
	}

	#
	$buf = join(', ', @horoscopes);
	$id  = $param->{'id'};

	$dbh->do("update osoal.horoscopes set $buf where (id = $id)");
}

sub list_horoscopes
{
	my($dbh, $sth, $id, $date);
	$dbh = shift(@_);

	#
	$sth = $dbh->prepare('select id, from_unixtime(time, "%W %D %M, %Y") from osoal.horoscopes
                          order by id desc');
	$sth->execute();

	print('<tr>
           <td>', "\n\n");

	while (($id, $date) = $sth->fetchrow_array())
	{
		print('<li><a href="', $ENV{'SCRIPT_NAME'}, '?id=', $id, '"><b>', $date, '</b></a></li>', "\n");
	}

	print('</td>
           </tr>', "\n\n");
}

#
#
#

sub main
{
	my($uid, $gid, $dbh, $cookie, $param, $horoscopes);

	#
	$param       = variables();
	$cookie      = cookie('osoal');
	($uid, $gid) = authenticate($cookie->{'nickname'}, $cookie->{'passwd'});

	#
	header('Horoscopes', 'horoscopes.png', '/');
	print('<div align="center">
               <table width="600" cellspacing="0" cellpadding="5" border="0">', "\n\n");

	#
	if ($dbh = DBI->connect("dbi:mysql:$MySQLserver;database=osoal",
				$MySQLuser, $MySQLpasswd))
	{
		if ($param->{'select'} eq 'Update')
		{
			input_horoscope($param);
		}

		elsif ($param->{'select'} eq 'Modify')
		{
			if ($gid == 1)
			{
				modify_horoscope($dbh, $param);
			}
		}

		elsif ($param->{'select'} eq 'List')
		{
			list_horoscopes($dbh);
		}

		else
		{
			if ($gid == 1)
			{
				if ($param->{'select'} eq 'Submit')
				{
					submit_horoscope($dbh, $param);
				}

				elsif ($param->{'select'} eq 'Delete')
				{
					delete_horoscope($dbh, $param->{'id'});
					$param->{'id'} = '';
				}

				elsif ($param->{'select'} eq 'Apply')
				{
					apply_horoscope($dbh, $param);
				}
			}

			#
			print('<tr>
                   <td>
                   <div align="center">
                   [ ');

			#
			$horoscopes = $dbh->selectrow_array('select count(*) from osoal.horoscopes');


			unless ($param->{'id'} =~ m/^\d+$/ && $param->{'id'} > 0)
			{
				$param->{'id'} = $horoscopes;
			}

			#
			if ($gid == 1)
			{
				print('<a href="', $ENV{'SCRIPT_NAME'}, '?select=Update"><b>Update</b></a> | ', "\n");

				if ($horoscopes)
				{
					print('<a href="', $ENV{'SCRIPT_NAME'}, '?select=Modify&id=', $param->{'id'}, '">
                           <b>Modify</b></a> |
                           <a href="', $ENV{'SCRIPT_NAME'}, '?select=Delete&id=', $param->{'id'}, '">
                           <b>Delete</b></a> | ');
				}
			}

			#
			if ($param->{'id'} > 1)
			{
				print('<a href="', $ENV{'SCRIPT_NAME'}, '?id=', $param->{'id'} - 1, '">
                       <b>Previous</b></a> | ');
			}

			if ($param->{'id'} < $horoscopes)
			{
				print('<a href="', $ENV{'SCRIPT_NAME'}, '?id=', $param->{'id'} + 1, '">
                       <b>Next</b></a> | ');
			}

			#
			print(' <a href="', $ENV{'SCRIPT_NAME'}, '?select=List"><b>List</b></a> |
                   <a href="/search.pl?location=Horoscopes"><b>Search</b></a> ]
                   </div>
                   <br>
                   <br>', "\n\n");

			#
			print_horoscope($dbh, $param->{'id'});
			print('</td>
                   </tr>', "\n\n");
		}

		$dbh->disconnect();
	}

	#
	print('</table>
               </div>', "\n\n");
	footer();
}

#
main();
