#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw/warningsToBrowser fatalsToBrowser/;
#use HTML::Template::Compiled;
#use DBI;
use Data::Dumper;
package Unit;
use Moose;
has 'begin' => ( is => 'rw' );
has 'end' => ( is => 'rw' );
has 'person' => ( is => 'rw' );
has 'task' => ( is => 'rw' );
has 'position' => ( is => 'ro' );
sub as_html {
my $self = shift;
my $o = '';
$o .= '
'. $self->begin .'-'. $self->end .' ';
$o .= '' . $self->person . ' ';
$o .= '' . $self->task . ' ';;
$o .= 'edit';
$o .= ' | ' . "\n";
return $o;
}
package TimeSlot;
use Moose;
has 'units' => ( isa => 'ArrayRef', is => 'rw' );
has 'day' => ( is => 'rw' );
has 'position' => ( is => 'ro' );
sub as_html {
my $self = shift;
my $o = '';
$o .= '' . "\n";
$o .= '' . $self-> day . ' |
' . "\n";
$o .= "\n" . $_->as_html . "
\n" for @{$self->{units}};
$o .= '
' . "\n";
return $o;
}
package TimeTable;
use Moose;
has 'weekdays' => ( isa => 'ArrayRef', is => 'rw' );
sub create_week {
my $self = shift;
my @days = @_;
@days = qw/ MO DI MI DO FR SA SO / unless @days;
@{$self->{weekdays}} = map { TimeSlot->new(day => $_) } @days;
$self->{weekdays}->[$_]->{position}=$_ for 0 .. $#{$self->{weekdays}};
}
sub populate_units {
my $self = shift;
die "TimeTable->populate_units needs weekdays to add on!"
unless @{$self->{weekdays}};
my @start_end = @_;
@start_end = qw/ 08:00 09:30 09:45 11:15 11:30 13:00 / unless @start_end;
die "TimeTable->populate_units needs an even list to work on!"
unless @start_end %2 == 0;
my $i = 0;
while (my ($s, $e) = splice @start_end, 0, 2) {
push @{$self->{weekdays}->[$_]->{units}},
Unit->new( begin => $s, end => $e, position => $i)
for 0..$#{$self->{weekdays}};
$i++;
}
}
sub as_html {
my $self = shift;
my $o = '';
$o .= '' . "\n"
. $_->as_html
. '
' . "\n" for @{$self->{weekdays}};;
return $o;
}
sub add_task {
my $self = shift;
my $d_pos = shift;
my $u_pos = shift;
my $task = shift || '';
$self->{weekdays}->[$d_pos]->{units}->[$u_pos]->task($task);
}
sub add_person {
my $self = shift;
my $d_pos = shift;
my $u_pos = shift;
my $person = shift || '';
$self->{weekdays}->[$d_pos]->{units}->[$u_pos]->person($person);
}
package main;
my $q = CGI->new;
my $tt = TimeTable->new;
$tt->create_week;
$tt->populate_units;
$tt->add_task(0, 1, 'use templating!');
$tt->add_person(0, 1, 'Foo Bar Buz');
print $q->header, $q->start_html, $tt->as_html, $q->end_html;