Projet

Général

Profil

Paste
Télécharger (21 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_pdf / views_pdf_plugin_display.inc @ e9f59ce0

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
        $form['#attached']['js'][] = array(
377
          'type' => 'setting',
378
          'data' => array(
379
            'urlIsAjaxTrusted' => array(
380
              $GLOBALS['base_url'] . '/' . current_path() => TRUE,
381
            ),
382
          ),
383
        );
384

    
385
        break;
386

    
387
      case 'displays':
388
        $form['#title'] .= t('Attach to');
389
        $displays = array();
390
        foreach ($this->view->display as $display_id => $display) {
391
          if (!empty($display->handler) && $display->handler->accept_attachments()) {
392
            $displays[$display_id] = $display->display_title;
393
          }
394
        }
395
        $form['displays'] = array(
396
          '#type' => 'checkboxes',
397
          '#description' => t('The feed icon will be available only to the selected displays.'),
398
          '#options' => $displays,
399
          '#default_value' => $this->get_option('displays'),
400
        );
401
        break;
402

    
403
      case 'css':
404
        $form['#title'] .= t('CSS File');
405
        $form['css_file'] = array(
406
          '#type' => 'textfield',
407
          '#description' => t('URL to a CSS file. This file is attached to all fields, rendered as HTML.'),
408
          '#default_value' => $this->get_option('css_file'),
409
        );
410
        break;
411
    }
412
  }
413

    
414
  /**
415
   * Provide a summary of the options.
416
   */
417
  function options_summary(&$categories, &$options) {
418
    parent::options_summary($categories, $options);
419

    
420
    $fonts = views_pdf_get_font_list();
421

    
422
    // Change Page title:
423
    $categories['page'] = array(
424
      'title' => t('PDF settings'),
425
      'column' => 'second',
426
      'build' => array(
427
        '#weight' => -10,
428
      ),
429
    );
430

    
431
    // Add for attach the display to others:
432
    $displays = array_filter($this->get_option('displays'));
433
    if (count($displays) > 1) {
434
      $attach_to = t('Multiple displays');
435
    }
436
    elseif (count($displays) == 1) {
437
      $display = array_shift($displays);
438
      if (!empty($this->view->display[$display])) {
439
        $attach_to = check_plain($this->view->display[$display]->display_title);
440
      }
441
    }
442

    
443
    if (!isset($attach_to)) {
444
      $attach_to = t('None');
445
    }
446

    
447
    $options['displays'] = array(
448
      'category' => 'page',
449
      'title' => t('Attach to'),
450
      'value' => $attach_to,
451
    );
452

    
453
    // Add for pdf page settings
454
    $options['pdf_page'] = array(
455
      'category' => 'page',
456
      'title' => t('PDF Page Settings'),
457
      'value' => $this->get_option('default_page_format'),
458
      'desc' => t('Define some PDF specific settings.'),
459
    );
460

    
461
    // Add for pdf font settings
462
    $options['pdf_fonts'] = array(
463
      'category' => 'page',
464
      'title' => t('PDF Fonts Settings'),
465
      'value' => t('!family at !size pt', array('!family' => $fonts[$this->get_option('default_font_family')], '!size' => $this->get_option('default_font_size'))),
466
      'desc' => t('Define some PDF specific settings.'),
467
    );
468

    
469
    // add for pdf template settings
470
    if ($this->get_option('leading_template') != '' || $this->get_option('template') != '' || $this->get_option('succeed_template') != '') {
471
      $isAnyTemplate = t('Yes');
472
    }
473
    else {
474
      $isAnyTemplate = t('No');
475
    }
476

    
477
    $options['pdf_template'] = array(
478
      'category' => 'page',
479
      'title' => t('PDF Template Settings'),
480
      'value' => $isAnyTemplate,
481
      'desc' => t('Define some PDF specific settings.'),
482
    );
483

    
484
    if ($this->get_option('css_file') == '') {
485
      $css_file = t('None');
486
    }
487
    else {
488
      $css_file = $this->get_option('css_file');
489
    }
490

    
491
    $options['css'] = array(
492
      'category' => 'page',
493
      'title' => t('CSS File'),
494
      'value' => $css_file,
495
      'desc' => t('Define a CSS file attached to all HTML output.'),
496
    );
497
  }
498

    
499
  /**
500
   * Handles the storage of the options.
501
   *
502
   */
503
  function options_submit(&$form, &$form_state) {
504
    // It is very important to call the parent function here:
505
    parent::options_submit($form, $form_state);
506
    switch ($form_state['section']) {
507
      case 'pdf_page':
508
        $this->set_option('default_page_format', $form_state['values']['default_page_format']);
509
        $this->set_option('default_page_format_custom', $form_state['values']['default_page_format_custom']);
510
        $this->set_option('default_page_orientation', $form_state['values']['default_page_orientation']);
511
        $this->set_option('unit', $form_state['values']['unit']);
512
        $this->set_option('margin_left', $form_state['values']['margin_left']);
513
        $this->set_option('margin_right', $form_state['values']['margin_right']);
514
        $this->set_option('margin_top', $form_state['values']['margin_top']);
515
        $this->set_option('margin_bottom', $form_state['values']['margin_bottom']);
516

    
517
        break;
518

    
519
      case 'pdf_fonts':
520
        $this->set_option('default_font_size', $form_state['values']['default_font_size']);
521
        $this->set_option('default_font_style', $form_state['values']['default_font_style']);
522
        $this->set_option('default_font_family', $form_state['values']['default_font_family']);
523
        $this->set_option('default_text_align', $form_state['values']['default_text_align']);
524
        $this->set_option('default_font_color', $form_state['values']['default_font_color']);
525

    
526
        break;
527

    
528
      case 'pdf_template':
529
        // Save new file:
530
        // Note: The jQuery update is required to use Ajax for file upload. With
531
        // default Drupal jQuery it will not work.
532
        // For upload with Ajax a iFrame is open and upload in it, because
533
        // normal forms are not allowed to handle directly.
534
        $destination = variable_get('views_pdf_template_stream', 'public://views_pdf_templates');
535

    
536
        if (file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
537
          $file = file_save_upload('template_file', array(), $destination);
538
          if (is_object($file)) {
539
            $file_name =  basename($file->destination, '.pdf');
540
            $form_state['values']['template'] = $file_name;
541
            $file->status |= FILE_STATUS_PERMANENT;
542
            $file = file_save($file);
543
          }
544
        }
545

    
546
        $this->set_option('leading_template', $form_state['values']['leading_template']);
547
        $this->set_option('template', $form_state['values']['template']);
548
        $this->set_option('succeed_template', $form_state['values']['succeed_template']);
549
        break;
550

    
551
      case 'displays':
552
        $this->set_option($form_state['section'], $form_state['values'][$form_state['section']]);
553
        break;
554

    
555
      case 'css':
556
        $this->set_option('css_file', $form_state['values']['css_file']);
557
        break;
558
    }
559
  }
560

    
561
  /**
562
   * Attach to another view.
563
   */
564
  function attach_to($display_id) {
565
    $displays = $this->get_option('displays');
566
    if (empty($displays[$display_id])) {
567
      return;
568
    }
569

    
570
    // Defer to the feed style; it may put in meta information, and/or
571
    // attach a feed icon.
572
    $plugin = $this->get_plugin();
573
    if ($plugin) {
574
      $clone = $this->view->clone_view();
575
      $clone->set_display($this->display->id);
576
      $clone->build_title();
577
      $plugin->attach_to($display_id, $this->get_path(), $clone->get_title());
578
    }
579
  }
580

    
581

    
582
}