<?php
// $Id: print_mail.install,v 1.1.2.11 2009/09/09 23:08:58 jcnventura Exp $

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

/**
 * Implementation of hook_install().
 */
function print_mail_install() {
  drupal_install_schema('print_mail');
}

/**
 * Implementation of hook_uninstall().
 */
function print_mail_uninstall() {
  drupal_uninstall_schema('print_mail');

  variable_del('print_mail_settings');
  variable_del('print_mail_show_link');
  variable_del('print_mail_link_pos');
  variable_del('print_mail_node_link_visibility');
  variable_del('print_mail_node_link_pages');
  variable_del('print_mail_link_class');
  variable_del('print_mail_sys_link_visibility');
  variable_del('print_mail_sys_link_pages');
  variable_del('print_mail_book_link');
  variable_del('print_mail_hourly_threshold');
  variable_del('print_mail_teaser_default');
  variable_del('print_mail_teaser_choice');
  variable_del('print_mail_link_text');
  variable_del('print_mail_link_use_alias');
  variable_del('print_mail_text_confirmation');
  variable_del('print_mail_text_message');
  variable_del('print_mail_text_subject');
  variable_del('print_mail_job_queue');
  $settings = db_query("SELECT name FROM {variable} WHERE name LIKE 'print\_mail\_display\_%'");
  while ($variable = db_fetch_object($settings)) {
    variable_del($variable->name);
  }
}

/**
 * Implementation of hook_schema().
 */
function print_mail_schema() {
  $schema['print_mail_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_mail_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,
      ),
      'sentcount' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'big',
      ),
      'sent_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_mail_update_6000() {
  global $conf;

  $ret = array();
  if (isset($conf['print_mail_settings'])) {
    $print_mail_settings = variable_get('print_mail_settings', '');
    variable_set('print_mail_link_pos', array('link' => ($print_html_settings['show_link'] ? 'link' : 0) ));
    variable_set('print_mail_show_link', max(1, $print_mail_settings['show_link']));
    variable_set('print_mail_node_link_visibility', $print_mail_settings['node_link_visibility']);
    variable_set('print_mail_node_link_pages', $print_mail_settings['node_link_pages']);
    variable_set('print_mail_link_class', $print_mail_settings['link_class']);
    variable_set('print_mail_sys_link_visibility', $print_mail_settings['sys_link_visibility']);
    variable_set('print_mail_sys_link_pages', $print_mail_settings['sys_link_pages']);
    variable_set('print_mail_book_link', $print_mail_settings['book_link']);
    variable_set('print_mail_teaser_default', $print_mail_settings['teaser_default']);
    variable_set('print_mail_teaser_choice', $print_mail_settings['teaser_choice']);
    variable_del('print_mail_settings');
  }
  return $ret;
}

/**
 * Update to version 6.x-1.1
 */
function print_mail_update_6001() {
  $schema['print_mail_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_mail_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,
      ),
      'sentcount' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'big',
      ),
      'sent_timestamp' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('path'),
  );

  $ret = array();
  db_create_table($ret, 'print_mail_node_conf', $schema['print_mail_node_conf']);
  db_create_table($ret, 'print_mail_page_counter', $schema['print_mail_page_counter']);
  return $ret;
}

/**
 * Update to version 6.x-1.2
 */
function print_mail_update_6003() {
  // Delete custom text strings set to the default
  $ret = array();
  $vars = array(
    'print_mail_link_text' => 'Send to friend',
    'print_mail_text_subject' => '!user has sent you a message from !site',
    'print_mail_text_message' => 'Message from sender',
    'print_mail_text_content' => '',
    'print_mail_text_confirmation' => 'Thank you for spreading the word about !site.',
  );

  $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_mail_update_6004() {
  // add new 'access send to friend' 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 send to friend, '. $role->perm);
      $ret[] = update_sql("UPDATE {permission} SET perm = '$role->perm' WHERE rid = $role->rid");
    }
  }
  return $ret;
}
