# Copyright 2002-2008 Josh Clark and Global Moxie, LLC. This code cannot be
# redistributed without permission from globalmoxie.com.  For more
# information, consult your Big Medium license.
#
# $Id: URL.pm 3043 2008-03-31 14:00:38Z josh $

package BigMed::URL;
use strict;
use warnings;
use utf8;
use Carp;
use base qw(BigMed::MiniContent);

my @data_schema = (
    {   name  => 'linkid',      #id of source of the link
        type  => 'system_id',
        index => 1,
    },
    {   name  => 'linktype',    #data_source of the source
        type  => 'raw_text',
        index => 1,
    },
    {   name    => 'text',
        type    => 'rich_text_inline',
        default => q{},
        index   => 1,
    },
    {   name    => 'url',
        type    => 'url',
        default => 'http://',
        index   => 1,
    },
    {   name => 'priority',
        type => 'number_zeroplus_integer',
    },
    {   name    => 'new_win',
        type    => 'value_list',
        options => ['default', 'yes', 'no'],
        labels  => {
            'default' => 'BM_Default',
            'yes'     => 'Yes',
            'no'      => 'No',
        },
    }
);

BigMed::URL->register_minicontent(
    elements      => \@data_schema,
    editor_fields => [
        { column => 'text', prompt_as => 'hidden' },
        { column => 'url',  prompt_as => 'hidden' },
        {   id        => 'link_info',
            prompt_as => 'link_info',
            parse_as  => 'link_info',
            required  => 1,
        },
        { column => 'new_win' },
    ],
    preview     => { html => \&preview_html },
    post_parse  => \&process_url,
    pre_preview => \&pre_preview_url,
);

sub preview_html {
    my ( $app, $obj, $roptions ) = @_;

    my $url  = $obj->url;
    my $text = $obj->text;
    my $status;
    if ( $url =~ m{^bm://(\d+)/(\d+)}ms ) {
        $url = $obj->stash('url');
    }
    $status = $url || $app->language('URL_Currently unpublished');

    my $html = $app->html_template(
        'wi_url_preview.tmpl',
        URL       => $url,
        TEXT      => $text,
        PUBSTATUS => $status,
    );
    my $window = $obj->new_win || 'default';
    $window =
        $window eq 'yes' ? $app->language('Yes')
      : $window eq 'no'  ? $app->language('No')
      : $app->language('BM_Default');
    return (
        PREVIEW_HTML => $html,
        STATUS_HTML  => $app->language('url:new_win') . ": $window"
    );
}

sub process_url {
    my ( $self, $rfields ) = @_;
    my $rlink_info = $rfields->{link_info} or return;    #probably an error
    $rfields->{url}  = $rlink_info->{url};
    $rfields->{text} = $rlink_info->{text};
    return;
}

sub pre_preview_url {
    my ( $app, $robj ) = @_;
    my $site = $app->current_site;
    foreach my $obj ( @{$robj} ) {
        next if !$obj->isa('BigMed::URL');
        if ( $obj->url && $obj->url =~ m{^bm://(\d+)/(\d+)}ms ) {
            my ( $sid, $pid ) = ( $1, $2 );
            my $page =
              BigMed::Content::Page->fetch( { site => $sid, id => $pid } )
              or next;
            $obj->set_text( $page->title );

            #url empty if not publicly available
            $obj->set_stash( 'url', $page->active_page_url($site) );
        }
    }
    return;
}

sub title_and_url {
    my $obj = shift;
    return () if !$obj->url; # can happen when obj is cloned to new site
    $obj->url =~ m{^bm://(\d+)/(\d+)}ms or return ( $obj->text, $obj->url );
    my ( $sid, $pid ) = ( $1, $2 );
    my $url = $obj->stash('url');
    if ( !$url ) {
        my $page =
          BigMed::Content::Page->fetch( { site => $sid, id => $pid } )
          or return ();
        my $site = shift or croak 'usage: $url->title_and_url($site)';
        $url = $page->active_page_url($site) or return ();
        $obj->set_stash( 'url', $url );
        $obj->set_text( $page->title );
    }
    return ( $obj->text, $url );
}

sub link_source_obj {
    my $obj    = shift;
    my $sclass = BigMed::Content->source_class( $obj->linktype );
    return $sclass->fetch( { site => $obj->site, id => $obj->linkid } );
}

###########################################################
# COPY CALLBACK
###########################################################

#used to convert cloned internal url objects to external. reasonable to do
#when copying individual pages to new site, but makes it difficult to
#update an entire site's cloned objects. instead, just leave the internal
#url format; the url will still resolve but just won't update on changes
#until the site itself is rebuilt. not ideal (a bug, really), but
#seems to be better than locking in the external id, which also won't
#update if the target url is changed.

=comments

BigMed::URL->add_callback( 'after_copy', \&_after_url_copy );

sub _after_url_copy {
    my ( $orig, $clone, $rparam ) = @_;

    #if url points to an internal id that is not part of the same site,
    #convert to a full url.

    my $url = $clone->url || q{};
    if ( $url =~ m{^bm://(\d+)/}ms ) {
        my $sid = $1;
        return 1 if $sid == $clone->site;    #already good
        defined( my $site = BigMed::Site->fetch($sid) ) or return;
        if (!$site) { #no such site, so no such url
            $clone->set_url(undef);
            return 1;
        }
        my $title;
        
        #url can be empty if internal page is inactive
        ( $title, $url ) = $clone->title_and_url($site);
        $clone->set_url($url);    #text also gets set via title_and_url
    }
    return 1;
}

=cut

1;

__END__

=head1 AUTHOR & COPYRIGHTS

This module and all Big Medium modules are copyright Josh Clark
and Global Moxie. All rights reserved.

Use of this module and the Big Medium content
management system are governed by Global Moxie's software licenses
and may not be used outside of the terms and conditions outlined
there.

For more information, visit the Global Moxie website at
L<http://globalmoxie.com/>.

Big Medium and Global Moxie are service marks of Global Moxie
and Josh Clark. All rights reserved.

=cut

