<?php

/**
 * @file
 * Install file of the print_pdf module
 *
 * @ingroup print
 */

/**
 * Implementation of hook_install().
 */
function print_pdf_install() {
  drupal_install_schema('print_pdf');

  // Module weight
  update_sql("UPDATE {system} SET weight = 2 WHERE name = 'print_pdf'");
}

/**
 * Implementation of hook_uninstall().
 */
function print_pdf_uninstall() {
  drupal_uninstall_schema('print_pdf');

  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\_%'");
  while ($variable = db_fetch_object($settings)) {
    variable_del($variable->name);
  }
}

/**
 * Implementation of hook_schema().
 */
function print_pdf_schema() {
  $schema['print_pdf_node_conf'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'link' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
      ),
      'comments' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
      ),
      'url_list' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
      ),
    ),
    'primary key' => array('nid'),
  );

  $schema['print_pdf_page_counter'] = array(
    'fields' => array(
      'path' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
      'totalcount' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'big',
      ),
      'timestamp' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('path'),
  );

  return $schema;
}

/**
 * Update to version 6.x-1.0
 */
function print_pdf_update_6000() {
  global $conf;

  $ret = array();
  if (isset($conf['print_pdf_settings'])) {
    $print_pdf_settings = variable_get('print_pdf_settings', '');
    variable_set('print_pdf_link_pos', array('link' => ($print_pdf_settings['show_link'] ? 'link' : 0) ));
    variable_set('print_pdf_show_link', max(1, $print_pdf_settings['show_link']));
    variable_set('print_pdf_node_link_visibility', $print_pdf_settings['node_link_visibility']);
    variable_set('print_pdf_node_link_pages', $print_pdf_settings['node_link_pages']);
    variable_set('print_pdf_link_class', $print_pdf_settings['link_class']);
    variable_set('print_pdf_sys_link_visibility', $print_pdf_settings['sys_link_visibility']);
    variable_set('print_pdf_sys_link_pages', $print_pdf_settings['sys_link_pages']);
    variable_set('print_pdf_book_link', $print_pdf_settings['book_link']);
    variable_set('print_pdf_pdf_tool', $print_pdf_settings['pdf_tool']);
    variable_set('print_pdf_content_disposition', $print_pdf_settings['content_disposition']);
    variable_set('print_pdf_paper_size', $print_pdf_settings['paper_size']);
    variable_set('print_pdf_page_orientation', $print_pdf_settings['page_orientation']);
    variable_del('print_pdf_settings');
  }
  return $ret;
}

/**
 * Update to version 6.x-1.1
 */
function print_pdf_update_6001() {
  $schema['print_pdf_node_conf'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'link' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => '1',
        'size' => 'tiny',
      ),
      'comments' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => '1',
        'size' => 'tiny',
      ),
      'url_list' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => '1',
        'size' => 'tiny',
      ),
    ),
    'primary key' => array('nid'),
  );

  $schema['print_pdf_page_counter'] = array(
    'fields' => array(
      'path' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
      'totalcount' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'big',
      ),
      'timestamp' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('path'),
  );

  $ret = array();
  db_create_table($ret, 'print_pdf_node_conf', $schema['print_pdf_node_conf']);
  db_create_table($ret, 'print_pdf_page_counter', $schema['print_pdf_page_counter']);
  return $ret;
}

/**
 * Update to version 6.x-1.2
 */
function print_pdf_update_6003() {
  // Delete custom text strings set to the default
  $ret = array();
  $vars = array(
    'print_pdf_link_text' => 'PDF version',
  );

  $t = get_t();

  foreach ($vars as $name => $default) {
    if (variable_get($name, '') == $t($default)) {
      variable_del($name);
    }
  }

  menu_rebuild();
  return $ret;
}

/**
 * Update to version 6.x-1.8
 */
function print_pdf_update_6004() {
  // add new 'access PDF version' permission to any role which has 'access print'
  $ret = array();
  $dbret = db_query('SELECT * FROM {permission}');
  while ($role = db_fetch_object($dbret)) {
    if ((strpos($role->perm, 'access print') !== FALSE)) {
      $role->perm = db_escape_string('access PDF version, '. $role->perm);
      $ret[] = update_sql("UPDATE {permission} SET perm = '$role->perm' WHERE rid = $role->rid");
    }
  }
  return $ret;
}

/**
 * Update to version 6.x-1.11
 */
function print_pdf_update_6005() {
  $ret = array();

  variable_del('print_pdf_wkhtmltopdf_xdisplay');
  variable_del('print_pdf_xvfb_options');

  // BLOCK_CACHE_GLOBAL -> 8
  $ret[] = update_sql("UPDATE {blocks} SET cache = 8 WHERE module = 'print_pdf' AND delta = '0'");
  return $ret;
}

/**
 * Update to version 6.x-1.11
 */
function print_pdf_update_6006() {
  $ret = array();
  // Module weight
  $ret[] = update_sql("UPDATE {system} SET weight = 2 WHERE name = 'print_pdf'");
  return $ret;
}

/**
 * Update to version 6.x-1.11
 */
function print_pdf_update_6007() {
  $ret = array();

  foreach (node_get_types() as $key => $value) {
    $print_pdf_display = variable_get('print_pdf_display_'. $value->type, 1);
    $print_pdf_display_comment = variable_get('print_pdf_display_comment_'. $value->type, 0);
    $print_pdf_display_urllist = variable_get('print_pdf_display_urllist_'. $value->type, 1);

    $ret[] = update_sql("UPDATE {print_pdf_node_conf} SET link = (link AND $print_pdf_display), comments = (comments OR $print_pdf_display_comment), url_list = (url_list AND $print_pdf_display_urllist) WHERE nid IN (SELECT nid FROM {node} WHERE type = '$value->type');");
  }
  return $ret;
}

/**
 * Enable block and help area links
 */
function print_pdf_update_6118() {
  $ret = array();

  $link_pos = variable_get('print_pdf_link_pos', array('link' => 'link', 'block' => 'block', 'help' => 'help'));
  $link_pos['block'] = 'block';
  $link_pos['help'] = 'help';
  variable_set('print_pdf_link_pos', $link_pos);

  return $ret;
}
