Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision f657bd8e

Von Tamino Steinert vor 8 Tagen hinzugefügt

S:C:Part: Drucken angepasst

Setze die von Template erwarteten Variablen.
Übersetze den Templatenamen.
Suche nach Template der Sprache.

Unterschiede anzeigen:

SL/Controller/Part.pm
29 29
use SL::Helper::PrintOptions;
30 30
use SL::Helper::UserPreferences::PartPickerSearch;
31 31
use SL::JSON;
32
use SL::MIME;
32 33
use SL::Locale::String qw(t8);
33 34
use SL::MoreCommon qw(save_form);
34 35
use SL::Presenter::EscapedText qw(escape is_escaped);
......
138 139
  $self->js->flash('info', t8('The item has been saved.'));
139 140

  
140 141
  my $formname    = $::form->{print_options}->{formname};
141
  my $language    = $::form->{print_options}->{language};
142 142
  my $format      = $::form->{print_options}->{format};
143 143
  my $media       = $::form->{print_options}->{media};
144 144
  my $printer_id  = $::form->{print_options}->{printer_id};
145 145
  my $copies      = $::form->{print_options}->{copies};
146
  my $language;
147
  if ($::form->{print_options}->{language_id}) {
148
    $language = SL::DB::Manager::Language->find_by(
149
      id => $::form->{print_options}->{language_id}
150
    );
151
  }
146 152

  
147
  my %result;
148 153
  eval {
149
    %result = SL::Template::LaTeX->parse_and_create_pdf(
150
       $formname . ".tex",
151
       SELF        => $self,
152
       part        => $self->part,
153
       template_meta => {
154
          formname   => 'part',
155
          language   => $language,
156
          extension  => 'pdf',
157
          format     => $format,
158
          media      => $media,
159
          today      => DateTime->today,
160
          lxconfig   => \%::lx_office_conf,
161
        },
162
      );
163
    if ($result{error}) {
164
      die t8('Conversion to PDF failed: #1', $result{error});
154

  
155
    my $template_ext;
156
    my $template_type;
157
    if ($format =~ /(opendocument|oasis)/i) {
158
      $template_ext  = 'odt';
159
      $template_type = 'OpenDocument';
160
    } elsif ($format =~ m{html}i) {
161
      $template_ext  = 'html';
162
      $template_type = 'HTML';
163
    }
164

  
165
    # search for the template
166
    my ($template_file, @template_files) = SL::Helper::CreatePDF->find_template(
167
      name        => $formname,
168
      extension   => $template_ext,
169
      language    => $language,
170
      printer_id  => $printer_id,
171
    );
172
    if (!defined $template_file) {
173
      die t8('Cannot find matching template for this print request. Please contact your template maintainer. I tried these: #1.', join ', ', map { "'$_'"} @template_files);
165 174
    }
166 175

  
167
    my $pdf = $result{file_name};
176
    my $print_form = Form->new('');
177
    $print_form->{part}       = $self->part;
178

  
179
    # for doc_filename
180
    $print_form->{type}       = 'part';
181
    $print_form->{formname}   = $formname;
182
    $print_form->{format}     = $format;
183
    $print_form->{partnumber} = $self->part->partnumber;
184
    $print_form->{language}   = $language && ("_" .
185
                                ($language->template_code || $language->description)
186
                              );
187

  
188
    # for tempalte variables
189
    $print_form->{template_meta}->{language} = $language;
190
    $print_form->{media} = $media;
191
    $print_form->{media} = 'file' if $print_form->{media} eq 'screen';
192
    my $default = SL::DB::Default->get;
193
    $print_form->{employee_company} = $default->company;
194
    $print_form->{currency}         = $default->currency->name;
195

  
196
    my $document = SL::Helper::CreatePDF->create_pdf(
197
      format        => $format,
198
      template_type => $template_type,
199
      template      => $template_file,
200
      variables     => $print_form,
201
      variable_content_types => {
202
        notes => 'html',
203
        # TODO: html cvars
204
      },
205
    );
168 206

  
169 207
    if ($media eq 'screen') {
170
      my $file_name =  $formname . '.pdf';
171
      $file_name    =~ s{[^\w\.]+}{_}g;
208
      my $doc_filename = $print_form->generate_attachment_filename();
172 209

  
173 210
      $self->send_file(
174
        $pdf,
175
        type => 'application/pdf',
176
        name => $file_name,
211
        \$document,
212
        type => SL::MIME->mime_type_from_ext($doc_filename),
213
        name => $doc_filename,
177 214
        js_no_render => 1,
178 215
      );
179
      unlink $result{file_name};
180 216
    } elsif ($media eq 'printer') {
181 217
      my $printer = SL::DB::Printer->new(id => $printer_id)->load;
182 218
      $printer->print_document(
183 219
        copies  => $copies,
184
        file_name => $result{file_name},
220
        content => $document,
185 221
      );
186 222

  
187 223
      $self->js->flash('info', t8('The document has been sent to the printer \'#1\'.', $printer->printer_description));
188
      unlink $result{file_name} if $result{file_name};
189 224
    } else {
190 225
      die t8('Media \'#1\' is not supported yet/anymore.', $media);
191 226
    }
192 227

  
193 228
    1;
194 229
  } or do {
195
    unlink $result{file_name} if $result{file_name};
196 230
    $self->js
197 231
      ->flash('error', t8("Creating the PDF failed!"))
198 232
      ->flash('error', $@);

Auch abrufbar als: Unified diff