You are here

function bugherd_init in BugHerd 7

Same name and namespace in other branches
  1. 6 bugherd.module \bugherd_init()

Implements hook_init().

File

./bugherd.module, line 11
BugHerd module functions.

Code

function bugherd_init() {
  global $user;
  if (!user_access('access bugherd')) {
    return;
  }
  $disable_on_admin = variable_get('bugherd_disable_on_admin', FALSE);
  if ($disable_on_admin && path_is_admin($_GET['q'])) {
    return;
  }
  $key = variable_get('bugherd_project_key');
  if (!$key) {
    if (user_access('administer bugherd')) {
      $url = url('admin/config/development/bugherd');
      drupal_set_message(t('BugHerd project key not set. <a href="@url">Configure BugHerd</a>.', array(
        '@url' => $url,
      )), 'warning', FALSE);
    }
    return;
  }
  $key = check_plain($key);
  $metadata = array(
    'user_id' => $user->uid,
    'logged_in' => $user->uid > 0,
  );
  if ($user->uid) {
    $metadata['user_name'] = format_username($user);
  }
  else {
    $metadata['anonymous'] = TRUE;
  }

  // Allow other modules to override the default
  drupal_alter('bugherd_metadata', $metadata);
  $json = json_encode($metadata);
  $script = <<<SCRIPT
  var BugHerdConfig = { metadata: {<span class="php-variable">$json</span>} };

  (function (d, t) {
    var bh = d.createElement(t), s = d.getElementsByTagName(t)[0];
    bh.type = 'text/javascript';
    bh.src = '//www.bugherd.com/sidebarv2.js?apikey={<span class="php-variable">$key</span>}';
    s.parentNode.insertBefore(bh, s);
  })(document, 'script');

SCRIPT;
  drupal_add_js($script, array(
    'type' => 'inline',
    'scope' => 'footer',
  ));
}