Gantry

 view release on metacpan or  search on metacpan

lib/Gantry/Plugins/Calendar.pm  view on Meta::CPAN

# $site->do_calendar_month( $r, @p ) 
#-------------------------------------------------
sub do_calendar_month {
    my ( $site, $name, $year, $month ) = @_;
    
    my $chk = Gantry::Utils::Validate->new();
    
    $site->template_disable( 1 );

    $name   = ''                            if ( ! $chk->is_text( $name ) );
    $year   = ( 1900 + ( localtime() )[5] ) if ( ! $chk->is_number( $year ) );
    $month  = ( 1 + ( localtime() )[4] )    if ( ! $chk->is_number( $month ) );
    
    my @output = (
        '<html><head></head><body>',
        '<script type="text/javascript">',
        '<!--',
        '   window.focus(); ',
        '   function SendDate(d) { ',
        qq!     window.opener.SetDate("$name",d); window.close(); !,
        '   } ',
        '//--> ',
        '</script>',

        _calendar_month( 
            $site->r,
            ( $site->location . "/calendar_month/$name" ), 
            $month, 
            $year, 
            1, 
            \&_calendar_day 
        ),
        '</body></html>',
    );

    return( join "\n", @output );

} # END $site->do_calendar

#-------------------------------------------------
# _calendar_day( $year, $month, $day )
#-------------------------------------------------
sub _calendar_day {
    my ( $year, $month, $day ) = @_;

    return( ht_a(   'javascript://', "$day", 
                    "onClick=\"SendDate('$month-$day-$year')\"" ) );
} # END calendar_day

#-------------------------------------------------
# $site->calendar_month_js( $form_id )
#-------------------------------------------------
sub calendar_month_js {
    my( $site, $form_id ) = @_;
    
    # prepend document. to form_id id is not specified
    if ( $form_id !~ /^document\./i ) {
        $form_id = 'document.' . $form_id;
    }
    
    my $popup_url = $site->location . "/calendar_month/";   
    
    return( qq!
        <script type="text/javascript">
            function SetDate(field, date) {
                eval( '${form_id}.' + field + '.value = date;' );
            }
    
            function datepopup(name) { 
                window.open('$popup_url'+name, 'Shortcut',
                            'height=250,width=250' + 
                            ',screenX=' + (window.screenX+150) + 
                            ',screenY=' + (window.screenY+100) + 
                            ',scrollbars,resizable' );


            }
        </script>
    ! );

} # end: calendar_month_js

#-------------------------------------------------
# _calendar_month( $r, etc... )
#-------------------------------------------------
sub _calendar_month {
    my ( $r, $root, $month, $year, $select, $function, @params ) = @_;

    my $chk = Gantry::Utils::Validate->new();
    
    if ( ( ! $chk->is_integer( $month ) ) 
                || ( $month > 13 ) || ( $month < 1 ) ) {
        return( 'Malformed month.' );
    }

    if ( ( ! $chk->is_integer( $year ) ) || ( length( $year ) != 4) ) {
        return( 'Malformed year.' );
    }

    # Fix up some variables.
    my $month_max   = Days_in_Month( $year, $month ); 
    my $offset      = Day_of_Week( $year, $month, 1 );
    $offset         = ( $offset == 7 ) ? 0 : $offset;
    $root           =~ s/\/$//;

    my @lines = ht_table( { 'cols' => '7' } );

    if ( defined $select && $select ) {

        my ( $syear, $smonth ) = Add_Delta_YMD( $year, $month, 1, 0, -6, 0);

        # Build the month options.
        my @items;
        for my $option ( -6..6 ) {
            push( @items, "$syear/$smonth", Month_to_Text($smonth)." $syear" );

            ( $syear, $smonth ) = Add_Delta_YMD( $syear, $smonth, 1, 0, 1, 0);
        }

        # Push on the month select box.
        push(   @lines, 
                ht_form_js( $root ),
                ht_tr(),
                ht_td(  { class => 'head', align => 'center' },
                        ht_a(   ( ( ( $month - 1 ) < 1 ) ?
                                        "$root/". ( $year - 1 ). "/12" : 
                                        "$root/$year/" . ($month - 1)   ), 
                                '&lt;&lt;' ) ),

                ht_td(  { class => 'head', align => 'center', colspan => '5' },

lib/Gantry/Plugins/Calendar.pm  view on Meta::CPAN

or

    use Gantry qw/... Calendar/;

=head1 DESCRIPTION

If you have a date field on a web form, which the user must supply,
you can use this module to help them.  When you've got it set up,
your form will have a link sitting next to the date's text entry box.
If the user clicks the link, a calendar pops up.  It allows for
navigation by time period.  Each date is just linked text.  When the user
presses one of the links, that date is entered into the text field
as if they had typed it.

To make this happen do the following.

=over 4

=item 1.

Add Calendar to the list in the use statement for Gantry in your
application's base module.  For example:

    use Gantry qw/... Calendar/;

=item 2.

In your module's _form method, add the following to the form hash:

    javascript => $site->calendar_month_js( 'your_form_name' );

where your_form_name must match the name entry in the hash.

=item 3.

Add the following to the hash for your date fields:

    date_select => 'User Label',

User Label will be the link text, so make it something like 'Select'
or 'Choose Date'.

=back

That's all.

=head1 HOW IT WORKS

The three steps above are simple, but they conceal quite a bit of careful
Perl and Javascript code.  This section explains what is actually
happening behind the scenes.

When you use the Calendar template, methods are exported into the site
object.  The important ones are calendar_month_js and do_calendar_month.
The global handler will call do_calendar_month for you.

calendar_month_js creates two Javascript functions:

=over 4

=item datepopup(form_name)

makes the calendar window popup.

=item SetDate(field_name, date)

sets the date when the user clicks on a date in the popup window.

=back

You must pass the name of the form to calendar_month_js, otherwise
its Javascript won't be able to tell the name to the popup window,
which will then be unable to set any dates on your form.

do_calendar_month is the method called by the handler when the window
pops up.  It generates the calendar and manages user interaction with
it.  It relies on the internal _calendar_month to make the actual
output.  When the user clicks on a date, its Javascript first
calls SetDate with the field name and the date value
and then closes the popup window.

=head1 FUNCTIONS 

The other functions are not directly useful to normal callers
but here is a complete list.

=head2 Method you call

=over 4

=item calendar_month_js

See above.

=back

=head2 Methods called by global handler

=over 4

=item do_calendar_month( $site, ... )

This is the only do_* method we currently use.

=item do_calendar_week( $site ... )

Might not work, not tested.  Meant to display weeks with time slots.

=item do_calendar_year( $site ... )

Might not work, not tested.  Meant to display a whole year at a time.

=back

=head2 Functions used internatlly

=over 4

=item @month = _calendar_month( $r, $root, $month, $year, $select, \&function, @param )

This function creates a month in html for display. C<$r> is the apache
request object. C<$root> is the uri root of the calendar, this is used
for paging. C<$month> and C<$year> are the month and year to show.
C<$select> should be a boolean value, true if the month select is to be
shown, false otherwise. C<\&function> is a function referenc for the
day. C<@params> are any params that need to be passed to C<\&function>

=item @week = _calendar_week( $r, $root, $day, $month, $year, \&function, @param )

This function creates a week in html for display. C<$r> is the apache
request object. C<$root> is the uri root of the week, this is used for
paging. C<$day>, C<$month>, and C<$year> are the day, month, and year of
the Wednesday of the week. C<\&function> should be a function reference
for the day function. C<@param> is for the parameters for the day
function that will be passed through.

=item @year = _calendar_year( $r, $root, $year, \&function, @param )

This function creates a year in html for display. C<$r> is the apache
request object. C<$root> is the uri root of the year, this is used for



( run in 0.940 second using v1.01-cache-2.11-cpan-364913b4093 )