You are here

newsletter_subscriber.install in Newsletter 7.2

Sets up the base table for newsletter subscribers and a table to store information about the subscriber types, install, uninstall and update functions for Newsletter subscriber module.

File

modules/subscriber/newsletter_subscriber.install
View source
<?php

/**
 * @file
 * Sets up the base table for newsletter subscribers
 * and a table to store information about the subscriber types,
 * install, uninstall and update functions for Newsletter subscriber module.
 */

/**
 * Implements hook_install().
 */
function newsletter_subscriber_install() {

  // Create default subscriber type
  $roles = user_roles(TRUE);
  $type = newsletter_subscriber_type_create(array(
    'type' => 'general',
    'name' => t('General subscriber'),
    'description' => '',
    'weight' => 0,
    'data' => array(
      'allow_anonymous' => TRUE,
      'roles' => array_keys($roles),
    ),
  ));
  $type
    ->save();

  // Add entity reference field of newsletter_lists
  $field = array(
    'field_name' => 'field_newsletter_list',
    'type' => 'entityreference',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'target_type' => 'newsletter_list',
      'handler' => 'base',
      'handler_settings' => array(
        'target_bundles' => array(
          'newsletter_list',
        ),
      ),
    ),
  );
  if (!field_info_field($field['field_name'])) {
    field_create_field($field);
  }

  // Attach it to subscriber entity
  $instance = array(
    'field_name' => 'field_newsletter_list',
    'entity_type' => 'newsletter_subscriber',
    'bundle' => 'general',
    'label' => 'Lists',
    'required' => TRUE,
    'widget' => array(
      'weight' => 0,
      'type' => 'options_buttons',
      'active' => 1,
    ),
    'settings' => array(
      'target_type' => 'newsletter_list',
      'handler' => 'base',
      'handler_settings' => array(
        'target_bundles' => array(
          'newsletter_list',
        ),
      ),
    ),
  );
  if (!field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle'])) {
    field_create_instance($instance);
  }
}

/**
 * Implements hook_uninstall().
 */
function newsletter_subscriber_uninstall() {
  db_query("DELETE FROM {variable} WHERE name LIKE 'newsletter_subscriber_%'");
  foreach (field_read_instances(array(
    'entity_type' => 'newsletter_subscriber',
  )) as $instance) {
    field_delete_instance($instance);
  }
  field_purge_batch(10);
}

/**
 * Implements hook_schema().
 */
function newsletter_subscriber_schema() {
  $schema = array();
  $schema['newsletter_subscriber'] = array(
    'description' => 'The base table for newsletter subscribers.',
    'fields' => array(
      'subscriber_id' => array(
        'description' => 'Primary Key: Identifier for a newsletter subscriber.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'The {newsletter_subscriber_type}.type of this subscriber.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'language' => array(
        'description' => 'The {languages}.language of the subscriber.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'uid' => array(
        'description' => 'The {users}.uid of the subscriber, 0 if anonymous.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'mail' => array(
        'description' => 'The subscriber\'s email.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'hostname' => array(
        'description' => 'Subscriber\'s IP, when he/she subscribed.',
        'type' => 'varchar',
        'length' => 22,
        'not null' => FALSE,
      ),
      'hash' => array(
        'description' => 'Subscriber\'s hash, used to confirm subscription and unsubscribe.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
      'status' => array(
        'description' => 'Status of the subscriber, 0 for inactive/unconfirmed, 1 for active.',
        'type' => 'int',
        'not null' => TRUE,
        'size' => 'tiny',
        'default' => 0,
      ),
      'confirmation_timestamp' => array(
        'description' => 'This subscriber confirmation timestamp of his/her mail.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the subscriber was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the subscriber was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'subscriber_id',
    ),
    'indexes' => array(
      'subscriber_type' => array(
        'type',
      ),
      'subscriber_hash' => array(
        'hash',
      ),
      'subscriber_uid' => array(
        'uid',
      ),
    ),
    'foreign keys' => array(
      'subscriber_user' => array(
        'table' => 'users',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
    ),
    'unique keys' => array(
      'subscriber_mail' => array(
        'mail',
      ),
    ),
  );
  $schema['newsletter_subscriber_type'] = array(
    'description' => 'Stores information about created {newsletter_subscriber} types.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique subscriber type identifier.',
      ),
      'type' => array(
        'description' => 'The machine-readable name of this subscriber type.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'The human-readable name of this subscriber type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'A brief description of this subscriber type.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'medium',
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'The weight of this subscriber type in relation to others.',
      ),
      'data' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of additional data related to this subscriber type.',
      ),
    ) + entity_exportable_schema_fields(),
    'primary key' => array(
      'id',
    ),
    'unique keys' => array(
      'type' => array(
        'type',
      ),
    ),
  );
  return $schema;
}