<?php
// $Id: messaging_template.install,v 1.1.2.4 2009/12/14 14:41:34 jareyero Exp $

/**
* Implementation of hook_schema().
*/
function messaging_template_schema() {
  $schema['messaging_template'] = array(
    'description' => 'Message templates.',
    'fields' => array(
      'tpid'    => array(      
        'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE,
        'description' => 'Unique template id.',
      ),
      'name' => array(
        'type' => 'varchar', 'length' => 100, 'not null' => TRUE, 'default' => '',
        'description' => 'Messaging send method key.',
      ),
      'type'    => array(
        'type' => 'varchar', 'length' => 100, 'not null' => TRUE, 'default' => '',
        'description' => 'Message group key.',
      ),
      'title' => array(
        'type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '',
        'description' => 'Messaging send method key.',
      ),
      'description' => array(
        'type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '',
        'description' => 'Destination identifier, it may be an email address.',
      ),
      'module' => array(
        'type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '',
        'description' => 'Module that owns this template.',
      ),
      'fallback'    => array(
        'type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '',
        'description' => 'Key for template fallback.',
      ),
      'enabled' => array(
        'type' => 'int', 'not null' => TRUE, 'default' => 0,
        'description' => 'Unix timestamp, when the message was created/stored.',
      ),
    ),    
    'primary key' => array('tpid'),
    'indexes' => array(
      'type' => array('name'),
    ),
  );

  $schema['messaging_template_text'] = array(
    'description' => 'Template text parts for message composition.',
    'fields' => array(
      'tptid'    => array(      
        'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE,
        'description' => 'Unique template text id.',
      ),
      'name'    => array(
        'type' => 'varchar', 'length' => 100, 'not null' => TRUE, 'default' => '',
        'description' => 'Message group key.',
      ),
      'method'    => array(
        'type' => 'varchar', 'length' => 50, 'not null' => TRUE, 'default' => '',
        'description' => 'Messaging send method.',
      ),
      'msgkey' => array(
        'type' => 'varchar', 'length' => 50, 'not null' => TRUE, 'default' => '',
        'description' => 'Message part key, should be unique within a group (header, footer,..).',
      ),
      'language' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
      'template' => array(
        'type' => 'text', 'not null' => TRUE, 'size' => 'big',
        'description' => 'Message template, multiline text with tokens for replacement.',
      ),
      'options' => array(
        'description' => 'Whether the template is overridden, default, fallback.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1, // Default is 'override'
      ),
      'format' => array(
        'description' => "The input format used by this text.",
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0
      ),
    ),
    'primary key' => array('tptid'),
    'indexes' => array(
      'name_method_key_lang' => array('name', 'method', 'msgkey', 'language'),
      'name'    => array('name'),
      'method' => array('method'),
    ),
  );

  return $schema;
}

/**
* Implementation of hook_install().
*/
function messaging_template_install() {
  // Create tables.
  drupal_install_schema('messaging_template');
  // Load old templates
  if (db_table_exists('messaging_template_parts')) {
    db_query("INSERT INTO {messaging_template_text}(name, method, msgkey, template, options) SELECT type, method, msgkey, message, 1 FROM {messaging_message_parts}");
    db_query("UPDATE {messaging_template_text} SET template = '' WHERE template = '<empty>'");
  }
}

/**
 * Implementation of hook_unistall()
 */
function messaging_template_uninstall() {
  drupal_uninstall_schema('messaging_template');  
}
