<?php

/**
 * @file
 * This file takes care of creating/deleting pm_email_notify table used
 * for storing user notification preferences
 */

function pm_email_notify_schema() {
  $schema = array();

  $schema['pm_email_notify'] = array(
    'description' => '{pm_email_notify} holds private message email notification settings for each user ',
    'fields' => array(
      'user_id' => array(
        'description'   => 'ID of the user',
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'email_notify_is_enabled' => array(
        'description'   => 'Email Notification Status (Enabled=1, Disabled=0)',
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
    ),
    'unique keys' => array(
      'user_id' => array('user_id')
    ),
  );

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function pm_email_notify_uninstall() {
  variable_del('pm_email_notify_body');
  variable_del('pm_email_notify_default');
  variable_del('pm_email_notify_subject');
  variable_del('pm_email_notify_from');
}

/**
 * Replace old placeholders with new tokens.
 */
function pm_email_notify_update_7000() {
  $replacements = array(
    '!author' => '[privatemsg_message:author]',
    '!username' => '[privatemsg_message:recipient]',
    '!author_uid' => '[privatemsg_message:author:uid]',
    '!pm_subject' => '[privatemsg_message:subject]',
    '!pm_body' => '[privatemsg_message:body]',
    '!thread' => '[privatemsg_message:thread_id]',
    '!site' => '[site:name]',
    '!login-url' => '[site:login-url]',
    '!uri' => '	[site:url]',
    '!uri_brief' => '[site:url-brief]',
    '!message' => '[privatemsg_message:url]',
    '!settings' => '[privatemsg_message:recipient:edit-url]',
  );
  // Only update if there was actually something saved.
  if ($subject = variable_get('pm_email_notify_subject', FALSE)) {
    variable_set('pm_email_notify_subject', str_replace(array_keys($replacements), array_values($replacements), $subject));
  }
  if ($body = variable_get('pm_email_notify_body', FALSE)) {
    variable_set('pm_email_notify_body', str_replace(array_keys($replacements), array_values($replacements), $body));
  }
}

