| 1 |
<?php
|
| 2 |
// $Id:$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Subscriptions module mail gateway helper functions for upgrading mail_edit D5 to D6.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_form_alter().
|
| 11 |
*
|
| 12 |
* Adds to the General Settings part at admin/settings/subscriptions.
|
| 13 |
*/
|
| 14 |
function _subscriptions_mail_form_mail_edit_list_form_alter(&$form, $form_state) {
|
| 15 |
$form['subscriptions'] = array(
|
| 16 |
'#type' => 'fieldset',
|
| 17 |
'#title' => t('!Subscriptions module', array('!Subscriptions' => 'Subscriptions')),
|
| 18 |
'#weight' => -5,
|
| 19 |
);
|
| 20 |
$form['subscriptions']['explain'] = array(
|
| 21 |
'#type' => 'item',
|
| 22 |
'#value' => t('You have !Subscriptions mail templates that were created under Drupal 5. Please import or remove these templates.', array('!Subscriptions' => 'Subscriptions')),
|
| 23 |
);
|
| 24 |
$lang_query = "SELECT DISTINCT(language) FROM {mail_edit} ORDER BY language ASC";
|
| 25 |
$lang_result = db_query($lang_query);
|
| 26 |
$mail_langs = array();//fetch all current translations
|
| 27 |
while($row = db_fetch_object($lang_result)) {
|
| 28 |
$mail_langs[$row->language] = $row->language;
|
| 29 |
}
|
| 30 |
foreach (language_list() as $lang_key => $language) {
|
| 31 |
$mail_langs[$lang_key] = $lang_key;
|
| 32 |
}//<!-- fetch all current translations
|
| 33 |
|
| 34 |
if (count($mail_langs) > 1) {
|
| 35 |
$form['subscriptions']['languages'] = array(
|
| 36 |
'#type' => 'select',
|
| 37 |
'#title' => t('Language(s)'),
|
| 38 |
'#default_value' => array(),
|
| 39 |
'#options' => $mail_langs,
|
| 40 |
'#description' => t('Select all the language(s) into which to import the Drupal 5 !Subscriptions mail templates.<br />Note: After successful importing, the old templates are removed automatically.', array('!Subscriptions' => 'Subscriptions')),
|
| 41 |
'#multiple' => TRUE,
|
| 42 |
);
|
| 43 |
}
|
| 44 |
else {
|
| 45 |
$form['subscriptions']['language'] = array(
|
| 46 |
'#type' => 'value',
|
| 47 |
'#value' => current($mail_langs),
|
| 48 |
);
|
| 49 |
}
|
| 50 |
$form['subscriptions']['import'] = array(
|
| 51 |
'#type' => 'submit',
|
| 52 |
'#value' => t('Import'),
|
| 53 |
);
|
| 54 |
$form['subscriptions']['remove'] = array(
|
| 55 |
'#type' => 'submit',
|
| 56 |
'#value' => t('Remove'),
|
| 57 |
);
|
| 58 |
$form['#validate'][] = 'subscriptions_mail_mail_edit_list_form_validate';
|
| 59 |
$form['#submit'][] = 'subscriptions_mail_mail_edit_list_form_submit';
|
| 60 |
}
|
| 61 |
|
| 62 |
function subscriptions_mail_mail_edit_list_form_validate($form, &$form_state) {
|
| 63 |
if ($form_state['values']['op'] == $form_state['values']['import']) {
|
| 64 |
if (!isset($form_state['values']['language']) && empty($form_state['values']['languages'])) {
|
| 65 |
$form['subscriptions']['languages']['#error'] = TRUE;
|
| 66 |
form_set_error('languages', t('Select at least one language to import into.'));
|
| 67 |
}
|
| 68 |
}
|
| 69 |
return $form;
|
| 70 |
}
|
| 71 |
|
| 72 |
function subscriptions_mail_mail_edit_list_form_submit($form, &$form_state) {
|
| 73 |
$languages = (isset($form_state['values']['language']) ? array($form_state['values']['language']) : $form_state['values']['languages']);
|
| 74 |
|
| 75 |
switch ($form_state['values']['op']) {
|
| 76 |
|
| 77 |
case $form_state['values']['remove']:
|
| 78 |
if (db_table_exists('subscriptions_mail_edit')) {
|
| 79 |
db_query("DELETE FROM {subscriptions_mail_edit}");
|
| 80 |
}
|
| 81 |
if (db_table_exists('mail_edit_d5')) {
|
| 82 |
db_query("DELETE FROM {mail_edit_d5} WHERE mailkey LIKE 'subscriptions-%'");
|
| 83 |
}
|
| 84 |
break;
|
| 85 |
|
| 86 |
case $form_state['values']['import']:
|
| 87 |
// Import digest records:
|
| 88 |
$old_mailkey = 'subscriptions-digest';
|
| 89 |
$new_mailkey = 'subscriptions_mail_digest';
|
| 90 |
$result = db_query("SELECT * FROM {mail_edit_registry} WHERE id = '%s'", $new_mailkey);
|
| 91 |
if ($registry = db_fetch_array($result)) {
|
| 92 |
if (_subscriptions_mail_mail_edit_import($old_mailkey, $new_mailkey, $registry['description'], $languages)) {
|
| 93 |
$digest_item = db_result(db_query("SELECT item_body FROM {subscriptions_mail_edit} WHERE mailkey = '%s'", $old_mailkey .'-item'));
|
| 94 |
$digest_separator = db_result(db_query("SELECT item_body FROM {subscriptions_mail_edit} WHERE mailkey = '%s'", $old_mailkey .'-separator'));
|
| 95 |
$digest_comment = db_result(db_query("SELECT item_body FROM {subscriptions_mail_edit} WHERE mailkey = '%s'", $old_mailkey .'-item-comment'));
|
| 96 |
foreach ($languages as $language) {
|
| 97 |
db_query("INSERT INTO {mail_edit} (description, subject, id, body, language) VALUES ('%s', '%s', '%s', '%s', '%s')", '', $digest_separator, $new_mailkey .'+item', $digest_item, $language);
|
| 98 |
db_query("INSERT INTO {mail_edit} (description, subject, id, body, language) VALUES ('%s', '%s', '%s', '%s', '%s')", '', '', $new_mailkey .'+comment', $digest_comment, $language);
|
| 99 |
}
|
| 100 |
db_query("DELETE FROM {subscriptions_mail_edit} WHERE mailkey LIKE '%s'", $new_mailkey .'-%');
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
// Import node/nid (+ comment) records:
|
| 105 |
$result = db_query("SELECT * FROM {mail_edit_registry} WHERE id = '%s'", $new_mailkey);
|
| 106 |
if ($registry = db_fetch_array($result)) {
|
| 107 |
if (_subscriptions_mail_mail_edit_import('subscriptions-node-nid', 'subscriptions_content_node-nid', $registry['description'], $languages)) {
|
| 108 |
$comment_item = db_result(db_query("SELECT item_body FROM {subscriptions_mail_edit} WHERE mailkey = 'subscriptions-comment-item'"));
|
| 109 |
$comment_separator = db_result(db_query("SELECT item_body FROM {subscriptions_mail_edit} WHERE mailkey = 'subscriptions-comment-separator'"));
|
| 110 |
foreach ($languages as $language) {
|
| 111 |
db_query("INSERT INTO {mail_edit} (description, subject, id, body, language) VALUES ('%s', '%s', '%s', '%s', '%s')", '', $comment_separator, 'subscriptions_content_node-nid+comment', $comment_item, $language);
|
| 112 |
}
|
| 113 |
db_query("DELETE FROM {subscriptions_mail_edit} WHERE mailkey LIKE 'subscriptions-comment-%'");
|
| 114 |
}
|
| 115 |
}
|
| 116 |
|
| 117 |
// Import node/tid records:
|
| 118 |
$result = db_query("SELECT * FROM {mail_edit_registry} WHERE id = '%s'", $new_mailkey);
|
| 119 |
if ($registry = db_fetch_array($result)) {
|
| 120 |
_subscriptions_mail_mail_edit_import('subscriptions-node-tid', 'subscriptions_taxonomy_node-tid', $registry['description'], $languages);
|
| 121 |
}
|
| 122 |
|
| 123 |
// Import node-type-* records:
|
| 124 |
$old_mailkey = 'subscriptions-node-type-';
|
| 125 |
$new_mailkey = 'subscriptions_content_node-type-';
|
| 126 |
$result = db_query("SELECT * FROM {mail_edit_registry} WHERE id LIKE '%s'", $new_mailkey .'%');
|
| 127 |
while ($registry = db_fetch_array($result)) {
|
| 128 |
$type = substr($registry['id'], strlen($new_mailkey));
|
| 129 |
_subscriptions_mail_mail_edit_import($old_mailkey . $type, $new_mailkey . $type, $registry['description'], $languages);
|
| 130 |
}
|
| 131 |
break;
|
| 132 |
}
|
| 133 |
|
| 134 |
$dummy = array();
|
| 135 |
if (db_table_exists('subscriptions_mail_edit') && db_result(db_query("SELECT COUNT(*) FROM {subscriptions_mail_edit}")) == 0) {
|
| 136 |
db_drop_table($dummy, 'subscriptions_mail_edit');
|
| 137 |
}
|
| 138 |
if (db_table_exists('mail_edit_d5') && db_result(db_query("SELECT COUNT(*) FROM {mail_edit_d5}")) == 0) {
|
| 139 |
db_drop_table($dummy, 'mail_edit_d5');
|
| 140 |
}
|
| 141 |
}
|
| 142 |
|
| 143 |
function _subscriptions_mail_mail_edit_import($old_mailkey, $new_mailkey, $description, $languages) {
|
| 144 |
$success = FALSE;
|
| 145 |
if ($row = db_fetch_array(db_query("SELECT * FROM {mail_edit_d5} WHERE mailkey = '%s'", $old_mailkey))) {
|
| 146 |
if (empty($row['description'])) {
|
| 147 |
$row['description'] = filter_xss($description, array());
|
| 148 |
}
|
| 149 |
$success = TRUE;
|
| 150 |
foreach ($languages as $language) {
|
| 151 |
$variables = array('%key' => $new_mailkey, '%language' => $language);
|
| 152 |
if (db_result(db_query("SELECT COUNT(*) FROM {mail_edit} WHERE id = '%s' AND language = '%s'", $new_mailkey, $language))) {
|
| 153 |
drupal_set_message(t("%language translation of %key cannot be imported because it's already defined.", $variables), 'warning');
|
| 154 |
$success = FALSE;
|
| 155 |
}
|
| 156 |
elseif (db_query("INSERT INTO {mail_edit} (description, subject, id, body, language) VALUES ('%s', '%s', '%s', '%s', '%s')", $row['description'], $row['subject'], $new_mailkey, $row['body'], $language) && db_affected_rows()) {
|
| 157 |
drupal_set_message(t('%language translation of %key has been imported.', $variables));
|
| 158 |
}
|
| 159 |
else {
|
| 160 |
drupal_set_message(t('%language translation of %key failed to import.', $variables), 'error');
|
| 161 |
$success = FALSE;
|
| 162 |
}
|
| 163 |
}
|
| 164 |
if ($success) {
|
| 165 |
db_query("DELETE FROM {mail_edit_d5} WHERE mailkey = '%s'", $old_mailkey);
|
| 166 |
}
|
| 167 |
}
|
| 168 |
return $success;
|
| 169 |
}
|