Projet

Général

Profil

Paste
Télécharger (17,3 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Table PDF style plugin
6
 */
7

    
8

    
9
/**
10
 * Style plugin to render each item as a row in a table.
11
 *
12
 * @ingroup views_style_plugins
13
 */
14
class views_pdf_plugin_style_table extends views_plugin_style {
15

    
16
  /**
17
   * Render the style
18
   */
19
  function render() {
20
    $output = '';
21

    
22
    $this->view->numberOfRecords = count($this->view->result);
23
    $this->view->pdf->drawTable($this->view, $this->options);
24

    
25
    return $output;
26
  }
27

    
28

    
29
  /**
30
   * Option definitions
31
   */
32
  function option_definition() {
33
    $options = parent::option_definition();
34

    
35
    $options['info'] = array('default' => array());
36
    $options['position'] = array('default' => array());
37

    
38
    return $options;
39
  }
40

    
41
  /**
42
   * Options form
43
   */
44
  function options_form(&$form, &$form_state) {
45
    parent::options_form($form, $form_state);
46
    $handlers = $this->display->handler->get_handlers('field');
47
    if (empty($handlers)) {
48
      $form['error_markup'] = array(
49
        '#markup' => '<div class="error messages">' . t('You need at least one field before you can configure your table settings') . '</div>',
50
      );
51
      return;
52
    }
53
    $attached = array(
54
      'css' => array(drupal_get_path('module', 'views_pdf') . '/theme/admin.css'),
55
    );
56

    
57
    $form['#theme'] = 'views_pdf_plugin_style_table';
58
    $form['#attached'] = $attached;
59

    
60
    $columns = $this->display->handler->get_field_labels();
61
    $fields = $this->display->handler->get_option('fields');
62

    
63
    $fonts = array_merge(array('default' => t('-- Default --')), views_pdf_get_font_list());
64

    
65
    $font_styles = array(
66
      'b' => t('Bold'),
67
      'i' => t('Italic'),
68
      'u' => t('Underline'),
69
      'd' => t('Line through'),
70
      'o' => t('Overline')
71
    );
72
    $align = array(
73
      'L' => t('Left'),
74
      'C' => t('Center'),
75
      'R' => t('Right'),
76
      'J' => t('Justify'),
77
    );
78

    
79
    $hyphenate = array(
80
      'none' => t('None'),
81
      'auto' => t('Detect automatically'),
82
    );
83
    $hyphenate = array_merge($hyphenate, views_pdf_get_hyphenations());
84

    
85
    foreach ($columns as $field => $column) {
86

    
87
      if ($fields[$field]['exclude'] == 1) {
88
        continue;
89
      }
90

    
91
      $safe = str_replace(array('][', '_', ' '), '-', $field);
92

    
93
      // the $id of the column for dependency checking.
94
      $id = 'edit-style-options-columns-' . $safe;
95

    
96
      // markup for the field name
97
      $form['info'][$field]['name'] = array(
98
        '#markup' => $column,
99
      );
100

    
101
      $form['info'][$field]['header_style']['text'] = array(
102
        '#type' => 'fieldset',
103
        '#title' => t('Text Settings'),
104
        '#collapsed' => FALSE,
105
        '#collapsible' => TRUE,
106
      );
107

    
108
      $form['info'][$field]['header_style']['text']['font_size'] = array(
109
        '#type' => 'textfield',
110
        '#size' => 10,
111
        '#title' => t('Font Size'),
112
        '#default_value' => isset($this->options['info'][$field]['header_style']['text']['font_size']) ? $this->options['info'][$field]['header_style']['text']['font_size'] : '',
113
      );
114

    
115
      $form['info'][$field]['header_style']['text']['font_family'] = array(
116
        '#type' => 'select',
117
        '#title' => t('Font Family'),
118
        '#required' => TRUE,
119
        '#options' => $fonts,
120
        '#size' => 5,
121
        '#default_value' => !empty($this->options['info'][$field]['header_style']['text']['font_family']) ? $this->options['info'][$field]['header_style']['text']['font_family'] : 'default',
122
      );
123

    
124
      $form['info'][$field]['header_style']['text']['font_style'] = array(
125
        '#type' => 'checkboxes',
126
        '#title' => t('Font Style'),
127
        '#options' => $font_styles,
128
        '#size' => 10,
129
        '#default_value' => $this->options['info'][$field]['header_style']['text']['font_style'] == NULL ? $this->display->handler->get_option('default_font_style') : $this->options['info'][$field]['header_style']['text']['font_style'],
130
      );
131
      $form['info'][$field]['header_style']['text']['align'] = array(
132
        '#type' => 'radios',
133
        '#title' => t('Alignment'),
134
        '#options' => $align,
135
        '#default_value' => $this->options['info'][$field]['header_style']['text']['align'] == NULL ? $this->display->handler->get_option('default_text_align') : $this->options['info'][$field]['header_style']['text']['align'],
136
      );
137

    
138
      $form['info'][$field]['header_style']['text']['hyphenate'] = array(
139
        '#type' => 'select',
140
        '#title' => t('Text Hyphenation'),
141
        '#options' => $hyphenate,
142
        '#description' => t('upload patterns from <a href="@url">ctan.org</a> to <br />sites/libraries/tcpdf/hyphenate_patterns', array('@url' => 'http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/tex')),
143
        '#default_value' => $this->options['info'][$field]['header_style']['text']['hyphenate'] == NULL ? $this->display->handler->get_option('default_text_hyphenate') : $this->options['info'][$field]['header_style']['text']['hyphenate'],
144
      );
145

    
146
      $form['info'][$field]['header_style']['text']['color'] = array(
147
        '#type' => 'textfield',
148
        '#title' => t('Text Color'),
149
        '#description' => t('Enter Any format: <br />000000 (HexRGB) - 000,000,000 (RGB) - 000,000,000,000 (CMYK)'),
150
        '#size' => 10,
151
        '#default_value' => $this->options['info'][$field]['header_style']['text']['color'] == NULL ? $this->display->handler->get_option('default_text_color') : $this->options['info'][$field]['header_style']['text']['color'],
152
      );
153

    
154
      $form['info'][$field]['header_style']['render'] = array(
155
        '#type' => 'fieldset',
156
        '#title' => t('Render Settings'),
157
        '#collapsed' => FALSE,
158
        '#collapsible' => TRUE,
159
      );
160

    
161
      $form['info'][$field]['header_style']['render']['is_html'] = array(
162
        '#type' => 'checkbox',
163
        '#title' => t('Render As HTML'),
164
        '#default_value' => isset($this->options['info'][$field]['header_style']['render']['is_html']) ? $this->options['info'][$field]['header_style']['render']['is_html'] : 1,
165
      );
166

    
167
      if (user_access('allow php in views pdf') && VIEWS_PDF_PHP) {
168
        $form['info'][$field]['header_style']['render']['eval_before'] = array(
169
          '#type' => 'textarea',
170
          '#title' => t('PHP Code Before Output'),
171
          '#default_value' => isset($this->options['info'][$field]['header_style']['render']['eval_before']) ? $this->options['info'][$field]['header_style']['render']['eval_before'] : '',
172
        );
173
        $form['info'][$field]['header_style']['render']['bypass_eval_before'] = array(
174
          '#type' => 'checkbox',
175
          '#title' => t('Use the PHP eval function instead php_eval.'),
176
          '#description' => t("WARNING: If you don't know the risk of using eval leave as it."),
177
          '#default_value' => !empty($this->options['info'][$field]['header_style']['render']['bypass_eval_before']) ? $this->options['info'][$field]['header_style']['render']['bypass_eval_before'] : FALSE,
178
        );
179

    
180
        $form['info'][$field]['header_style']['render']['eval_after'] = array(
181
          '#type' => 'textarea',
182
          '#title' => t('PHP Code After Output'),
183
          '#default_value' => isset($this->options['info'][$field]['header_style']['render']['eval_after']) ? $this->options['info'][$field]['header_style']['render']['eval_after'] : '',
184
        );
185
        $form['info'][$field]['header_style']['render']['bypass_eval_after'] = array(
186
          '#type' => 'checkbox',
187
          '#title' => t('Use the PHP eval function instead php_eval.'),
188
          '#description' => t("WARNING: If you don't know the risk of using eval leave as it."),
189
          '#default_value' => !empty($this->options['info'][$field]['header_style']['render']['bypass_eval_after']) ? $this->options['info'][$field]['header_style']['render']['bypass_eval_after'] : FALSE,
190
        );
191
      }
192

    
193
      $form['info'][$field]['body_style']['text'] = array(
194
        '#type' => 'fieldset',
195
        '#title' => t('Text Settings'),
196
        '#collapsed' => FALSE,
197
        '#collapsible' => TRUE,
198
      );
199
      $form['info'][$field]['body_style']['text']['font_size'] = array(
200
        '#type' => 'textfield',
201
        '#size' => 10,
202
        '#title' => t('Font Size'),
203
        '#default_value' => isset($this->options['info'][$field]['body_style']['text']['font_size']) ? $this->options['info'][$field]['body_style']['text']['font_size'] : '',
204
      );
205

    
206
      $form['info'][$field]['body_style']['text']['font_family'] = array(
207
        '#type' => 'select',
208
        '#title' => t('Font Family'),
209
        '#required' => TRUE,
210
        '#options' => $fonts,
211
        '#size' => 5,
212
        '#default_value' => !empty($this->options['info'][$field]['body_style']['text']['font_family']) ? $this->options['info'][$field]['body_style']['text']['font_family'] : 'default',
213
      );
214

    
215
      $form['info'][$field]['body_style']['text']['font_style'] = array(
216
        '#type' => 'checkboxes',
217
        '#title' => t('Font Style'),
218
        '#options' => $font_styles,
219
        '#size' => 10,
220
        '#default_value' => $this->options['info'][$field]['body_style']['text']['font_style'] == NULL ? $this->display->handler->get_option('default_font_style') : $this->options['info'][$field]['body_style']['text']['font_style'],
221
      );
222
      $form['info'][$field]['body_style']['text']['align'] = array(
223
        '#type' => 'radios',
224
        '#title' => t('Alignment'),
225
        '#options' => $align,
226
        '#default_value' => $this->options['info'][$field]['body_style']['text']['align'] == NULL ? $this->display->handler->get_option('default_text_align') : $this->options['info'][$field]['body_style']['text']['align'],
227
      );
228

    
229
      $form['info'][$field]['body_style']['text']['hyphenate'] = array(
230
        '#type' => 'select',
231
        '#title' => t('Text Hyphenation'),
232
        '#options' => $hyphenate,
233
        '#description' => t('upload patterns from <a href="@url">ctan.org</a> to <br />sites/libraries/tcpdf/hyphenate_patterns', array('@url' => 'http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/tex')),
234
        '#default_value' => $this->options['info'][$field]['body_style']['text']['hyphenate'] == NULL ? $this->display->handler->get_option('default_text_hyphenate') : $this->options['info'][$field]['body_style']['text']['hyphenate'],
235
      );
236

    
237
      $form['info'][$field]['body_style']['text']['color'] = array(
238
        '#type' => 'textfield',
239
        '#title' => t('Text Color'),
240
        '#description' => t('Enter Any format: <br />000000 (HexRGB) - 000,000,000 (RGB) - 000,000,000,000 (CMYK)'),
241
        '#size' => 20,
242
        '#default_value' => $this->options['info'][$field]['body_style']['text']['color'] == NULL ? $this->display->handler->get_option('default_text_color') : $this->options['info'][$field]['body_style']['text']['color'],
243
      );
244

    
245
      $form['info'][$field]['body_style']['render'] = array(
246
        '#type' => 'fieldset',
247
        '#title' => t('Render Settings'),
248
        '#collapsed' => FALSE,
249
        '#collapsible' => TRUE,
250
      );
251

    
252
      $form['info'][$field]['body_style']['render']['is_html'] = array(
253
        '#type' => 'checkbox',
254
        '#title' => t('Render As HTML'),
255
        '#default_value' => isset($this->options['info'][$field]['body_style']['render']['is_html']) ? $this->options['info'][$field]['body_style']['render']['is_html'] : 1,
256
      );
257

    
258
      if (user_access('allow php in views pdf') && VIEWS_PDF_PHP) {
259
        $form['info'][$field]['body_style']['render']['eval_before'] = array(
260
          '#type' => 'textarea',
261
          '#title' => t('PHP Code Before Output'),
262
          '#default_value' => isset($this->options['info'][$field]['body_style']['render']['eval_before']) ? $this->options['info'][$field]['body_style']['render']['eval_before'] : '',
263
        );
264
        $form['info'][$field]['body_style']['render']['bypass_eval_before'] = array(
265
          '#type' => 'checkbox',
266
          '#title' => t('Use the PHP eval function instead php_eval.'),
267
          '#description' => t("WARNING: If you don't know the risk of using eval leave as it."),
268
          '#default_value' => !empty($this->options['info'][$field]['body_style']['render']['bypass_eval_before']) ? $this->options['info'][$field]['body_style']['render']['bypass_eval_before'] : FALSE,
269
        );
270

    
271
        $form['info'][$field]['body_style']['render']['eval_after'] = array(
272
          '#type' => 'textarea',
273
          '#title' => t('PHP Code After Output'),
274
          '#default_value' => isset($this->options['info'][$field]['body_style']['render']['eval_after']) ? $this->options['info'][$field]['body_style']['render']['eval_after'] : '',
275
        );
276
        $form['info'][$field]['body_style']['render']['bypass_eval_after'] = array(
277
          '#type' => 'checkbox',
278
          '#title' => t('Use the PHP eval function instead php_eval.'),
279
          '#description' => t("WARNING: If you don't know the risk of using eval leave as it."),
280
          '#default_value' => !empty($this->options['info'][$field]['body_style']['render']['bypass_eval_after']) ? $this->options['info'][$field]['body_style']['render']['bypass_eval_after'] : FALSE,
281
        );
282
      }
283

    
284
      $form['info'][$field]['position']['width'] = array(
285
        '#type' => 'textfield',
286
        '#size' => 10,
287
        '#title' => t('Width'),
288
        '#default_value' => isset($this->options['info'][$field]['position']['width']) ? $this->options['info'][$field]['position']['width'] : '',
289
      );
290
    }
291

    
292
    // Some general options
293
    $form['position'] = array(
294
      '#type' => 'fieldset',
295
      '#title' => t('Table Position'),
296
      '#collapsed' => FALSE,
297
      '#collapsible' => TRUE,
298
    );
299

    
300
    $form['position']['last_writing_position'] = array(
301
      '#type' => 'checkbox',
302
      '#title' => t('Relative to last writing position'),
303
      '#default_value' => isset($this->options['position']['last_writing_position']) ? $this->options['position']['last_writing_position'] : 1,
304
    );
305

    
306
    $form['position']['x'] = array(
307
      '#type' => 'textfield',
308
      '#size' => 10,
309
      '#title' => t('X'),
310
      '#description' => t('X Coordinate (relative to page with margins)'),
311
      '#default_value' => isset($this->options['position']['x']) ? $this->options['position']['x'] : '',
312
    );
313
    $form['position']['y'] = array(
314
      '#type' => 'textfield',
315
      '#size' => 10,
316
      '#title' => t('Y'),
317
      '#description' => t('Y Coordinate (relative to page with margins)'),
318
      '#default_value' => isset($this->options['position']['y']) ? $this->options['position']['y'] : '',
319
    );
320
    $form['position']['width'] = array(
321
      '#type' => 'textfield',
322
      '#size' => 10,
323
      '#title' => t('Table Width'),
324
      '#default_value' => isset($this->options['position']['width']) ? $this->options['position']['width'] : '',
325
    );
326

    
327
    $form['position']['row_height'] = array(
328
      '#type' => 'textfield',
329
      '#size' => 10,
330
      '#title' => t('Row Height'),
331
      '#default_value' => isset($this->options['position']['row_height']) ? $this->options['position']['row_height'] : '',
332
    );
333

    
334
    parent::options_form($form, $form_state);
335

    
336
  }
337

    
338
  /**
339
   * Options form storage
340
   */
341
  function options_submit(&$form, &$form_state) {
342
    $default_font_style = $this->display->handler->get_option('default_font_style');
343
    foreach ($form_state['values']['style_options']['info'] as $id => $field) {
344

    
345
      // Reset to default, if the elements are equal to the default settings
346
      if (
347
        count(array_diff($default_font_style, $field['header_style']['text']['font_style'])) == 0 &&
348
        count(array_diff($field['header_style']['text']['font_style'], $default_font_style)) == 0) {
349
        $form_state['values']['style_options']['info'][$id]['header_style']['text']['font_style'] = NULL;
350
      }
351

    
352
      if ($field['header_style']['text']['align'] == $this->display->handler->get_option('default_text_align')) {
353
        $form_state['values']['style_options']['info'][$id]['header_style']['text']['align'] = NULL;
354
      }
355

    
356
      if ($field['header_style']['text']['hyphenate'] == $this->display->handler->get_option('default_text_hyphenate')) {
357
        $form_state['values']['style_options']['info'][$id]['header_style']['text']['hyphenate'] = NULL;
358
      }
359

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

    
367
      if ($field['body_style']['text']['align'] == $this->display->handler->get_option('default_text_align')) {
368
        $form_state['values']['style_options']['info'][$id]['body_style']['text']['align'] = NULL;
369
      }
370

    
371
      if ($field['body_style']['text']['hyphenate'] == $this->display->handler->get_option('default_text_hyphenate')) {
372
        $form_state['values']['style_options']['info'][$id]['body_style']['text']['hyphenate'] = NULL;
373
      }
374

    
375
    }
376

    
377
  }
378

    
379
  /**
380
   * Attach this view to another display as a feed.
381
   *
382
   * Provide basic functionality for all export style views like attaching a
383
   * feed image link.
384
   */
385
  function attach_to($display_id, $path, $title) {
386
    $display = $this->view->display[$display_id]->handler;
387
    $url_options = array();
388
    $input = $this->view->get_exposed_input();
389
    if ($input) {
390
      $url_options['query'] = $input;
391
    }
392

    
393
    if (empty($this->view->feed_icon)) {
394
      $this->view->feed_icon = '';
395
    }
396
    $this->view->feed_icon .= theme(
397
      'views_pdf_icon',
398
      array(
399
        'path' => $this->view->get_url(NULL, $path),
400
        'title' => $title,
401
        'options' => $url_options
402
      )
403
    );
404
  }
405

    
406
}