diff --git a/privatemsg.info b/privatemsg.info index 06757c4b4abecf0a719bb88404d5641e4f85b86c..afc6446e57e2a26263c0681c7ded8363ae57986a 100644 --- a/privatemsg.info +++ b/privatemsg.info @@ -1,5 +1,4 @@ ; $Id$ name = Privatemsg -description = Allow private messages between users. Requires Subscriptions 5.x-2.x. -dependencies = mail_edit subscriptions +description = Allow private messages between users. package = Mail diff --git a/privatemsg.install b/privatemsg.install index b85ad08d28d14dda3802f7ce13101dad247f4aef..0b74c8849b4b82e5598ae4dfc874beb08d2148d9 100644 --- a/privatemsg.install +++ b/privatemsg.install @@ -20,6 +20,7 @@ function privatemsg_install() { format int NOT NULL DEFAULT 0, thread int NOT NULL DEFAULT 0, type varchar(255) NOT NULL, + variables longtext, key (recipient), key (folder), key(type), @@ -43,11 +44,6 @@ function privatemsg_install() { thread int NOT NULL DEFAULT 0, key (recipient) ) /*!40100 DEFAULT CHARACTER SET utf8 */;"); - db_query("CREATE TABLE {privatemsg_mail_edit} ( - type varchar(255) NOT NULL, - mailkey varchar(255) NOT NULL, - PRIMARY KEY (mailkey) - ) /*!40100 DEFAULT CHARACTER SET utf8 */;"); db_query('CREATE TABLE {privatemsg_block_user} ( author int unsigned NOT NULL, recipient int unsigned NOT NULL, @@ -77,6 +73,7 @@ function privatemsg_install() { recipient_del smallint NOT NULL DEFAULT 0, thread int NOT NULL DEFAULT 0, type varchar(255) NOT NULL, + variables text, PRIMARY KEY (id) )"); db_query("CREATE INDEX {privatemsg_folder_index} ON {privatemsg}(folder)"); @@ -105,11 +102,6 @@ function privatemsg_install() { db_query("CREATE INDEX {privatemsg_archive_recipient} ON {privatemsg_archive}(recipient)"); db_query('CREATE SEQUENCE {privatemsg}_id_seq INCREMENT 1 START 1'); db_query('CREATE SEQUENCE {privatemsg_folder}_fid_seq INCREMENT 1 START 1'); - db_query("CREATE TABLE {privatemsg_mail_edit} ( - type varchar(255) NOT NULL, - mailkey varchar(255) NOT NULL, - PRIMARY KEY (mailkey) - ) /*!40100 DEFAULT CHARACTER SET utf8 */;"); db_query('CREATE TABLE {privatemsg_block_user} ( author int unsigned NOT NULL, recipient int unsigned NOT NULL, @@ -154,23 +146,26 @@ function privatemsg_install() { do { $i = db_next_id('{privatemsg_folder}_fid'); } while ($i < 1); // In case this api ever changes to start at zero.. - db_query("INSERT INTO {privatemsg_mail_edit} (type, mailkey) VALUES ('private-message', 'private-message')"); - db_query("INSERT INTO {mail_edit} (mailkey, subject, description) VALUES ('private-message', '', '%s')", st('When someone contacts you directly')); } function privatemsg_uninstall() { db_query("DROP TABLE {privatemsg}"); db_query("DROP TABLE {privatemsg_folder}"); db_query("DROP TABLE {privatemsg_archive}"); + if (db_table_exists('privatemsg_block_user')) { + db_query("DROP TABLE {privatemsg_block_user}"); + } + + // The following two tables are now a part of the pm_subscriptions module, + // but have been retained to handle updated installs that do not use the + // pm_subscriptions module. if (db_table_exists('privatemsg_mails')) { db_query("DROP TABLE {privatemsg_mails}"); } if (db_table_exists('privatemsg_mail_edit')) { db_query("DROP TABLE {privatemsg_mail_edit}"); } - if (db_table_exists('privatemsg_block_user')) { - db_query("DROP TABLE {privatemsg_block_user}"); - } + db_query("DELETE FROM {variable} WHERE name LIKE 'privatemsg_%'"); cache_clear_all('variables', 'cache'); } @@ -243,40 +238,13 @@ function privatemsg_update_3() { return $ret; } -function _privatemsg_abort_update(&$ret) { - // Keep our caller update_data() from setting the installed_schema_version: - $ret['#finished'] = FALSE; - // Keep update_data()'s caller update_do_updates() from going into an endless loop (HACK!): - unset($_SESSION['update_remaining'][key($_SESSION['update_remaining'])]); -} - function privatemsg_update_4() { $ret = array(); - if (!module_exists('mail_edit')) { - // If Privatemsg 5.x-1.8 is replaced by 5.x-2.x and the Mail Editor module is not enabled, then this will fail! - module_disable(array('privatemsg')); - $ret[] = array('success' => FALSE, 'query' => 'The required Mail Editor module is missing — the Privatemsg module has been disabled!
Be sure to re-run the database update after re-enabling the Privatemsg module!'); - _privatemsg_abort_update($ret); - return $ret; - } - switch ($GLOBALS['db_type']) { case 'mysql': case 'mysqli': $ret[] = update_sql("ALTER TABLE {privatemsg} ADD type varchar(255) NOT NULL default '', ADD KEY (type)"); - if (!db_table_exists('privatemsg_mail_edit')) { - if (db_table_exists('privatemsg_mails')) { - $ret[] = update_sql('RENAME TABLE {privatemsg_mails} TO {privatemsg_mail_edit}'); - } - else { - $ret[] = update_sql("CREATE TABLE {privatemsg_mail_edit} ( - type varchar(255) NOT NULL, - mailkey varchar(255) NOT NULL, - PRIMARY KEY (mailkey) - ) /*!40100 DEFAULT CHARACTER SET utf8 */;"); - } - } $ret[] = update_sql("ALTER TABLE {privatemsg} CHANGE subject subject varchar(255) NOT NULL default ''"); if (!db_table_exists('privatemsg_block_user')) { $ret[] = update_sql('CREATE TABLE {privatemsg_block_user} ( @@ -289,18 +257,6 @@ function privatemsg_update_4() { case 'pgsql': db_add_column($ret, 'privatemsg', 'type', 'varchar(255)', array('not null' => TRUE, 'default' => "''")); $ret[] = update_sql("CREATE INDEX {privatemsg_folder_type} ON {privatemsg}(type)"); - if (!db_table_exists('privatemsg_mail_edit')) { - if (db_table_exists('privatemsg_mails')) { - $ret[] = update_sql('ALTER TABLE {privatemsg_mails} RENAME TO {privatemsg_mail_edit}'); - } - else { - $ret[] = update_sql("CREATE TABLE {privatemsg_mail_edit} ( - type varchar(255) NOT NULL, - mailkey varchar(255) NOT NULL, - PRIMARY KEY (mailkey) - ) /*!40100 DEFAULT CHARACTER SET utf8 */;"); - } - } db_change_column($ret, 'privatemsg', 'subject', 'subject', 'varchar(255)', array('not null' => TRUE, 'default' => "''")); if (!db_table_exists('privatemsg_block_user')) { $ret[] = update_sql('CREATE TABLE {privatemsg_block_user} ( @@ -311,55 +267,25 @@ function privatemsg_update_4() { } break; } - $ret[] = update_sql("INSERT INTO {privatemsg_mail_edit} (type, mailkey) VALUES ('private-message', 'private-message')"); - if (!module_exists('subscriptions')) { - // If Privatemsg 5.x-1.8 is replaced by 5.x-2.x and the Subscriptions module is not enabled, then Privatemsg will not run correctly! - module_disable(array('privatemsg')); - $ret[] = array('success' => FALSE, 'query' => 'The required Subscriptions module is missing — the Privatemsg module has been disabled!'); - } return $ret; } +/** + * Add variables column for use by the subscriptions module. + */ function privatemsg_update_5() { $ret = array(); - if (!module_exists('privatemsg')) { - // privatemsg_update_5() is called even if privatemsg_update_4() disabled Privatemsg, so we have to abort here... - $ret[] = array('success' => FALSE, 'query' => 'The Privatemsg module is disabled.
Be sure to re-run the database update after re-enabling the Privatemsg module!'); - _privatemsg_abort_update($ret); - return $ret; - } - - if (!db_result(db_query("SELECT COUNT(*) FROM {mail_edit} WHERE mailkey = 'private-message'"))) { - $ret[] = update_sql("INSERT INTO {mail_edit} (mailkey, subject, description) VALUES ('private-message', '', '%s')", st('When someone contacts you directly')); - } - - if (drupal_get_installed_schema_version('subscriptions') < 1) { - module_disable(array('privatemsg')); - $ret[] = array('success' => FALSE, 'query' => 'You must upgrade the Subscriptions module to run this version of the Privatemsg module — the Privatemsg module has been disabled!'); - } - else { - $ret[] = array('success' => TRUE, 'query' => 'Subscriptions module: OK'); - } - return $ret; -} - -function privatemsg_update_6() { - $ret = array(); switch ($GLOBALS['db_type']) { case 'mysql': case 'mysqli': - // add name_length column (for sorting results by the length of username - - // this needs a whole new column for performance on autocomplete callbacks) - $ret[] = update_sql("ALTER TABLE {users} ADD name_length INT NOT NULL DEFAULT 0"); - $ret[] = update_sql("UPDATE {users} SET name_length = LENGTH(name)"); - // keep the column automatically updated ... - // note: the following will fail if the database user used by drupal - // does not have the SUPER privelege, but the update script will tell you, - // and you will have to create these 2 triggers manually - $ret[] = update_sql("CREATE TRIGGER user_name_length_insert BEFORE INSERT ON {users} FOR EACH ROW SET NEW.name_length = LENGTH(NEW.name)"); - $ret[] = update_sql("CREATE TRIGGER user_name_length_update BEFORE UPDATE ON {users} FOR EACH ROW SET NEW.name_length = LENGTH(NEW.name)"); + $ret[] = update_sql("ALTER TABLE {privatemsg} ADD variables longtext"); + break; + case 'pgsql': + db_add_column($ret, 'privatemsg', 'variables', 'text'); + break; } + return $ret; } diff --git a/privatemsg.module b/privatemsg.module index f7c7fe75cb9bdcc0ed5908f4a46aa11f34b6534f..62079b4c001fb6803353cadddf336b09f23d1efc 100644 --- a/privatemsg.module +++ b/privatemsg.module @@ -273,7 +273,7 @@ function privatemsg_menu($may_cache) { * Implementation of hook_perm(). */ function privatemsg_perm() { - return array('access private messages', 'administer private messages', 'create new folder', 'subscribe to private messages'); + return array('access private messages', 'administer private messages', 'create new folder'); } /** @@ -474,37 +474,6 @@ function privatemsg_form_alter($form_id, &$form) { ), ); break; - case 'mail_edit_form': - $mailkey = $form['mailkey']['#value']; - if ($mailkey == 'private-message') { - unset($form['body'], $form['subject'], $form['help']); - } - elseif (substr($mailkey, 0, 19) != 'subscriptions-pmsg-') { - $form['privatemsg_type'] = array( - '#type' => 'textfield', - '#title' => t('Private message type'), - '#default_value' => db_result(db_query("SELECT type FROM {privatemsg_mail_edit} WHERE mailkey = '%s'", $mailkey)), - '#description' => t('Fill this in to send a private message instead of the mail (when the recipient has an account on %site). In the folder view, users will be able to filter on this. Example: contact mails.', array('%site' => variable_get('site_name', t('Drupal')))), - '#weight' => 1, - ); - $form['#submit']['privatemsg_mail_edit_submit'] = array(); - } - else { - $form['help']['#variables']['!message_sender'] = t('The name of the sender of the private message'); - } - } -} - -function privatemsg_mail_edit_submit($form_id, $form_values) { - if ($form_values['privatemsg_type']) { - $args = array($form_values['privatemsg_type'], $form_values['mailkey']); - if ($form_values['insert']) { - db_query("INSERT {privatemsg_mail_edit} (type, mailkey) VALUES ('%s', '%s')", $args); - } - else { - db_query("UPDATE {privatemsg_mail_edit} SET type = '%s' WHERE mailkey = '%s'", $args); - } - cache_clear_all(); } } @@ -524,30 +493,6 @@ function privatemsg_block($op = 'list', $delta = 0, $edit = array()) { } } -/** - * Implementation of hook_subscriptions(). - */ -function privatemsg_subscriptions($op) { - switch ($op) { - case 'menu': - return array( - 'pmsg' => array( - 'access' => 'subscribe to private messages', - 'title' => t('private messages'), - ), - ); - case 'stype': - case 'ptype': - return array('pmsg'); - case 'stype_map': - return array( - 'pmsg' => array('sid' => 'message_id'), - ); - } -} - - - function privatemsg_message_allowed($recipient, $author = NULL) { $author = $author ? $author : $GLOBALS['user']->uid; $user = user_load(array('uid' => $recipient)); @@ -1706,7 +1651,8 @@ function _privatemsg_send($sender, $recipient, $subject, $body, $format, $thread if ($points = variable_get('privatemsg_userpoints', 0)) { module_invoke('userpoints', 'userpointsapi', 'points', $points, $sender->uid, 'privatemsg'); } - privatemsg_subscriptions_handle(privatemsg_load($message_id)); + module_invoke('pm_subscriptions', 'subscriptions_handle', privatemsg_load($message_id)); + return $result ? $message_id : $result; } @@ -2423,7 +2369,7 @@ function privatemsg_autocomplete($string) { if ($search != '') { $sql = "SELECT name FROM {users} u WHERE status <> 0 AND LOWER(name) LIKE LOWER('%s%%') AND "; $sql .= variable_get('privatemsg_default_allow', 1) ? '(data NOT LIKE \'%%:16:"privatemsg_allow";i:0%%\' OR data IS NULL)' : 'data LIKE \'%%:16:"privatemsg_allow";i:1%%\''; - $sql .= ' ORDER BY name_length ASC, name ASC'; + $sql .= ' ORDER BY name ASC'; watchdog('privatemsg', $sql); $result = db_query_range($sql, $search, 0, 10); $prefix = count($names) ? implode(', ', $names) .', ' : ''; @@ -2522,7 +2468,7 @@ function privatemsg_invoke_privatemsg(&$message, $op) { function privatemsg_load($message_id) { global $user; - $sql = 'SELECT p.id, u.uid, u.name, p.author, p.timestamp, p.subject, p.message, p.newmsg, p.recipient, p.folder, p.format, p.thread, p.type, p.variables FROM {privatemsg} p INNER JOIN {users} u ON p.author = u.uid WHERE p.id = %d'; + $sql = 'SELECT p.id, u.uid, u.name, p.author, p.timestamp, p.subject, p.message, p.newmsg, p.recipient, p.folder, p.format, p.thread, p.type FROM {privatemsg} p INNER JOIN {users} u ON p.author = u.uid WHERE p.id = %d'; $params = array($message_id); if (!user_access('administer private messages')) { $sql .= ' AND (recipient = %d OR author = %d)'; @@ -2532,132 +2478,3 @@ function privatemsg_load($message_id) { $result = db_query($sql, $params); return db_fetch_object($result); } - -function privatemsg_subscriptions_mailvars_pmsg($sid, $object_id, $placeholders, $sql_args) { - $message = privatemsg_load($object_id); - $sql = "SELECT u.uid, u.name, u.mail, u.language - FROM {users} u - INNER JOIN {subscriptions} s ON u.uid = s.uid AND send_interval != -1 - INNER JOIN {subscriptions_sent} ss ON u.uid = ss.uid - WHERE u.status= 1 AND s.stype = 'pmsg%s' AND s.uid = %d"; - $result = db_query($sql, $sid, $message->recipient); - $mailvars = array( - 'result' => $result, - '!message_sender' => $message->name, - '!subject' => $message->subject, - '!title' => $message->subject, - '!url' => url('privatemsg/view/'. $object_id, NULL, NULL, TRUE), - 'postfix' => 'pmsg-'. $sid, - 'send_self' => TRUE, - '!type' => 'private message', - ); - $unserialized = unserialize($message->variables); - if (is_array($unserialized)) { - $mailvars = array_merge($mailvars, $unserialized); - } - return $mailvars; -} - -function privatemsg_subscriptions_held_pmsg($message, $uid) { - privatemsg_subscriptions_handle($message, TRUE); - return TRUE; -} - -function privatemsg_subscriptions_handle($message, $cron = FALSE) { - if (!$cron) { - subscriptions_hold($message, 'pmsg', 'insert', $message->uid); - } - $variables = ($unserialized = unserialize($message->variables)) ? $unserialized : array(); - privatemsg_mail_edit_variables($variables); - subscriptions_mailvars($message->type, $message->id, $message->author, 'pmsg', $cron); -} - -function privatemsg_subscriptions_page_pmsg($account) { - return drupal_get_form('privatemsg_subscriptions_form', $account); -} - -function privatemsg_subscriptions_form($account) { - include_once drupal_get_path('module', 'subscriptions') .'/subscriptions.admin.inc'; - $result = db_query(" - SELECT type, description, send_interval, stype - FROM {privatemsg_mail_edit} p - LEFT JOIN {mail_edit} m ON p.mailkey = m.mailkey - LEFT JOIN {subscriptions} s ON concat('pmsg', type) = stype AND uid = %d - ORDER BY type", $account->uid); - $intervals = _subscriptions_send_intervals(); - $default_value = array(); - while ($type = db_fetch_object($result)) { - $index = $type->type; - $checkboxes[$index] = $type->description; - $key = _privatemsg_get_type_key($type->type); - if (!isset($account->$key) || $account->$key) { - $default_value[] = $index; - $select_default = $type->send_interval; - } - else { - $select_default = -1; - } - $form['subscriptions']['select'][$index] = array( - '#type' => 'select', - '#options' => $intervals, - '#default_value' => $select_default, - ); - } - $form['subscriptions']['checkbox'] = array( - '#type' => 'checkboxes', - '#options' => $checkboxes, - '#default_value' => $default_value, - ); - $form['subscriptions']['select']['#tree'] = TRUE; - $form['subscriptions']['#theme'] = 'subscriptions_form_table'; - $form['submit'] = array( - '#type' => 'submit', - '#value' => t('Save'), - ); - $form['account'] = array('#type' => 'value', '#value' => $account); - return $form; -} - -function privatemsg_subscriptions_form_submit($form_id, $form_values) { - $uid = $form_values['account']->uid; - db_query("DELETE FROM {subscriptions} WHERE stype LIKE 'pmsg%%' AND uid = %d", $uid); - foreach (array_keys(array_filter($form_values['checkbox'])) as $sid) { - subscriptions_add(0, $uid, 'pmsg'. $sid, $form_values['select'][$sid]); - } - $account = new stdClass(); - $account->uid = $uid; - $privatemsg_settings = array(); - foreach($form_values['checkbox'] as $type => $checked) { - $key = _privatemsg_get_type_key($type); - $privatemsg_settings[$key] = $checked ? 1 : 0; - // special case - type 'private-message' indicates whether a user wants to - // allow contact via direct private message at all. we need to link this to the - // privatemsg_allow preference setting, so that the "contact" tab reacts correctly - if ($type == 'private-message') { - $privatemsg_settings['privatemsg_allow'] = $checked ? 1 : 0; - } - } - $account = user_save($account, $privatemsg_settings); -} - -function privatemsg_mailkeys() { - $mailkeys = array(); - $result = db_query('SELECT type FROM {privatemsg_mail_edit}'); - while ($type = db_fetch_object($result)) { - $mailkeys['subscriptions-pmsg-'. $type->type] = 'subscriptions-pmsg-'. $type->type; - } - $mailkeys['private-message'] = 'private-message'; - return $mailkeys; -} - -function privatemsg_mail_edit_variables(&$variables, $mailkey = NULL, $sender = NULL, $recipient = NULL) { - static $stored_variables = array(); - if (isset($mailkey)) { - if (substr($mailkey, 0, 18) == 'subscriptions-pmsg') { - $variables = $stored_variables + $variables; - } - } - else { - $stored_variables = $variables; - } -}