| 1 | package Strehler::Element::Role::Slugged; | ||||||||||
| 2 | 12 | $Strehler::Element::Role::Slugged::VERSION = '1.6.8'; | |||||||||
| 3 | 12 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 12 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 12 | ▶ | use Dancer2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 12 | ▶ | use Strehler::Helpers; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | sub to_slug | ||||||||||
| 9 | 26 | ▶ | { | ||||||||
|
Subroutine
to_slug: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 10 | 26 | my $self = shift; | |||||||||
| 11 | 26 | my $lan = shift; | |||||||||
| 12 | 26 | return 'title_' . $lan; | |||||||||
| 13 | } | ||||||||||
| 14 | |||||||||||
| 15 | sub multilang_slug | ||||||||||
| 16 | 1 | ▶ | { | ||||||||
|
Subroutine
multilang_slug: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 17 | 1 | return 1; | |||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | sub save_slug | ||||||||||
| 21 | 27 | ▶ | { | ||||||||
|
Subroutine
save_slug: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 22 | 27 | my $self = shift; | |||||||||
| 23 | 27 | my $id = shift; | |||||||||
| 24 | 27 | my $form = shift; | |||||||||
| 25 | 27 | my $lan = shift; | |||||||||
| 26 | 27 | my $slug_param = $self->to_slug($lan); | |||||||||
| 27 | 27 | ▶ | if($form->param_value($slug_param)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | { | ||||||||||
| 29 | 22 | return $id . '-' . Strehler::Helpers::slugify($form->param_value($slug_param)); | |||||||||
| 30 | } | ||||||||||
| 31 | else | ||||||||||
| 32 | { | ||||||||||
| 33 | 5 | return undef; | |||||||||
| 34 | } | ||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | sub get_by_slug | ||||||||||
| 38 | 2 | ▶ | { | ||||||||
|
Subroutine
get_by_slug: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 39 | 2 | my $self = shift; | |||||||||
| 40 | 2 | my $slug = shift; | |||||||||
| 41 | 2 | my $language = shift; | |||||||||
| 42 | |||||||||||
| 43 | 2 | my $chapter; | |||||||||
| 44 | 2 | ▶ | if($self->multilang_slug()) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | { | ||||||||||
| 46 | 1 | my $children = $self->multilang_children(); | |||||||||
| 47 | 1 | my @chapters = $self->get_schema()->resultset($self->ORMObj())->search( | |||||||||
| 48 | { | ||||||||||
| 49 | "$children.slug" => $slug, | ||||||||||
| 50 | "$children.language" => $language, | ||||||||||
| 51 | }, | ||||||||||
| 52 | { join => $children } | ||||||||||
| 53 | ); | ||||||||||
| 54 | 1 | $chapter = $chapters[0]; | |||||||||
| 55 | } | ||||||||||
| 56 | else | ||||||||||
| 57 | { | ||||||||||
| 58 | 1 | $chapter = $self->get_schema()->resultset($self->ORMObj())->find({ slug => $slug }); | |||||||||
| 59 | } | ||||||||||
| 60 | 2 | ▶ | if($chapter) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | { | ||||||||||
| 62 | 2 | return $self->new($chapter->id); | |||||||||
| 63 | } | ||||||||||
| 64 | else | ||||||||||
| 65 | { | ||||||||||
| 66 | 0 | return undef; | |||||||||
| 67 | } | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 12 | 1; | |||||||||
| 71 | |||||||||||
| 72 | =encoding utf8 | ||||||||||
| 73 | |||||||||||
| 74 | =head1 NAME | ||||||||||
| 75 | |||||||||||
| 76 | Strehler::Element::Role::Slugged - A role to manage element's slug | ||||||||||
| 77 | |||||||||||
| 78 | =head1 DESCRIPTION | ||||||||||
| 79 | |||||||||||
| 80 | A slugged Strehler element provides an authomatic management of a slug (multilanguage or not). Slug can then be used to retrieve the element itself. | ||||||||||
| 81 | |||||||||||
| 82 | Default implementation that come with the role is the one used by L<Strehler::Element::Article> but methods can be overridden for different behaviours. | ||||||||||
| 83 | |||||||||||
| 84 | A slugged element must include, in its database representation, a field named slug (VARCHAR), in the main table or in the table of multilanguage contents. | ||||||||||
| 85 | |||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||
| 87 | |||||||||||
| 88 | my $article = Strehler::Element::Article->get_by_slug('a-slug-suitable-for-web', $language) | ||||||||||
| 89 | |||||||||||
| 90 | =head1 FUNCTIONS | ||||||||||
| 91 | |||||||||||
| 92 | =over 4 | ||||||||||
| 93 | |||||||||||
| 94 | =item multilang_slug | ||||||||||
| 95 | |||||||||||
| 96 | Return 1 if the slug is multilanguage and comes from the multilanguage contents table. | ||||||||||
| 97 | |||||||||||
| 98 | =item to_slug | ||||||||||
| 99 | |||||||||||
| 100 | Return the field, from the form, that will be used to make the slug. | ||||||||||
| 101 | |||||||||||
| 102 | =item save_slug | ||||||||||
| 103 | |||||||||||
| 104 | Hook used by save_form method from L<Strehler::Element>. | ||||||||||
| 105 | |||||||||||
| 106 | It takes the field with the name returned by to_slug from the POSTed form and create a slug from it using L<Strehler::Helpers> slugify function. | ||||||||||
| 107 | |||||||||||
| 108 | Slug is guaranteed unique because, in any case, element ID is preferred to the slug string. | ||||||||||
| 109 | |||||||||||
| 110 | =item get_by_slug | ||||||||||
| 111 | |||||||||||
| 112 | Function to retrieve elements using slug. | ||||||||||
| 113 | |||||||||||
| 114 | An element that implements this method returns true when slugged() function (L<Strehler::Element::Role::Configured>) is called. | ||||||||||
| 115 | |||||||||||
| 116 | =back | ||||||||||
| 117 | |||||||||||
| 118 | =cut | ||||||||||
| 119 | |||||||||||