| 1 | package Daje::Workflow::Activities::Document::Builder; | ||
| 2 | use Mojo::Base 'Daje::Workflow::Common::Activity::Base', -base, -signatures; | ||
| 3 | use v5.40; | ||
| 4 | |||
| 5 | use Data::Dumper; | ||
| 6 | use Daje::Workflow::Templates; | ||
| 7 | use Template; | ||
| 8 | |||
| 9 | # NAME | ||
| 10 | # ==== | ||
| 11 | # | ||
| 12 | # Daje::Workflow::Document::Builder::Activity - It's the document builder | ||
| 13 | # | ||
| 14 | # | ||
| 15 | # REQUIRES | ||
| 16 | # ======== | ||
| 17 | # | ||
| 18 | # use Mojo::Base; | ||
| 19 | # use Template; | ||
| 20 | # | ||
| 21 | # DESCRIPTION | ||
| 22 | # =========== | ||
| 23 | # | ||
| 24 | # Daje::Workflow::Document::Builder::Activity Builds a document from data and | ||
| 25 | # a template. The output is than stored in the context for further processing, | ||
| 26 | # | ||
| 27 | # $self->context->{context}->{document} = $document; | ||
| 28 | # | ||
| 29 | # | ||
| 30 | # Its mainly intended for single documents like simple mails etc. | ||
| 31 | # | ||
| 32 | # | ||
| 33 | # LICENSE | ||
| 34 | # ======= | ||
| 35 | # | ||
| 36 | # Copyright (C) janeskil1525. | ||
| 37 | # | ||
| 38 | # This library is free software; you can redistribute it and/or modify | ||
| 39 | # it under the same terms as Perl itself. | ||
| 40 | # | ||
| 41 | # AUTHOR | ||
| 42 | # ====== | ||
| 43 | # | ||
| 44 | # janeskil1525 E<lt>janeskil1525@gmail.comE<gt> | ||
| 45 | # | ||
| 46 | # | ||
| 47 | |||
| 48 | our $VERSION = "0.01"; | ||
| 49 | |||
| 50 | |||
| 51 | sub process($self) { | ||
| 52 | |||
| 53 | $self->model->insert_history( | ||
| 54 | "Build documents", | ||
| 55 | "Daje::Workflow::Activities::Document::Builder::process", | ||
| 56 | 1 | ||
| 57 | ); | ||
| 58 | |||
| 59 | my $data = $self->context->{context}; | ||
| 60 | if(my $tt = $self->_load_template()) { | ||
| 61 | if(my $templates = $self->_load_templates() and $self->error->has_error() == 0) { | ||
| 62 | if($self->error->has_error() == 0) { | ||
| 63 | my $document = ""; | ||
| 64 | if($tt->process(\$templates, $self->context->{context}->{data}, \$document) == 1) { | ||
| 65 | $self->context->{context}->{document} = $document; | ||
| 66 | } else { | ||
| 67 | $self->error->add_error($tt->error()); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _load_templates($self) { | ||
| 75 | my $tpl = $self->context->{context}->{template}->{data_section}; | ||
| 76 | my $templates = Daje::Workflow::Templates->new( | ||
| 77 | data_sections => $tpl, | ||
| 78 | source => $self->context->{context}->{template}->{source}, | ||
| 79 | error => $self->error, | ||
| 80 | )->load_templates(); | ||
| 81 | my $template = $templates->{data_sec}->{$tpl}; | ||
| 82 | |||
| 83 | $self->error->add_error("No template named '$tpl' found") unless length($template) > 0; | ||
| 84 | return $template; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _load_template($self) { | ||
| 88 | my $tt = Template->new( | ||
| 89 | { | ||
| 90 | INTERPOLATE => 1 | ||
| 91 | } | ||
| 92 | ) or $self->error->add_error("$Template::ERROR\n"); | ||
| 93 | |||
| 94 | return $tt; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | 1; | ||
| 101 | __END__ |