JiftyX::CloudTags - JiftyX::CloudTags documentation


JiftyX-CloudTags documentation Contained in the JiftyX-CloudTags distribution.

Index


Code Index:

NAME

Top

JiftyX::CloudTags

VERSION

Top

Version 0.01

SYNOPSIS

Top

    use JiftyX::CloudTags;

    my $cloudtag = JiftyX::CloudTags->new( 'LabelCollection'  ,
        text_by => 'name',
        size_by => 'related_posts',
        link_format => '?id=%i',
    );
    $cloudtag->render;

in more detail:

    my $cloudtag = JiftyX::CloudTags->new( 'LabelCollection'  ,
        text_by => 'name',
        size_by => 'related_posts',

        link_format => '?id=%i&text=%t&%{custom_column}',

        min_fontsize => 9,
        max_fontsize => 72,
        degree => 6,

        min_quantity => 0,
        max_quantity => 100,

        break_width => 200,   # in pixel

    );
    $cloudtag->render;







EXPORT

Top

A list of functions that can be exported. You can delete this section if you don't export anything, such as for a purely object-oriented module.

FUNCTIONS

Top

set_tags COLLECTION or COLLECTION_NAME , ARGS

COLLECTION or COLLECTION_NAME
ARGS

Arguments:

size_by

column name

text_by

column name

In string. %i is for id , %t is for text. %{custom_column} for custom column name of your model object.

Optional Arguments:

min_quantity
max_quantity

if you've know the quantity boundary , then we dont need to find the boundary by iterating collection items

min_fontsize

the minimal font size

max_fontsize

the maximal fontsize

degree

font size degree , the quantiy of the model will be multiply by the font size degree

break_width

break line if the tag text width overflows

find_quantity COLLECTION , SIZE_BY

find_quantity method returns (min,max) list. by searching the max,min value in collection object.

COLLECCTION

COLLECTION is a Jifty::DBI::Collection Object. it will be something like MyApp::Model::LabelCollection object in your application.

SIZE_BY

the column name of your model.

render

return the rendered html of cloudtags.

AUTHOR

Top

Cornelius, <cornelius.howl at gmail.com>

BUGS

Top

Please report any bugs or feature requests to bug-jiftyx-cloudtags at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JiftyX-CloudTags. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc JiftyX::CloudTags




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=JiftyX-CloudTags

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/JiftyX-CloudTags

* CPAN Ratings

http://cpanratings.perl.org/d/JiftyX-CloudTags

* Search CPAN

http://search.cpan.org/dist/JiftyX-CloudTags/

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


JiftyX-CloudTags documentation Contained in the JiftyX-CloudTags distribution.

package JiftyX::CloudTags;

use warnings;
use strict;
use Mouse;
use JiftyX::ModelHelpers;

our $VERSION = '0.01';

has 'collection'          => ( is => 'rw', isa => 'Object' );
has 'args'                => (
    is => 'rw',
    isa => 'HashRef'
);

has 'default_link_format' => ( 
    is => 'rw', 
    isa => 'Str' , 
    default => '?id=%i&text=%t&custom=%{hit}'
);

sub set_tags {
    my $self             = shift;
    my $collection_class = shift;
    my %args             = @_;


    my $collection;
    if( ref $collection_class ) {
        $collection = $collection_class;
    }
    else {
        $collection = M($collection_class);
        $collection->unlimit;
    }
    $collection->order_by( column => $args{text_by}, order => 'desc' );
    $self->collection( $collection );
    $self->args( \%args );
}

sub find_quantity {
    my $collection = shift;
    my $size_by    = shift;
    my ( $min_quantity, $max_quantity ) = ( 0, 0 );
    while( my $c = $collection->next ) {
        my $size = ( ref $c->$size_by ? $c->$size_by->count : $c->$size_by );
        $min_quantity = $size if( $size < $min_quantity );
        $max_quantity = $size if( $size > $max_quantity );
    };
    return ( $min_quantity, $max_quantity );
}


sub render {
    my $self = shift;
    my $collection = $self->collection;
    my %args = %{ $self->args };
    my $link_format = $args{link_format} || $self->default_link_format;

    my $min_fontsize = $args{min_fontsize} || 9;
    my $max_fontsize = $args{max_fontsize} || 48;
    my $fontsize_degree = $max_fontsize - $min_fontsize ;

    my ( $min_quantity , $max_quantity );
    $min_quantity ||= $args{min_quantity};
    $max_quantity ||= $args{max_quantity};
    unless( $min_quantity || $max_quantity ) {
        ( $min_quantity , $max_quantity ) = find_quantity( $collection , $args{size_by} );
    }

    my $degree = $args{degree}
        || ( $fontsize_degree / ( $max_quantity - $min_quantity ) );

    my $offset = 0;
    my $div_width = $args{break_width} || -1;

    my $output = '';
    while( my $c = $collection->next ) {
        my ( $text_acc, $size_acc ) = ( $args{text_by}, $args{size_by} );

        my $id = $c->id;
        my $text = $c->$text_acc;
        my $size = ( ref $c->$size_acc ? $c->$size_acc->count : $c->size_acc );
        my $fontsize = int( $size * $degree + $min_fontsize );

        my $url = $link_format;
        $url =~ s/%i/$id/g;
        $url =~ s/%t/$text/g;

        # custom column
        $url =~ s/\%\{(\w+)\}/ $c->$1 /eg;

        # my $url = 
        $output .= qq|
                        <span class="cloudtags" style="font-size: ${fontsize}px;">
                                <a href="$url">$text</a>
                        </span>
                |;

        $offset += length($text) * $fontsize;
        if ( $div_width != -1 and $offset > $div_width ) {
            $output .= q|<br/>|;
            $offset = 0;
        }
    }

    $output;
}

1; 

__END__