Slovo
view release on metacpan or search on metacpan
lib/Slovo/Controller/Celini.pm view on Meta::CPAN
package Slovo::Controller::Celini;
use Mojo::Base 'Slovo::Controller', -signatures;
use Role::Tiny::With;
with 'Slovo::Controller::Role::Stranica';
use Mojo::Collection 'c';
my sub _redirect_to_new_celina_url ($c, $page, $celina) {
# https://tools.ietf.org/html/rfc7538#section-3
my $status = $c->req->method =~ /GET|HEAD/i ? 301 : 308;
$c->res->code($status);
return $c->redirect_to(
para_with_lang => {
paragraph_alias => $celina->{alias},
page_alias => $page->{alias},
lang => $celina->{language}});
}
# Prepares collection of parent ids of celiny in which a celina can be put.
my sub _celini_options ($c, $id, $page_id, $user, $l) {
my $celini = $c->celini;
my $opts = {
where => {
page_id => $page_id,
$id ? (id => {'!=' => $id}) : (), %{$celini->writable_by($user)},
language => $celini->language_like($l),
permissions => {-like => 'd%'}}};
my $options = $celini->all($opts)->map(sub { ["â$_->{title}â" => $_->{id}] });
unshift @$options, ['ÐÑ Ð½Ð¸ÐºÐ¾Ñ' => 0];
return $options;
}
# ANY /<page_alias:str>/<paragraph_alias:cel>.<lang:lng>.html
# ANY /<page_alias:str>/<paragraph_alias:cel>.html
# Display a content element in a page in the site.
sub execute ($c, $page, $user, $l, $preview) {
# TODO celina breadcrumb
#my $path = [split m|/|, $c->stash->{'paragraph'}];
#my $path = $c->celini->breadcrumb($p_alias, $path, $l, $user, $preview);
my $stash = $c->stash;
my $alias = $stash->{'paragraph_alias'};
my $celina = $c->celini->find_for_display($alias, $user, $l, $preview,
{page_id => $page->{id}, box => $stash->{boxes}[0]});
unless ($celina) {
$celina = $c->celini->find_where(
{page_id => $c->not_found_id, language => $l, data_type => 'title'});
return $c->render(celina => $celina, status => $c->not_found_code);
}
# Celina was found, but with a new alias.
return $c->_redirect_to_new_celina_url($page, $celina) if $celina->{alias} ne $alias;
return $c->is_fresh(last_modified => $celina->{tstamp})
? $c->rendered(304)
: $c->render(
celina => $celina,
paragraph_alias => $celina->{alias},
lang => $celina->{language});
}
# Check for pid and page_id parameters and redirect to pages' index page if
# needed, so the user can choose in which page to create the new writing.
my sub _validate_create ($c, $u, $l, $str) {
my $int = qr/^\d{1,10}$/;
my $v = $c->validation;
$v->optional(page_id => 'trim')->like($int);
$v->optional(pid => 'trim')->like($int);
my $in = $v->output;
my $celini = $c->celini;
my $data_types = $c->stash('data_types');
my $root = $str->find_where(
{dom_id => $c->stash('domain')->{id}, page_type => $str->root_page_type});
if (!defined $in->{pid} && !defined $in->{page_id}) {
$c->flash(message => 'ÐÑма подаден Ð½Ð¾Ð¼ÐµÑ Ð½Ð° ÑÑÑаниÑа! '
. 'ÐзбеÑеÑе ÑÑÑаниÑа, в коÑÑо да ÑÑздадеÑе новоÑо пиÑанѥ!');
$c->redirect_to($c->url_for('home_stranici')->query(pid => $root->{id}));
return;
}
elsif (defined $in->{pid} && !defined $in->{page_id}) {
my $cel = $celini->find_where({
pid => $in->{pid},
language => $celini->language_like($l),
%{$celini->writable_by($u)}});
unless ($cel) {
$c->flash(message => 'ÐÑмаÑе пÑава да пиÑеÑе в Ñаздел Ñ Ð½Ð¾Ð¼ÐµÑ '
. $in->{pid}
. '. ÐзбеÑеÑе ÑÑÑаниÑа, в коÑÑо да ÑÑздадеÑе новоÑо пиÑанѥ!');
$c->redirect_to($c->url_for('home_stranici')->query(pid => $root->{id}));
return;
}
$in->{page_id} = $cel->{page_id};
}
elsif (!defined $in->{pid} && defined $in->{page_id}) {
my $cel = $celini->find_where({
page_id => $in->{page_id},
data_type => $data_types->[0], # note
language => $celini->language_like($l),
( run in 2.171 seconds using v1.01-cache-2.11-cpan-71847e10f99 )