<%doc>

=head1 MACROS

These are some default macros which are used by various templates in the
system.

=head2 link

This creates an <A HREF="... to a command in the Apache::MVC system by
catenating the base URL, table, command, and any arguments.

=cut

</%doc>

<%method link>
<a href="<%$ARGS{base} . $table%>/<%$command%>/<%$additional%>"><%$label%></a>

<%args>
$base
$table
$command
$additional=>""
$label
</%args>
</%method>

<%doc>
=head2 maybe_link_view

C<maybe_link_view> takes something returned from the database - either
some ordinary data, or an object in a related class expanded by a
has-a relationship. If it is an object, it constructs a link to the view
command for that object. Otherwise, it just displays the data.

=cut

</%doc>

<%method maybe_link_view>
%    if (ref $item) { # It's an object, i.e. a has-a
<& macros:link, base       => $base,
         table      =>$item->table, 
         command    =>"view", 
         additional =>$item->id, 
         label     =>$item &>
%    } else {
        <%$item%>
%}

<%args>
$item
$base
</%args>
</%method>

<%doc>
=head2 display_line

C<display_line> is used in the list template to display a row from the
database, by iterating over the columns and displaying the data for each
column. It misses out the C<id> column by default, and magically
URLifies columns called C<url>. This may be considered too much magic
for some.

=cut

</%doc>

<%method display_line>
%foreach my $col (@{$classmetadata->{columns}}) {
%  next if $col eq "id";
        <td>
%       if ($col eq "url") {
            <A HREF=<% $item->{url}%> ><%$item->{url}%></A>
%       }elsif ($col eq $item->stringify_column) {
            <& macros:maybe_link_view, item=>$item,base=>$base&>
%       } else {
            <& macros:maybe_link_view, item=>$item->$col, base=>$base&>
%       }
        </td>
%}
    <& macros:button, item=>$item, action => "edit",base=>$base &>
    <& macros:button, item=>$item, action =>"delete",base=>$base &>

<%args>
$item
$base
$classmetadata
</%args>
</%method>

<%doc>
=head2 button

This is a generic button, which performs an action on an object.

=cut

</%doc>

<%method button>
<TD>
<FORM METHOD="post" ACTION="<%$base%>/<%$item->table%>/<%$action%>/<%$item->id%>">
    <INPUT TYPE="submit" NAME="<%$action%>" VALUE="<%$action%>">
</FORM>
</TD>

<%args>
$base
$item
$action
</%args>
</%method>

<%doc>
=head2 view_related

This takes an object, and looks up the C<related_accessors>; this should
give a list of accessors that can be called to get a list of related
objects. It then displays a title for that accessor, (i.e. "Beers" for a
brewery) calls the accesor, and displays a list of the results. 

=cut

</%doc>

<%method view_related>
%foreach my $accessor (@{$classmetadata->{related_accessors}}) {
        <H3>  <%ucfirst($accessor)%> </H3>
        <UL id="vlist">
%   foreach my $thing ($object->$accessor) {
            <LI><&macros:maybe_link_view,%ARGS,item=>$thing&></LI>
%    }
        </UL>
%}

<%args>
$classmetadata
$object
$base
</%args>
</%method>


