Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views_pdf / views_pdf_plugin_row_fields.inc @ 87dbc3bf

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']['minimal_space'] = array(
262
        '#type' => 'textfield',
263
        '#title' => t('Minimal Space'),
264
        '#description' => t('Specify here the minimal space, which is needed on the page, that the content is placed on the page.'),
265
        '#default_value' => isset($this->options['formats'][$field]['render']['minimal_space']) ? $this->options['formats'][$field]['render']['minimal_space'] : 1,
266
      );
267

    
268
      $form['formats'][$field]['render']['eval_before'] = array(
269
        '#type' => 'textarea',
270
        '#title' => t('PHP Code Before Output'),
271
        '#default_value' => isset($this->options['formats'][$field]['render']['eval_before']) ? $this->options['formats'][$field]['render']['eval_before'] : '',
272
      );
273
      $form['formats'][$field]['render']['bypass_eval_before'] = array(
274
        '#type' => 'checkbox',
275
        '#title' => t('Use the PHP eval function instead php_eval.'),
276
        '#description' => t("WARNING: If you don't know the risk of using eval leave as it."),
277
        '#default_value' => !empty($this->options['formats'][$field]['render']['bypass_eval_before']) ? $this->options['formats'][$field]['render']['bypass_eval_before'] : FALSE,
278
      );
279

    
280
      $form['formats'][$field]['render']['eval_after'] = array(
281
        '#type' => 'textarea',
282
        '#title' => t('PHP Code After Output'),
283
        '#default_value' => isset($this->options['formats'][$field]['render']['eval_after']) ? $this->options['formats'][$field]['render']['eval_after'] : '',
284
      );
285
      $form['formats'][$field]['render']['bypass_eval_after'] = array(
286
        '#type' => 'checkbox',
287
        '#title' => t('Use the PHP eval function instead php_eval.'),
288
        '#description' => t("WARNING: If you don't know the risk of using eval leave as it."),
289
        '#default_value' => !empty($this->options['formats'][$field]['render']['bypass_eval_after']) ? $this->options['formats'][$field]['render']['bypass_eval_after'] : FALSE,
290
      );
291

    
292
    }
293

    
294
    $form['leading_template'] = array(
295
      '#type' => 'select',
296
      '#options' => $templates,
297
      '#title' => t('Leading PDF Template'),
298
      '#required' => FALSE,
299
      '#description' => t('Here you specify a PDF file to be printed in front of every row.'),
300
      '#default_value' => $this->options['leading_template'],
301
    );
302

    
303

    
304
    $form['template'] = array(
305
      '#type' => 'select',
306
      '#options' => $row_templates,
307
      '#title' => t('Template PDF'),
308
      '#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.'),
309
      '#default_value' => $this->options['template'],
310
    );
311

    
312

    
313
    $form['succeed_template'] = array(
314
      '#type' => 'select',
315
      '#options' => $templates,
316
      '#title' => t('Succeed PDF Template'),
317
      '#required' => FALSE,
318
      '#description' => t('Here you specify a PDF file to be printed after the main content.'),
319
      '#default_value' => $this->options['succeed_template'],
320
    );
321

    
322

    
323
    $form['template_file'] = array(
324
      '#type' => 'file',
325
      '#title' => t('Upload New Template File'),
326
    );
327

    
328

    
329
  }
330

    
331
  /**
332
   * Stores the options
333
   */
334
  function options_submit(&$form, &$form_state) {
335
    $default = $this->display->handler->get_option('default_font_style');
336
    foreach ($form_state['values']['row_options']['formats'] as $id => $field) {
337

    
338
      // Reset to default, if the elements are equal to the default settings
339
      if (count(array_diff($default, $field['text']['font_style'])) == 0 && count(array_diff($field['text']['font_style'], $default)) == 0) {
340
        $form_state['values']['row_options']['formats'][$id]['text']['font_style'] = NULL;
341
      }
342

    
343
      if ($field['text']['align'] == $this->display->handler->get_option('default_text_align')) {
344
        $form_state['values']['row_options']['formats'][$id]['text']['align'] = NULL;
345
      }
346

    
347
      if ($field['text']['hyphenate'] == $this->display->handler->get_option('default_text_hyphenate')) {
348
        $form_state['values']['row_options']['formats'][$id]['text']['hyphenate'] = NULL;
349
      }
350
    }
351

    
352
    // Save new file:
353
    // Note: The jQuery update is required to use Ajax for file upload. With
354
    // default Drupal jQuery it will not work.
355
    // For upload with Ajax a iFrame is open and upload in it, because
356
    // normal forms are not allowed to handle directly.
357
    $destination = variable_get('views_pdf_template_stream', 'public://views_pdf_templates');
358

    
359
    if (file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
360
      // The file field is not called "template_file" as expected, it calls
361
      // "row_options". The reason for that is not clear.
362
      $file = file_save_upload('row_options', array(), $destination);
363
      if (is_object($file)) {
364
        $file_name =  basename($file->destination, '.pdf');
365
        $form_state['values']['row_options']['template'] = $file_name;
366
        $file->status |= FILE_STATUS_PERMANENT;
367
        $file = file_save($file);
368
      }
369
    }
370

    
371
    if ($form_state['values']['row_options']['leading_template'] == t('-- None --')) {
372
      $form_state['values']['row_options']['leading_template'] = '';
373
    }
374

    
375
    if ($form_state['values']['row_options']['template'] == t('-- None --')) {
376
      $form_state['values']['row_options']['template'] = '';
377
    }
378

    
379
    if ($form_state['values']['row_options']['succeed_template'] == t('-- None --')) {
380
      $form_state['values']['row_options']['succeed_template'] = '';
381
    }
382

    
383
  }
384
}