Projet

Général

Profil

Paste
Télécharger (20,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_pdf / views_pdf_plugin_display.inc @ 11b63505

1
<?php
2

    
3
/**
4
 * @file
5
 * PDF display plugin.
6
 */
7

    
8
/**
9
 * This class contains all the functionality of the PDF display.
10
 */
11
class views_pdf_plugin_display extends views_plugin_display_page {
12

    
13
  /**
14
   * Define the display type
15
   */
16
  function get_style_type() { return 'pdf'; }
17

    
18
  /**
19
   * Disable the breadcrumb
20
   */
21
  function uses_breadcrumb() { return FALSE; }
22

    
23
  function has_path() { return TRUE; }
24

    
25

    
26
  /**
27
   * Render the display
28
   */
29
  function render() {
30
    // Generall document layout
31

    
32
    // Set default code
33
    $this->view->pdf->SetFont('');
34

    
35
    // Add leading pages
36
    $path = $this->view->pdf->getTemplatePath($this->get_option('leading_template'));
37
    $this->view->pdf->addPdfDocument($path);
38

    
39
    // Set the default background template
40
    $path = $this->view->pdf->getTemplatePath($this->get_option('template'));
41
    $this->view->pdf->setDefaultPageTemplate($path, 'main');
42

    
43
    // Render the items
44
    $this->view->style_plugin->render();
45

    
46
    // Add succeed pages
47
    $path = $this->view->pdf->getTemplatePath($this->get_option('succeed_template'));
48
    $this->view->pdf->addPdfDocument($path);
49

    
50
  }
51

    
52
  /**
53
   * Provide the preview. Because PDF cannot be embedded good in HTML, we do not
54
   * provide a preview.
55
   */
56
  function preview() {
57
    return t('The PDF display does not provide a preview.');
58
  }
59

    
60
  /**
61
   * This function executes the PDF display.
62
   */
63
  function execute($path_to_store_pdf = '', $destination = 'I') {
64

    
65
    // Defines external configuration for TCPDF library
66
    if (!defined('K_TCPDF_EXTERNAL_CONFIG')) {
67
      $tcpdf_path = drupal_realpath(views_pdf_get_library('tcpdf'));
68
      $cache_path = 'public://views_pdf_cache/';
69
      file_prepare_directory($cache_path, FILE_CREATE_DIRECTORY);
70
      global $base_url;
71
      define('K_TCPDF_EXTERNAL_CONFIG', TRUE);
72
      define('K_PATH_MAIN', dirname($_SERVER['SCRIPT_FILENAME']));
73
      define('K_PATH_URL', $base_url);
74
      define('K_PATH_FONTS', $tcpdf_path . '/fonts/');
75
      define('K_PATH_CACHE', drupal_realpath($cache_path));
76
      define('K_PATH_IMAGES', '');
77
      define('K_BLANK_IMAGE', $tcpdf_path . '/images/_blank.png');
78
      define('K_CELL_HEIGHT_RATIO', 1.25);
79
      define('K_SMALL_RATIO', 2/3);
80
    }
81

    
82
    if ($this->get_option('default_page_format') == 'custom') {
83
      if (preg_match('~([0-9\.]+)x([0-9\.]+)~', $this->get_option('default_page_format_custom'), $result)) {
84
        $format[0] = $result[1]; // width
85
        $format[1] = $result[2]; // height
86
      }
87
      else {
88
        $format = 'A4';
89
      }
90

    
91
    }
92
    else {
93
      $format = $this->get_option('default_page_format');
94
    }
95

    
96
    $orientation = $this->get_option('default_page_orientation'); // P or L
97
    $unit = $this->get_option('unit');
98
    $this->view->pdf = views_pdf_get_new_pdf_instance($orientation, $unit, $format);
99

    
100
    // Set margins: top, left, right
101
    $this->view->pdf->SetMargins($this->get_option('margin_left'), $this->get_option('margin_top'), $this->get_option('margin_right'), TRUE);
102

    
103
    // Set auto page break: margin bottom:
104
    $this->view->pdf->SetAutoPageBreak(TRUE, $this->get_option('margin_bottom'));
105

    
106
    $this->view->pdf->setDefaultFontSize($this->get_option('default_font_size'));
107
    $this->view->pdf->setDefaultFontFamily($this->get_option('default_font_family'));
108
    $this->view->pdf->setDefaultFontStyle($this->get_option('default_font_style'));
109
    $this->view->pdf->setDefaultTextAlign($this->get_option('default_text_align'));
110
    $this->view->pdf->setDefaultFontColor($this->get_option('default_font_color'));
111

    
112
    $this->view->pdf->setViewsHeader($this->view->display_handler->render_header());
113
    $this->view->pdf->setViewsFooter($this->view->display_handler->render_footer());
114

    
115
    $html = $this->view->render($this->display->id);
116

    
117
    if (!empty($html)) {
118
      echo $html;
119
    }
120

    
121
    if (empty($path_to_store_pdf)) {
122
      $path_to_store_pdf = $this->view->get_title();
123
      $this->view->pdf->SetTitle($this->view->get_title());
124
    }
125

    
126
    if (!preg_match('/\.pdf$/', $path_to_store_pdf)) {
127
      $path_to_store_pdf .= '.pdf';
128
    }
129

    
130
    ob_clean();
131
    if ($destination == 'I') {
132
      echo $this->view->pdf->Output($path_to_store_pdf, $destination);
133
      exit();
134
    }
135
    else {
136
      return $this->view->pdf->Output($path_to_store_pdf, $destination);
137
    }
138
  }
139

    
140
  /**
141
   * This method defines the default sections and the appropriated values.
142
   *
143
   */
144
  function defaultable_sections($section = NULL) {
145
    if (in_array($section, array('style_options', 'style_plugin', 'row_options', 'row_plugin'))) {
146
      return FALSE;
147
    }
148

    
149
    $sections = parent::defaultable_sections($section);
150

    
151
    // Tell views our sitename_title option belongs in the title section.
152
    if ($section == 'title') {
153
      $sections[] = 'sitename_title';
154
    }
155
    elseif (!$section) {
156
      $sections['title'][] = 'sitename_title';
157
    }
158
    return $sections;
159
  }
160

    
161
  /**
162
   * The option definition.
163
   */
164
  function option_definition() {
165
    $options = parent::option_definition();
166

    
167
    $options['displays'] = array('default' => array());
168

    
169
    // Overrides for standard stuff:
170
    $options['style_plugin']['default'] = 'pdf_unformatted';
171
    $options['style_options']['default']  = array('mission_description' => FALSE, 'description' => '');
172
    $options['sitename_title']['default'] = FALSE;
173
    $options['row_plugin']['default'] = 'pdf_fields';
174
    $options['defaults']['default']['style_plugin'] = FALSE;
175
    $options['defaults']['default']['style_options'] = FALSE;
176
    $options['defaults']['default']['row_plugin'] = FALSE;
177
    $options['defaults']['default']['row_options'] = FALSE;
178

    
179
    // New Options
180
    $options['default_page_format'] = array('default' => 'A4');
181
    $options['default_page_format_custom'] = array('default' => '');
182
    $options['default_page_orientation'] = array('default' => 'P');
183
    $options['unit'] = array('default' => 'mm');
184
    $options['margin_left'] = array('default' => '15');
185
    $options['margin_right'] = array('default' => '15');
186
    $options['margin_top'] = array('default' => '15');
187
    $options['margin_bottom'] = array('default' => '15');
188

    
189
    $options['leading_template'] = array('default' => '');
190
    $options['template'] = array('default' => '');
191
    $options['succeed_template'] = array('default' => '');
192

    
193
    $options['default_font_family'] = array('default' => 'helvetica');
194
    $options['default_font_style'] = array('default' => array());
195
    $options['default_font_size'] = array('default' => '11');
196
    $options['default_text_align'] = array('default' => 'L');
197
    $options['default_font_color'] = array('default' => '000000');
198
    $options['default_text_hyphenate'] = array('default' => 'none');
199

    
200
    $options['css_file'] = array('default' => '');
201

    
202
    return $options;
203
  }
204

    
205
  /**
206
   * Option form
207
   */
208
  function options_form(&$form, &$form_state) {
209
    parent::options_form($form, $form_state);
210

    
211
    switch ($form_state['section']) {
212
      case 'pdf_page':
213
        $form['#title'] .= t('PDF Page Options');
214
        $form['default_page_format'] = array(
215
          '#type' => 'select',
216
          '#title' => t('Default Page Format'),
217
          '#required' => TRUE,
218
          '#options' => views_pdf_get_page_formats(),
219
          '#description' => t('This is the default page format. If you specifiy a different format in the template section, this settings will be override.'),
220
          '#default_value' => $this->get_option('default_page_format'),
221
        );
222
        $form['default_page_format_custom'] = array(
223
          '#type' => 'textfield',
224
          '#title' => t('Custom Page Format'),
225
          '#description' => t('Here you can specifiy a custom page format. The schema is "[width]x[height]".'),
226
          '#default_value' => $this->get_option('default_page_format_custom'),
227
        );
228
        $form['default_page_orientation'] = array(
229
          '#type' => 'radios',
230
          '#title' => t('Default Page Orientation'),
231
          '#required' => TRUE,
232
          '#options' => array('P' => t('Portrait'), 'L' => t('Landscape')),
233
          '#description' => t('This is the default page orientation.'),
234
          '#default_value' => $this->get_option('default_page_orientation'),
235
        );
236
        $form['unit'] = array(
237
          '#type' => 'select',
238
          '#title' => t('Unit'),
239
          '#required' => TRUE,
240
          '#options' => array('mm' => t('mm: Millimeter'), 'pt' => t('pt: Point'), 'cm' => t('cm: Centimeter'), 'in' => t('in: Inch')),
241
          '#description' => t('This is the unit for the entered unit data. If you change this option all defined units were changed, but not converted.'),
242
          '#default_value' => $this->get_option('unit'),
243
        );
244
        $form['margin_left'] = array(
245
          '#type' => 'textfield',
246
          '#title' => t('Margin: Left'),
247
          '#required' => TRUE,
248
          '#default_value' => $this->get_option('margin_left'),
249
        );
250
        $form['margin_right'] = array(
251
          '#type' => 'textfield',
252
          '#title' => t('Margin: Right'),
253
          '#required' => TRUE,
254
          '#default_value' => $this->get_option('margin_right'),
255
        );
256
        $form['margin_top'] = array(
257
          '#type' => 'textfield',
258
          '#title' => t('Margin: Top'),
259
          '#required' => TRUE,
260
          '#default_value' => $this->get_option('margin_top'),
261
        );
262
        $form['margin_bottom'] = array(
263
          '#type' => 'textfield',
264
          '#title' => t('Margin: Bottom'),
265
          '#required' => TRUE,
266
          '#default_value' => $this->get_option('margin_bottom'),
267
        );
268
        break;
269
      case 'pdf_fonts':
270
        $fonts = views_pdf_get_font_list();
271
        $font_styles = array(
272
          'b' => t('Bold'),
273
          'i' => t('Italic'),
274
          'u' => t('Underline'),
275
          'd' => t('Line through'),
276
          'o' => t('Overline')
277
        );
278

    
279
        $align = array(
280
          'L' => t('Left'),
281
          'C' => t('Center'),
282
          'R' => t('Right'),
283
          'J' => t('Justify'),
284
        );
285

    
286
        $hyphenate = array(
287
          'none' => t('None'),
288
          'auto' => t('Detect automatically'),
289
        );
290
        $hyphenate = array_merge($hyphenate, views_pdf_get_hyphenations());
291

    
292
        $form['#title'] .= t('PDF Default Font Options');
293
        $form['description'] = array(
294
          '#prefix' => '<div class="description form-item">',
295
          '#suffix' => '</div>',
296
          '#value' => t('Here you specify a the default font settings for the document.'),
297
        );
298
        $form['default_font_size'] = array(
299
          '#type' => 'textfield',
300
          '#title' => t('Font Size'),
301
          '#size' => 10,
302
          '#default_value' => $this->get_option('default_font_size'),
303
        );
304
        $form['default_font_family'] = array(
305
          '#type' => 'select',
306
          '#title' => t('Font Family'),
307
          '#options' => $fonts,
308
          '#size' => 5,
309
          '#default_value' => $this->get_option('default_font_family'),
310
        );
311
        $form['default_font_style'] = array(
312
          '#type' => 'checkboxes',
313
          '#title' => t('Font Style'),
314
          '#options' => $font_styles,
315
          '#default_value' => $this->get_option('default_font_style'),
316
        );
317
        $form['default_text_align'] = array(
318
          '#type' => 'radios',
319
          '#title' => t('Text Alignment'),
320
          '#options' => $align,
321
          '#default_value' => $this->get_option('default_text_align'),
322
        );
323
        $form['default_text_hyphenate'] = array(
324
          '#type' => 'select',
325
          '#title' => t('Text Hyphenation'),
326
          '#options' => $hyphenate,
327
          '#description' => t('If you want to use hyphenation, then you need to download from <a href="@url">ctan.org</a> your needed pattern set. Then upload it to the dir "hyphenate_patterns" in the TCPDF lib directory. Perhaps you need to create the dir first. If you select the automated detection, then we try to get the language of the current node and select an appropriate hyphenation pattern.', array('@url' => 'http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/tex')),
328
          '#default_value' => $this->get_option('default_text_hyphenate'),
329
        );
330

    
331
        $form['default_font_color'] = array(
332
          '#type' => 'textfield',
333
          '#title' => t('Text Color'),
334
          '#description' => t('If a value is entered without a comma, it will be interpreted as a hexadecimal RGB color. Normal RGB can be used by separating the components by a comma. e.g 255,255,255 for white. A CMYK color can be entered in the same way as RGB. e.g. 0,100,0,0 for magenta.'),
335
          '#size' => 20,
336
          '#default_value' => $this->get_option('default_font_color'),
337
        );
338

    
339
        break;
340

    
341
      case 'pdf_template':
342
        $form['#title'] .= t('PDF Templates');
343

    
344
        $templates = array_merge(array(t('-- None --')), views_pdf_get_pdf_templates());
345
        $form['leading_template'] = array(
346
          '#type' => 'select',
347
          '#options' => $templates,
348
          '#title' => t('Leading PDF Template'),
349
          '#required' => FALSE,
350
          '#description' => t('Here you specify a PDF file to be printed in front of every row.'),
351
          '#default_value' => $this->get_option('leading_template'),
352
        );
353

    
354
        $form['template'] = array(
355
          '#type' => 'select',
356
          '#options' => $templates,
357
          '#title' => t('Template PDF'),
358
          '#description' => t('Here you specify a PDF file on which the content is printed. The first page of this document is used for the first page, in the target document. The second page is used for the second page in the target document and so on. If the target document has more that this template file, the last page of the template will be repeated. The leading document has no effect on the order of the pages.'),
359
          '#default_value' => $this->get_option('template'),
360
        );
361

    
362
        $form['succeed_template'] = array(
363
          '#type' => 'select',
364
          '#options' => $templates,
365
          '#title' => t('Succeed PDF Template'),
366
          '#required' => FALSE,
367
          '#description' => t('Here you specify a PDF file to be printed after the main content.'),
368
          '#default_value' => $this->get_option('succeed_template'),
369
        );
370

    
371
        $form['template_file'] = array(
372
          '#type' => 'file',
373
          '#title' => t('Upload New Template File'),
374
        );
375

    
376
        break;
377

    
378
      case 'displays':
379
        $form['#title'] .= t('Attach to');
380
        $displays = array();
381
        foreach ($this->view->display as $display_id => $display) {
382
          if (!empty($display->handler) && $display->handler->accept_attachments()) {
383
            $displays[$display_id] = $display->display_title;
384
          }
385
        }
386
        $form['displays'] = array(
387
          '#type' => 'checkboxes',
388
          '#description' => t('The feed icon will be available only to the selected displays.'),
389
          '#options' => $displays,
390
          '#default_value' => $this->get_option('displays'),
391
        );
392
        break;
393

    
394
      case 'css':
395
        $form['#title'] .= t('CSS File');
396
        $form['css_file'] = array(
397
          '#type' => 'textfield',
398
          '#description' => t('URL to a CSS file. This file is attached to all fields, rendered as HTML.'),
399
          '#default_value' => $this->get_option('css_file'),
400
        );
401
        break;
402
    }
403
  }
404

    
405
  /**
406
   * Provide a summary of the options.
407
   */
408
  function options_summary(&$categories, &$options) {
409
    parent::options_summary($categories, $options);
410

    
411
    $fonts = views_pdf_get_font_list();
412

    
413
    // Change Page title:
414
    $categories['page'] = array(
415
      'title' => t('PDF settings'),
416
      'column' => 'second',
417
      'build' => array(
418
        '#weight' => -10,
419
      ),
420
    );
421

    
422
    // Add for attach the display to others:
423
    $displays = array_filter($this->get_option('displays'));
424
    if (count($displays) > 1) {
425
      $attach_to = t('Multiple displays');
426
    }
427
    elseif (count($displays) == 1) {
428
      $display = array_shift($displays);
429
      if (!empty($this->view->display[$display])) {
430
        $attach_to = check_plain($this->view->display[$display]->display_title);
431
      }
432
    }
433

    
434
    if (!isset($attach_to)) {
435
      $attach_to = t('None');
436
    }
437

    
438
    $options['displays'] = array(
439
      'category' => 'page',
440
      'title' => t('Attach to'),
441
      'value' => $attach_to,
442
    );
443

    
444
    // Add for pdf page settings
445
    $options['pdf_page'] = array(
446
      'category' => 'page',
447
      'title' => t('PDF Page Settings'),
448
      'value' => $this->get_option('default_page_format'),
449
      'desc' => t('Define some PDF specific settings.'),
450
    );
451

    
452
    // Add for pdf font settings
453
    $options['pdf_fonts'] = array(
454
      'category' => 'page',
455
      'title' => t('PDF Fonts Settings'),
456
      'value' => t('!family at !size pt', array('!family' => $fonts[$this->get_option('default_font_family')], '!size' => $this->get_option('default_font_size'))),
457
      'desc' => t('Define some PDF specific settings.'),
458
    );
459

    
460
    // add for pdf template settings
461
    if ($this->get_option('leading_template') != '' || $this->get_option('template') != '' || $this->get_option('succeed_template') != '') {
462
      $isAnyTemplate = t('Yes');
463
    }
464
    else {
465
      $isAnyTemplate = t('No');
466
    }
467

    
468
    $options['pdf_template'] = array(
469
      'category' => 'page',
470
      'title' => t('PDF Template Settings'),
471
      'value' => $isAnyTemplate,
472
      'desc' => t('Define some PDF specific settings.'),
473
    );
474

    
475
    if ($this->get_option('css_file') == '') {
476
      $css_file = t('None');
477
    }
478
    else {
479
      $css_file = $this->get_option('css_file');
480
    }
481

    
482
    $options['css'] = array(
483
      'category' => 'page',
484
      'title' => t('CSS File'),
485
      'value' => $css_file,
486
      'desc' => t('Define a CSS file attached to all HTML output.'),
487
    );
488
  }
489

    
490
  /**
491
   * Handles the storage of the options.
492
   *
493
   */
494
  function options_submit(&$form, &$form_state) {
495
    // It is very important to call the parent function here:
496
    parent::options_submit($form, $form_state);
497
    switch ($form_state['section']) {
498
      case 'pdf_page':
499
        $this->set_option('default_page_format', $form_state['values']['default_page_format']);
500
        $this->set_option('default_page_format_custom', $form_state['values']['default_page_format_custom']);
501
        $this->set_option('default_page_orientation', $form_state['values']['default_page_orientation']);
502
        $this->set_option('unit', $form_state['values']['unit']);
503
        $this->set_option('margin_left', $form_state['values']['margin_left']);
504
        $this->set_option('margin_right', $form_state['values']['margin_right']);
505
        $this->set_option('margin_top', $form_state['values']['margin_top']);
506
        $this->set_option('margin_bottom', $form_state['values']['margin_bottom']);
507

    
508
        break;
509

    
510
      case 'pdf_fonts':
511
        $this->set_option('default_font_size', $form_state['values']['default_font_size']);
512
        $this->set_option('default_font_style', $form_state['values']['default_font_style']);
513
        $this->set_option('default_font_family', $form_state['values']['default_font_family']);
514
        $this->set_option('default_text_align', $form_state['values']['default_text_align']);
515
        $this->set_option('default_font_color', $form_state['values']['default_font_color']);
516

    
517
        break;
518

    
519
      case 'pdf_template':
520
        // Save new file:
521
        // Note: The jQuery update is required to use Ajax for file upload. With
522
        // default Drupal jQuery it will not work.
523
        // For upload with Ajax a iFrame is open and upload in it, because
524
        // normal forms are not allowed to handle directly.
525
        $destination = variable_get('views_pdf_template_stream', 'public://views_pdf_templates');
526

    
527
        if (file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
528
          $file = file_save_upload('template_file', array(), $destination);
529
          if (is_object($file)) {
530
            $file_name =  basename($file->destination, '.pdf');
531
            $form_state['values']['template'] = $file_name;
532
            $file->status |= FILE_STATUS_PERMANENT;
533
            $file = file_save($file);
534
          }
535
        }
536

    
537
        $this->set_option('leading_template', $form_state['values']['leading_template']);
538
        $this->set_option('template', $form_state['values']['template']);
539
        $this->set_option('succeed_template', $form_state['values']['succeed_template']);
540
        break;
541

    
542
      case 'displays':
543
        $this->set_option($form_state['section'], $form_state['values'][$form_state['section']]);
544
        break;
545

    
546
      case 'css':
547
        $this->set_option('css_file', $form_state['values']['css_file']);
548
        break;
549
    }
550
  }
551

    
552
  /**
553
   * Attach to another view.
554
   */
555
  function attach_to($display_id) {
556
    $displays = $this->get_option('displays');
557
    if (empty($displays[$display_id])) {
558
      return;
559
    }
560

    
561
    // Defer to the feed style; it may put in meta information, and/or
562
    // attach a feed icon.
563
    $plugin = $this->get_plugin();
564
    if ($plugin) {
565
      $clone = $this->view->clone_view();
566
      $clone->set_display($this->display->id);
567
      $clone->build_title();
568
      $plugin->attach_to($display_id, $this->get_path(), $clone->get_title());
569
    }
570
  }
571

    
572

    
573
}