Projet

Général

Profil

Paste
Télécharger (7,06 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / token_insert / token_insert.module @ 5721e759

1
<?php
2
/**
3
 * @file
4
 * wysiwyg plugin hook.
5
 */
6

    
7
function token_insert_permission() {
8
  return array(
9
      'administer token insert settings' => array(
10
          'title' => t('administer token insert settings'),
11
          'description' => t('Administer token insert settings')),
12
      'use token insert' => array(
13
          'title' => t('use token insert'),
14
          'description' => t('Allows user to use the token insert module')),
15
      );
16
}
17

    
18
function token_insert_menu() {
19

    
20
  $items['admin/config/content/token_insert'] = array(
21
    'title' => 'Token insert',
22
    'description' => 'Change the token users can insert.',
23
    'type' => MENU_NORMAL_ITEM,
24
    'page callback' => 'drupal_get_form',
25
    'page arguments' => array('token_insert_settings_form'),
26
    'access arguments' => array('administer token insert settings'),
27
  );
28

    
29
  return $items;
30
}
31

    
32
/**
33
 * Implements hook_theme().
34
 */
35
function token_insert_theme() {
36
  $info['token_insert_tree'] = array(
37
    'variables' => array(
38
    ),
39
  );
40

    
41
  return $info;
42
}
43

    
44
function token_insert_settings_form($form_state) {
45
  $form = array();
46
  $form['general'] = array(
47
    '#type' => 'fieldset',
48
    '#title' => t('General settings'),
49
    '#description' => t('Configure general token insert settings'),
50
    '#collapsible' => FALSE,
51
  );
52

    
53
  $form['general']['token_insert_use_tokens_per_role'] = array(
54
    '#type' => 'checkbox',
55
    '#title' => t('Use tokens per role'),
56
    '#default_value' => variable_get('token_insert_use_tokens_per_role', 0),
57
  );
58

    
59
  $form['general']['token_insert_max_depth'] = array(
60
    '#type' => 'textfield',
61
    '#title' => t('Maximum depth'),
62
    '#default_value' => variable_get('token_insert_max_depth', 1),
63
    '#description' => t('An integer with the maximum number of token levels to recurse'),
64
    '#element_validate' => array('element_validate_integer_positive'),
65
  );
66

    
67
  $roles ['global'] = 'global';
68
  if (variable_get('token_insert_use_tokens_per_role', 0)) {
69
    $roles += user_roles(TRUE, 'use token insert');
70
  }
71
  $all_tokens = token_get_info();
72

    
73
  foreach ($roles as $rid => $role) {
74
    $title = t("Available tokens for @role", array('@role' => $role));
75
    $description = t("Available tokens for users with the '@role' role", array('@role' => $role));
76
    if ($rid == 'global') {
77
      $title = t('Globally available tokens');
78
      $description = t('Tokens available for every user');
79
    }
80
    $form[$rid . '_available_tokens'] = array(
81
      '#type' => 'fieldset',
82
      '#title' => $title,
83
      '#description' => $description,
84
      '#collapsible' => TRUE,
85
      '#collapsed' => TRUE,
86
    );
87
    $form[$rid . '_available_tokens'][$rid . '_tokens'] = array(
88
      '#type' => 'vertical_tabs',
89
    );
90
    foreach ($all_tokens['tokens'] as $category => $tokens) {
91
      $form[$rid . '_available_tokens']['tokens'][$category] = array(
92
        '#type' => 'fieldset',
93
        '#title' => t($all_tokens['types'][$category]['name']),
94
        '#collapsible' => TRUE,
95
        '#collapsed' => TRUE,
96
        '#group' => $rid . '_tokens',
97
        '#description' => t('Select ' . $all_tokens['types'][$category]['name'] . ' tokens available for insertion.'),
98
      );
99

    
100
      $options = array();
101
      $defaults = array();
102

    
103
      foreach ($tokens as $token => $description) {
104
        $full_token = '[' . $category . ':' . $token . ']';
105
        $options[$full_token] = $category . ' : [' . $token . '] : ' . t($description['description']);
106
        $defaults[$full_token] = 0;
107
      }
108

    
109
      $form[$rid . '_available_tokens']['tokens'][$category]['token_insert_' . $rid . '_all_tokens_' . $category] = array(
110
        '#type' => 'checkbox',
111
        '#title' => t('All tokens'),
112
        '#default_value' => variable_get('token_insert_' . $rid . '_all_tokens_' . $category, FALSE),
113
        '#description' => t('Make all ' . $all_tokens['types'][$category]['name'] . ' tokens available for insertion.'),
114
      );
115

    
116
      $form[$rid . '_available_tokens']['tokens'][$category]['token_insert_' . $rid . '_used_tokens_' . $category] = array(
117
        '#type' => 'checkboxes',
118
        '#title' => t('Select tokens'),
119
        '#default_value' => variable_get('token_insert_' . $rid . '_used_tokens_' . $category, array()),
120
        '#options' => $options,
121
        '#states' => array(
122
          'visible' => array(   // action to take.
123
            ':input[name=token_insert_' . $rid . '_all_tokens_' . $category . ']' => array('checked' => FALSE),
124
          ),
125
        ),
126

    
127
      );
128
    }
129
  }
130

    
131
  return system_settings_form($form);
132
}
133

    
134
function token_insert_help($path, $arg) {
135
  if ($path == 'admin/help#token_insert') {
136
    $output = <<<EOT
137
    <h3>Description</h3>
138
    <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>
139
    <p>This module contains three modules:</p>
140
    <ul>
141
      <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>
142
      <li>Token insert (text): Add a fieldset under each textarea, works for both plain text fields and wysiwyg fields.</li>
143
      <li>Token insert (wysiwyg): Adds an extra button to wysiwyg editors and opens a popup to select the token to insert.</li>
144
    </ul>
145
    <h3>Dependencies</h3>
146
    <ul>
147
      <li>Token</li>
148
    </ul>
149
    <h3>Recommended</h3>
150
    <ul>
151
      <li>Token filter</li>
152
    </ul>
153
    <h3>Thanks to</h3>
154
    <ul>
155
      <li>Attiks</li>
156
      <li>Jelle</li>
157
    </ul>
158
EOT;
159
  return $output;
160
  }
161

    
162
}
163

    
164
function theme_token_insert_tree() {
165
  module_load_include('pages.inc', 'token');
166
  module_load_include('inc', 'token_insert');
167
  $info = token_get_info();
168

    
169
  $options = array(
170
    'flat' => FALSE,
171
    'restricted' => FALSE,
172
    'depth' => variable_get('token_insert_max_depth', 1) <= 0 ? 1 : variable_get('token_insert_max_depth', 1),
173
  );
174
  $tokens = token_insert_get_tokens(TRUE);
175
  foreach (token_insert_get_allowed_token_types() as $token_type) {
176
    $row = _token_token_tree_format_row($token_type, $info['types'][$token_type], TRUE);
177
    unset($row['data']['value']);
178
    $rows[] = $row;
179
    $tree = token_flatten_tree(array_intersect_key(token_build_tree($token_type, $options), $tokens));
180
    foreach ($tree as $token => $token_info) {
181
      if (!isset($token_info['parent'])) {
182
        $token_info['parent'] = $token_type;
183
      }
184
      $row = _token_token_tree_format_row($token, $token_info);
185
      unset($row['data']['value']);
186
      $rows[] = $row;
187
    }
188
  }
189
  $element = array(
190
    '#theme' => 'tree_table',
191
    '#header' => array(
192
      t('Name'),
193
      t('Token'),
194
      t('Description'),
195
    ),
196
    '#rows' => $rows,
197
    '#attributes' => array('class' => array('token-tree', 'token-insert-table')),
198
    '#empty' => t('No tokens available'),
199
    '#attached' => array(
200
      'js' => array(drupal_get_path('module', 'token') . '/token.js'),
201
      'css' => array(drupal_get_path('module', 'token') . '/token.css'),
202
      'library' => array(array('token', 'treeTable')),
203
    ),
204
    '#caption' => t('Click a token to insert it into the field.'),
205
    '#click_insert' => FALSE,
206
  );
207
  $output = drupal_render($element);
208
  return $output;
209
}