You are here

function _mail_edit_load in Mail Editor 7

Loads and returns a template.

Parameters

string $id:

string|object $language:

bool $force_always:

Return value

array|null|bool Returns FALSE if we get an invalid $id or $language, NULL if we have no template yet, or a keyed array with the default and/or customized templates.

3 calls to _mail_edit_load()
mail_edit_load in ./mail_edit.module
Loads and returns a template.
mail_edit_template_form in ./mail_edit.admin.inc
Form builder function to prepare the template edit form.
_mail_edit_mail_alter in ./mail_edit.alter.inc
Implements hook_mail_alter().

File

./mail_edit.alter.inc, line 20
Implementation of hook_mail_alter() for the Mail Editor module.

Code

function _mail_edit_load($id, $language, $force_always = FALSE) {
  if (is_string($language)) {
    $languages = language_list();
    $language = isset($languages[$language]) ? $languages[$language] : NULL;
  }
  if (!is_object($language)) {

    // If the language is unknown, we bail out.
    return FALSE;
  }

  // Find out if this mail id is in our registry.
  if (!cache_get('mail_edit_registry')) {
    _mail_edit_module_load_include('admin.inc');
    _mail_edit_key_registry_rebuild();
  }
  $query = db_select('mail_edit_registry', 'mer')
    ->fields('mer')
    ->condition('mer.id', $id);
  if (!($template_type = $query
    ->execute()
    ->fetchAssoc())) {

    // If we don't have any registry record we shouldn't attempt to alter it.
    return FALSE;
  }
  $module = $template_type['module'];
  $mailkey = $template_type['mailkey'];

  // Load mail template if we have altered it.
  $query = db_select('mail_edit', 'me')
    ->fields('me')
    ->condition('me.id', $id)
    ->condition('me.language', $language->language);
  $template = $query
    ->execute()
    ->fetchAssoc();
  $default_template = module_invoke($module, 'mail_edit_text', $mailkey, $language);
  if ($template) {
    if ($default_template) {
      $template += $default_template;
    }
  }
  else {
    if (!$default_template) {
      return NULL;
    }
    if (empty($default_template['always']) && !$force_always) {
      return NULL;
    }
    $template = $default_template + array(
      'default' => TRUE,
    );
  }
  $template += array(
    'language' => $language,
    'type' => $template_type,
  );
  return $template;
}