=head1 NAME XAO::DO::Web::Utility - Miscellaneous utility displayable functions =head1 SYNOPSIS Currently is only useful in XAO::Web site context. =head1 DESCRIPTION This is a collection of various functions that do not fit well into other objects and are not worth creating separate objects for them (at least at present time). =head1 METHODS Utility object is based on Action object (see L) and therefor what it does depends on the "mode" argument. For each mode there is a separate method with usually very similar name. See below for the list of mode names and their method counterparts. =over =cut ############################################################################### package XAO::DO::Web::Utility; use strict; use POSIX qw(mktime); use XAO::Utils qw(:args :debug :html); use XAO::Objects; use base XAO::Objects->load(objname => 'Web::Action'); use vars qw($VERSION); $VERSION=(0+sprintf('%u.%03u',(q$Id: Utility.pm,v 2.1 2005/01/14 01:39:57 am Exp $ =~ /\s(\d+)\.(\d+)\s/))) || die "Bad VERSION"; sub check_mode ($$) { my $self=shift; my $args=get_args(\@_); my $mode=$args->{mode}; if($mode eq "select-time-range") { $self->select_time_range($args); } elsif($mode eq "tracking-url") { $self->tracking_url($args); } elsif($mode eq "config-param") { $self->config_param($args); } elsif($mode eq "pass-cgi-params") { $self->pass_cgi_params($args); } elsif($mode eq "current-url") { $self->show_current_url($args); } elsif($mode eq "base-url") { $self->show_base_url($args); } elsif($mode eq "show-pagedesc") { $self->show_pagedesc($args); } elsif($mode eq "number-ordinal-suffix") { $self->number_ordinal_suffix($args); } else { $self->throw("check_mode - Unknown mode '$mode'"); } } ############################################################################### =item 'tracking-url' => tracking_url (%) Displays tracking URL for given carrier and tracking number. Arguments are "carrier" and "tracknum". Supported carriers are: =over =item * 'usps' =item * 'ups' =item * 'fedex' =item * 'dhl' =item * 'yellow' (see http://www.yellowcorp.com/) =back Example: <%Utility mode="tracking-url" carrier="usps" tracknum="VV1234567890"%> Would display: http://www.framed.usps.com/cgi-bin/cttgate/ontrack.cgi?tracknbr=VV1234567890 =cut sub tracking_url ($%) { my $self=shift; my $args=get_args(\@_); my $carrier=$args->{carrier}; my $tracknum=$args->{tracknum}; my $url; if(lc($carrier) eq 'usps') { $url='http://www.framed.usps.com/cgi-bin/cttgate/ontrack.cgi' . '?tracknbr=' . t2ht($tracknum); } elsif(lc($carrier) eq 'ups') { $url='http://wwwapps.ups.com/WebTracking/processInputRequest' . '?HTMLVersion=5.0&sort_by=status&tracknums_displayed=1' . '&TypeOfInquiryNumber=T&loc=en_US&AgreeToTermsAndConditions=yes' . '&InquiryNumber1=' . t2ht($tracknum); } elsif(lc($carrier) eq 'fedex') { $url='http://fedex.com/cgi-bin/tracking' . '?tracknumbers=' . t2ht($tracknum) . '&action=track&language=english&cntry_code=us'; } elsif(lc($carrier) eq 'dhl') { $url='http://www.dhl-usa.com/cgi-bin/tracking.pl' . '?AWB=' . t2ht($tracknum) . 'LAN=ENG&TID=US_ENG&FIRST_DB=US'; } elsif(lc($carrier) eq 'yellow') { $tracknum=sprintf('%09u',int($tracknum)); $url='http://www2.yellowcorp.com/cgi-bin/gx.cgi/applogic+yfsgentracing.E000YfsTrace' . '?diff=protrace&PRONumber=' . t2ht($tracknum); } else { eprint "Unknown carrier '$carrier'"; $url=''; } $self->textout($url); } ############################################################################### =item 'config-param' => config_param (%) Displays site configuration parameter with given "name". Example: <%Utility mode="config-param" name="customer_support" default="aa@bb.com"%> Would display whatever is set in site's Config.pm modules for variable "customer_support" or "aa@bb.com" if it is not set. =cut sub config_param ($%) { my $self=shift; my $args=get_args(\@_); my $config=$self->siteconfig; $args->{name} || throw XAO::E::DO::Web::Utility "config_param - no 'name' given"; my $value=$config->get($args->{name}); $value=$args->{default} if !defined($value) && defined($args->{default}); $self->textout($value) if defined $value; } ############################################################################### =item 'pass-cgi-params' => pass_cgi_params (%) Builds a piece of HTML code containing current CGI parameters in either form or query formats depending on "result" argument (values are "form" or "query" respectfully). List of parameters to be copied must be in "params" arguments and may end with asterisk (*) to include parameters by template. In addition to that you can exclude some parameters that wer listed in "params" by putting their names (or name templates) into "except" argument. Form example:
<%Utility mode="pass-cgi-params" result="form" params="aa,bb,cc"%>
Would produce:
Actual output would be slightly different because no carriage return symbol would be inserted between hidden tags. This is done for rare situations when your code is space sensitive and you do not want to mess it. Query example: ">Sort by price If current page arguments were "sku=123&category=234&sortby=vendor" then the output would be: Sort by price All special symbols in parameter values would be properly escaped, you do not need to worry about that. =cut sub pass_cgi_params ($%) { my $self=shift; my $args=get_args(\@_); ## # Creating list of exceptions # my %except; foreach my $param (split(/[,\s]/,$args->{except} || '')) { $param=~s/\s//gs; next unless length($param); if(index($param,'*') != -1) { $param=substr($param,0,index($param,'*')); foreach my $p ($self->cgi->param) { next unless index($p,$param) == 0; $except{$p}=1; } next; } $except{$param}=1; } ## # Expanding parameters in list # my @params; foreach my $param (split(/[,\s]/,$args->{params})) { $param=~s/\s//gs; next unless length($param); if(index($param,'*') != -1) { $param=substr($param,0,index($param,'*')); foreach my $p ($self->cgi->param) { next unless defined $p; next unless index($p,$param) == 0; push @params,$p; } next; } push @params,$param; } ## # Creating HTML code that will pass these parameters. # my $html; my $result=$args->{result} || 'query'; foreach my $param (@params) { next if $except{$param}; my $value=$self->cgi->param($param); next unless defined $value; if($result eq 'form') { $html.=''; } else { $html.='&' if $html; $html.=t2hq($param) . '=' . t2hq($value); } } $self->textout($html) if defined $html; } ############################################################################### =item 'current-url' => show_current_url () Prints out current page URL without parameters. Accepts the same arguments as Page's pageurl method and displays the same value. =cut sub show_current_url ($;%) { my $self=shift; $self->textout($self->pageurl(@_)); } ############################################################################### =item 'base-url' => show_base_url () Prints out base site URL without parameters. Accepts the same arguments as Page's base_url() method and displays the same value. =cut sub show_base_url ($;%) { my $self=shift; $self->textout($self->base_url(@_)); } ############################################################################### =item 'number-ordinal-suffix' => number_ordinal_suffix (%) Displays a two-letter suffix to make a number into an ordinal, i.e. 2 into "2-nd", 43 into "43-rd", 1001 into "1001-st" and so on. Takes one argument -- 'number'. =cut sub number_ordinal_suffix ($%) { my $self=shift; my $args=get_args(\@_); use integer; my $number=int($args->{number} || 0); $number=-$number if $number<0; $number=$number % 100; my $nl=$number%10; my $suffix; if(($number>10 && $number<20) || $nl==0 || $nl>3) { $suffix='th'; } elsif($nl == 1) { $suffix='st'; } elsif($nl == 2) { $suffix='nd'; } elsif($nl == 2) { $suffix='nd'; } else { $suffix='rd'; } $self->textout($suffix); } ############################################################################### =item 'show-pagedesc' => show_pagedesc (%) Displays value of pagedesc structure (see L) with the given "name". Default name is "fullpath". Useful for processing tree-to-object mapped documents. =cut sub show_pagedesc ($) { my $self=shift; my $args=get_args(\@_); my $name=$args->{name} || 'fullpath'; $self->textout($self->clipboard->get('pagedesc')->{$name} || ''); } ############################################################################### =item 'select-time-range' => select_time_range(%) Displays a list of