<?php

/**
 * @file
 *   The install and update code for the Views Send module.
 *
 * @ingroup views_send.
 */

/**
 * Implements hook_schema().
 */
function views_send_schema() {
  $schema['views_send_spool'] = array(
    'description' => 'Table holds e-mails that are being send on cron.',
    'fields' => array(
      'eid' => array(
        'description' => 'The primary identifier for an e-mail.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'uid' => array(
        'description' => 'The user that has sent the message.',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0),
      'timestamp' => array(
        'description' => 'The Unix timestamp when the message was added to spool.',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0),
      'status' => array(
        'description' => 'Status: 0 = pending; 1 = sent.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0),
      'tentatives' => array(
        'description' => 'How many times we tried to send this message.',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'default' => 0),
      'from_name' => array(
        'description' => 'The real name of the sender.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => ''),
      'from_mail' => array(
        'description' => 'The sender e-mail address.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'to_name' => array(
        'description' => 'The real name of the recipient.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => ''),
      'to_mail' => array(
        'description' => 'The recipient e-mail address.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'subject' => array(
        'description' => 'The e-mail subject.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'body' => array(
        'description' => 'The e-mail body.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',),
      'headers' => array(
        'description' => 'The e-mail additional headers.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',),
    ),
    'indexes' => array(
      'uid' => array('uid'),
      'timestamp' => array('timestamp'),
    ),
    'primary key' => array('eid'),
  );
  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function views_send_uninstall() {
}

/**
 * Add retry to the default configuration.
 */
function views_send_update_8101() {
  $config = \Drupal::configFactory()->getEditable('views_send.settings');
  $config->set('retry', 5);
  $config->save(TRUE);
}
