3 // A collaborative project by Matt Westgate <drupal at asitis dot org>
4 // and Richard Bennett <richard.b@gritechnologies.com>
8 * Integrate the TinyMCE editor (http://tinymce.moxiecode.com/) into Drupal.
12 * Implementation of hook_menu().
14 * Add the JavaScript file to the page head. Doing this in a menu hook is a
15 * little less expensive that in *_init since we don't need to load the head for
16 * Drupal-cached pages.
19 function tinymce_menu($may_cache) {
21 drupal_set_html_head('<script language="javascript" type="text/javascript" src="'.
drupal_get_path('module', 'tinymce') .
'/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>');
26 * Implementation of hook_help().
28 function tinymce_help($section) {
30 case
'admin/modules#description':
31 return t('The TinyMCE Javascript HTML WYSIWYG editor.');
33 case
'admin/settings/tinymce#pages':
34 return "node/*\nuser/*\ncomment/*";
39 * Implementation of hook_perm().
41 function tinymce_perm() {
42 return array('use tinymce');
46 * Implementation of hook_settings().
48 function tinymce_settings() {
49 drupal_set_title(t('TinyMCE settings'));
51 //Check if TinyMCE is installed.
52 $tinymce_loc = drupal_get_path('module', 'tinymce') .
'/tinymce/';
53 if (!is_dir($tinymce_loc)) {
54 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');
57 $group = form_radios(t('Default theme'), 'tinymce_theme', variable_get('tinymce_theme', 'simple'), _tinymce_get_themes());
58 $output = form_group('', $group);
60 $group = form_radios(t('Use TinyMCE'), 'tinymce_all', variable_get('tinymce_all', 1), array(t('on specific pages'), t('on all textareas')));
61 if (!variable_get('tinymce_all', 1)) {
62 $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><front></em>' is the front page."));
64 $output .
= form_group('', $group);
68 function tinymce_textarea($op, $name) {
69 if (!user_access('use tinymce')) return NULL
;
72 $theme_name = $user->tinymce_theme ?
$user->tinymce_theme
: variable_get('tinymce_theme', 'simple');
73 $user_status = $user->tinymce_status
!= NULL ?
$user->tinymce_status
: TRUE
;
75 if ($op == 'pre' && _tinymce_page_match() && $user_status) {
78 // Build a default list of TinyMCE settings.
79 $init['mode'] = 'textareas';
80 $init['theme'] = $theme_name;
81 $init['document_base_url'] = "$base_url/";
83 // Merge and overrivide user-defined TinyMCE settings.
84 $init = array_merge($init, (array) theme('tinymce_theme', $init, $textarea_name, $theme_name));
85 foreach ($init as
$k => $v) {
86 $settings[] = $k.
' : "'.
$v.
'"';
88 $tinymce_settings = implode(",\n", $settings);
91 <script language
="javascript" type
="text/javascript">
103 * Implementation of hook_user().
105 function tinymce_user($type, &$edit, &$user, $category = NULL
) {
106 if ($type == 'form' && $category == 'account' && user_access('use tinymce')) {
107 $user_status = $edit['tinymce_status'] != NULL ?
$edit['tinymce_status'] : ($user->tinymce_status
!= NULL ?
$user->tinymce_status
: 1);
108 $form = form_radios(t('Use TinyMCE'), 'tinymce_status', $user_status, array(t('Disabled'), t('Enabled')));
110 $form .
= form_radios(t('Default theme'), 'tinymce_theme', $edit['tinymce_theme'] ?
$edit['tinymce_theme'] : variable_get('tinymce_theme', 'simple'), _tinymce_get_themes());
113 $form .
= form_hidden('tinymce_theme', $edit['tinymce_theme']);
115 return array(array('title' => t('TinyMCE settings'), 'data' => $form));
117 if ($type == 'validate') {
118 return array('tinymce_theme' => $edit['tinymce_theme'], 'tinymce_status' => $edit['tinymce_status']);
123 * @addtogroup themeable
128 * Customize a TinyMCE theme.
131 * An array of settings TinyMCE should invoke a theme. You may override any
132 * of the TinyMCE settings. Details here:
134 * http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm
136 * @param textarea_name
137 * The name of the textarea TinyMCE wants to enable.
140 * The default theme name to be enabled for this textarea. The sitewide
141 * default is 'simple', but the user may also override this.
143 function theme_tinymce_theme($init, $textarea_name, $theme_name) {
144 switch ($theme_name) {
146 $init['extended_valid_elements'] = 'a[href|target|name]';
147 $init['plugins'] = 'table';
148 $init['theme_advanced_buttons3_add_before'] = 'tablecontrols,separator';
149 $init['plugins'] = 'table,emotions,iespell, print';
150 $init['theme_advanced_buttons3_add'] = 'emotions,iespell,separator,print';
155 /** @} End of addtogroup themeable */
158 * Grab the themes available to TinyMCE.
160 * TinyMCE themes control the functionality and buttons that are available to a
161 * user. Themes are only looked for within the default TinyMCE theme directory.
164 * An array of theme names.
166 function _tinymce_get_themes() {
167 static
$themes = array();
170 $theme_loc = drupal_get_path('module', 'tinymce') .
'/tinymce/jscripts/tiny_mce/themes/';
171 if (is_dir($theme_loc) && $dh = opendir($theme_loc)) {
172 while (($file = readdir($dh)) !== false
) {
173 if ($file != '.' && $file != '..' && is_dir($theme_loc .
$file)) {
174 $themes[$file] = $file;
186 * Determine if TinyMCE can render the current page.
189 * TRUE if can render, FALSE if not allowed.
191 function _tinymce_page_match() {
192 $edit = $_POST['edit'];
194 //Kill TinyMCE if we're editing a textarea with PHP in it!
196 if ($edit['format'] == 2) {
201 // PHP input formats are #2 in the filters table.
202 preg_match("|^node/(\d+)(/edit)$|", $_GET['q'], $match);
203 if (intval($match[1]) > 0) {
204 if (db_result(db_query('SELECT format FROM {node} WHERE nid = %d AND format = 2', $match[1]))) {
210 if (variable_get('tinymce_all', 1)) {
215 $pages = variable_get('tinymce_pages', tinymce_help('admin/settings/tinymce#pages'));
217 $path = drupal_get_path_alias($_GET['q']);
218 $regexp = '/^('.
preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'.
variable_get('site_frontpage', 'node') .
'\2'), preg_quote($pages, '/')) .
')$/';
219 $page_match = preg_match($regexp, $path);