newsletter.install in Newsletter 7
Same filename and directory in other branches
Contains install,uninstall and update functions for Newsletter module.
File
newsletter.installView source
<?php
/**
* @file
* Contains install,uninstall and update functions for Newsletter module.
*/
/**
* Implements hook_install().
*/
function newsletter_install() {
$mail_modes = variable_get('mail_system', array(
'default-system' => 'DefaultMailSystem',
));
$mail_modes['newsletter'] = 'NewsletterMailSystem';
variable_set('mail_system', $mail_modes);
$personal_info_form_defaults = array(
'firstname' => 'show',
'lastname' => 'show',
'gender' => 'show',
'receive_format' => 'show',
);
variable_set('newsletter_personal_info_form', $personal_info_form_defaults);
variable_set('newsletter_send_confirm', TRUE);
variable_set('newsletter_send_welcome', TRUE);
variable_set('newsletter_send_unsubscribe', TRUE);
if (!db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE machine_name = 'newsletter_categories'")
->fetchField()) {
// Create vocabulary for newsletter lists categories.
$vocabulary = new stdClass();
$vocabulary->name = 'Newsletter categories';
$vocabulary->machine_name = 'newsletter_categories';
$vocabulary->description = 'Vocabulary used to store the categories of newsletter lists';
$vocabulary->module = 'newsletter';
$vocabulary->hierarchy = 0;
taxonomy_vocabulary_save($vocabulary);
}
// Create fields and instances.
$fields_instances = newsletter_fields_instances();
foreach ($fields_instances as $op => $data) {
foreach ($data as $specs) {
switch ($op) {
case 'field':
if (!field_info_field($specs['field_name'])) {
field_create_field($specs);
}
break;
case 'instance':
if (!field_info_instance($specs['entity_type'], $specs['field_name'], $specs['bundle'])) {
field_create_instance($specs);
}
break;
}
}
}
// Add extra collumn to handle exposed newsletter lists tids.
newsletter_schema_alter($schema);
foreach ($schema['field_data_field_newsletter_list']['fields'] as $name => $spec) {
db_add_field('field_data_field_newsletter_list', $name, $spec);
}
// Create dummy data
newsletter_create_examples();
}
/**
* Implements hook_uninstall().
*/
function newsletter_uninstall() {
db_query("DELETE FROM {variable} WHERE name LIKE 'newsletter_%'");
$mail_modes = variable_get('mail_system');
unset($mail_modes['newsletter']);
variable_set('mail_system', $mail_modes);
$fields_instances = newsletter_fields_instances();
foreach ($fields_instances['instance'] as $data) {
field_delete_instance($data);
}
field_purge_batch(10);
// Delete newsletter categories vocabulary if exists
$vocabulary = taxonomy_vocabulary_machine_name_load('newsletter_categories');
if ($vocabulary) {
taxonomy_vocabulary_delete($vocabulary->vid);
}
}
/**
* Implements hook_schema_alter().
*/
function newsletter_schema_alter(&$schema) {
$schema['field_data_field_newsletter_list']['fields']['target_id_tids'] = array(
'description' => 'The serialized terms this subscriber selected(only if list is exposed, else NULL).',
'type' => 'text',
'not null' => FALSE,
'default' => NULL,
);
}
/**
* Implements hook_schema().
*/
function newsletter_schema() {
$schema = array();
$schema['newsletter_subscriber'] = array(
'description' => 'Contains the subscriber emails with their list relations.',
'fields' => array(
'nsid' => array(
'description' => 'The primary identifier for a subscriber.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'uid' => array(
'description' => 'The user id (if the user is registered) of the subscriber.',
'type' => 'int',
'not null' => FALSE,
'default' => NULL,
),
'email' => array(
'description' => 'The subscriber\'s email.',
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
),
'firstname' => array(
'description' => 'The subscriber\'s full name.',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
'lastname' => array(
'description' => 'The subscriber\'s full name.',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
'gender' => array(
'description' => 'The subscriber\'s gender.',
'type' => 'varchar',
'length' => 24,
'not null' => FALSE,
),
'receive_format' => array(
'description' => 'The subscriber\'s Preferred newsletter format.',
'type' => 'varchar',
'length' => 11,
'not null' => FALSE,
),
'ip' => 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,
),
'confirmed' => array(
'description' => 'Whether this user has confirm his subscription through email.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'confirmation_timestamp' => array(
'description' => 'The confirmation timestamp of this subscriber.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'created' => array(
'description' => 'The creation timestamp of this subscriber.',
'type' => 'int',
'not null' => FALSE,
'default' => 0,
),
),
'indexes' => array(
'subscriber_created' => array(
'created',
),
),
'unique keys' => array(
'subscriber_email' => array(
'email',
),
'subscriber_uid' => array(
'uid',
),
),
'primary key' => array(
'nsid',
),
);
$schema['newsletter_list'] = array(
'description' => 'Contains the newsletter subscriber lists with their templates relations.',
'fields' => array(
'nlid' => array(
'description' => 'The unique ID of this list.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'title' => array(
'description' => 'The list\'s title.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'send_rate' => array(
'description' => 'How often to send this list.Manual,Daily,Weekly or Monthly.',
'type' => 'varchar',
'length' => 11,
'not null' => TRUE,
'default' => 'Manual',
),
'last_sent' => array(
'description' => 'Timestamp this list was last sent.',
'type' => 'int',
'not null' => FALSE,
),
'send_again' => array(
'description' => 'Date this list needs to be sent again.',
'mysql_type' => 'date',
'pgsql_type' => 'timestamp',
'sqlsrv_type' => 'date',
'type' => NULL,
'not null' => FALSE,
),
'created' => array(
'description' => 'The creation timestamp of this subscriber.',
'type' => 'int',
'not null' => FALSE,
'default' => 0,
),
),
'unique keys' => array(
'list_title' => array(
'title',
),
),
'foreign keys' => array(
'tracked_template' => array(
'table' => 'newsletter_template',
'columns' => array(
'ntid' => 'ntid',
),
),
),
'indexes' => array(
'list_created' => array(
'created',
),
),
'primary key' => array(
'nlid',
),
);
$schema['newsletter_template'] = array(
'description' => 'Contains the newsletter templates.',
'fields' => array(
'ntid' => array(
'description' => 'The unique ID of this template.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'subject' => array(
'description' => 'The template\'s title/subject .',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'basic' => array(
'description' => 'Whether this template is basic or not.',
'type' => 'int',
'not null' => FALSE,
'default' => 0,
),
'exposed' => array(
'description' => 'Boolean.whether this list is exposed or not',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'created' => array(
'description' => 'The creation timestamp of this template.',
'type' => 'int',
'not null' => FALSE,
'default' => 0,
),
),
'unique keys' => array(
'template_subject' => array(
'subject',
),
),
'indexes' => array(
'template_created' => array(
'created',
),
),
'primary key' => array(
'ntid',
),
);
$schema['newsletter_newsletter'] = array(
'description' => 'Contains the newsletters that are sent with their statistics.',
'fields' => array(
'nnid' => array(
'description' => 'The primary identifier for a newsletter.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'title' => array(
'description' => 'The newsletter\'s title.(If this newsletter is automatic then it gets its list title)',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'send_id' => array(
'description' => 'The # of sent times.',
'type' => 'int',
'not null' => FALSE,
'default' => 0,
),
'custom' => array(
'description' => 'Whether this newsletter sent as a custom one.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'ntid' => array(
'description' => 'The {newsletter_templates}.id this newsletter uses.',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
'subscribers_sent' => array(
'description' => 'No Of subscribers this newsletter sent to.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => 0,
),
'clicks' => array(
'description' => 'how many clicks this newsletter/list generated for the website.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'opens' => array(
'description' => 'how many times this newsletter/list opened by subscriber.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'last_sent' => array(
'description' => 'The last sent timestamp of this newsletter.',
'type' => 'int',
'not null' => FALSE,
'default' => 0,
),
),
'foreign keys' => array(
'tracked_template' => array(
'table' => 'newsletter_templates',
'columns' => array(
'ntid' => 'ntid',
),
),
),
'indexes' => array(
'newsletter_send_id' => array(
'send_id',
),
'newsletter_last_sent' => array(
'last_sent',
),
),
'primary key' => array(
'nnid',
),
);
return $schema;
}
/*
* @return
* A keyed array containing fields and instances.
*/
function newsletter_fields_instances() {
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $vocabulary) {
// Field names have 32 char limit.
$field_name = 'field_' . substr($vocabulary->machine_name, 0, 26);
$fields[] = array(
'field_name' => $field_name,
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $vocabulary->machine_name,
'parent' => 0,
),
),
),
);
if ($vocabulary->machine_name == 'newsletter_categories') {
$instances[] = array(
'field_name' => 'field_newsletter_categories',
'entity_type' => 'newsletter_list',
'bundle' => 'newsletter_list',
'label' => 'Category',
'widget' => array(
'weight' => 0,
'type' => 'taxonomy_autocomplete',
),
);
}
else {
$instances[] = array(
'field_name' => $field_name,
'entity_type' => 'newsletter_template',
'bundle' => 'newsletter_template',
'label' => $vocabulary->name,
'widget' => array(
'weight' => 0,
'type' => 'options_select',
),
);
}
}
$fields[] = array(
'field_name' => 'field_newsletter_list',
'type' => 'entityreference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'target_type' => 'newsletter_list',
'handler' => 'base',
'handler_submit' => 'Change handler',
'handler_settings' => array(
'target_bundles' => array(),
'sort' => array(
'type' => 'none',
),
),
),
);
$fields[] = array(
'field_name' => 'field_newsletter_template',
'type' => 'entityreference',
'settings' => array(
'target_type' => 'newsletter_template',
'handler' => 'base',
'handler_submit' => 'Change handler',
'handler_settings' => array(
'target_bundles' => array(),
'sort' => array(
'type' => 'none',
),
),
),
);
$fields[] = array(
'field_name' => 'field_list_description',
'type' => 'text',
);
$fields[] = array(
'field_name' => 'field_newsletter_body',
'type' => 'text_with_summary',
);
$instances[] = array(
'field_name' => 'field_list_description',
'entity_type' => 'newsletter_list',
'bundle' => 'newsletter_list',
'label' => 'Description',
'widget' => array(
'weight' => 0,
'type' => 'text_textfield',
),
);
$instances[] = array(
'field_name' => 'field_newsletter_list',
'entity_type' => 'newsletter_subscriber',
'bundle' => 'newsletter_subscriber',
'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',
),
),
),
);
$instances[] = array(
'field_name' => 'field_newsletter_template',
'entity_type' => 'newsletter_list',
'bundle' => 'newsletter_list',
'label' => 'Template',
'required' => TRUE,
'widget' => array(
'weight' => 0,
'type' => 'options_select',
'active' => 1,
),
'settings' => array(
'target_type' => 'newsletter_template',
'handler' => 'base',
'handler_settings' => array(
'target_bundles' => array(
'newsletter_template',
),
),
),
);
$instances[] = array(
'field_name' => 'field_newsletter_body',
'entity_type' => 'newsletter_template',
'bundle' => 'newsletter_template',
'label' => 'Body',
'widget' => array(
'type' => 'text_textarea_with_summary',
),
'settings' => array(
'display_summary' => TRUE,
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
),
),
);
$instances[] = array(
'field_name' => 'field_newsletter_body',
'entity_type' => 'newsletter_newsletter',
'bundle' => 'newsletter_newsletter',
'label' => 'Body',
'widget' => array(
'type' => 'text_textarea_with_summary',
),
'settings' => array(
'display_summary' => TRUE,
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
),
),
);
return array(
'field' => $fields,
'instance' => $instances,
);
}
function newsletter_create_examples() {
$controller = entity_get_controller('newsletter_template');
$confirmation_body = "Hello [newsletter:subscriber_email],<br />\n\n";
$confirmation_body .= "We received your request for information from [site:name] <br />\n";
$confirmation_body .= "Before we begin sending you the information you requested, we want to be certain we have your permission.<br />\n";
$confirmation_body .= "Click the link below to give us permission to send you information. It's fast and easy! If you cannot click the full URL below, please copy the URL and paste it into your web browser.<br /><br />\n\n";
$confirmation_body .= "Confirm by clicking the URL below:<br /><br />\n";
$confirmation_body .= "<a href=\"[newsletter:confirmation_url]\">[newsletter:confirmation_url]</a><br /><br />\n\n";
$confirmation_body .= "If you do not want to subscribe, simply ignore this message.<br /><br />\n\n";
$confirmation_body .= "Best Regards,<br />\n";
$confirmation_body .= "[site:name]<br />\n";
$confirmation_body .= "[site:url]<br /><br />\n\n";
$confirmation_body .= "Request generated by:<br />\n";
$confirmation_body .= "IP: [newsletter:subscription_ip]<br />\n";
$confirmation_body .= "Date: [newsletter:subscription_time]<br />\n";
$template = $controller
->create();
$template->basic = 1;
$template->subject = 'Newsletter confirmation';
$template->field_newsletter_body[LANGUAGE_NONE][0] = array(
'value' => $confirmation_body,
'format' => 'filtered_html',
);
$controller
->save($template);
$welcome_body = "Hello [newsletter:subscriber_email],<br /><br />\n\n";
$welcome_body .= "Thank you for your subscription. You are now signed up to our newsletter!<br />\n";
$welcome_body .= "We will keep your personal information private and secure. We will not share your email address with any third parties. <br />\n";
$welcome_body .= "To ensure you receive future subscribers-only news, please add this e-mail to your e-mail address book or safe senders list.<br />\n\n\n";
$welcome_body .= "Sincerely,<br />\n";
$welcome_body .= "[site:name]<br />\n";
$welcome_body .= "[site:url]<br /><br />\n\n";
$welcome_body .= "----<br />\n";
$welcome_body .= "You are receiving this email because on [newsletter:subscription_time] at [site:url] you subscribed to receive our e-newsletters.<br /><br />\n";
$welcome_body .= "You can unsubscribe via the link below:<br />\n";
$welcome_body .= "<a href=\"[newsletter:unsubscribe_url]\">Unsubscribe</a>\n";
$template = $controller
->create();
$template->basic = 1;
$template->subject = 'Newsletter welcome';
$template->field_newsletter_body[LANGUAGE_NONE][0] = array(
'value' => $welcome_body,
'format' => 'filtered_html',
);
$controller
->save($template);
$unsubscribe_body = "Dear [newsletter:subscriber_email],<br />\n\n";
$unsubscribe_body .= "This email is being sent to notify you that we have received a request to remove the following record from our mailing list:<br /><br />\n\n";
$unsubscribe_body .= "Email: [newsletter:subscriber_email]<br /><br />\n\n";
$unsubscribe_body .= "This record was removed from our database.<br />\n";
$unsubscribe_body .= "If you do receive further unwanted email from our system, please let us know and we'd be happy to address the issue immediately.<br /><br />\n\n";
$unsubscribe_body .= "Sincerely,<br />\n";
$unsubscribe_body .= "[site:name]<br />\n";
$unsubscribe_body .= "[site:url] <br />\n";
$template = $controller
->create();
$template->basic = 1;
$template->subject = 'Newsletter unsubscribe';
$template->field_newsletter_body[LANGUAGE_NONE][0] = array(
'value' => $unsubscribe_body,
'format' => 'filtered_html',
);
$controller
->save($template);
$body = "Hello [newsletter:subscriber_email],<br /><br />\n\n";
$body .= "Here is what's new on <a href=\"[site:url]\">[site:name]</a> over the past few days. Your comments and feedback are always welcome.<br />\n\n";
$body .= "<ol>\n";
$body .= " [repeat] \n";
$body .= "<li><a href=\"[node:url]\">[node:title]</a> -[node:created]<br /> [node:summary]<br /></li>\n";
$body .= " [/repeat] \n\n";
$body .= "</ol>Sincerely,<br /> <a href=\"[site:url]\">[site:name]</a><br /> <br /> <br />\n\n";
$body .= "--<br />You are receiving this email because on [newsletter:subscription_time] you subscribed to receive our newsletter list : [newsletter:list_name].<br /><br />\n\n";
$body .= "You can unsubscribe by clicking the link below:<br /> \n";
$body .= "<a href=\"[newsletter:unsubscribe_url]\">Unsubscribe</a><br />\n";
$template = $controller
->create();
$template->subject = 'Default';
$template->field_newsletter_body[LANGUAGE_NONE][0] = array(
'value' => $body,
'format' => 'filtered_html',
);
// Assign all terms to this template from a random vocabulary
$vocabulary = db_query_range("SELECT vid, machine_name FROM {taxonomy_vocabulary} WHERE machine_name <> 'newsletter_categories'", 0, 1)
->fetchObject();
if ($vocabulary) {
$terms = taxonomy_get_tree($vocabulary->vid);
$field = 'field_' . $vocabulary->machine_name;
foreach ($terms as $term) {
$template->{$field}[LANGUAGE_NONE][]['tid'] = $term->tid;
}
}
$controller
->save($template);
// Create Standard List
$controller = entity_get_controller('newsletter_list');
$list = $controller
->create();
$list->title = 'Standard list';
$list->field_newsletter_template[LANGUAGE_NONE][0]['target_id'] = 4;
$list->field_list_description[LANGUAGE_NONE][0]['value'] = 'An example newsletter list with Manual Send Rate.';
$list->send_rate = 'Manual';
$controller
->save($list);
}
/**
* Add field to newsletter_list to support lists configurable on roles.
*/
function newsletter_update_7100(&$sandbox) {
db_add_field('newsletter_list', 'role', array(
'description' => 'Which roles to send this list to.all for all roles.',
'type' => 'int',
'length' => 10,
'not null' => FALSE,
'default' => NULL,
));
}
/**
* Remove role field from newsletter_list
* since this feature goes in its own module.
*/
function newsletter_update_7101(&$sandbox) {
db_drop_field('newsletter_list', 'role');
}
/**
* Add a text_with_summary field to use with newsletter_template.
*/
function newsletter_update_7102(&$sandbox) {
$field = array(
'field_name' => 'field_newsletter_body',
'type' => 'text_with_summary',
);
field_create_field($field);
}
/**
* Attach a text_with_summary field to newsletter_template
* and newsletter_newsletter.Remove obsolete database fields.
*/
function newsletter_update_7103(&$sandbox) {
$instance = array(
'field_name' => 'field_newsletter_body',
'entity_type' => 'newsletter_template',
'bundle' => 'newsletter_template',
'label' => 'Body',
'widget' => array(
'type' => 'text_textarea_with_summary',
),
'settings' => array(
'display_summary' => TRUE,
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
),
),
);
field_create_instance($instance);
// Change values and attach same field to newsletter
$instance['entity_type'] = 'newsletter_newsletter';
$instance['bundle'] = 'newsletter_newsletter';
field_create_instance($instance);
// Transfer old data to field
$ntids = db_query('SELECT ntid FROM {newsletter_template}')
->fetchCol();
foreach ($ntids as $ntid) {
$template = newsletter_template_load($ntid);
$template->field_newsletter_body[LANGUAGE_NONE][0] = array(
'value' => db_query('SELECT body FROM {newsletter_template} WHERE ntid = :ntid', array(
':ntid' => $ntid,
))
->fetchField(),
'format' => db_query('SELECT format FROM {newsletter_template} WHERE ntid = :ntid', array(
':ntid' => $ntid,
))
->fetchField(),
);
entity_get_controller('newsletter_template')
->save($template);
}
// Drop the fields
db_drop_field('newsletter_template', 'body');
db_drop_field('newsletter_template', 'format');
}
/**
* Update entityreference fields with new entityrefence specs.
*/
function newsletter_update_7104(&$sandbox) {
$fields[] = field_info_field('field_newsletter_list');
$fields[] = field_info_field('field_newsletter_template');
foreach ($fields as $field) {
$field['settings']['handler_submit'] = 'Change handler';
$field['settings']['handler_settings'] = array(
'target_bundles' => array(),
'sort' => array(
'type' => 'none',
),
);
field_update_field($field);
}
}
Functions
Name | Description |
---|---|
newsletter_create_examples | |
newsletter_fields_instances | |
newsletter_install | Implements hook_install(). |
newsletter_schema | Implements hook_schema(). |
newsletter_schema_alter | Implements hook_schema_alter(). |
newsletter_uninstall | Implements hook_uninstall(). |
newsletter_update_7100 | Add field to newsletter_list to support lists configurable on roles. |
newsletter_update_7101 | Remove role field from newsletter_list since this feature goes in its own module. |
newsletter_update_7102 | Add a text_with_summary field to use with newsletter_template. |
newsletter_update_7103 | Attach a text_with_summary field to newsletter_template and newsletter_newsletter.Remove obsolete database fields. |
newsletter_update_7104 | Update entityreference fields with new entityrefence specs. |