* Check if the TinyMCE engine is installed and prompt with instructions if it isn't.
[project/tinymce.git] / tinymce.module
1 <?php
2 // $Id$
3 // A collaborative project by Matt Westgate <drupal at asitis dot org>
4 // and Richard Bennett <richard.b@gritechnologies.com>
5
6 /**
7 * @file
8 * Integrate the TinyMCE editor (http://tinymce.moxiecode.com/) into Drupal.
9 */
10
11 /**
12 * Implementation of hook_menu().
13 *
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.
17 *
18 */
19 function tinymce_menu($may_cache) {
20 if (!$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>');
22 }
23 }
24
25 /**
26 * Implementation of hook_help().
27 */
28 function tinymce_help($section) {
29 switch ($section) {
30 case 'admin/modules#description':
31 return t('The TinyMCE Javascript HTML WYSIWYG editor.');
32
33 case 'admin/settings/tinymce#pages':
34 return "node/*\nuser/*\ncomment/*";
35 }
36 }
37
38 /**
39 * Implementation of hook_perm().
40 */
41 function tinymce_perm() {
42 return array('use tinymce');
43 }
44
45 /**
46 * Implementation of hook_settings().
47 */
48 function tinymce_settings() {
49 drupal_set_title(t('TinyMCE settings'));
50
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');
55 }
56
57 $group = form_radios(t('Default theme'), 'tinymce_theme', variable_get('tinymce_theme', 'simple'), _tinymce_get_themes());
58 $output = form_group('', $group);
59
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>&lt;front&gt;</em>' is the front page."));
63 }
64 $output .= form_group('', $group);
65 return $output;
66 }
67
68 function tinymce_textarea($op, $name) {
69 if (!user_access('use tinymce')) return NULL;
70
71 global $user;
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;
74
75 if ($op == 'pre' && _tinymce_page_match() && $user_status) {
76 global $base_url;
77
78 // Build a default list of TinyMCE settings.
79 $init['mode'] = 'textareas';
80 $init['theme'] = $theme_name;
81 $init['document_base_url'] = "$base_url/";
82
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. '"';
87 }
88 $tinymce_settings = implode(",\n", $settings);
89
90 $output = <<<EOD
91 <script language="javascript" type="text/javascript">
92 tinyMCE.init({
93 $tinymce_settings
94 });
95 </script>
96 EOD;
97
98 return $output;
99 }
100 }
101
102 /**
103 * Implementation of hook_user().
104 */
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')));
109 if ($user_status) {
110 $form .= form_radios(t('Default theme'), 'tinymce_theme', $edit['tinymce_theme'] ? $edit['tinymce_theme'] : variable_get('tinymce_theme', 'simple'), _tinymce_get_themes());
111 }
112 else {
113 $form .= form_hidden('tinymce_theme', $edit['tinymce_theme']);
114 }
115 return array(array('title' => t('TinyMCE settings'), 'data' => $form));
116 }
117 if ($type == 'validate') {
118 return array('tinymce_theme' => $edit['tinymce_theme'], 'tinymce_status' => $edit['tinymce_status']);
119 }
120 }
121
122 /**
123 * @addtogroup themeable
124 * @{
125 */
126
127 /**
128 * Customize a TinyMCE theme.
129 *
130 * @param init
131 * An array of settings TinyMCE should invoke a theme. You may override any
132 * of the TinyMCE settings. Details here:
133 *
134 * http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm
135 *
136 * @param textarea_name
137 * The name of the textarea TinyMCE wants to enable.
138 *
139 * @param theme_name
140 * The default theme name to be enabled for this textarea. The sitewide
141 * default is 'simple', but the user may also override this.
142 */
143 function theme_tinymce_theme($init, $textarea_name, $theme_name) {
144 switch ($theme_name) {
145 case 'advanced':
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';
151 return $init;
152 }
153 }
154
155 /** @} End of addtogroup themeable */
156
157 /**
158 * Grab the themes available to TinyMCE.
159 *
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.
162 *
163 * @return
164 * An array of theme names.
165 */
166 function _tinymce_get_themes() {
167 static $themes = array();
168
169 if (!$themes) {
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;
175 }
176 }
177 closedir($dh);
178 asort($themes);
179 }
180 }
181
182 return $themes;
183 }
184
185 /**
186 * Determine if TinyMCE can render the current page.
187 *
188 * @return
189 * TRUE if can render, FALSE if not allowed.
190 */
191 function _tinymce_page_match() {
192 $edit = $_POST['edit'];
193
194 //Kill TinyMCE if we're editing a textarea with PHP in it!
195 if ($edit) {
196 if ($edit['format'] == 2) {
197 return FALSE;
198 }
199 }
200 else {
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]))) {
205 return FALSE;
206 }
207 }
208 }
209
210 if (variable_get('tinymce_all', 1)) {
211 return TRUE;
212 }
213 else {
214 $page_match = FALSE;
215 $pages = variable_get('tinymce_pages', tinymce_help('admin/settings/tinymce#pages'));
216 if ($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);
220 }
221 return $page_match;
222 }
223 }
224 ?>