You are here

token_insert.module in Token Insert 7.2

Same filename and directory in other branches
  1. 6.2 token_insert.module

wysiwyg plugin hook.

File

token_insert.module
View source
<?php

/**
 * @file
 * wysiwyg plugin hook.
 */
function token_insert_permission() {
  return array(
    'administer token insert settings' => array(
      'title' => t('administer token insert settings'),
      'description' => t('Administer token insert settings'),
    ),
    'use token insert' => array(
      'title' => t('use token insert'),
      'description' => t('Allows user to use the token insert module'),
    ),
  );
}
function token_insert_menu() {
  $items['admin/config/content/token_insert'] = array(
    'title' => 'Token insert',
    'description' => 'Change the token users can insert.',
    'type' => MENU_NORMAL_ITEM,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'token_insert_settings_form',
    ),
    'access arguments' => array(
      'administer token insert settings',
    ),
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function token_insert_theme() {
  $info['token_insert_tree'] = array(
    'variables' => array(),
  );
  return $info;
}
function token_insert_settings_form($form_state) {
  $form = array();
  $form['general'] = array(
    '#type' => 'fieldset',
    '#title' => t('General settings'),
    '#description' => t('Configure general token insert settings'),
    '#collapsible' => FALSE,
  );
  $form['general']['token_insert_use_tokens_per_role'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use tokens per role'),
    '#default_value' => variable_get('token_insert_use_tokens_per_role', 0),
  );
  $form['general']['token_insert_max_depth'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum depth'),
    '#default_value' => variable_get('token_insert_max_depth', 1),
    '#description' => t('An integer with the maximum number of token levels to recurse'),
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
  );
  $roles['global'] = 'global';
  if (variable_get('token_insert_use_tokens_per_role', 0)) {
    $roles += user_roles(TRUE, 'use token insert');
  }
  $all_tokens = token_get_info();
  foreach ($roles as $rid => $role) {
    $title = t("Available tokens for @role", array(
      '@role' => $role,
    ));
    $description = t("Available tokens for users with the '@role' role", array(
      '@role' => $role,
    ));
    if ($rid == 'global') {
      $title = t('Globally available tokens');
      $description = t('Tokens available for every user');
    }
    $form[$rid . '_available_tokens'] = array(
      '#type' => 'fieldset',
      '#title' => $title,
      '#description' => $description,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form[$rid . '_available_tokens'][$rid . '_tokens'] = array(
      '#type' => 'vertical_tabs',
    );
    foreach ($all_tokens['tokens'] as $category => $tokens) {
      $form[$rid . '_available_tokens']['tokens'][$category] = array(
        '#type' => 'fieldset',
        '#title' => t($all_tokens['types'][$category]['name']),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#group' => $rid . '_tokens',
        '#description' => t('Select ' . $all_tokens['types'][$category]['name'] . ' tokens available for insertion.'),
      );
      $options = array();
      $defaults = array();
      foreach ($tokens as $token => $description) {
        $full_token = '[' . $category . ':' . $token . ']';
        $options[$full_token] = $category . ' : [' . $token . '] : ' . t($description['description']);
        $defaults[$full_token] = 0;
      }
      $form[$rid . '_available_tokens']['tokens'][$category]['token_insert_' . $rid . '_all_tokens_' . $category] = array(
        '#type' => 'checkbox',
        '#title' => t('All tokens'),
        '#default_value' => variable_get('token_insert_' . $rid . '_all_tokens_' . $category, FALSE),
        '#description' => t('Make all ' . $all_tokens['types'][$category]['name'] . ' tokens available for insertion.'),
      );
      $form[$rid . '_available_tokens']['tokens'][$category]['token_insert_' . $rid . '_used_tokens_' . $category] = array(
        '#type' => 'checkboxes',
        '#title' => t('Select tokens'),
        '#default_value' => variable_get('token_insert_' . $rid . '_used_tokens_' . $category, array()),
        '#options' => $options,
        '#states' => array(
          'visible' => array(
            // action to take.
            ':input[name=token_insert_' . $rid . '_all_tokens_' . $category . ']' => array(
              'checked' => FALSE,
            ),
          ),
        ),
      );
    }
  }
  $form = system_settings_form($form);
  $form['#submit'][] = 'token_insert_settings_form_submit';
  return $form;
}
function token_insert_settings_form_submit() {
  cache_clear_all('tree-rendered:token-insert-tree', 'cache_token', TRUE);
}
function token_insert_help($path, $arg) {
  if ($path == 'admin/help#token_insert') {
    $output = <<<EOT
    <h3>Description</h3>
    <p>This module allows you to insert tokens into a textarea. It supports both plain text and wysiwyg textareas. The format used for the insert is compatible with Token filter.</p>
    <p>This module contains three modules:</p>
    <ul>
      <li>Token insert UI: Allows you to select which tokens are available for the insert, by default all tokens are shown. This module doesn't have to be enabled to use the others.</li>
      <li>Token insert (text): Add a fieldset under each textarea, works for both plain text fields and wysiwyg fields.</li>
      <li>Token insert (wysiwyg): Adds an extra button to wysiwyg editors and opens a popup to select the token to insert.</li>
    </ul>
    <h3>Dependencies</h3>
    <ul>
      <li>Token</li>
    </ul>
    <h3>Recommended</h3>
    <ul>
      <li>Token filter</li>
    </ul>
    <h3>Thanks to</h3>
    <ul>
      <li>Attiks</li>
      <li>Jelle</li>
    </ul>
EOT;
    return $output;
  }
}
function theme_token_insert_tree() {
  $roles = array();
  if (variable_get('token_insert_use_tokens_per_role', 0)) {
    global $user;
    $roles = array_keys($user->roles);
  }
  $roles[] = 'global';
  sort($roles);

  // Use the token cache bin. This way, whenever token cache gets cleared, the
  // token insert tree gets cleared as well.
  $element = array(
    '#cache' => array(
      'cid' => 'tree-rendered:token-insert-tree:' . implode('-', $roles),
      'bin' => 'cache_token',
    ),
  );
  if ($cached_output = token_render_cache_get($element)) {
    return $cached_output;
  }
  module_load_include('pages.inc', 'token');
  module_load_include('inc', 'token_insert');
  $info = token_get_info();
  $options = array(
    'flat' => FALSE,
    'restricted' => FALSE,
    'depth' => variable_get('token_insert_max_depth', 1) <= 0 ? 1 : variable_get('token_insert_max_depth', 1),
  );
  $tokens = token_insert_get_tokens(TRUE);
  foreach (token_insert_get_allowed_token_types() as $token_type) {
    $row = _token_token_tree_format_row($token_type, $info['types'][$token_type], TRUE);
    unset($row['data']['value']);
    $tree = token_flatten_tree(array_intersect_key(token_build_tree($token_type, $options), $tokens));
    if ($tree) {
      $rows[] = $row;
      foreach ($tree as $token => $token_info) {
        if (!isset($token_info['parent'])) {
          $token_info['parent'] = $token_type;
        }
        $row = _token_token_tree_format_row($token, $token_info);
        unset($row['data']['value']);
        $row['data']['token']['data'] = '<a href="#" title="' . t('Insert this token into your form') . '">' . $token . '</a>';
        $rows[] = $row;
      }
    }
  }
  $element += array(
    '#theme' => 'tree_table',
    '#header' => array(
      t('Name'),
      t('Token'),
      t('Description'),
    ),
    '#rows' => $rows,
    '#attributes' => array(
      'class' => array(
        'token-tree',
        'token-insert-table',
      ),
    ),
    '#empty' => t('No tokens available'),
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'token') . '/token.js',
      ),
      'css' => array(
        drupal_get_path('module', 'token') . '/token.css',
      ),
      'library' => array(
        array(
          'token',
          'treeTable',
        ),
      ),
    ),
    '#caption' => t('Click a token to insert it into the field.'),
    '#click_insert' => FALSE,
  );
  $output = drupal_render($element);
  token_render_cache_set($output, $element);
  return $output;
}