Projet

Général

Profil

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

root / htmltest / sites / all / modules / views_pdf / views_pdf_plugin_display.inc @ 011029ce

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
    }
124

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

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

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

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

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

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

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

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

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

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

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

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

    
201
    return $options;
202
  }
203

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

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

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

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

    
291
        $form['#title'] .= t('PDF Default Font Options');
292
        $form['description'] = array(
293
          '#prefix' => '<div class="description form-item">',
294
          '#suffix' => '</div>',
295
          '#value' => t('Here you specify a the default font settings for the document.'),
296
        );
297
        $form['default_font_size'] = array(
298
          '#type' => 'textfield',
299
          '#title' => t('Font Size'),
300
          '#size' => 10,
301
          '#default_value' => $this->get_option('default_font_size'),
302
        );
303
        $form['default_font_family'] = array(
304
          '#type' => 'select',
305
          '#title' => t('Font Family'),
306
          '#options' => $fonts,
307
          '#size' => 5,
308
          '#default_value' => $this->get_option('default_font_family'),
309
        );
310
        $form['default_font_style'] = array(
311
          '#type' => 'checkboxes',
312
          '#title' => t('Font Style'),
313
          '#options' => $font_styles,
314
          '#default_value' => $this->get_option('default_font_style'),
315
        );
316
        $form['default_text_align'] = array(
317
          '#type' => 'radios',
318
          '#title' => t('Text Alignment'),
319
          '#options' => $align,
320
          '#default_value' => $this->get_option('default_text_align'),
321
        );
322
        $form['default_text_hyphenate'] = array(
323
          '#type' => 'select',
324
          '#title' => t('Text Hyphenation'),
325
          '#options' => $hyphenate,
326
          '#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')),
327
          '#default_value' => $this->get_option('default_text_hyphenate'),
328
        );
329

    
330
        $form['default_font_color'] = array(
331
          '#type' => 'textfield',
332
          '#title' => t('Text Color'),
333
          '#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.'),
334
          '#size' => 20,
335
          '#default_value' => $this->get_option('default_font_color'),
336
        );
337

    
338
        break;
339

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

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

    
353
        $form['template'] = array(
354
          '#type' => 'select',
355
          '#options' => $templates,
356
          '#title' => t('Template PDF'),
357
          '#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.'),
358
          '#default_value' => $this->get_option('template'),
359
        );
360

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

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

    
375
        break;
376

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

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

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

    
410
    $fonts = views_pdf_get_font_list();
411

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

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

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

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

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

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

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

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

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

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

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

    
507
        break;
508

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

    
516
        break;
517

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

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

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

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

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

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

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

    
571

    
572
}