# 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: JS.pm 3240 2008-08-23 10:46:47Z josh $

package BigMed::Format::JS;
use strict;
use warnings;
use utf8;
use Carp;
use base qw(BigMed::Format);
use BigMed::JSON;

my $JS = 'BigMed::Format::JS';
$JS->register_format( 'JS', suffix => 'js', is_active => \&can_build );
$JS->add_section_flag('js_disable_feed');
$JS->add_page_flag('js_disable_feed');

sub can_build {
    my $context = shift;
    return 0 if !$context->site->get_pref_value('js_enable_feed');
    my %secflag = $context->section ? $context->section->flags : ();
    return !$secflag{'js_disable_feed'};
}

$JS->add_group(
    name  => 'js_feeds',
    prefs => {
        'js_enable_feed' => {
            edit_type   => 'boolean',
            default     => 1,
            sitewide    => 1,
            priority    => 100,
            edit_params => { option_label => 'JS_enablefeed_checkbox', },
        },
        'js_newsgadget_text' => {
            default     => 'Put our news gadget on your website',
            sitewide    => 1,
            edit_type   => 'simple_text',
            edit_params => {
                description => 'PREFS_JS_DESC_newsgadget_text',
                required    => 1,
            },
            priority => 95,
        },
        'js_numdisplay' => {
            edit_type   => 'number_positive_integer',
            default     => 20,
            sitewide    => 1,
            priority    => 90,
            edit_params => {
                container_class => 'bmcpDividerField',
                description     => 'JS_numdisplay_desc',
            },
        },
        'js_sort_order' => {
            edit_type   => 'sort_order',
            default     => 'pub_time:priority:mod_time|d:d:d',
            sitewide    => 1,
            priority    => 80,
            edit_params => {
                description => 'SORT_DESC_default sort order',
                numfields   => 3,
                required    => 1,
            },
        },
        'js_allow_images' => {
            edit_type   => 'boolean',
            default     => 1,
            sitewide    => 1,
            priority    => 70,
            edit_params => { option_label => 'JS_allowimages_checkbox', },
        },
        'js_image_size' => {
            default     => '60x60',
            edit_type   => 'value_list',
            sitewide    => 1,
            options     => \&_image_size_options,
            labels      => \&_image_size_labels,
            edit_params => { required => 1, },
            priority    => 65,
        },
        'js_feedbuilder_head' => {
            default     => 'News Gadget for Your Website or Blog',
            sitewide    => 1,
            edit_type   => 'rich_text_inline',
            edit_params => {
                container_class => 'bmcpDividerField',
                description     => 'BM_rich_text_inline_notice',
                required        => 1,
            },
            priority => 58,
        },
        'js_feedbuilder_intro' => {
            default =>
              q{RichText:A web "gadget" (sometimes called a widget or badge) }
              . q{is a snippet of code that you add to your website to }
              . q{display links to our latest content on your site. }
              . q{It's easy: Just follow the four simple steps below. }
              . q{We'll take care of the rest, automatically updating your }
              . q{site with our freshest content.},
            sitewide  => 1,
            edit_type => 'rich_text_brief',
            priority  => 55,
        },
        'js_feedbuilder_step1' => {
            default     => 'Step 1. Choose your preferences',
            sitewide    => 1,
            edit_type   => 'rich_text_inline',
            edit_params => {
                description => 'BM_rich_text_inline_notice',
                required    => 1,
            },
            priority => 53,
        },
        'js_feedbuilder_step2' => {
            default     => 'Step 2. Build the links',
            sitewide    => 1,
            edit_type   => 'rich_text_inline',
            edit_params => {
                description => 'BM_rich_text_inline_notice',
                required    => 1,
            },
            priority => 50,
        },
        'js_feedbuilder_step3' => {
            default     => 'Step 3. Preview',
            sitewide    => 1,
            edit_type   => 'rich_text_inline',
            edit_params => {
                description => 'BM_rich_text_inline_notice',
                required    => 1,
            },
            priority => 48,
        },
        'js_feedbuilder_step4' => {
            default     => 'Step 4. Copy and paste',
            sitewide    => 1,
            edit_type   => 'rich_text_inline',
            edit_params => {
                description => 'BM_rich_text_inline_notice',
                required    => 1,
            },
            priority => 45,
        },
        'js_feedbuilder_step4_desc' => {
            default =>
              q{RichText:Copy and paste this code into your site's HTML }
              . q{where you want the links to appear:},
            sitewide    => 1,
            edit_type   => 'rich_text_brief',
            edit_params => { required => 1, },
            priority    => 43,
        },
        'js_number_label' => {
            default     => 'Number of links',
            sitewide    => 1,
            edit_type   => 'simple_text',
            edit_params => {
                container_class => 'bmcpDividerField',
                description     => 'PREFS_JS_DESC_number_label',
                required        => 1,
            },
            priority => 38,
        },
        'js_desc_label' => {
            default     => 'Include page descriptions (if available)',
            sitewide    => 1,
            edit_type   => 'simple_text',
            edit_params => {
                description => 'PREFS_JS_DESC_desc_label',
                required    => 1,
            },
            priority => 36,
        },
        'js_image_label' => {
            default     => 'Include images (if available)',
            sitewide    => 1,
            edit_type   => 'simple_text',
            edit_params => {
                description => 'PREFS_JS_DESC_image_label',
                required    => 1,
            },
            priority => 34,
        },
        'js_window_label' => {
            default     => 'Open links in new window',
            sitewide    => 1,
            edit_type   => 'simple_text',
            edit_params => {
                description => 'PREFS_JS_DESC_window_label',
                required    => 1,
            },
            priority => 32,
        },
        'js_build_button' => {
            default     => 'Build Links',
            sitewide    => 1,
            edit_type   => 'simple_text',
            edit_params => { required => 1, },
            priority    => 30,
        },
    }
);

$JS->register_template(
    {   name        => 'jsfeed',
        description => 'JS_TMPL_DESC_jsfeed',
        level       => ['top', 'section'],
        filename    => 'bm~feed',
    },
);

$JS->add_widget(
    name            => 'jslinks',
    collector       => \&collect_items,
    assembler       => \&assemble_items,
    limit_pref      => 'js_numdisplay',
    sort_order_pref => 'js_sort_order',
);

$JS->add_widget(
    name    => 'secid',
    handler => \&wi_secid,
);

$JS->add_widget(
    name    => 'jsurl',
    handler => \&wi_jsurl,
);

sub wi_jsurl {
    my $site = $_[0]->site;
    my $url = $site->directory_url( $_[0]->section || $site->homepage_obj );
    $url =~ s{^https?://[^/]+}{}msi;
    my $dot = BigMed->bigmed->env('DOT');
    return "$url/bm${dot}feed.js";
}

sub wi_secid {
    my $site = $_[0]->site;
    my $section = $_[0]->section || $site->homepage_obj;
    return $site->id . q{-} . $section->id;
}

sub collect_items {
    my ( $widget, $context, $obj ) = @_;
    my %flag = $obj->flags;
    return 0
      if $flag{hideall}
      || $flag{js_disable_feed}
      || $obj->subtype eq 'section';

    my ( $site, $section ) = ( $context->site, $context->section );
    if ( !$section || $section->is_homepage ) {    #homepage
        return 0 if $flag{html_nohome} || !$JS->allowed_on_home( $context, $obj );
    }
    else {
        if ( $flag{html_nomain} )
        {    #no parents; disallow on secs not explictly assigned
            my %belongs = map { $_ => 1 } $obj->sections;
            return 0 if !$belongs{ $section->id };
        }
        return 0 if !$JS->allowed_on_section( $context, $obj );
    }

    my $rcache = $context->relation_cache;

    my $title = $obj->title;
    my $desc  = $JS->inline_rich_text( $obj->description );
    my $url   = $obj->active_page_url(
        $site,
        {   section => $section,
            rcache  => $rcache,
            rkids   => $context->active_descendants,
        }
    );
    my $img_url;

    if ( $JS->stash_pref( $context, 'js_allow_images' ) ) {
        my $size = $JS->stash_pref( $context, 'js_image_size' );
        foreach my $pr ( $obj->sorted_related_objects( 'media', $rcache ) ) {
            next if $pr->[1]->data_label ne 'image';
            my %meta = $pr->[0]->metadata;
            my $pos  = $meta{link_position};
            next if !$pos || ( $pos ne 'links' && $pos ne 'all' );
            my %formats = $pr->[1]->formats;
            next if !$formats{$size};
            $img_url =
              ( $formats{$size} =~ s/^url://msi )
              ? $formats{$size}
              : $site->image_url . "/$formats{$size}";
            last;
        }
    }
    return $widget->add_to_collection(
        {   t => $title,
            u => $url,
            d => $desc,
            i => $img_url
        }
    );
}

sub assemble_items {
    my ( $widget, $context ) = @_;
    return objToJson( $widget->collection );
}

sub _image_size_options {
    my ( $app, $site, $sec_id ) = @_;
    require BigMed::Media::Image;
    return ( ( map { $_->[1] } BigMed::Media::Image->image_formats($site) ),
        'orig' );
}

sub _image_size_labels {
    my ( $app, $site, $sec_id ) = @_;
    require BigMed::Media::Image;
    return (
        'orig' => $app->language('PREFS_IMAGE_ORIGINAL'),
        map { $_ => $app->language("PREFS_IMAGE_$_") }
          BigMed::Media::Image->default_sizes
    );
}

1;

__END__

=head1 BigMed::Format::JS

=head1 Synopsis

=head1 Description

=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

