Project

General

Profile

Paste
Download (10.2 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / print / print_pdf / print_pdf.install @ 76bdcd04

1
<?php
2

    
3
/**
4
 * @file
5
 * Install, update and uninstall functions for the print_pdf module.
6
 *
7
 * @ingroup print
8
 */
9

    
10
/**
11
 * Implements hook_enable().
12
 */
13
function print_pdf_enable() {
14
  // Module weight.
15
  db_update('system')
16
    ->fields(array(
17
      'weight' => 2,
18
    ))
19
    ->condition('type', 'module')
20
    ->condition('name', 'print_pdf')
21
    ->execute();
22
}
23

    
24
/**
25
 * Implements hook_uninstall().
26
 */
27
function print_pdf_uninstall() {
28
  variable_del('print_pdf_autoconfig');
29
  variable_del('print_pdf_cache_enabled');
30
  variable_del('print_pdf_cache_lifetime');
31
  variable_del('print_pdf_content_disposition');
32
  variable_del('print_pdf_display_sys_urllist');
33
  variable_del('print_pdf_filename');
34
  variable_del('print_pdf_images_via_file');
35
  variable_del('print_pdf_link_text');
36
  variable_del('print_pdf_link_text_enabled');
37
  variable_del('print_pdf_page_orientation');
38
  variable_del('print_pdf_paper_size');
39
  variable_del('print_pdf_pdf_tool');
40

    
41
  variable_del('print_pdf_book_link');
42
  variable_del('print_pdf_link_class');
43
  variable_del('print_pdf_link_pos');
44
  variable_del('print_pdf_link_teaser');
45
  variable_del('print_pdf_link_use_alias');
46
  variable_del('print_pdf_show_link');
47
  variable_del('print_pdf_sys_link_pages');
48
  variable_del('print_pdf_sys_link_visibility');
49

    
50
  $settings = db_query("SELECT name FROM {variable} WHERE name LIKE 'print\_pdf\_display\_%'");
51
  foreach ($settings as $variable) {
52
    variable_del($variable->name);
53
  }
54
}
55

    
56
/**
57
 * Implements hook_schema().
58
 */
59
function print_pdf_schema() {
60
  $schema['print_pdf_node_conf'] = array(
61
    'description' => 'PDF version node-specific configuration settings',
62
    'fields' => array(
63
      'nid' => array(
64
        'type' => 'int',
65
        'unsigned' => TRUE,
66
        'not null' => TRUE,
67
        'description' => 'The {node}.nid of the node.',
68
      ),
69
      'link' => array(
70
        'type' => 'int',
71
        'unsigned' => TRUE,
72
        'not null' => TRUE,
73
        'default' => 1,
74
        'size' => 'tiny',
75
        'description' => 'Show link',
76
      ),
77
      'comments' => array(
78
        'type' => 'int',
79
        'unsigned' => TRUE,
80
        'not null' => TRUE,
81
        'default' => 1,
82
        'size' => 'tiny',
83
        'description' => 'Show link in individual comments',
84
      ),
85
      'url_list' => array(
86
        'type' => 'int',
87
        'unsigned' => TRUE,
88
        'not null' => TRUE,
89
        'default' => 1,
90
        'size' => 'tiny',
91
        'description' => 'Show Printer-friendly URLs list',
92
      ),
93
      'size' => array(
94
        'type' => 'varchar',
95
        'length' => 9,
96
        'description' => 'Paper size',
97
      ),
98
      'orientation' => array(
99
        'type' => 'varchar',
100
        'length' => 9,
101
        'description' => 'Page orientation',
102
      ),
103

    
104
    ),
105
    'primary key' => array('nid'),
106
  );
107

    
108
  $schema['print_pdf_page_counter'] = array(
109
    'description' => 'PDF version access counter',
110
    'fields' => array(
111
      'path' => array(
112
        'type' => 'varchar',
113
        'length' => 255,
114
        'not null' => TRUE,
115
        'description' => 'Page path',
116
      ),
117
      'totalcount' => array(
118
        'type' => 'int',
119
        'unsigned' => TRUE,
120
        'not null' => TRUE,
121
        'default' => 0,
122
        'size' => 'big',
123
        'description' => 'Number of page accesses',
124
      ),
125
      'timestamp' => array(
126
        'type' => 'int',
127
        'unsigned' => TRUE,
128
        'not null' => TRUE,
129
        'default' => 0,
130
        'description' => 'Last access',
131
      ),
132
    ),
133
    'primary key' => array('path'),
134
  );
135

    
136
  return $schema;
137
}
138

    
139
/**
140
 * Implements hook_requirements().
141
 */
142
function print_pdf_requirements($phase) {
143
  $requirements = array();
144
  $t = get_t();
145
  switch ($phase) {
146
    // At runtime, make sure that a PDF generation tool is selected.
147
    case 'runtime':
148
      $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
149
      if (empty($print_pdf_pdf_tool)) {
150
        $requirements['print_pdf_tool'] = array(
151
          'title' => $t('Printer, email and PDF versions - PDF generation library'),
152
          'value' => $t('No PDF tool selected'),
153
          'description' => $t('Please configure it in the !url.', array('!url' => l($t('PDF settings page'), 'admin/config/user-interface/print/pdf'))),
154
          'severity' => REQUIREMENT_ERROR,
155
        );
156
      }
157
      else {
158
        // Tool is defined, get some data from it's handler module.
159
        $tool = explode('|', $print_pdf_pdf_tool);
160
        $function = $tool[0] . '_pdf_tool_info';
161
        $info = function_exists($function) ? $function() : array();
162

    
163
        // Is the file there?
164
        if (!is_file($tool[1]) || !is_readable($tool[1])) {
165
          $requirements['print_pdf_tool'] = array(
166
            'title' => $t('Printer, email and PDF versions - PDF generation library'),
167
            'value' => $t('File not found'),
168
            'description' => $t('The currently selected PDF generation library (%file) is no longer accessible.', array('%file' => $tool[1])),
169
            'severity' => REQUIREMENT_ERROR,
170
          );
171
        }
172
        else {
173
          // Get the version number.
174
          $function = $tool[0] . '_pdf_tool_version';
175
          if (function_exists($function)) {
176
            $version = $function($tool[1]);
177

    
178
            if (isset($info['min_version']) && version_compare($version, $info['min_version'], '<')) {
179
              $requirements['print_pdf_tool_version'] = array(
180
                'title' => $t('Printer, email and PDF versions - PDF generation library'),
181
                'value' => $t('Unsupported %lib version', array('%lib' => $info['name'])),
182
                'description' => $t('The currently selected version of %lib (@version) is not supported. Please update to a !url.', array(
183
                  '%lib' => $info['name'],
184
                  '@version' => $version,
185
                  '!url' => l($t('newer version'), $info['url']),
186
                )),
187
                'severity' => REQUIREMENT_ERROR,
188
              );
189
            }
190
            else {
191
              $requirements['print_pdf_tool_version'] = array(
192
                'title' => $t('Printer, email and PDF versions - PDF generation library'),
193
                'value' => $info['name'] . ' ' . $version,
194
              );
195
            }
196
          }
197
        }
198

    
199
        // If auto-config is on, check for write access to the appropriate dirs.
200
        if (variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT)) {
201
          $directories = array();
202
          if (isset($info['public_dirs'])) {
203
            foreach ($info['public_dirs'] as $dir) {
204
              $directories[] = 'public://print_pdf/' . $tool[0] . '/' . $dir;
205
            }
206
          }
207
          if (isset($info['tool_dirs'])) {
208
            foreach ($info['tool_dirs'] as $dir) {
209
              $directories[] = dirname($tool[1]) . '/' . $dir;
210
            }
211
          }
212

    
213
          foreach ($directories as $dir) {
214
            if (!is_dir($dir) || !is_writable($dir)) {
215
              $requirements['print_pdf_tool_' . $dir] = array(
216
                'title' => $t('%lib directory', array('%lib' => $info['name'])),
217
                'value' => $t('Non-writable permissions'),
218
                'description' => $t('You must change the %libdir permissions to be writable, as %lib requires write-access to that directory.', array('%lib' => $info['name'], '%libdir' => $dir)),
219
                'severity' => REQUIREMENT_ERROR,
220
              );
221
            }
222
          }
223
        }
224
      }
225
      break;
226
  }
227
  return $requirements;
228
}
229

    
230
/**
231
 * Remove hardcoded numeric deltas from all blocks.
232
 */
233
function print_pdf_update_7000(&$sandbox) {
234
  $renamed_deltas = array(
235
    'print_pdf' => array(
236
      '0' => 'print_pdf-top',
237
    ),
238
  );
239

    
240
  update_fix_d7_block_deltas($sandbox, $renamed_deltas, array());
241

    
242
  if (variable_get('print_pdf_filename', '') == '[site-name] - [title] - [mod-yyyy]-[mod-mm]-[mod-dd]') {
243
    variable_set('print_pdf_filename', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]');
244
  }
245
}
246

    
247
/**
248
 * Delete old variables.
249
 */
250
function print_pdf_update_7200(&$sandbox) {
251
  variable_del('print_pdf_settings');
252

    
253
  variable_del('print_pdf_node_link_pages');
254
  variable_del('print_pdf_node_link_visibility');
255
}
256

    
257
/**
258
 * Update pdf_tool variable to new module|path format.
259
 */
260
function print_pdf_update_7201(&$sandbox) {
261
  $tool = explode('|', variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT));
262

    
263
  if (count($tool) == 1) {
264
    // Not an array yet, update variable to new format.
265
    if (preg_match('!dompdf_config.inc.php$!', $tool[0])) {
266
      $tool[0] = 'print_pdf_dompdf|' . $tool[0];
267
    }
268
    elseif (preg_match('!tcpdf.php$!', $tool[0])) {
269
      $tool[0] = 'print_pdf_tcpdf|' . $tool[0];
270
    }
271
    elseif (preg_match('!wkhtmltopdf!', $tool[0])) {
272
      $tool[0] = 'print_pdf_wkhtmltopdf|' . $tool[0];
273
    }
274
    else {
275
      $tool[0] = PRINT_PDF_PDF_TOOL_DEFAULT;
276
    }
277

    
278
    variable_set('print_pdf_pdf_tool', $tool[0]);
279
  }
280
}
281

    
282
/**
283
 * Enable block and help area links.
284
 */
285
function print_pdf_update_7202(&$sandbox) {
286
  $link_pos = variable_get('print_pdf_link_pos', drupal_json_decode('{ "link": "link", "block": "block", "help": "help" }'));
287
  $link_pos['block'] = 'block';
288
  $link_pos['help'] = 'help';
289
  variable_set('print_pdf_link_pos', $link_pos);
290
}
291

    
292
/**
293
 * Add Size and Orientation fields for per content type Size and Orientation.
294
 */
295
function print_pdf_update_7203(&$sandbox) {
296
  $spec = array(
297
    'type' => 'varchar',
298
    'length' => 9,
299
    'description' => 'Paper size',
300
  );
301
  db_add_field('print_pdf_node_conf', 'size', $spec);
302
  $spec = array(
303
    'type' => 'varchar',
304
    'length' => 9,
305
    'description' => 'Page orientation',
306
  );
307
  db_add_field('print_pdf_node_conf', 'orientation', $spec);
308
}
309

    
310
/**
311
 * Enable the PDF generation sub-module being used.
312
 */
313
function print_pdf_update_7204(&$sandbox) {
314
  // Since update_7201 already stored the correct module in the array, use that.
315
  $tool = explode('|', variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT));
316

    
317
  if (count($tool) == 2) {
318
    module_enable(array($tool[0]), FALSE);
319
  }
320
}
321

    
322
/**
323
 * Increase size of the path field in the print_pdf_page_counter table.
324
 */
325
function print_pdf_update_7205(&$sandbox) {
326
  db_drop_primary_key('print_pdf_page_counter');
327
  db_change_field('print_pdf_page_counter', 'path', 'path',
328
    array(
329
      'type' => 'varchar',
330
      'length' => 255,
331
      'not null' => TRUE,
332
      'description' => 'Page path',
333
    ),
334
    array('primary key' => array('path')));
335
}