Add code formatter, format all code

This commit is contained in:
Rumperuu 2021-03-20 12:09:50 +00:00
parent 602a59f1c3
commit 47a55f6322
120 changed files with 8061 additions and 6967 deletions

View file

@ -1,8 +1,9 @@
package Pear::LocalLoop::Plugin::BootstrapPagination;
# nabbed from Mojolicious::Plugin::BootstrapPagination -
# nabbed from Mojolicious::Plugin::BootstrapPagination -
# https://github.com/csroli/Mojolicious-Plugin-BootstrapPagination
use Mojo::Base 'Mojolicious::Plugin';
use POSIX( qw/ceil/ );
use POSIX(qw/ceil/);
use Mojo::ByteStream 'b';
use strict;
@ -11,85 +12,170 @@ use warnings;
our $VERSION = "0.14";
# Homer: Well basically, I just copied the plant we have now.
# Then, I added some fins to lower wind resistance.
# Then, I added some fins to lower wind resistance.
# And this racing stripe here I feel is pretty sharp.
# Burns: Agreed. First prize!
sub register{
my ( $self, $app, $args ) = @_;
$args ||= {};
sub register {
my ( $self, $app, $args ) = @_;
$args ||= {};
$app->helper( bootstrap_pagination => sub{
my ( $self, $actual, $count, $opts ) = @_;
my %bs4classes = (list_class => "page-item", anchor_class => "page-link");
$app->helper(
bootstrap_pagination => sub {
my ( $self, $actual, $count, $opts ) = @_;
my %bs4classes =
( list_class => "page-item", anchor_class => "page-link" );
my $localize = ( $opts->{localize} || $args->{localize} ) ?
( $opts->{localize} || $args->{localize} ) : undef;
my $localize =
( $opts->{localize} || $args->{localize} )
? ( $opts->{localize} || $args->{localize} )
: undef;
$count = ceil($count);
return "" unless $count > 1;
$opts = {} unless $opts;
my $round = $opts->{round} || $args->{round} || 4;
my $param = $opts->{param} || $args->{param} || "page";
my $class = $opts->{class} || $args->{class} || "";
my $bs4 = $opts->{bootstrap4} || $args->{bootstrap4} || undef;
$count = ceil($count);
return "" unless $count > 1;
$opts = {} unless $opts;
my $round = $opts->{round} || $args->{round} || 4;
my $param = $opts->{param} || $args->{param} || "page";
my $class = $opts->{class} || $args->{class} || "";
my $bs4 = $opts->{bootstrap4} || $args->{bootstrap4} || undef;
if ($class ne ""){
$class = " " . $class;
}
my $outer = $opts->{outer} || $args->{outer} || 2;
my $query = exists $opts->{query} ? $opts->{query} : $args->{query} || "";
my $start = $opts->{start} // $args->{start} // 1;
my @current = ( $actual - $round .. $actual + $round );
my @first = ($start.. $start + $outer - 1);
my @tail = ( $count - $outer + 1 .. $count );
my @ret = ();
my $last = undef;
foreach my $number( sort { $a <=> $b } @current, @first, @tail ){
next if ( $last && $last == $number && $start > 0 ) || ( defined $last && $last == $number && $start == 0 );
next if ( $number <= 0 && $start > 0) || ( $number < 0 && $start == 0 );
last if ( $number > $count && $start > 0 ) || ( $number >= $count && $start == 0 );
push @ret, ".." if( $last && $last + 1 != $number );
push @ret, $number;
$last = $number;
}
my $html = "<ul class=\"pagination$class\">";
if( $actual == $start ){
$html .= "<li class=\"disabled".($bs4?" ".$bs4classes{list_class}:"")."\"><a".($bs4?" class=\"".$bs4classes{anchor_class}."\"":"")." href=\"#\" >&laquo;</a></li>";
} else {
$html .= "<li".($bs4?" class=\"".$bs4classes{list_class}."\"":"")."><a".($bs4?" class=\"".$bs4classes{anchor_class}."\"":"")." href=\"" . $self->url_with->query( [$param => $actual - 1] ) . $query . "\" >&laquo;</a></li>";
}
my $last_num = -1;
foreach my $number( @ret ){
my $show_number = $start > 0 ? $number : ( $number =~ /\d+/ ? $number + 1 : $number );
if ( $class ne "" ) {
$class = " " . $class;
}
my $outer = $opts->{outer} || $args->{outer} || 2;
my $query =
exists $opts->{query} ? $opts->{query} : $args->{query} || "";
my $start = $opts->{start} // $args->{start} // 1;
my @current = ( $actual - $round .. $actual + $round );
my @first = ( $start .. $start + $outer - 1 );
my @tail = ( $count - $outer + 1 .. $count );
my @ret = ();
my $last = undef;
if ( $localize ) {
$show_number = $localize->($self, $show_number);
foreach my $number ( sort { $a <=> $b } @current, @first, @tail ) {
next
if ( $last && $last == $number && $start > 0 )
|| ( defined $last && $last == $number && $start == 0 );
next
if ( $number <= 0 && $start > 0 )
|| ( $number < 0 && $start == 0 );
last
if ( $number > $count && $start > 0 )
|| ( $number >= $count && $start == 0 );
push @ret, ".." if ( $last && $last + 1 != $number );
push @ret, $number;
$last = $number;
}
my $html = "<ul class=\"pagination$class\">";
if ( $actual == $start ) {
$html .=
"<li class=\"disabled"
. ( $bs4 ? " " . $bs4classes{list_class} : "" ) . "\"><a"
. (
$bs4 ? " class=\"" . $bs4classes{anchor_class} . "\"" : "" )
. " href=\"#\" >&laquo;</a></li>";
}
else {
$html .= "<li"
. ( $bs4 ? " class=\"" . $bs4classes{list_class} . "\"" : "" )
. "><a"
. (
$bs4 ? " class=\"" . $bs4classes{anchor_class} . "\"" : "" )
. " href=\""
. $self->url_with->query( [ $param => $actual - 1 ] )
. $query
. "\" >&laquo;</a></li>";
}
my $last_num = -1;
foreach my $number (@ret) {
my $show_number =
$start > 0
? $number
: ( $number =~ /\d+/ ? $number + 1 : $number );
if ($localize) {
$show_number = $localize->( $self, $show_number );
}
if ( $number eq ".." && $last_num < $actual ) {
my $offset = ceil( ( $actual - $round ) / 2 ) + 1;
$html .= "<li"
. ( $bs4 ? " class=\"" . $bs4classes{list_class} . "\""
: "" )
. "><a"
. ( $bs4 ? " class=\"" . $bs4classes{anchor_class} . "\""
: "" )
. " href=\""
. $self->url_with->query(
[ $param => $start == 0 ? $offset + 1 : $offset ] )
. $query
. "\" >&hellip;</a></li>";
}
elsif ( $number eq ".." && $last_num > $actual ) {
my $back = $count - $outer + 1;
my $forw = $round + $actual;
my $offset = ceil( ( ( $back - $forw ) / 2 ) + $forw );
$html .= "<li"
. ( $bs4 ? " class=\"" . $bs4classes{list_class} . "\""
: "" )
. "><a"
. ( $bs4 ? " class=\"" . $bs4classes{anchor_class} . "\""
: "" )
. " href=\""
. $self->url_with->query(
[ $param => $start == 0 ? $offset + 1 : $offset ] )
. $query
. "\" >&hellip;</a></li>";
}
elsif ( $number == $actual ) {
$html .=
"<li class=\"active"
. ( $bs4 ? " " . $bs4classes{list_class} : "" )
. "\"><span"
. ( $bs4
? " class=\"" . $bs4classes{anchor_class} . "\""
: "" )
. ">$show_number</span></li>";
}
else {
$html .= "<li"
. ( $bs4 ? " class=\"" . $bs4classes{list_class} . "\""
: "" )
. "><a"
. ( $bs4 ? " class=\"" . $bs4classes{anchor_class} . "\""
: "" )
. " href=\""
. $self->url_with->query( [ $param => $number ] )
. $query
. "\">$show_number</a></li>";
}
$last_num = $number;
}
if ( $actual == $count ) {
$html .=
"<li class=\"disabled"
. ( $bs4 ? " " . $bs4classes{list_class} : "" ) . "\"><a"
. (
$bs4 ? " class=\"" . $bs4classes{anchor_class} . "\"" : "" )
. " href=\""
. $self->url_with->query( [ $param => $actual + 1 ] )
. $query
. "\" >&raquo;</a></li>";
}
else {
$html .= "<li"
. ( $bs4 ? " class=\"" . $bs4classes{list_class} . "\"" : "" )
. "><a"
. (
$bs4 ? " class=\"" . $bs4classes{anchor_class} . "\"" : "" )
. " href=\""
. $self->url_with->query( [ $param => $actual + 1 ] )
. $query
. "\" >&raquo;</a></li>";
}
$html .= "</ul>";
return b($html);
}
if( $number eq ".." && $last_num < $actual ){
my $offset = ceil( ( $actual - $round ) / 2 ) + 1 ;
$html .= "<li".($bs4?" class=\"".$bs4classes{list_class}."\"":"")."><a".($bs4?" class=\"".$bs4classes{anchor_class}."\"":"")." href=\"" . $self->url_with->query( [$param => $start == 0 ? $offset + 1 : $offset] ) . $query ."\" >&hellip;</a></li>";
}
elsif( $number eq ".." && $last_num > $actual ) {
my $back = $count - $outer + 1;
my $forw = $round + $actual;
my $offset = ceil( ( ( $back - $forw ) / 2 ) + $forw );
$html .= "<li".($bs4?" class=\"".$bs4classes{list_class}."\"":"")."><a".($bs4?" class=\"".$bs4classes{anchor_class}."\"":"")." href=\"" . $self->url_with->query( [$param => $start == 0 ? $offset + 1 : $offset] ) . $query ."\" >&hellip;</a></li>";
} elsif( $number == $actual ) {
$html .= "<li class=\"active".($bs4?" ".$bs4classes{list_class}:"")."\"><span".($bs4?" class=\"".$bs4classes{anchor_class}."\"":"").">$show_number</span></li>";
} else {
$html .= "<li".($bs4?" class=\"".$bs4classes{list_class}."\"":"")."><a".($bs4?" class=\"".$bs4classes{anchor_class}."\"":"")." href=\"" . $self->url_with->query( [$param => $number] ) . $query ."\">$show_number</a></li>";
}
$last_num = $number;
}
if( $actual == $count ){
$html .= "<li class=\"disabled".($bs4?" ".$bs4classes{list_class}:"")."\"><a".($bs4?" class=\"".$bs4classes{anchor_class}."\"":"")." href=\"" . $self->url_with->query( [$param => $actual + 1] ) . $query . "\" >&raquo;</a></li>";
} else {
$html .= "<li".($bs4?" class=\"".$bs4classes{list_class}."\"":"")."><a".($bs4?" class=\"".$bs4classes{anchor_class}."\"":"")." href=\"" . $self->url_with->query( [$param => $actual + 1] ) . $query . "\" >&raquo;</a></li>";
}
$html .= "</ul>";
return b( $html );
} );
);
}

View file

@ -2,23 +2,28 @@ package Pear::LocalLoop::Plugin::Currency;
use Mojo::Base 'Mojolicious::Plugin';
sub register {
my ( $plugin, $app, $cong ) = @_;
my ( $plugin, $app, $cong ) = @_;
$app->helper( parse_currency => sub {
my ( $c, $currency_string ) = @_;
my $value;
if ( $currency_string =~ /^£([\d.]+)/ ) {
$value = $1 * 1;
} elsif ( $currency_string =~ /^([\d.]+)/ ) {
$value = $1 * 1;
}
return $value;
});
$app->helper(
parse_currency => sub {
my ( $c, $currency_string ) = @_;
my $value;
if ( $currency_string =~ /^£([\d.]+)/ ) {
$value = $1 * 1;
}
elsif ( $currency_string =~ /^([\d.]+)/ ) {
$value = $1 * 1;
}
return $value;
}
);
$app->helper( format_currency_from_db => sub {
my ( $c, $value ) = @_;
return sprintf( '£%.2f', $value / 100000 );
});
$app->helper(
format_currency_from_db => sub {
my ( $c, $value ) = @_;
return sprintf( '£%.2f', $value / 100000 );
}
);
}
1;

View file

@ -4,85 +4,96 @@ use Mojo::Base 'Mojolicious::Plugin';
use DateTime::Format::Strptime;
sub register {
my ( $plugin, $app, $conf ) = @_;
my ( $plugin, $app, $conf ) = @_;
$app->helper( human_datetime_parser => sub {
return DateTime::Format::Strptime->new( pattern => '%x %X' );
});
$app->helper( format_human_datetime => sub {
my ( $c, $datetime_obj ) = @_;
return $c->human_datetime_parser->format_datetime(
$datetime_obj,
$app->helper(
human_datetime_parser => sub {
return DateTime::Format::Strptime->new( pattern => '%x %X' );
}
);
});
$app->helper( iso_datetime_parser => sub {
return DateTime::Format::Strptime->new( pattern => '%Y-%m-%dT%H:%M:%S.%3N%z' );
});
$app->helper( iso_date_parser => sub {
return DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' );
});
$app->helper( iso_month_parser => sub {
return DateTime::Format::Strptime->new( pattern => '%Y-%m' );
});
$app->helper( parse_iso_date => sub {
my ( $c, $date_string ) = @_;
return $c->iso_date_parser->parse_datetime(
$date_string,
$app->helper(
format_human_datetime => sub {
my ( $c, $datetime_obj ) = @_;
return $c->human_datetime_parser->format_datetime( $datetime_obj, );
}
);
});
$app->helper( format_iso_date => sub {
my ( $c, $datetime_obj ) = @_;
return $c->iso_date_parser->format_datetime(
$datetime_obj,
$app->helper(
iso_datetime_parser => sub {
return DateTime::Format::Strptime->new(
pattern => '%Y-%m-%dT%H:%M:%S.%3N%z' );
}
);
});
$app->helper( parse_iso_month => sub {
my ( $c, $date_string ) = @_;
return $c->iso_month_parser->parse_datetime(
$date_string,
$app->helper(
iso_date_parser => sub {
return DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' );
}
);
});
$app->helper( format_iso_month => sub {
my ( $c, $datetime_obj ) = @_;
return $c->iso_month_parser->format_datetime(
$datetime_obj,
$app->helper(
iso_month_parser => sub {
return DateTime::Format::Strptime->new( pattern => '%Y-%m' );
}
);
});
$app->helper( parse_iso_datetime => sub {
my ( $c, $date_string ) = @_;
return $c->iso_datetime_parser->parse_datetime(
$date_string,
$app->helper(
parse_iso_date => sub {
my ( $c, $date_string ) = @_;
return $c->iso_date_parser->parse_datetime( $date_string, );
}
);
});
$app->helper( format_iso_datetime => sub {
my ( $c, $datetime_obj ) = @_;
return unless defined $datetime_obj;
return $c->iso_datetime_parser->format_datetime(
$datetime_obj,
$app->helper(
format_iso_date => sub {
my ( $c, $datetime_obj ) = @_;
return $c->iso_date_parser->format_datetime( $datetime_obj, );
}
);
});
$app->helper( db_datetime_parser => sub {
return shift->schema->storage->datetime_parser;
});
$app->helper( format_db_datetime => sub {
my ( $c, $datetime_obj ) = @_;
$datetime_obj->set_time_zone('UTC');
return $c->db_datetime_parser->format_datetime(
$datetime_obj,
$app->helper(
parse_iso_month => sub {
my ( $c, $date_string ) = @_;
return $c->iso_month_parser->parse_datetime( $date_string, );
}
);
$app->helper(
format_iso_month => sub {
my ( $c, $datetime_obj ) = @_;
return $c->iso_month_parser->format_datetime( $datetime_obj, );
}
);
$app->helper(
parse_iso_datetime => sub {
my ( $c, $date_string ) = @_;
return $c->iso_datetime_parser->parse_datetime( $date_string, );
}
);
$app->helper(
format_iso_datetime => sub {
my ( $c, $datetime_obj ) = @_;
return unless defined $datetime_obj;
return $c->iso_datetime_parser->format_datetime( $datetime_obj, );
}
);
$app->helper(
db_datetime_parser => sub {
return shift->schema->storage->datetime_parser;
}
);
$app->helper(
format_db_datetime => sub {
my ( $c, $datetime_obj ) = @_;
$datetime_obj->set_time_zone('UTC');
return $c->db_datetime_parser->format_datetime( $datetime_obj, );
}
);
});
}

View file

@ -4,36 +4,36 @@ use Mojo::Base 'Mojolicious::Plugin';
use Mojo::Loader qw/ find_modules load_class /;
sub register {
my ( $plugin, $app, $cong ) = @_;
my ( $plugin, $app, $cong ) = @_;
if ( defined $app->config->{minion} ) {
$app->log->debug('Setting up Minion tasks');
$app->plugin('Minion' => $app->config->{minion} );
if ( defined $app->config->{minion} ) {
$app->log->debug('Setting up Minion tasks');
$app->plugin( 'Minion' => $app->config->{minion} );
$app->log->debug('Loaded Minion Job packages:');
$app->log->debug('Loaded Minion Job packages:');
my $job_namespace = __PACKAGE__ . '::Job';
my @modules = find_modules $job_namespace;
for my $package ( @modules ) {
my ( $job_name ) = $package =~ /${job_namespace}::(.*)$/;
$app->log->debug( $package );
if (my $e = load_class $package) {
die ref $e ? "Exception: $e" : "$package not found";
}
$app->minion->add_task(
$job_name => sub {
my ( $job, @args ) = @_;
my $job_runner = $package->new(
job => $job,
);
$job_runner->run( @args );
my $job_namespace = __PACKAGE__ . '::Job';
my @modules = find_modules $job_namespace;
for my $package (@modules) {
my ($job_name) = $package =~ /${job_namespace}::(.*)$/;
$app->log->debug($package);
if ( my $e = load_class $package) {
die ref $e ? "Exception: $e" : "$package not found";
}
$app->minion->add_task(
$job_name => sub {
my ( $job, @args ) = @_;
my $job_runner = $package->new( job => $job, );
$job_runner->run(@args);
}
);
}
);
# $app->minion->enqueue('test' => [ 'test arg 1', 'test_arg 2' ] );
}
else {
$app->log->debug('No Minion Config');
}
# $app->minion->enqueue('test' => [ 'test arg 1', 'test_arg 2' ] );
} else {
$app->log->debug('No Minion Config');
}
}

View file

@ -1,12 +1,12 @@
package Pear::LocalLoop::Plugin::Minion::Job;
use Mojo::Base -base;
has [ qw/ job / ];
has [qw/ job /];
has app => sub { shift->job->app };
sub run {
die ( __PACKAGE__ . " must implement run sub" );
die( __PACKAGE__ . " must implement run sub" );
}
1;

View file

@ -4,12 +4,12 @@ use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job';
use Pear::LocalLoop::Import::LCCCsv::Postcodes;
sub run {
my ( $self, $filename ) = @_;
my ( $self, $filename ) = @_;
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Postcodes->new(
csv_file => $filename,
schema => $self->app->schema
)->import_csv;
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Postcodes->new(
csv_file => $filename,
schema => $self->app->schema
)->import_csv;
}
1;

View file

@ -4,12 +4,12 @@ use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job';
use Pear::LocalLoop::Import::LCCCsv::Suppliers;
sub run {
my ( $self, $filename ) = @_;
my ( $self, $filename ) = @_;
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Suppliers->new(
csv_file => $filename,
schema => $self->app->schema
)->import_csv;
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Suppliers->new(
csv_file => $filename,
schema => $self->app->schema
)->import_csv;
}
1;

View file

@ -4,13 +4,13 @@ use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job';
use Pear::LocalLoop::Import::LCCCsv::Transactions;
sub run {
my ($self, $filename, $entity_id) = @_;
my ( $self, $filename, $entity_id ) = @_;
Pear::LocalLoop::Import::LCCCsv::Transactions->new(
csv_file => $filename,
schema => $self->app->schema,
target_entity_id => $entity_id,
)->import_csv;
Pear::LocalLoop::Import::LCCCsv::Transactions->new(
csv_file => $filename,
schema => $self->app->schema,
target_entity_id => $entity_id,
)->import_csv;
}
1;

View file

@ -2,29 +2,34 @@ package Pear::LocalLoop::Plugin::Minion::Job::entity_postcode_lookup;
use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job';
sub run {
my ( $self, $entity_id ) = @_;
my ( $self, $entity_id ) = @_;
my $entity_rs = $self->app->schema->resultset('Entity');
$entity_rs = $entity_rs->search({id => $entity_id }) if $entity_id;
my $entity_rs = $self->app->schema->resultset('Entity');
$entity_rs = $entity_rs->search( { id => $entity_id } ) if $entity_id;
while ( my $entity = $entity_rs->next ) {
my $obj = $entity->type_object;
next unless $obj;
while ( my $entity = $entity_rs->next ) {
my $obj = $entity->type_object;
next unless $obj;
my $postcode_obj = Geo::UK::Postcode::Regex->parse( $obj->postcode );
my $postcode_obj = Geo::UK::Postcode::Regex->parse( $obj->postcode );
unless ( defined $postcode_obj && $postcode_obj->{non_geographical} ) {
my $pc_result = $self->app->schema->resultset('GbPostcode')->find({
incode => $postcode_obj->{incode},
outcode => $postcode_obj->{outcode},
});
if ( defined $pc_result ) {
$entity->update_or_create_related('postcode', {
gb_postcode => $pc_result,
});
}
unless ( defined $postcode_obj && $postcode_obj->{non_geographical} ) {
my $pc_result = $self->app->schema->resultset('GbPostcode')->find(
{
incode => $postcode_obj->{incode},
outcode => $postcode_obj->{outcode},
}
);
if ( defined $pc_result ) {
$entity->update_or_create_related(
'postcode',
{
gb_postcode => $pc_result,
}
);
}
}
}
}
}
1;

View file

@ -2,11 +2,11 @@ package Pear::LocalLoop::Plugin::Minion::Job::leaderboards_recalc;
use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job';
sub run {
my ( $self, @args ) = @_;
my ( $self, @args ) = @_;
my $leaderboard_rs = $self->app->schema->resultset('Leaderboard');
my $leaderboard_rs = $self->app->schema->resultset('Leaderboard');
$leaderboard_rs->recalculate_all;
$leaderboard_rs->recalculate_all;
}
1;

View file

@ -2,12 +2,12 @@ package Pear::LocalLoop::Plugin::Minion::Job::test;
use Mojo::Base 'Pear::LocalLoop::Plugin::Minion::Job';
sub run {
my ( $self, @args ) = @_;
my ( $self, @args ) = @_;
$self->job->app->log->debug( 'Testing Job' );
for my $arg ( @args ) {
$self->job->app->log->debug( $arg );
}
$self->job->app->log->debug('Testing Job');
for my $arg (@args) {
$self->job->app->log->debug($arg);
}
}
1;

View file

@ -5,54 +5,72 @@ use Geo::UK::Postcode::Regex;
use GIS::Distance;
sub register {
my ( $plugin, $app, $conf ) = @_;
my ( $plugin, $app, $conf ) = @_;
$app->helper( get_location_from_postcode => sub {
my ( $c, $postcode, $usertype ) = @_;
my $postcode_obj = Geo::UK::Postcode::Regex->parse( $postcode );
$app->helper(
get_location_from_postcode => sub {
my ( $c, $postcode, $usertype ) = @_;
my $postcode_obj = Geo::UK::Postcode::Regex->parse($postcode);
my $location;
my $location;
unless ( defined $postcode_obj && $postcode_obj->{non_geographical} ) {
my $pc_result = $c->schema->resultset('GbPostcode')->find({
incode => $postcode_obj->{incode},
outcode => $postcode_obj->{outcode},
});
if ( defined $pc_result ) {
# Force truncation here as SQLite is stupid
$location = {
latitude => (
$usertype eq 'customer'
? int($pc_result->latitude * 100 ) / 100
: $pc_result->latitude
),
longitude => (
$usertype eq 'customer'
? int($pc_result->longitude * 100 ) / 100
: $pc_result->longitude
),
};
}
}
return $location;
});
unless ( defined $postcode_obj
&& $postcode_obj->{non_geographical} )
{
my $pc_result = $c->schema->resultset('GbPostcode')->find(
{
incode => $postcode_obj->{incode},
outcode => $postcode_obj->{outcode},
}
);
if ( defined $pc_result ) {
$app->helper( get_distance_from_coords => sub {
my ( $c, $buyer, $seller ) = @_;
# Force truncation here as SQLite is stupid
$location = {
latitude => (
$usertype eq 'customer'
? int( $pc_result->latitude * 100 ) / 100
: $pc_result->latitude
),
longitude => (
$usertype eq 'customer'
? int( $pc_result->longitude * 100 ) / 100
: $pc_result->longitude
),
};
}
}
return $location;
}
);
my $gis = GIS::Distance->new();
$app->helper(
get_distance_from_coords => sub {
my ( $c, $buyer, $seller ) = @_;
my $buyer_lat = $buyer->latitude;
my $buyer_long = $buyer->longitude;
my $seller_lat = $seller->latitude;
my $seller_long = $seller->longitude;
my $gis = GIS::Distance->new();
if ( $buyer_lat && $buyer_long
&& $seller_lat && $seller_long ) {
return int( $gis->distance( $buyer_lat, $buyer_long => $seller_lat, $seller_long )->meters );
}
return;
});
my $buyer_lat = $buyer->latitude;
my $buyer_long = $buyer->longitude;
my $seller_lat = $seller->latitude;
my $seller_long = $seller->longitude;
if ( $buyer_lat
&& $buyer_long
&& $seller_lat
&& $seller_long )
{
return int(
$gis->distance(
$buyer_lat,
$buyer_long => $seller_lat,
$seller_long
)->meters
);
}
return;
}
);
}
1;

View file

@ -2,16 +2,19 @@ package Pear::LocalLoop::Plugin::TemplateHelpers;
use Mojo::Base 'Mojolicious::Plugin';
sub register {
my ( $plugin, $app, $conf ) = @_;
my ( $plugin, $app, $conf ) = @_;
$app->helper( truncate_text => sub {
my ( $c, $string, $length ) = @_;
if ( length $string < $length ) {
return $string;
} else {
return substr( $string, 0, $length - 3 ) . '...';
}
});
$app->helper(
truncate_text => sub {
my ( $c, $string, $length ) = @_;
if ( length $string < $length ) {
return $string;
}
else {
return substr( $string, 0, $length - 3 ) . '...';
}
}
);
}

View file

@ -9,73 +9,98 @@ use DateTime::Format::Strptime;
use Try::Tiny;
sub register {
my ( $plugin, $app, $conf ) = @_;
my ( $plugin, $app, $conf ) = @_;
$app->validator->add_check( email => sub {
my ( $validation, $name, $email ) = @_;
return Email::Valid->address( $email ) ? undef : 1;
});
$app->validator->add_check(
email => sub {
my ( $validation, $name, $email ) = @_;
return Email::Valid->address($email) ? undef : 1;
}
);
$app->validator->add_check( in_resultset => sub {
my ( $validation, $name, $value, $key, $rs ) = @_;
return $rs->search({ $key => $value })->count ? undef : 1;
});
$app->validator->add_check(
in_resultset => sub {
my ( $validation, $name, $value, $key, $rs ) = @_;
return $rs->search( { $key => $value } )->count ? undef : 1;
}
);
$app->validator->add_check( not_in_resultset => sub {
my ( $validation, $name, $value, $key, $rs ) = @_;
return $rs->search({ $key => $value })->count ? 1 : undef;
});
$app->validator->add_check(
not_in_resultset => sub {
my ( $validation, $name, $value, $key, $rs ) = @_;
return $rs->search( { $key => $value } )->count ? 1 : undef;
}
);
$app->validator->add_check( postcode => sub {
my ( $validation, $name, $value ) = @_;
return is_valid_pc( $value ) ? undef : 1;
});
$app->validator->add_check(
postcode => sub {
my ( $validation, $name, $value ) = @_;
return is_valid_pc($value) ? undef : 1;
}
);
$app->validator->add_check( number => sub {
my ( $validation, $name, $value ) = @_;
return looks_like_number( $value ) ? undef : 1;
});
$app->validator->add_check(
number => sub {
my ( $validation, $name, $value ) = @_;
return looks_like_number($value) ? undef : 1;
}
);
$app->validator->add_check( gt_num => sub {
my ( $validation, $name, $value, $check ) = @_;
return $value > $check ? undef : 1;
});
$app->validator->add_check(
gt_num => sub {
my ( $validation, $name, $value, $check ) = @_;
return $value > $check ? undef : 1;
}
);
$app->validator->add_check( lt_num => sub {
my ( $validation, $name, $value, $check ) = @_;
return $value < $check ? undef : 1;
});
$app->validator->add_check(
lt_num => sub {
my ( $validation, $name, $value, $check ) = @_;
return $value < $check ? undef : 1;
}
);
$app->validator->add_check( filetype => sub {
my ( $validation, $name, $value, $filetype ) = @_;
my ( undef, undef, $extension ) = fileparse $value->filename, qr/\.[^.]*/;
$extension =~ s/^\.//;
return $app->types->type($extension) eq $filetype ? undef : 1;
});
$app->validator->add_check(
filetype => sub {
my ( $validation, $name, $value, $filetype ) = @_;
my ( undef, undef, $extension ) = fileparse $value->filename,
qr/\.[^.]*/;
$extension =~ s/^\.//;
return $app->types->type($extension) eq $filetype ? undef : 1;
}
);
$app->validator->add_check( is_iso_date => sub {
my ( $validation, $name, $value ) = @_;
$value = $app->iso_date_parser->parse_datetime( $value );
return defined $value ? undef : 1;
});
$app->validator->add_check(
is_iso_date => sub {
my ( $validation, $name, $value ) = @_;
$value = $app->iso_date_parser->parse_datetime($value);
return defined $value ? undef : 1;
}
);
$app->validator->add_check( is_full_iso_datetime => sub {
my ( $validation, $name, $value ) = @_;
$value = $app->parse_iso_datetime( $value );
return defined $value ? undef : 1;
});
$app->validator->add_check(
is_full_iso_datetime => sub {
my ( $validation, $name, $value ) = @_;
$value = $app->parse_iso_datetime($value);
return defined $value ? undef : 1;
}
);
$app->validator->add_check( is_object => sub {
my ( $validation, $name, $value ) = @_;
return ref ( $value ) eq 'HASH' ? undef : 1;
});
$app->validator->add_check(
is_object => sub {
my ( $validation, $name, $value ) = @_;
return ref($value) eq 'HASH' ? undef : 1;
}
);
$app->validator->add_check( in_range => sub {
my ( $validation, $name, $value, $low, $high ) = @_;
return $low < $value && $value < $high ? undef : 1;
});
$app->validator->add_check(
in_range => sub {
my ( $validation, $name, $value, $low, $high ) = @_;
return $low < $value && $value < $high ? undef : 1;
}
);
$app->helper( validation_error => sub { _validation_error(@_) } );
$app->helper( validation_error => sub { _validation_error(@_) } );
}
=head2 validation_error
@ -87,92 +112,92 @@ set the errors up as required. Renders out the errors as an array, with status
=cut
sub _validation_error {
my ( $c, $sub_name ) = @_;
my ( $c, $sub_name ) = @_;
my $val_data = $c->validation_data->{ $sub_name };
return unless defined $val_data;
my $data = $c->stash->{api_json};
my $val_data = $c->validation_data->{$sub_name};
return unless defined $val_data;
my $data = $c->stash->{api_json};
my @errors = _validate_set( $c, $val_data, $data );
my @errors = _validate_set( $c, $val_data, $data );
if ( scalar @errors ) {
my @sorted_errors = sort @errors;
$c->render(
json => {
success => Mojo::JSON->false,
errors => \@sorted_errors,
},
status => 400,
);
return \@errors;
}
if ( scalar @errors ) {
my @sorted_errors = sort @errors;
$c->render(
json => {
success => Mojo::JSON->false,
errors => \@sorted_errors,
},
status => 400,
);
return \@errors;
}
return;
return;
}
sub _validate_set {
my ( $c, $val_data, $data, $parent_name ) = @_;
my ( $c, $val_data, $data, $parent_name ) = @_;
my @errors;
my @errors;
# MUST get a raw validation object
my $validation = $c->app->validator->validation;
$validation->input( $data );
# MUST get a raw validation object
my $validation = $c->app->validator->validation;
$validation->input($data);
for my $val_data_key ( keys %$val_data ) {
for my $val_data_key ( keys %$val_data ) {
$validation->topic( $val_data_key );
$validation->topic($val_data_key);
my $val_set = $val_data->{$val_data_key};
my $val_set = $val_data->{$val_data_key};
my $custom_check_prefix = {};
my $custom_check_prefix = {};
for my $val_error ( @{$val_set->{validation}} ) {
my ( $val_validator ) = keys %$val_error;
for my $val_error ( @{ $val_set->{validation} } ) {
my ($val_validator) = keys %$val_error;
unless (
$validation->validator->checks->{$val_validator}
|| $val_validator =~ /required|optional/
) {
$c->app->log->warn( 'Unknown Validator [' . $val_validator . ']' );
next;
}
unless ( $validation->validator->checks->{$val_validator}
|| $val_validator =~ /required|optional/ )
{
$c->app->log->warn(
'Unknown Validator [' . $val_validator . ']' );
next;
}
if ( my $custom_prefix = $val_error->{ $val_validator }->{ error_prefix } ) {
$custom_check_prefix->{ $val_validator } = $custom_prefix;
}
my $val_args = $val_error->{ $val_validator }->{ args };
$validation->$val_validator(
( $val_validator =~ /required|optional/ ? $val_data_key : () ),
( defined $val_args ? @$val_args : () )
);
if ( my $custom_prefix =
$val_error->{$val_validator}->{error_prefix} )
{
$custom_check_prefix->{$val_validator} = $custom_prefix;
}
my $val_args = $val_error->{$val_validator}->{args};
# stop bothering checking if failed, validation stops after first failure
last if $validation->has_error( $val_data_key );
$validation->$val_validator(
( $val_validator =~ /required|optional/ ? $val_data_key : () ),
( defined $val_args ? @$val_args : () )
);
# stop bothering checking if failed, validation stops after first failure
last if $validation->has_error($val_data_key);
}
if ( $validation->has_error($val_data_key) ) {
my ($check) = @{ $validation->error($val_data_key) };
my $error_prefix =
defined $custom_check_prefix->{$check}
? $custom_check_prefix->{$check}
: $check;
my $error_string = join( '_',
$error_prefix, ( defined $parent_name ? $parent_name : () ),
$val_data_key, );
push @errors, $error_string;
}
elsif ( defined $val_set->{children} ) {
push @errors,
_validate_set( $c, $val_set->{children}, $data->{$val_data_key},
$val_data_key );
}
}
if ( $validation->has_error( $val_data_key ) ) {
my ( $check ) = @{ $validation->error( $val_data_key ) };
my $error_prefix = defined $custom_check_prefix->{ $check }
? $custom_check_prefix->{ $check }
: $check;
my $error_string = join ('_',
$error_prefix,
( defined $parent_name ? $parent_name : () ),
$val_data_key,
);
push @errors, $error_string;
} elsif ( defined $val_set->{ children } ) {
push @errors, _validate_set(
$c,
$val_set->{ children },
$data->{ $val_data_key },
$val_data_key );
}
}
return @errors;
return @errors;
}
1;