summaryrefslogtreecommitdiffstats
path: root/tinymce.module
blob: e3e6e44969920eb497d8b199c7d2b72259191c38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
// $Id$
// A collaborative project by Matt Westgate <drupal at asitis dot org>
// and Richard Bennett <richard.b@gritechnologies.com>

/**
 * @file
 * Integrate the TinyMCE editor (http://tinymce.moxiecode.com/) into Drupal.
 */

/**
 * Implementation of hook_menu().
 *
 * Add the JavaScript file to the page head. Doing this in a menu hook is a
 * little less expensive that in *_init since we don't need to load the head for
 * Drupal-cached pages.
 *
 */
function tinymce_menu($may_cache) {
  if (!$may_cache) {
    drupal_set_html_head('<script language="javascript" type="text/javascript" src="'. drupal_get_path('module', 'tinymce') .'/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>');
  }
}

/**
 * Implementation of hook_help().
 */
function tinymce_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('The TinyMCE Javascript HTML WYSIWYG editor.');

    case 'admin/settings/tinymce#pages':
      return "node/*\nuser/*\ncomment/*";
  }
}

/**
 * Implementation of hook_perm().
 */
function tinymce_perm() {
  return array('access tinymce', 'choose own tinymce theme');
}

/**
 * Implementation of hook_settings().
 */
function tinymce_settings() {
  drupal_set_title(t('TinyMCE settings'));

  //Check if TinyMCE is installed.
  $tinymce_loc = drupal_get_path('module', 'tinymce') .'/tinymce/';
  if (!is_dir($tinymce_loc)) {
    drupal_set_message(t('Could not find the TinyMCE engine installed at <strong>%tinymce-directory</strong>. Please <a href="http://tinymce.moxiecode.com/">download TinyMCE</a>, uncompress it and copy the folder into %tinymce-path.', array('%tinymce-path' => drupal_get_path('module', 'tinymce'), '%tinymce-directory' => drupal_get_path('module', 'tinymce'). '/tinymce/')), 'error');
  }

  $group = form_radios(t('Default theme'), 'tinymce_theme', variable_get('tinymce_theme', 'simple'), _tinymce_get_themes());
  $output = form_group(t('Themes'), $group);

  $group = form_radios(t('access tinymce'), 'tinymce_all', variable_get('tinymce_all', 1), array(t('on specific pages'), t('on all textareas')));
  if (!variable_get('tinymce_all', 1)) {
    $group .= form_textarea(t('Pages'), 'tinymce_pages', variable_get('tinymce_pages', tinymce_help('admin/settings/tinymce#pages')), 40, 5, t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '<em>blog</em>' for the blog page and '<em>blog/*</em>' for every personal blog. '<em>&lt;front&gt;</em>' is the front page."));
  }
  $output .= form_group('', $group);
  return $output;
}

function tinymce_textarea($op, $name) {
  static $is_running = FALSE;
  if (!user_access('access tinymce')) return NULL;

  global $user;
  $theme_name = $user->tinymce_theme ? $user->tinymce_theme : variable_get('tinymce_theme', 'simple');
  $user_status = $user->tinymce_status != NULL ? $user->tinymce_status : TRUE;

  if ($op == 'pre' && _tinymce_page_match() && $user_status) {
    global $base_url;

    // Build a default list of TinyMCE settings.
    $init['mode'] = 'textareas';
    $init['theme'] = $theme_name;
    $init['document_base_url'] = "$base_url/";

    // Merge and overrivide user-defined TinyMCE settings.
    $init = array_merge($init, (array) theme('tinymce_theme', $init, $textarea_name, $theme_name, $is_running));
    foreach ($init as $k => $v) {
      $settings[] = $k. ' : "'. $v. '"';
    }
    $tinymce_settings = implode(",\n", $settings);

$output = <<<EOD
<script language="javascript" type="text/javascript">
  tinyMCE.init({
    $tinymce_settings
  });
</script>
EOD;

    // We only invoke TinyMCE once per request, not once per textarea. We could
    // do this check earlier in the conditional, but it's probably wise to let
    // the themed functions know what's going on.
    if (!$is_running) {
      $is_running = TRUE;
      return $output;
    }
  }
}

/**
 * Implementation of hook_user().
 */
function tinymce_user($type, &$edit, &$user, $category = NULL) {
  if ($type == 'form' && $category == 'account' && user_access('access tinymce')) {
    $user_status = $edit['tinymce_status'] != NULL ? $edit['tinymce_status'] : ($user->tinymce_status != NULL ? $user->tinymce_status : 1);
    $form = form_radios(t('Status'), 'tinymce_status', $user_status, array(t('Disabled'), t('Enabled')), t('Would you like to enable rich-text editing of your content?'));
    if ($user_status && user_access('choose own tinymce theme')) {
      $form .= form_radios(t('Default theme'), 'tinymce_theme', $edit['tinymce_theme'] ? $edit['tinymce_theme'] : variable_get('tinymce_theme', 'simple'), _tinymce_get_themes());
    }
    else {
      $form .= form_hidden('tinymce_theme', $edit['tinymce_theme'] ? $edit['tinymce_theme'] : variable_get('tinymce_theme', 'simple'));
    }
    return array(array('title' => t('TinyMCE settings'), 'data' => $form));
  }
  if ($type == 'validate') {
    return array('tinymce_theme' => $edit['tinymce_theme'], 'tinymce_status' => $edit['tinymce_status']);
  }
}

/**
 * @addtogroup themeable
 * @{
 */

/**
 * Customize a TinyMCE theme.
 *
 * @param init
 *   An array of settings TinyMCE should invoke a theme. You may override any
 *   of the TinyMCE settings. Details here:
 *
 *    http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm
 *
 * @param textarea_name
 *   The name of the textarea TinyMCE wants to enable.
 *
 * @param theme_name
 *   The default theme name to be enabled for this textarea. The sitewide
 *   default is 'simple', but the user may also override this.
 *
 * @param is_running
 *   A boolean flag that identifies id TinyMCE is currently running for this
 *   request life cycle. If it's already running then anything returned by this
 *   will be ignored. This is necessary since TinyMCE works by being invoked
 *   once per request and not once per textarea.
 */
function theme_tinymce_theme($init, $textarea_name, $theme_name, $is_running) {
  switch ($theme_name) {
    case 'advanced':
      $init['extended_valid_elements'] = 'a[href|target|name]';
      $init['plugins'] = 'table';
      $init['theme_advanced_buttons3_add_before'] = 'tablecontrols,separator';
      $init['plugins'] = 'table,emotions,iespell, print';
      $init['theme_advanced_buttons3_add'] = 'emotions,iespell,separator,print';
      return $init;
  }
}

/** @} End of addtogroup themeable */

/**
 * Grab the themes available to TinyMCE.
 *
 * TinyMCE themes control the functionality and buttons that are available to a
 * user. Themes are only looked for within the default TinyMCE theme directory.
 *
 * @return
 *   An array of theme names.
 */
function _tinymce_get_themes() {
  static $themes = array();

  if (!$themes) {
    $theme_loc = drupal_get_path('module', 'tinymce') .'/tinymce/jscripts/tiny_mce/themes/';
    if (is_dir($theme_loc) && $dh = opendir($theme_loc)) {
      while (($file = readdir($dh)) !== false) {
        if ($file != '.' && $file != '..' && is_dir($theme_loc . $file)) {
          $themes[$file] = $file;
        }
      }
      closedir($dh);
      asort($themes);
    }
  }

  return $themes;
}

/**
 * Determine if TinyMCE can render the current page.
 *
 * @return
 *   TRUE if can render, FALSE if not allowed.
 */
function _tinymce_page_match() {
  $edit = $_POST['edit'];

  //Kill TinyMCE if we're editing a textarea with PHP in it!
  if ($edit) {
    if ($edit['format'] == 2) {
      return FALSE;
    }
  }
  else {
    // PHP input formats are #2 in the filters table.
    preg_match("|^node/(\d+)(/edit)$|", $_GET['q'], $match);
    if (intval($match[1]) > 0) {
      if (db_result(db_query('SELECT format FROM {node} WHERE nid = %d AND format = 2', $match[1]))) {
        return FALSE;
      }
    }
  }

  if (variable_get('tinymce_all', 1)) {
    return TRUE;
  }
  else {
    $page_match = FALSE;
    $pages = variable_get('tinymce_pages', tinymce_help('admin/settings/tinymce#pages'));
    if ($pages) {
      $path = drupal_get_path_alias($_GET['q']);
      $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. variable_get('site_frontpage', 'node') .'\2'), preg_quote($pages, '/')) .')$/';
      $page_match = preg_match($regexp, $path);
    }
    return $page_match;
  }
}
?>