Add a simple random recipe selector
This commit is contained in:
parent
50b7e883f3
commit
c73ed0d98b
120
blocks/ORB/Random.pm
Normal file
120
blocks/ORB/Random.pm
Normal file
@ -0,0 +1,120 @@
|
||||
## @file
|
||||
# This file contains the implementation of the delete page.
|
||||
#
|
||||
# @author Chris Page <chris@starforge.co.uk>
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see http://www.gnu.org/licenses/.
|
||||
|
||||
## @class
|
||||
package ORB::Random;
|
||||
|
||||
use strict;
|
||||
use parent qw(ORB::Common); # This class extends the ORB common class
|
||||
use experimental qw(smartmatch);
|
||||
use v5.14;
|
||||
use JSON;
|
||||
|
||||
|
||||
sub _generate_random {
|
||||
my $self = shift;
|
||||
my $type = shift // "Mains*";
|
||||
my $errors;
|
||||
|
||||
$type =~ s/\*/%/g;
|
||||
|
||||
# Try to fetch the data.
|
||||
my $recipe = $self -> {"system"} -> {"recipe"} -> get_random_recipe($type);
|
||||
return $self -> _fatal_error("{L_RANDOM_FAILED_NOTFOUND}")
|
||||
unless($recipe -> {"id"});
|
||||
|
||||
# Redirect to the view page for the recipe
|
||||
return $self -> redirect($self -> build_url(block => "view",
|
||||
pathinfo => [ $recipe -> {"id"} ],
|
||||
params => "f=".time(),
|
||||
api => []))
|
||||
if(!$errors);
|
||||
|
||||
return $self -> _fatal_error($self -> {"template"} -> load_template("error/error_list.tem",
|
||||
{
|
||||
"%(message)s" => "{L_RANDOM_FAILED}",
|
||||
"%(errors)s" => $errors
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
## @method private @ _fatal_error($error)
|
||||
# Generate the tile and content for an error page.
|
||||
#
|
||||
# @param error A string containing the error message to display
|
||||
# @return The title of the error page and an error message to place in the page.
|
||||
sub _fatal_error {
|
||||
my $self = shift;
|
||||
my $error = shift;
|
||||
|
||||
return ("{L_RANDOM_ERROR_FATAL}",
|
||||
$self -> {"template"} -> load_template("error/page_error.tem",
|
||||
{ "%(message)s" => $error,
|
||||
"%(url-logout)s" => $self -> build_url(block => "login", pathinfo => ["signout"])
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
## @method private $ _dispatch_ui()
|
||||
# Implements the core behaviour dispatcher for non-api functions. This will
|
||||
# inspect the state of the pathinfo and invoke the appropriate handler
|
||||
# function to generate content for the user.
|
||||
#
|
||||
# @return A string containing the page HTML.
|
||||
sub _dispatch_ui {
|
||||
my $self = shift;
|
||||
|
||||
my @pathinfo = $self -> {"cgi"} -> multi_param("pathinfo");
|
||||
my ($title, $body, $extrahead, $extrajs) = $self -> _generate_random($pathinfo[0]);
|
||||
|
||||
# Done generating the page content, return the filled in page template
|
||||
return $self -> generate_orb_page(title => $title,
|
||||
content => $body,
|
||||
extrahead => $extrahead,
|
||||
extrajs => $extrajs,
|
||||
active => '-',
|
||||
doclink => 'random');
|
||||
}
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Module interface functions
|
||||
|
||||
## @method $ page_display()
|
||||
# Generate the page content for this module.
|
||||
sub page_display {
|
||||
my $self = shift;
|
||||
|
||||
# Is this an API call, or a normal page operation?
|
||||
my $apiop = $self -> is_api_operation();
|
||||
if(defined($apiop)) {
|
||||
# API call - dispatch to appropriate handler.
|
||||
given($apiop) {
|
||||
default {
|
||||
return $self -> api_response($self -> api_errorhash('bad_op',
|
||||
$self -> {"template"} -> replace_langvar("API_BAD_OP")))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return $self -> _dispatch_ui();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
1;
|
Loading…
x
Reference in New Issue
Block a user