You are here

function messaging_update_method_update in Messaging 6.4

Same name and namespace in other branches
  1. 6 messaging.install \messaging_update_method_update()
  2. 6.2 messaging.install \messaging_update_method_update()
  3. 6.3 messaging.install \messaging_update_method_update()
  4. 7 messaging.install \messaging_update_method_update()

Udate sending method, change for a new one

2 calls to messaging_update_method_update()
messaging_update_6002 in ./messaging.install
Update sending methods names
messaging_update_method_disable in ./messaging.install
Disable a sending method and return an alternative one

File

./messaging.install, line 196

Code

function messaging_update_method_update($old, $new) {

  // Replace some variables
  if ($old == variable_get('messaging_default_method', NULL)) {
    if ($new) {
      variable_set('messaging_default_method', $new);
    }
    else {
      variable_del('messaging_default_method');
    }
  }

  // Create new destinations if needed for users that had an old one, but only if the address type is different
  // It is possible to create new bulk destinations when we know the field and the table it comes from
  if ($new) {
    $old_type = messaging_method_info($old, 'address_type');
    $new_type = messaging_method_info($new, 'address_type');
    if ($old_type && $new_type && $old_type != $new_type && ($field = messaging_address_info($new_type, 'field_name')) && ($table = messaging_address_info($new_type, 'field_table'))) {
      $sql = "INSERT INTO {messaging_destination} (uid, type, address)";
      if ($new_type == 'user') {

        // Special case, destination is uid, it is much more straight forward
        $sql .= " SELECT d1.uid, '%s', d1.uid FROM {messaging_destination} d1";
      }
      else {
        $sql .= " SELECT d1.uid, '%s', t.{$field} FROM {messaging_destination} d1";
        $sql .= " INNER JOIN {" . $table . "} t ON d1.uid = t.uid";

        // Have destination data on the other table
      }

      // Don't have already a destination of this type ($new_type)
      $sql .= " LEFT JOIN {messaging_destination} d2 ON d1.uid = d2.uid AND d2.type = '%s'";
      $sql .= " WHERE d1.uid > 0 AND d1.type = '%s' AND d2.uid IS NULL";
      db_query($sql, $new_type, $new_type, $old_type);
      if ($created = db_affected_rows()) {
        drupal_set_message(t("Created @count user destinations for new sending method.", array(
          '@count' => $created,
        )));
      }
    }
  }

  // Inform all modules this method is being replaced
  module_invoke_all('messaging', 'method update', $old, $new);

  // Delete all stored messages for this method
  messaging_store()
    ->delete_multiple(array(
    'method' => $old,
  ));
}