You are here

heartbeat.admin.inc in Heartbeat 6.2

Same filename and directory in other branches
  1. 6.4 heartbeat.admin.inc
  2. 6.3 heartbeat.admin.inc

File

heartbeat.admin.inc
View source
<?php

// by Jochen Stals - ONE-agency - www.one-agency.be

/**
 * Function to maintain and administer heartbeat settings
 *
 * @return settingsform
 */
function heartbeat_admin_settings() {
  $form['heartbeat_enabled'] = array(
    '#title' => t('Do you want to enable the heatbeat?'),
    '#type' => 'checkbox',
    '#size' => 5,
    '#default_value' => variable_get('heartbeat_enabled', 1),
  );
  return system_settings_form($form);
}

/**
 * Overview list of  heartbeat messages
 * This page must be viewed to make the messages 
 * appear in the database after a module is installed
 * as well as make them translatable 
 */
function heartbeat_messages_overview() {
  global $base_url;

  // Clear the rules cache to avoid problems
  rules_clear_cache();
  $intro = '';

  // Invoke a hook function of contributed modules
  // so that their messages are known in the database
  $info = heartbeat_gather_messages();

  // Fetch the heartbeat_message objects
  $messages = heartbeat_messages('all', true, true);
  if (!empty($info)) {
    drupal_set_message(t('New messages were added to heartbeat.'));
  }
  if (count($messages) <= 0) {
    return t('Sorry, no heartbeat messages yet');
  }
  foreach ($messages as $message) {

    // Additional things to do if messages need to be translatable
    if (module_exists('locale')) {
      $languages = locale_language_list();

      // Pretend we are showing the strings to translate (display none)
      $message->description .= '<div style="display: none;">';
      foreach ($languages as $lang => $human_language) {
        $message->description .= $human_language . ': ' . t($message->message, array(), $lang) . '<br />';
        $message->description .= $human_language . ': ' . t($message->message_concat, array(), $lang) . '<br />';
      }
      $message->description .= '</div>';

      // Show the admin user that there are things that need translation
      $report = array();
      foreach ($languages as $lang => $human_language) {
        if ($lang != 'en') {

          // Look into the messages
          if (t($message->message, array(), $lang) == t($message->message, array(), 'en')) {
            $label = t('translate message in @human_language', array(
              '@human_language' => strip_tags($human_language),
            ));
            $options = array(
              'query' => 'op=Search&string=' . str_replace(" ", "+", $message->message),
            );
            $report[] = l($label, 'admin/build/translate/search', $options);
          }

          // Look into the message_concat groupings
          if (t($message->message_concat, array(), $lang) == t($message->message_concat, array(), 'en')) {
            $label = t('translate grouping message in @human_language', array(
              '@human_language' => strip_tags($human_language),
            ));
            $options = array(
              'query' => 'op=Search&string=' . str_replace(" ", "+", $message->message_concat),
            );
            $report[] = l($label, 'admin/build/translate/search', $options);
          }

          // Look into the variables to detect message parts that need translation
          if (eregi("#", $message->variables_string)) {
            $report[] = '<small>' . t('In at least one of the messages a part must be translated separately') . '</small>';
          }
        }
      }

      // Add a report of todo translations to the list
      if ($report != array()) {
        $message->description .= '<br />' . implode(', ', $report);
      }
    }
    $rows[] = array(
      $message->event,
      $message->description,
      $message->karma_index,
      l(t('edit'), $base_url . "/admin/settings/heartbeat/messages/" . $message->event),
    );
  }
  $headers = array(
    t('Event'),
    t('Description'),
    t('Karma index'),
    t('Operations'),
  );
  $intro .= '<h2>Modules that describe heartbeat messages.</h2>';
  $intro .= '<p>The messages that go with this event are passed to the t-function when parsed to view.';
  $intro .= '</p>';
  if (module_exists('locale')) {
    $intro .= '<p>';
    $intro .= 'This means that once they are viewed, they are available for you to translate.';
    $intro .= 'Altering these messages will clear the rules cache and the altered message ';
    $intro .= 'will need translation if you are dealing with a multilingual site. The links show only the untranslated messages.';
    $intro .= '</p>';
  }
  return t($intro) . theme('table', $headers, $rows);
}

/**
 * Function to maintain and administer heartbeat messages
 *
 * @return settingsform
 */
function heartbeat_messages_edit(&$form_state, $event = '', $edit = array()) {
  $form = array();
  $form[$event] = array(
    '#type' => 'fieldset',
    '#title' => t($event),
    '#collapsible' => true,
    '#collapsed' => FALSE,
    '#prefix' => '<p><strong>You can use the token replacements in your message.</strong></p>',
  );
  $result = db_query("SELECT * FROM {heartbeat_messages} WHERE event = '%s' LIMIT 1", $event);
  $message = db_fetch_object($result);
  $variables = heartbeat_decode_message_variables($message->variables);

  // the replacement of @ from # is only needed to view them like that.
  // The actual implementation needs the # for partial message translations
  $form[$event]['message'] = array(
    '#type' => 'textarea',
    '#title' => t($message->description),
    '#cols' => 10,
    '#rows' => 1,
    '#default_value' => empty($edit['message']) ? $message->message : $edit['message'],
    '#prefix' => '<p>' . t('<strong>Token replacements</strong>: <br />') . str_replace("#", "@", implode(', ', array_keys($variables))) . '.</p>',
  );
  $form[$event]['message_concat'] = array(
    '#type' => 'textarea',
    '#cols' => 10,
    '#rows' => 1,
    '#default_value' => empty($edit['message_concat']) ? $message->message_concat : $edit['message_concat'],
  );
  $form[$event]['karma_index'] = array(
    '#type' => 'textfield',
    '#title' => t('karma index'),
    '#required' => TRUE,
    '#default_value' => empty($edit['karma_index']) ? $message->karma_index : $edit['karma_index'],
  );
  $form[$event]['event'] = array(
    '#type' => 'hidden',
    '#default_value' => empty($edit['event']) ? $event : $edit['event'],
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  if (isset($edit['event'])) {
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
    $form['event'] = array(
      '#type' => 'value',
      '#value' => $event,
    );
  }
  return $form;
}

/**
 * Accept the form submission heartbeat messages.
 */
function heartbeat_messages_edit_submit($form, &$form_state) {
  if ($form_state['clicked_button']['#value'] == t('Save')) {
    db_query("UPDATE {heartbeat_messages} SET message ='%s', message_concat ='%s', karma_index = %d \n   WHERE event = '%s'", $form_state['values']['message'], $form_state['values']['message_concat'], $form_state['values']['karma_index'], $form_state['values']['event']);
  }

  // Clear the rules cache to avoid problems
  rules_clear_cache();

  //$form_state['redirect'] = 'admin/settings/heartbeat/messages';
  drupal_set_message('Settings updated');
  return;
}

Functions

Namesort descending Description
heartbeat_admin_settings Function to maintain and administer heartbeat settings
heartbeat_messages_edit Function to maintain and administer heartbeat messages
heartbeat_messages_edit_submit Accept the form submission heartbeat messages.
heartbeat_messages_overview Overview list of heartbeat messages This page must be viewed to make the messages appear in the database after a module is installed as well as make them translatable