You are here

function mailsystem_update_7300 in Mail System 7.3

Remove dynamically generated mailsystem classes and convert configuration.

File

./mailsystem.install, line 49
Contains install and update functions for mailsytem.

Code

function mailsystem_update_7300() {

  // In previous versions, mailsystem used to dynamically generate helper
  // classes when two distinct mailsystem classes were used for formatting
  // and delivery of email. Those generated classes are located under
  // files/mailsystem and follow a common naming scheme:
  // class1__class2.mail.inc.
  //
  // In order to rebuild the old configuration, we retrieve the names of the
  // involved classes by parsing the filename of the generated classes. We use
  // the derived class names for building up the settings for the newly
  // introduced delegator class.
  // Check if a local mailsystem directory really exists.
  if (!drupal_realpath(file_build_uri('mailsystem'))) {
    return t('Converted !count dynamically generated mailsystem classes', array(
      '!count' => 0,
    ));
  }
  $class_dir = drupal_realpath(file_build_uri('mailsystem')) . '/';
  $class_dir = substr($class_dir, strlen(DRUPAL_ROOT) + 1);
  $genclasses = db_select('registry', 'r')
    ->fields('r', array(
    'name',
    'filename',
  ))
    ->condition('filename', db_like($class_dir) . '%', 'LIKE')
    ->execute()
    ->fetchAllKeyed();
  $delegate_settings = array();
  $fallback_classes = array();
  foreach ($genclasses as $classname => $filename) {
    $classes = explode('__', $classname);
    if (count($classes) == 2) {
      $delegate_settings[$classname] = array(
        'format' => $classes[0],
        'mail' => $classes[1],
      );
    }
    else {
      $fallback_classes[$classname] = $classname;
      watchdog('Mail System', 'Cannot update generated mail system class %class. Custom mail system settings using this class are set to the default mail system.', array(
        '%class' => $classname,
      ), WATCHDOG_WARNING);
    }
  }

  // Now each generated class in the mail_system variable is replaced by
  // MailsystemDelegateMailSystem class and the settings for it are written
  // into the appropriate variable.
  $mail_system = mailsystem_get();
  foreach ($mail_system as $id => $class) {
    if (isset($delegate_settings[$class])) {
      $mail_system[$id] = $delegate_settings[$class];
    }
    elseif (isset($fallback_classes[$class])) {
      watchdog('Mail System', 'Setting mail system for key %key to the default mail system.', array(
        '%key' => $id,
      ), WATCHDOG_WARNING);
      $mail_system[$id] = mailsystem_default_value();
    }
  }
  mailsystem_set($mail_system);
  if (!empty($genclasses)) {

    // Remove the generated classes from the registry.
    db_delete('registry')
      ->condition('name', array_keys($genclasses))
      ->execute();

    // Finally remove all the generated class files from the files directory.
    foreach ($genclasses as $classname => $filename) {
      file_unmanaged_delete($filename);
    }
  }
  return t('Converted !count dynamically generated mailsystem classes', array(
    '!count' => count($delegate_settings),
  ));
}