You are here

function messaging_template_edit_form in Messaging 6.3

Edit message formats

1 string reference to 'messaging_template_edit_form'
messaging_template_admin_edit in messaging_template/messaging_template.admin.inc
Message groups edit page

File

messaging_template/messaging_template.admin.inc, line 169
Messaging Framework - Admin UI

Code

function messaging_template_edit_form($form_state, $name) {
  global $language;
  drupal_add_js(drupal_get_path('module', 'messaging_template') . '/messaging_template.js', 'module');
  drupal_add_js(array(
    'messagingTemplateEdit' => array(
      'lookup' => url('admin/messaging/template/edit'),
    ),
  ), 'setting');
  $info = messaging_template_info($name);
  $form['description'] = array(
    '#type' => 'item',
    '#title' => $info['title'],
    '#value' => messaging_template_admin_description($name),
  );
  $form['name'] = array(
    '#type' => 'value',
    '#value' => $name,
  );
  $form['language'] = array(
    '#type' => 'value',
    '#value' => $language->language,
  );
  $form['template'] = array(
    //'#title' => t('Template'),

    //'#type' => 'fieldset',
    '#tree' => TRUE,
  );
  foreach (messaging_template_method_list() as $method => $methodname) {
    $form['template'][$method] = array(
      '#type' => 'fieldset',
      '#title' => $methodname,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );

    // Get defaults for this template from modules with fallbacks
    $defaults = messaging_template_get_defaults($name, $language, TRUE);
    foreach (messaging_template_get_keys($name) as $key => $keyname) {

      // First, get template without fallback
      $template = messaging_template_get_template($name, $method, $key, $language, FALSE, FALSE);
      if ($type_fallback = messaging_template_fallback($name)) {
        $fallback = messaging_template_get_template($type_fallback, $method, $key, $language);
      }
      if ($method_default = messaging_template_method_default($method)) {
        $default = messaging_template_get_template($name, $method_default, $key, $language);
      }
      else {

        // If not default for 'default' method, try fallback
        if (isset($defaults[$key])) {
          $default = $defaults[$key];
        }
        else {
        }
      }

      // Text will be NULL if no template nor fallback
      if (!$template) {
        $text = $default ? $default->template : '';
        $status = MESSAGING_TEMPLATE_DEFAULT;
      }
      elseif ($template->options == MESSAGING_TEMPLATE_FALLBACK) {
        $text = $fallback ? $fallback->template : FALSE;
        $status = MESSAGING_TEMPLATE_FALLBACK;
      }
      else {
        $text = $template->template;
        $status = MESSAGING_TEMPLATE_OVERRIDE;
      }

      // Set template values to form elements
      $form['template'][$method][$key] = array(
        '#type' => 'fieldset',
        '#title' => $keyname,
        //'#theme' => 'messaging_template_text_part',
        '#attributes' => array(
          'class' => 'messaging-template-part',
        ),
      );
      $form['template'][$method][$key]['tptid'] = array(
        '#type' => 'value',
        '#value' => $template ? $template->tptid : NULL,
      );
      $form['template'][$method][$key]['template'] = array(
        //'#title' => $keyname,
        '#type' => 'textarea',
        '#default_value' => $text ? $text : '',
        // Adjust size to actual number of lines
        '#rows' => $text ? count(explode("\n", $text)) : 2,
        '#disabled' => $status != MESSAGING_TEMPLATE_OVERRIDE,
        '#attributes' => array(
          'class' => 'messaging-template-part-text',
        ),
      );

      // Store fallback and default texts if any
      $form['template'][$method][$key]['fallback'] = array(
        '#type' => 'hidden',
        '#value' => $fallback ? $fallback->template : '',
      );
      $form['template'][$method][$key]['default'] = array(
        '#type' => 'hidden',
        '#value' => $default ? $default->template : '',
      );

      // Compute default options
      $options[MESSAGING_TEMPLATE_OVERRIDE] = t('Override');
      $options[MESSAGING_TEMPLATE_DEFAULT] = t('Default');
      $options[MESSAGING_TEMPLATE_FALLBACK] = messaging_template_fallback_description($name, $method, $fallback);
      $form['template'][$method][$key]['options'] = array(
        //'#title' => $keyname,
        '#type' => 'radios',
        '#options' => $options,
        '#default_value' => $status,
        '#attributes' => array(
          'class' => 'template-select-override',
        ),
      );
    }
  }

  // Tokens for text replacement
  if ($tokens = messaging_template_token_get_list($name)) {
    $form['tokens'] = array(
      '#type' => 'item',
      '#title' => t('Available Tokens'),
      '#value' => messaging_template_token_description($tokens),
    );

    //$form['tokens'] += messaging_template_token_description($tokens);
  }
  $form['submit']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save template'),
  );
  $form['submit']['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset to defaults'),
  );
  return $form;
}