Projet

Général

Profil

Paste
Télécharger (16,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_pdf / views_pdf_plugin_row_fields.inc @ b42754b9

1
<?php
2

    
3
/**
4
 * @file
5
 * PDF row fields plugin provides the the fields plugin for unformatted style.
6
 */
7

    
8

    
9
/**
10
 * This class contains all the functionality of the field PDF style.
11
 */
12
class views_pdf_plugin_row_fields extends views_plugin_row {
13
  /**
14
   * Renders the rows.
15
   */
16
  function render($row) {
17
    $options = $this->option_definition();
18

    
19
    // Header of a record.
20
    $path = $this->view->pdf->getTemplatePath($this->options['leading_template']);
21
    $this->view->pdf->addPdfDocument($path);
22

    
23
    // Set row page template.
24
    $path = $this->view->pdf->getTemplatePath($this->options['template'], $row, $this->view);
25
    $this->view->pdf->setDefaultPageTemplate($path, 'row', 'row');
26

    
27
    // Due of limitations of field renderer, we invoke them
28
    // here and not in the field render function.
29
    foreach ($this->view->field as $id => $field) {
30
      if (isset($this->options['formats'][$id])) {
31
        $options = $this->options['formats'][$id];
32
      }
33
      else {
34
        $options = array();
35
      }
36

    
37
      $this->view->pdf->drawContent($row, $options, $this->view, $id);
38

    
39
      // Set or update header / footer options per row
40
      // this ensures that we write the last record for each page
41
      // in the cache.
42
      $this->view->pdf->setHeaderFooter($row, $this->options, $this->view);
43
    }
44

    
45
    // Footer of a record.
46
    $path = $this->view->pdf->getTemplatePath($this->options['succeed_template']);
47
    $this->view->pdf->addPdfDocument($path);
48

    
49
    // Reset the row page number.
50
    $this->view->pdf->resetRowPageNumber();
51

    
52
  }
53

    
54
  /**
55
   * Option definitions
56
   */
57
  function option_definition() {
58
    $options = parent::option_definition();
59

    
60
    $options['formats'] = array('default' => array());
61
    $options['leading_template'] = array('default' => '');
62
    $options['template'] = array('default' => '');
63
    $options['succeed_template'] = array('default' => '');
64

    
65
    return $options;
66
  }
67

    
68
  /**
69
   * Provide a form for setting options.
70
   */
71
  function options_form(&$form, &$form_state) {
72

    
73
    $options = $this->display->handler->get_field_labels();
74
    $fields = $this->display->handler->get_option('fields');
75

    
76
    $fonts = array_merge(array('default' => t('-- Default --')), views_pdf_get_font_list());
77
    $font_styles = array(
78
      'b' => t('Bold'),
79
      'i' => t('Italic'),
80
      'u' => t('Underline'),
81
      'd' => t('Line through'),
82
      'o' => t('Overline')
83
    );
84
    $templates = array_merge(array(t('-- None --')), views_pdf_get_pdf_templates());
85

    
86
    $file_fields = array();
87

    
88
    foreach ($this->display->handler->get_handlers('field') as $id => $handler) {
89
      $info = field_read_field($id);
90

    
91
      if (isset($info['type']) && $info['type'] == 'file') {
92
        $file_fields[$id] = $info['field_name'];
93
      }
94
    }
95

    
96
    $row_templates = array_merge($templates, $file_fields);
97

    
98
    $relativeElements = array(
99
      'page' => t('Page'),
100
      'header_footer' => t('In header / footer'),
101
      'last_position' => t('Last Writing Position'),
102
      'self' => t('Field: Self'),
103
    );
104

    
105
    $align = array(
106
      'L' => t('Left'),
107
      'C' => t('Center'),
108
      'R' => t('Right'),
109
      'J' => t('Justify'),
110
    );
111

    
112
    $hyphenate = array(
113
      'none' => t('None'),
114
      'auto' => t('Detect automatically'),
115
    );
116
    $hyphenate = array_merge($hyphenate, views_pdf_get_hyphenations());
117

    
118

    
119
    if (empty($this->options['inline'])) {
120
      $this->options['inline'] = array();
121
    }
122
    $form['formats'] = array(
123
      '#prefix' => '<div class="description form-item">',
124
      '#suffix' => '</div>',
125
      '#value' => t('Here you can define some style settings for each field.'),
126
    );
127

    
128
    foreach ($options as $field => $option) {
129

    
130
      if (isset($fields[$field]['exclude']) && $fields[$field]['exclude'] == 1) {
131
        continue;
132
      }
133

    
134

    
135
      $form['formats'][$field] = array(
136
        '#type' => 'fieldset',
137
        '#title' => check_plain($option),
138
        '#collapsed' => TRUE,
139
        '#collapsible' => TRUE,
140
      );
141

    
142
      $form['formats'][$field]['position'] = array(
143
        '#type' => 'fieldset',
144
        '#title' => t('Position Settings'),
145
        '#collapsed' => FALSE,
146
        '#collapsible' => TRUE,
147
      );
148

    
149
      $form['formats'][$field]['position']['object'] = array(
150
        '#type' => 'select',
151
        '#title' => t('Position relative to'),
152
        '#required' => FALSE,
153
        '#options' => $relativeElements,
154
        '#default_value' => !empty($this->options['formats'][$field]['position']['object']) ? $this->options['formats'][$field]['position']['object'] : 'last_position',
155
      );
156

    
157
      $form['formats'][$field]['position']['corner'] = array(
158
        '#type' => 'radios',
159
        '#title' => t('Position relative to corner'),
160
        '#required' => FALSE,
161
        '#options' => array(
162
          'top_left' => t('Top Left'),
163
          'top_right' => t('Top Right'),
164
          'bottom_left' => t('Bottom Left'),
165
          'bottom_right' => t('Bottom Right'),
166
        ),
167
        '#default_value' => !empty($this->options['formats'][$field]['position']['corner']) ? $this->options['formats'][$field]['position']['corner'] : 'top_left',
168
      );
169

    
170
      $relativeElements['field_' . $field] = t('Field: !field', array('!field' => $option));
171

    
172

    
173
      $form['formats'][$field]['position']['x'] = array(
174
        '#type' => 'textfield',
175
        '#title' => t('Position X'),
176
        '#required' => FALSE,
177
        '#default_value' => !empty($this->options['formats'][$field]['position']['x']) ? $this->options['formats'][$field]['position']['x'] : '',
178
      );
179

    
180
      $form['formats'][$field]['position']['y'] = array(
181
        '#type' => 'textfield',
182
        '#title' => t('Position Y'),
183
        '#required' => FALSE,
184
        '#default_value' => !empty($this->options['formats'][$field]['position']['y']) ? $this->options['formats'][$field]['position']['y'] : '',
185
      );
186

    
187
      $form['formats'][$field]['position']['width'] = array(
188
        '#type' => 'textfield',
189
        '#title' => t('Width'),
190
        '#required' => FALSE,
191
        '#default_value' => !empty($this->options['formats'][$field]['position']['width']) ? $this->options['formats'][$field]['position']['width'] : '',
192
      );
193

    
194
      $form['formats'][$field]['position']['height'] = array(
195
        '#type' => 'textfield',
196
        '#title' => t('Height'),
197
        '#required' => FALSE,
198
        '#default_value' => !empty($this->options['formats'][$field]['position']['height']) ? $this->options['formats'][$field]['position']['height'] : '',
199
      );
200

    
201
      $form['formats'][$field]['text'] = array(
202
        '#type' => 'fieldset',
203
        '#title' => t('Text Settings'),
204
        '#collapsed' => FALSE,
205
        '#collapsible' => TRUE,
206
      );
207

    
208
      $form['formats'][$field]['text']['font_size'] = array(
209
        '#type' => 'textfield',
210
        '#title' => t('Font Size'),
211
        '#size' => 10,
212
        '#default_value' => isset($this->options['formats'][$field]['text']['font_size']) ? $this->options['formats'][$field]['text']['font_size'] : '',
213
      );
214
      $form['formats'][$field]['text']['font_family'] = array(
215
        '#type' => 'select',
216
        '#title' => t('Font Family'),
217
        '#required' => TRUE,
218
        '#options' => $fonts,
219
        '#size' => 5,
220
        '#default_value' => isset($this->options['formats'][$field]['text']['font_family']) ? $this->options['formats'][$field]['text']['font_family'] : 'default',
221
      );
222
      $form['formats'][$field]['text']['font_style'] = array(
223
        '#type' => 'checkboxes',
224
        '#title' => t('Font Style'),
225
        '#options' => $font_styles,
226
        '#size' => 10,
227
        '#default_value' => !isset($this->options['formats'][$field]['text']['font_style']) ? $this->display->handler->get_option('default_font_style') : $this->options['formats'][$field]['text']['font_style'],
228
      );
229
      $form['formats'][$field]['text']['align'] = array(
230
        '#type' => 'radios',
231
        '#title' => t('Alignment'),
232
        '#options' => $align,
233
        '#default_value' => !isset($this->options['formats'][$field]['text']['align']) ? $this->display->handler->get_option('default_text_align') : $this->options['formats'][$field]['text']['align'],
234
      );
235
      $form['formats'][$field]['text']['hyphenate'] = array(
236
        '#type' => 'select',
237
        '#title' => t('Text Hyphenation'),
238
        '#options' => $hyphenate,
239
        '#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')),
240
        '#default_value' => !isset($this->options['formats'][$field]['text']['hyphenate']) ? $this->display->handler->get_option('default_text_hyphenate') : $this->options['formats'][$field]['text']['hyphenate'],
241
      );
242
      $form['formats'][$field]['text']['color'] = array(
243
        '#type' => 'textfield',
244
        '#title' => t('Text Color'),
245
        '#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.'),
246
        '#size' => 20,
247
        '#default_value' => !isset($this->options['formats'][$field]['text']['color']) ? $this->display->handler->get_option('default_text_color') : $this->options['formats'][$field]['text']['color'],
248
      );
249
      $form['formats'][$field]['render'] = array(
250
        '#type' => 'fieldset',
251
        '#title' => t('Render Settings'),
252
        '#collapsed' => FALSE,
253
        '#collapsible' => TRUE,
254
      );
255
      $form['formats'][$field]['render']['is_html'] = array(
256
        '#type' => 'checkbox',
257
        '#title' => t('Render As HTML'),
258
        '#default_value' => isset($this->options['formats'][$field]['render']['is_html']) ? $this->options['formats'][$field]['render']['is_html'] : 1,
259
      );
260

    
261
      $form['formats'][$field]['render']['custom_layout'] = array(
262
        '#type' => 'checkbox',
263
        '#title' => t('Enable custom layout hook.'),
264
        '#description' => t("Allow a custom module to alter the layout of this field."),
265
        '#default_value' => !empty($this->options['formats'][$field]['render']['custom_layout']) ? $this->options['formats'][$field]['render']['custom_layout'] : FALSE,
266
      );
267
      $form['formats'][$field]['render']['minimal_space'] = array(
268
        '#type' => 'textfield',
269
        '#title' => t('Minimal Space'),
270
        '#description' => t('Specify here the minimal space, which is needed on the page, that the content is placed on the page.'),
271
        '#default_value' => isset($this->options['formats'][$field]['render']['minimal_space']) ? $this->options['formats'][$field]['render']['minimal_space'] : 1,
272
      );
273
      if (user_access('allow php in views pdf') && VIEWS_PDF_PHP) {
274
        $form['formats'][$field]['render']['eval_before'] = array(
275
          '#type' => 'textarea',
276
          '#title' => t('PHP Code Before Output'),
277
          '#default_value' => isset($this->options['formats'][$field]['render']['eval_before']) ? $this->options['formats'][$field]['render']['eval_before'] : '',
278
        );
279
        $form['formats'][$field]['render']['bypass_eval_before'] = array(
280
          '#type' => 'checkbox',
281
          '#title' => t('Use the PHP eval function instead php_eval.'),
282
          '#description' => t("WARNING: If you don't know the risk of using eval leave as it."),
283
          '#default_value' => !empty($this->options['formats'][$field]['render']['bypass_eval_before']) ? $this->options['formats'][$field]['render']['bypass_eval_before'] : FALSE,
284
        );
285
        $form['formats'][$field]['render']['custom_post'] = array(
286
        '#type' => 'checkbox',
287
        '#title' => t('Enable custom post render hook.'),
288
        '#description' => t("Allow a custom module hook to be executed after the rendering of this field."),
289
        '#default_value' => !empty($this->options['formats'][$field]['render']['custom_post']) ? $this->options['formats'][$field]['render']['custom_post'] : FALSE,
290
      );
291

    
292
        $form['formats'][$field]['render']['eval_after'] = array(
293
          '#type' => 'textarea',
294
          '#title' => t('PHP Code After Output'),
295
          '#default_value' => isset($this->options['formats'][$field]['render']['eval_after']) ? $this->options['formats'][$field]['render']['eval_after'] : '',
296
        );
297
        $form['formats'][$field]['render']['bypass_eval_after'] = array(
298
          '#type' => 'checkbox',
299
          '#title' => t('Use the PHP eval function instead php_eval.'),
300
          '#description' => t("WARNING: If you don't know the risk of using eval leave as it."),
301
          '#default_value' => !empty($this->options['formats'][$field]['render']['bypass_eval_after']) ? $this->options['formats'][$field]['render']['bypass_eval_after'] : FALSE,
302
        );
303
      }
304

    
305
    }
306

    
307
    $form['leading_template'] = array(
308
      '#type' => 'select',
309
      '#options' => $templates,
310
      '#title' => t('Leading PDF Template'),
311
      '#required' => FALSE,
312
      '#description' => t('Here you specify a PDF file to be printed in front of every row.'),
313
      '#default_value' => $this->options['leading_template'],
314
    );
315

    
316

    
317
    $form['template'] = array(
318
      '#type' => 'select',
319
      '#options' => $row_templates,
320
      '#title' => t('Template PDF'),
321
      '#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. This option does not override the same option for the whole document. This template will be applyed addtionaly. The page format is defined by the document template, if it is defined.'),
322
      '#default_value' => $this->options['template'],
323
    );
324

    
325

    
326
    $form['succeed_template'] = array(
327
      '#type' => 'select',
328
      '#options' => $templates,
329
      '#title' => t('Succeed PDF Template'),
330
      '#required' => FALSE,
331
      '#description' => t('Here you specify a PDF file to be printed after the main content.'),
332
      '#default_value' => $this->options['succeed_template'],
333
    );
334

    
335

    
336
    $form['template_file'] = array(
337
      '#type' => 'file',
338
      '#title' => t('Upload New Template File'),
339
    );
340

    
341
    $form['#attached']['js'][] = array(
342
          'type' => 'setting',
343
          'data' => array(
344
            'urlIsAjaxTrusted' => array(
345
              $GLOBALS['base_url'] . '/' . current_path() => TRUE,
346
            ),
347
          ),
348
        );
349

    
350

    
351
  }
352

    
353
  /**
354
   * Stores the options
355
   */
356
  function options_submit(&$form, &$form_state) {
357
    $default = $this->display->handler->get_option('default_font_style');
358
    foreach ($form_state['values']['row_options']['formats'] as $id => $field) {
359

    
360
      // Reset to default, if the elements are equal to the default settings
361
      if (count(array_diff($default, $field['text']['font_style'])) == 0 && count(array_diff($field['text']['font_style'], $default)) == 0) {
362
        $form_state['values']['row_options']['formats'][$id]['text']['font_style'] = NULL;
363
      }
364

    
365
      if ($field['text']['align'] == $this->display->handler->get_option('default_text_align')) {
366
        $form_state['values']['row_options']['formats'][$id]['text']['align'] = NULL;
367
      }
368

    
369
      if ($field['text']['hyphenate'] == $this->display->handler->get_option('default_text_hyphenate')) {
370
        $form_state['values']['row_options']['formats'][$id]['text']['hyphenate'] = NULL;
371
      }
372
    }
373

    
374
    // Save new file:
375
    // Note: The jQuery update is required to use Ajax for file upload. With
376
    // default Drupal jQuery it will not work.
377
    // For upload with Ajax a iFrame is open and upload in it, because
378
    // normal forms are not allowed to handle directly.
379
    $destination = variable_get('views_pdf_template_stream', 'public://views_pdf_templates');
380

    
381
    if (file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
382
      // The file field is not called "template_file" as expected, it calls
383
      // "row_options". The reason for that is not clear.
384
      $file = file_save_upload('row_options', array(), $destination);
385
      if (is_object($file)) {
386
        $file_name =  basename($file->destination, '.pdf');
387
        $form_state['values']['row_options']['template'] = $file_name;
388
        $file->status |= FILE_STATUS_PERMANENT;
389
        $file = file_save($file);
390
      }
391
    }
392

    
393
    if ($form_state['values']['row_options']['leading_template'] == t('-- None --')) {
394
      $form_state['values']['row_options']['leading_template'] = '';
395
    }
396

    
397
    if ($form_state['values']['row_options']['template'] == t('-- None --')) {
398
      $form_state['values']['row_options']['template'] = '';
399
    }
400

    
401
    if ($form_state['values']['row_options']['succeed_template'] == t('-- None --')) {
402
      $form_state['values']['row_options']['succeed_template'] = '';
403
    }
404

    
405
  }
406
}