Projet

Général

Profil

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

root / drupal7 / sites / all / modules / print / print_pdf / print_pdf.admin.inc @ a2baadd1

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the administrative functions of the PDF version module.
6
 *
7
 * This file is included by the PDF version module, and includes the
8
 * settings form.
9
 *
10
 * @ingroup print
11
 */
12

    
13
/**
14
 * Menu callback for the PDF version module settings form.
15
 *
16
 * @ingroup forms
17
 * @see _print_pdf_tools()
18
 */
19
function print_pdf_settings() {
20
  $pdf_tools = _print_pdf_tools();
21
  $current_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
22
  $pdf_tool_default = (in_array($current_pdf_tool, $pdf_tools)) ? $current_pdf_tool : PRINT_PDF_PDF_TOOL_DEFAULT;
23

    
24
  if ($pdf_tools != -1) {
25
    $form['settings'] = array(
26
      '#type' => 'fieldset',
27
      '#title' => t('PDF options'),
28
    );
29

    
30
    $form['settings']['print_pdf_pdf_tool'] = array(
31
      '#type' => 'radios',
32
      '#title' => t('PDF generation tool'),
33
      '#options' => $pdf_tools,
34
      '#default_value' => $pdf_tool_default,
35
      '#description' => t('This option selects the PDF generation tool being used by this module to create the PDF version.'),
36
    );
37

    
38
    $form['settings']['print_pdf_link_pos'] = array(
39
      '#type' => 'checkboxes',
40
      '#title' => t('PDF version link'),
41
      '#default_value' => variable_get('print_pdf_link_pos', drupal_json_decode(PRINT_PDF_LINK_POS_DEFAULT)),
42
      '#options' => array('link' => t('Links area'), 'corner' => t('Content corner'), 'block' => t('Block'), 'help' => t('Help area')),
43
      '#description' => t('Choose the location of the link(s) to the PDF version. The Links area is usually below the node content, whereas the Content corner is placed in the upper-right corner of the node content. Unselect all options to disable the link. Even if the link is disabled, you can still view the PDF version of a node by going to !path/nid where nid is the numeric id of the node.', array('!path' => PRINTPDF_PATH)),
44
    );
45

    
46
    $form['settings']['print_pdf_link_teaser'] = array(
47
      '#type' => 'checkbox',
48
      '#title' => t('Display link to the PDF version in teaser'),
49
      '#default_value' => variable_get('print_pdf_link_teaser', PRINT_PDF_LINK_TEASER_DEFAULT),
50
      '#description' => t('Enabling this will display the link in teaser mode.'),
51
    );
52

    
53
    $form['settings']['adv_link'] = array(
54
      '#type' => 'fieldset',
55
      '#title' => t('Advanced link options'),
56
      '#collapsible' => TRUE,
57
      '#collapsed' => FALSE,
58
    );
59

    
60
    $form['settings']['adv_link']['print_pdf_show_link'] = array(
61
      '#type' => 'radios',
62
      '#title' => t('Link style'),
63
      '#default_value' => variable_get('print_pdf_show_link', PRINT_PDF_SHOW_LINK_DEFAULT),
64
      '#options' => array(1 => t('Text only'), 2 => t('Icon only'), 3 => t('Icon and Text')),
65
      '#description' => t('Select the visual style of the link.'),
66
    );
67

    
68
    $form['settings']['adv_link']['print_pdf_link_use_alias'] = array(
69
      '#type' => 'checkbox',
70
      '#title' => t('Use URL alias instead of node ID'),
71
      '#default_value' => variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT),
72
      '#description' => t('Enabling this will create the link using the URL alias instead of the node ID.'),
73
    );
74

    
75
    $form['settings']['adv_link']['print_pdf_link_class'] = array(
76
      '#type' => 'textfield',
77
      '#title' => t('Link class'),
78
      '#default_value' => variable_get('print_pdf_link_class', PRINT_PDF_LINK_CLASS_DEFAULT),
79
      '#size' => 60,
80
      '#maxlength' => 250,
81
      '#description' => t('This can be used by themers to change the link style or by jQuery modules to open in a new window (e.g. greybox or thickbox). Multiple classes can be specified, separated by spaces.'),
82
    );
83

    
84
    $form['settings']['adv_link']['print_pdf_node_link_visibility'] = array(
85
      '#type' => 'radios',
86
      '#title' => t('Link visibility'),
87
      '#default_value' => variable_get('print_pdf_node_link_visibility', PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT),
88
      '#options' => array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.')),
89
    );
90

    
91
    $form['settings']['adv_link']['print_pdf_node_link_pages'] = array(
92
      '#type' => 'textarea',
93
      '#default_value' => variable_get('print_pdf_node_link_pages', PRINT_PDF_NODE_LINK_PAGES_DEFAULT),
94
      '#rows' => 3,
95
      '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
96
    );
97

    
98
    if (module_exists('php')) {
99
      $access = user_access('use PHP for settings');
100

    
101
      if ($form['settings']['adv_link']['print_pdf_node_link_visibility']['#default_value'] == 2 && !$access) {
102
        $form['settings']['adv_link']['print_pdf_node_link_visibility'] = array('#type' => 'value', '#value' => 2);
103
        $form['settings']['adv_link']['print_pdf_node_link_pages'] = array('#type' => 'value', '#value' => $form['settings']['adv_link']['print_pdf_node_link_pages']['#default_value']);
104
      }
105
      elseif ($access) {
106
        $form['settings']['adv_link']['print_pdf_node_link_visibility']['#options'][] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
107
        $form['settings']['adv_link']['print_pdf_node_link_pages']['#description'] .= ' ' . t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
108
      }
109
    }
110

    
111
    $form['settings']['adv_link']['print_pdf_sys_link_visibility'] = array(
112
      '#type' => 'radios',
113
      '#title' => t('Show link in system (non-content) pages'),
114
      '#description' => 'Any page that is not a Drupal node. Usually pages generated by Drupal or a module such as Views or Panels.',
115
      '#default_value' => variable_get('print_pdf_sys_link_visibility', PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT),
116
      '#options' => array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.')),
117
    );
118

    
119
    $form['settings']['adv_link']['print_pdf_sys_link_pages'] = array(
120
      '#type' => 'textarea',
121
      '#default_value' => variable_get('print_pdf_sys_link_pages', PRINT_PDF_SYS_LINK_PAGES_DEFAULT),
122
      '#rows' => 3,
123
      '#description' => t('Setting this option will add a PDF version page link on pages created by Drupal or the enabled modules.') . '<br />' .
124
                        t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
125
    );
126

    
127
    if (module_exists('php')) {
128
      if ($form['settings']['adv_link']['print_pdf_sys_link_visibility']['#default_value'] == 2 && !$access) {
129
        $form['settings']['adv_link']['print_pdf_sys_link_visibility'] = array('#type' => 'value', '#value' => 2);
130
        $form['settings']['adv_link']['print_pdf_sys_link_pages'] = array('#type' => 'value', '#value' => $form['settings']['adv_link']['print_pdf_sys_link_pages']['#default_value']);
131
      }
132
      elseif ($access) {
133
        $form['settings']['adv_link']['print_pdf_sys_link_visibility']['#options'][] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
134
        $form['settings']['adv_link']['print_pdf_sys_link_pages']['#description'] .= ' ' . t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
135
      }
136
    }
137

    
138
    $form['settings']['adv_link']['print_pdf_book_link'] = array(
139
      '#type' => 'radios',
140
      '#title' => t('Link in book hierarchy nodes'),
141
      '#default_value' => variable_get('print_pdf_book_link', PRINT_PDF_BOOK_LINK_DEFAULT),
142
      '#options' => array(t('No link'), t('Current page and sub-pages'), t('Current page only')),
143
    );
144

    
145
    $form['settings']['print_pdf_content_disposition'] = array(
146
      '#type' => 'radios',
147
      '#title' => t('Open PDF in'),
148
      '#options' => array(t('Same browser window'), t('New browser window'), t('Save dialog')),
149
      '#default_value' => variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT),
150
      '#description' => t("Select the desired method for opening the PDF in the user's browser."),
151
    );
152

    
153
    $form['settings']['print_pdf_paper_size'] = array(
154
      '#type' => 'select',
155
      '#title' => t('Paper size'),
156
      '#options' => array('4A0' => '4A0', '2A0' => '2A0', 'A0' => 'A0',
157
                          'A1' => 'A1', 'A2' => 'A2', 'A3' => 'A3', 'A4' => 'A4',
158
                          'A5' => 'A5', 'A6' => 'A6', 'A7' => 'A7', 'A8' => 'A8',
159
                          'A9' => 'A9', 'A10' => 'A10', 'B0' => 'B0', 'B1' => 'B1',
160
                          'B2' => 'B2', 'B3' => 'B3', 'B4' => 'B4', 'B5' => 'B5',
161
                          'B6' => 'B6', 'B7' => 'B7', 'B8' => 'B8', 'B9' => 'B9',
162
                          'B10' => 'B10', 'C0' => 'C0', 'C1' => 'C1', 'C2' => 'C2',
163
                          'C3' => 'C3', 'C4' => 'C4', 'C5' => 'C5', 'C6' => 'C6',
164
                          'C7' => 'C7', 'C8' => 'C8', 'C9' => 'C9', 'C10' => 'C10',
165
                          'RA0' => 'RA0', 'RA1' => 'RA1', 'RA2' => 'RA2',
166
                          'RA3' => 'RA3', 'RA4' => 'RA4', 'SRA0' => 'SRA0',
167
                          'SRA1' => 'SRA1', 'SRA2' => 'SRA2', 'SRA3' => 'SRA3',
168
                          'SRA4' => 'SRA4', 'LETTER' => 'Letter', 'LEGAL' => 'Legal',
169
                          'EXECUTIVE' => 'Executive', 'FOLIO' => 'Folio',
170
                    ),
171
      '#default_value' => variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT),
172
      '#description' => t('Choose the paper size of the generated PDF.'),
173
    );
174

    
175
    $form['settings']['print_pdf_page_orientation'] = array(
176
      '#type' => 'select',
177
      '#title' => t('Page orientation'),
178
      '#options' => array('portrait' => t('Portrait'), 'landscape' => t('Landscape')),
179
      '#default_value' => variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT),
180
      '#description' => t('Choose the page orientation of the generated PDF.'),
181
    );
182
    $form['settings']['print_pdf_images_via_file'] = array(
183
      '#type' => 'checkbox',
184
      '#title' => t('Access images via local file access'),
185
      '#default_value' => variable_get('print_pdf_images_via_file', PRINT_PDF_IMAGES_VIA_FILE_DEFAULT),
186
      '#description' => t("Enabling this option will make the tool use local file access for image files. This option is not recommended to use in conjunction with modules like imagecache which generate the image after it's first accessed. However, it may be necessary in low-end hosting services where the web server is not allowed to open URLs and the user can't modify that configuration setting."),
187
    );
188
    $form['settings']['print_pdf_autoconfig'] = array(
189
      '#type' => 'checkbox',
190
      '#title' => t('Auto-configure the PDF tool settings'),
191
      '#default_value' => variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT),
192
      '#description' => t('If you disable this option, the pdf tool settings must be configured manually. For TCDPF, edit the tcpdf/config/tcpdf_config.php file. For dompdf, edit the dompdf/dompdf_config.inc.php file.'),
193
    );
194
    $form['settings']['print_pdf_font_family'] = array(
195
      '#type' => 'textfield',
196
      '#title' => t('Font family'),
197
      '#default_value' => variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT),
198
      '#size' => 60,
199
      '#maxlength' => 250,
200
      '#description' => t('(TCPDF only) Set the font family to be used. Examples: %examples.', array('%examples' => 'helvetica, times, courier, dejavusans, dejavuserif, freesans, freeserif, freemono')) . '<br />' .
201
                        t("CAUTION: TCPDF embeds the complete font in the generated PDF. If you're not using Unicode, then helvetica or times are safe choices that will keep the PDF small. Unicode fonts can increase the size of the PDF to the 1MB region."),
202
    );
203
    $form['settings']['print_pdf_font_size'] = array(
204
      '#type' => 'textfield',
205
      '#title' => t('Font size'),
206
      '#default_value' => variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT),
207
      '#size' => 2,
208
      '#maxlength' => 3,
209
      '#description' => t('(TCPDF only) Set the font size to be used for normal text. This is the base value for the scaling applied to other text styles.'),
210
    );
211
    $form['settings']['print_pdf_font_subsetting'] = array(
212
      '#type' => 'checkbox',
213
      '#title' => t('Enable font subsetting'),
214
      '#default_value' => variable_get('print_pdf_font_subsetting', PRINT_PDF_FONT_SUBSETTING_DEFAULT),
215
      '#description' => t('(TCPDF only) Only embed those font characters that are actually used.  This can generates smaller PDF files but may significantly slow down processing.'),
216
    );
217
    $form['settings']['print_pdf_dompdf_unicode'] = array(
218
      '#type' => 'checkbox',
219
      '#title' => t("Use dompdf's Unicode Mode"),
220
      '#default_value' => variable_get('print_pdf_dompdf_unicode', PRINT_PDF_DOMPDF_UNICODE_DEFAULT),
221
      '#description' => t("If enabled, dompdf's Unicode mode is used. If not, the module will attempt to convert some non-ASCII chars to ISO-8859-1."),
222
    );
223
    $form['settings']['print_pdf_wkhtmltopdf_options'] = array(
224
      '#type' => 'textfield',
225
      '#title' => t('wkhtmltopdf options'),
226
      '#size' => 60,
227
      '#maxlength' => 500,
228
      '#default_value' => variable_get('print_pdf_wkhtmltopdf_options', PRINT_PDF_WKHTMLTOPDF_OPTIONS),
229
      '#description' => t('(wkhtmltopdf only) Set any additional options to be passed to the wkhtmltopdf executable. Tokens may be used in these options (see list below).'),
230
    );
231

    
232
      $form['settings']['print_pdf_filename'] = array(
233
        '#type' => 'textfield',
234
        '#title' => t('PDF filename'),
235
        '#default_value' => variable_get('print_pdf_filename', PRINT_PDF_FILENAME_DEFAULT),
236
        '#description' => t("If left empty the generated filename defaults to the node's path. Tokens may be used to build the filename (see following list). The .pdf extension will be appended automatically."),
237
      );
238
    if (module_exists('token')) {
239
      $form['settings']['print_pdf_filename_patterns'] = array(
240
        '#type' => 'fieldset',
241
        '#title' => t('Replacement patterns'),
242
        '#collapsible' => TRUE,
243
        '#collapsed' => TRUE,
244
      );
245
      $form['settings']['print_pdf_filename_patterns']['descriptions'] = array(
246
        '#theme' => 'token_tree',
247
        '#token_types' => array('node'),
248
      );
249
    }
250

    
251
    $form['settings']['print_pdf_display_sys_urllist'] = array(
252
      '#type' => 'checkbox',
253
      '#title' => t('Printer-friendly URLs list in system pages'),
254
      '#default_value' => variable_get('print_pdf_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT),
255
      '#description' => t('Enabling this option will display a list of printer-friendly destination URLs at the bottom of the page.'),
256
    );
257

    
258
    $form['#validate'][] = '_print_pdf_settings_validate';
259
  }
260
  else {
261
    $form['settings'] = array(
262
      '#type' => 'markup',
263
      '#markup' => '<p>' . t("No PDF generation tool found! Please download a supported PHP PDF generation tool. Check this module's INSTALL.txt for more details.") . '</p>',
264
    );
265
  }
266

    
267
  return system_settings_form($form);
268
}
269

    
270
/**
271
 * Validate print_pdf_settings form.
272
 */
273
function _print_pdf_settings_validate($form, &$form_state) {
274
  if (empty($form_state['values']['print_pdf_pdf_tool'])) {
275
    form_set_error('print_pdf_pdf_tool', t("No PDF tool selected"));
276
  }
277
  if ($form_state['values']['print_pdf_font_size'] < 1) {
278
    form_set_error('print_pdf_font_size', t("Font size must be at least 1."));
279
  }
280
}
281

    
282
/**
283
 * Auxiliary function to locate suitable PDF generation tools
284
 *
285
 * @return
286
 *   array of filenames with the include-able PHP file of the located tools
287
 */
288
function _print_pdf_tools() {
289
  $pattern = '!^(?:dompdf_config.inc.php|tcpdf.php|wkhtmltopdf.*)$!';
290
  $tools = array_keys(file_scan_directory(drupal_get_path('module', 'print'), $pattern));
291
  $tools = array_merge($tools, array_keys(file_scan_directory(PRINT_PDF_LIB_PATH, $pattern)));
292
  if (module_exists('libraries')) {
293
    $tools = array_merge($tools, array_keys(file_scan_directory(libraries_get_path('dompdf'), '/^dompdf_config.inc.php$/')));
294
    $tools = array_merge($tools, array_keys(file_scan_directory(libraries_get_path('tcpdf'), '/^tcpdf.php$/')));
295
    $tools = array_merge($tools, array_keys(file_scan_directory(libraries_get_path('wkhtmltopdf'), '/^wkhtmltopdf/')));
296
  }
297

    
298

    
299
  $num_tools = count($tools);
300

    
301
  if ($num_tools == 0) {
302
    variable_set('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
303
    return -1;
304
  }
305
  else {
306
    return array_combine($tools, $tools);
307
  }
308
}
309

    
310
/**
311
 * Menu callback for the PDF version module text strings settings form.
312
 *
313
 * @ingroup forms
314
 */
315
function print_pdf_strings_settings() {
316
  drupal_set_message(t("Saving these strings will disable their translation via Drupal's language system. Use the reset button to return them to the original state."), 'warning');
317

    
318
  $form['print_pdf_text'] = array(
319
    '#type' => 'fieldset',
320
    '#title' => t('Text strings'),
321
  );
322

    
323
  $form['print_pdf_text']['print_pdf_link_text'] = array(
324
    '#type' => 'textfield',
325
    '#title' => t('Link text'),
326
    '#default_value' => variable_get('print_pdf_link_text', t('PDF version')),
327
    '#description' => t('Text used in the link to the PDF version.'),
328
  );
329
  $form['print_pdf_text']['reset'] = array(
330
    '#type' => 'submit',
331
    '#value' => t('Reset to defaults'),
332
    '#submit' => array('print_pdf_strings_settings_delete'),
333
  );
334

    
335
  return system_settings_form($form);
336
}
337

    
338
/**
339
 * Reset button callback for text strings settings form
340
 *
341
 * @ingroup forms
342
 */
343
function print_pdf_strings_settings_delete() {
344
  variable_del('print_pdf_link_text');
345
}