<?php

/**
 * @file
 * Install, update and uninstall functions for the print_pdf module.
 *
 * @ingroup print
 */

/**
 * Implements hook_enable().
 */
function print_pdf_enable() {
  $t = get_t();

  // Module weight
  db_update('system')
    ->fields(array(
      'weight' => 2,
    ))
    ->condition('type', 'module')
    ->condition('name', 'print_pdf')
    ->execute();
}

/**
 * Implements hook_uninstall().
 */
function print_pdf_uninstall() {
  variable_del('print_pdf_settings');
  variable_del('print_pdf_show_link');
  variable_del('print_pdf_link_pos');
  variable_del('print_pdf_link_teaser');
  variable_del('print_pdf_node_link_visibility');
  variable_del('print_pdf_node_link_pages');
  variable_del('print_pdf_link_class');
  variable_del('print_pdf_sys_link_visibility');
  variable_del('print_pdf_sys_link_pages');
  variable_del('print_pdf_book_link');
  variable_del('print_pdf_pdf_tool');
  variable_del('print_pdf_content_disposition');
  variable_del('print_pdf_paper_size');
  variable_del('print_pdf_page_orientation');
  variable_del('print_pdf_images_via_file');
  variable_del('print_pdf_font_family');
  variable_del('print_pdf_font_size');
  variable_del('print_pdf_link_text');
  variable_del('print_pdf_link_use_alias');
  variable_del('print_pdf_filename');
  variable_del('print_pdf_autoconfig');
  variable_del('print_pdf_dompdf_unicode');
  variable_del('print_pdf_wkhtmltopdf_options');
  variable_del('print_pdf_display_sys_urllist');
  $settings = db_query("SELECT name FROM {variable} WHERE name LIKE 'print\_pdf\_display\_%'");
  foreach ($settings as $variable) {
    variable_del($variable->name);
  }
}

/**
 * Implements hook_schema().
 */
function print_pdf_schema() {
  $schema['print_pdf_node_conf'] = array(
    'description' => 'PDF version node-specific configuration settings',
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The {node}.nid of the node.',
      ),
      'link' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
        'description' => 'Show link',
      ),
      'comments' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
        'description' => 'Show link in individual comments',
      ),
      'url_list' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
        'description' => 'Show Printer-friendly URLs list',
      ),
    ),
    'primary key' => array('nid'),
  );

  $schema['print_pdf_page_counter'] = array(
    'description' => 'PDF version access counter',
    'fields' => array(
      'path' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'description' => 'Page path',
      ),
      'totalcount' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'big',
        'description' => 'Number of page accesses',
      ),
      'timestamp' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Last access',
      ),
    ),
    'primary key' => array('path'),
  );

  return $schema;
}

/**
 * Remove hardcoded numeric deltas from all blocks
 */
function print_pdf_update_7000(&$sandbox) {
  $renamed_deltas = array(
    'print_pdf' => array(
      '0' => 'print_pdf-top',
    ),
  );

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

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

/**
 * Enable block and help area links
 */
function print_pdf_update_7101(&$sandbox) {
  $link_pos = variable_get('print_pdf_link_pos', drupal_json_decode('{ "link": "link", "block": "block", "help": "help" }'));
  $link_pos['block'] = 'block';
  $link_pos['help'] = 'help';
  variable_set('print_pdf_link_pos', $link_pos);
}
