* Backported some of the components from CVS: documentation and cleaned up profile...
[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 function tinymce_menu($may_cache) {
15 $items = array();
16 if ($may_cache) {
17 $items[] = array('path' => 'admin/settings/tinymce', 'title' => t('tinymce'),
18 'callback' => 'tinymce_admin',
19 'access' => user_access('administer tinymce'));
20 }
21 return $items;
22 }
23
24 /**
25 * Implementation of hook_help().
26 */
27 function tinymce_help($section) {
28 switch ($section) {
29 case 'admin/modules#description':
30 return t('The TinyMCE Javascript HTML WYSIWYG editor.');
31
32 case 'admin/settings/tinymce#pages':
33 return "node/*\nuser/*\ncomment/*";
34
35 case 'admin/settings/tinymce':
36 return t('<p>TinyMCE adds what-you-see-is-what-you-get (WYSIWYG) html editing to textareas. Profiles can be defined based on user roles. TinyMCE profile can define which pages receive this TinyMCE capability, what buttons or themes are enabled for the editor, how the editor is displayed, and a few other editor functions. The default profile setting uses the "simple" TinyMCE theme which just shows the most minimal buttons set (bold, italic, underline, etc), but many other settings are available by switching to "advanced". And by default, textareas on affected pages will be automatically swapped out with the rich text editor. Users may disable the editor for any textarea without reloading the page. This setting can be reversed so that pages load with conventional textarea form fields. A link below each textarea allows TinyMCE to be turned on or off "on the fly".</p><p>Be aware that things can get confusing if a user is a member of roles defined in multiple profiles. In this case, the user will receive the profile for role with lowest role id they belong to. Did I mention it was confusing?</p>');
37 }
38 }
39
40 /**
41 * Implementation of hook_perm().
42 */
43 function tinymce_perm() {
44 return array('administer tinymce', 'access tinymce');
45 }
46
47 /**
48 * Implementation of hook_img_assist_head().
49 */
50 function tinymce_img_assist_head() {
51 global $base_url;
52 // The tinymce docs say to include tiny_mce_popup.js, but this was killing IE!
53 $popup_path = $base_url .'/'. drupal_get_path('module', 'tinymce'). '/tinymce/jscripts/tiny_mce/tiny_mce_popup.js';
54 $img_assist_prop = $base_url .'/'. drupal_get_path('module', 'img_assist'). '/properties.js';
55 $clean_url = variable_get('clean_url', 0);
56 $img_template = variable_get('img_assist_img_html', img_assist_help('img_assist/template'));
57 $img_template = str_replace("\r\n", "\n", $img_template);
58 $img_template = str_replace("\n", '\n', addslashes($img_template));
59
60 $output = <<<EOD
61 <script language="javascript" src="$img_assist_prop"></script>
62 <script language="javascript">
63 var clean_url = $clean_url;
64 var img_template = "$img_template";
65
66 function insertImage(form) {
67 if (window.opener) {
68 form['edit[thumbpath]'].value = window.opener.tinyMCE.convertURL(form['edit[thumbpath]'].value);
69 form['edit[filepath]'].value = window.opener.tinyMCE.convertURL(form['edit[filepath]'].value);
70 form['edit[nodePath]'].value = window.opener.tinyMCE.convertURL(form['edit[nodePath]'].value);
71
72 var img = generate_image_tag(form, 'html');
73 //img = img.replace(/\\r|\\n|\\t/g, '');
74
75 window.opener.tinyMCE.execInstanceCommand(myTextarea.name, 'mceInsertContent', false, img, true);
76 }
77 }
78 </script>
79 EOD;
80
81 return $output;
82 }
83
84 /**
85 * Implementation of hook_img_assist_on_submit().
86 */
87 function tinymce_img_assist_on_submit() {
88 return 'parent.insertImage(this.form);';
89 }
90
91 /**
92 * Implementation of hook_textarea().
93 */
94 function tinymce_textarea($op, $textarea_name) {
95 static $is_running = FALSE;
96 if (!user_access('access tinymce')) return NULL;
97
98 global $user;
99 global $base_url;
100 static $profile_name;
101
102 // Since tinymce_config() makes a db hit, only call it when we're pretty sure
103 // we're gonna render tinymce.
104 $valid_so_far = FALSE;
105 if ($op == 'post') {
106 if (!$profile_name) {
107 $profile_name = db_result(db_query('SELECT s.name FROM {tinymce_settings} s INNER JOIN {tinymce_role} r ON r.name = s.name WHERE r.rid IN (%s)', implode(',', array_keys($user->roles))));
108 }
109 $profile = tinymce_profile_load($profile_name);
110 $init = tinymce_config($profile);
111 $init['elements'] = 'edit['. $textarea_name .']';
112 $valid_so_far = TRUE;
113 }
114
115 if ($valid_so_far && _tinymce_page_match($profile)) {
116 // Merge user-defined TinyMCE settings.
117 $init = (array) theme('tinymce_theme', $init, $textarea_name, $init['theme'], $is_running);
118 $settings = array();
119 foreach ($init as $k => $v) {
120 if (strtolower($v) != 'true' && strtolower($v) != 'false') {
121 $v = '"'. $v. '"';
122 }
123 $settings[] = $k. ' : '. $v;
124 }
125 $tinymce_settings = implode(",\n ", $settings);
126
127 if (function_exists('img_assist_help')) {
128 $img_assist_js_on = $base_url .'/'. url('img_assist/add&editor=tinymce') .'&textarea=';
129 $img_assist_js_off = $base_url .'/'. url('img_assist/add') .'&textarea=';
130 $img_assist_on = $base_url .'/'. url('img_assist/add&editor=tinymce') .'&textarea=edit['. $textarea_name .']';
131 $img_assist_off = $base_url .'/'. url('img_assist/add') .'&textarea=edit['. $textarea_name .']';
132 }
133
134 $enable = t('enable rich-text');
135 $disable = t('disable rich-text');
136
137 $tinymce_invoke = <<<EOD
138 <script language="javascript" type="text/javascript">
139 tinyMCE.init({
140 $tinymce_settings
141 });
142 </script>
143 EOD;
144
145 $js_toggle = <<<EOD
146 <script language="javascript" type="text/javascript">
147 function mceToggle(id, linkid) {
148 element = document.getElementById(id);
149 link = document.getElementById(linkid);
150 img_assist = document.getElementById('img_assist-link-'+ id);
151
152 if (tinyMCE.getEditorId(element.name) == null) {
153 tinyMCE.addMCEControl(element, element.name);
154 element.togg = 'on';
155 link.innerHTML = '$disable';
156 link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
157 if (img_assist)
158 img_assist.href = "$img_assist_js_on"+ element.name;
159 link.blur();
160 }
161 else {
162 tinyMCE.removeMCEControl(tinyMCE.getEditorId(element.name));
163 element.togg = 'off';
164 link.innerHTML = '$enable';
165 link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
166 if (img_assist)
167 img_assist.href = "$img_assist_js_off"+ element.name;
168 link.blur();
169 }
170 }
171 </script>
172 EOD;
173
174 $status = isset($user->tinymce_status) ? $user->tinymce_status : variable_get('tinymce_default_state', 0);
175 $link_text = $status == 1 ? $disable : $enable;
176 $no_wysiwyg = t('Your current web browser does not support WYSIWYG editing.');
177 $wysiwyg_link = <<<EOD
178 <script language="javascript" type="text/javascript">
179 img_assist = document.getElementById('img_assist-link-edit-$textarea_name');
180 if (img_assist) {
181 img_assist.href = tinyMCE.getEditorId('edit-$textarea_name') == null ? "$img_assist_on" : "$img_assist_off";
182 }
183 if (typeof(document.execCommand) == 'undefined') {
184 img_assist.href = "$img_assist_off";
185 document.write('<div style="font-size:x-small">$no_wysiwyg</div>');
186 }
187 else {
188 document.write("<div><a href=\"javascript:mceToggle('edit-$textarea_name', 'wysiwyg4$textarea_name');\" id=\"wysiwyg4$textarea_name\">$link_text</div></a>");
189 }
190 </script>
191 EOD;
192
193 // We only load the TinyMCE js file once per request
194 if (!$is_running && !empty($init)) {
195 $is_running = TRUE;
196 // For some crazy reason IE will only load this JS file if the absolute reference is given to it.
197 drupal_set_html_head('<script language="javascript" type="text/javascript" src="'. $base_url .'/'. drupal_get_path('module', 'tinymce') .'/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>');
198 drupal_set_html_head($js_toggle);
199 // We have to do this becuase of some unfocused CSS in certain themes. See http://drupal.org/node/18879 for details
200 drupal_set_html_head('<style type="text/css" media="all">.mceEditor img { display: inline; }</style>');
201 }
202 // Load a TinyMCE init for each textarea.
203 drupal_set_html_head($tinymce_invoke);
204
205 return $wysiwyg_link;
206 }
207 }
208
209 /**
210 * Implementation of hook_user().
211 */
212 function tinymce_user($type, &$edit, &$user, $category = NULL) {
213 if ($type == 'form' && $category == 'account' && user_access('access tinymce')) {
214 $user_status = $edit['tinymce_status'] != NULL ? $edit['tinymce_status'] : ($user->tinymce_status != NULL ? $user->tinymce_status : variable_get('tinymce_default_state', 0));
215 $form = form_radios(t('Default status'), 'tinymce_status', $user_status, array(t('Off'), t('On')), t('Should rich-text editing be enabled or disabled by default in textarea fields?'));
216 return array(array('title' => t('TinyMCE settings'), 'data' => $form));
217 }
218 if ($type == 'validate') {
219 return array('tinymce_status' => $edit['tinymce_status']);
220 }
221 }
222
223
224 /**
225 * @addtogroup themeable
226 * @{
227 */
228
229 /**
230 * Customize a TinyMCE theme.
231 *
232 * @param init
233 * An array of settings TinyMCE should invoke a theme. You may override any
234 * of the TinyMCE settings. Details here:
235 *
236 * http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm
237 *
238 * @param textarea_name
239 * The name of the textarea TinyMCE wants to enable.
240 *
241 * @param theme_name
242 * The default tinymce theme name to be enabled for this textarea. The
243 * sitewide default is 'simple', but the user may also override this.
244 *
245 * @param is_running
246 * A boolean flag that identifies id TinyMCE is currently running for this
247 * request life cycle. It can be ignored.
248 */
249 function theme_tinymce_theme($init, $textarea_name, $theme_name, $is_running) {
250 switch ($textarea_name) {
251 // Disable tinymce for these textareas
252 case 'log':
253 case 'img_assist_pages':
254 case 'caption':
255 unset($init);
256 break;
257
258 // Force the 'simple' theme for some of the smaller textareas.
259 case 'signature':
260 case 'site_mission':
261 case 'site_footer':
262 case 'settings][access_pages':
263 $init['theme'] = 'simple';
264 unset($init['theme_advanced_toolbar_location']);
265 unset($init['theme_advanced_toolbar_align']);
266 unset($init['theme_advanced_path_location']);
267 unset($init['theme_advanced_blockformats']);
268 unset($init['theme_advanced_styles']);
269 break;
270 }
271
272 // Add some extra features when using the advanced theme.
273 switch ($theme_name) {
274 case 'advanced':
275 $init['extended_valid_elements'] = 'a[href|target|name|title|onclick]';
276 $init['theme_advanced_buttons3_add_before'] = 'tablecontrols,separator';
277 $init['plugins'] = file_exists(drupal_get_path('module', 'tinymce'). '/tinymce/jscripts/tiny_mce/plugins/drupalimage') ? 'drupalimage,table,emotions,print' : 'table,emotions,print';
278 $init['theme_advanced_buttons3_add'] = 'drupalimage,emotions,separator,print';
279 break;
280 }
281
282 // Always return $init; !!
283 return $init;
284 }
285
286 /** @} End of addtogroup themeable */
287
288 /**
289 * Grab the themes available to TinyMCE.
290 *
291 * TinyMCE themes control the functionality and buttons that are available to a
292 * user. Themes are only looked for within the default TinyMCE theme directory.
293 *
294 * @return
295 * An array of theme names.
296 */
297 function _tinymce_get_themes() {
298 static $themes = array();
299
300 if (!$themes) {
301 $theme_loc = drupal_get_path('module', 'tinymce') .'/tinymce/jscripts/tiny_mce/themes/';
302 if (is_dir($theme_loc) && $dh = opendir($theme_loc)) {
303 while (($file = readdir($dh)) !== false) {
304 if (!in_array($file, array('.', '..', 'CVS')) && is_dir($theme_loc . $file)) {
305 $themes[$file] = $file;
306 }
307 }
308 closedir($dh);
309 asort($themes);
310 }
311 }
312
313 return $themes;
314 }
315
316 /********************************************************************
317 * Module Functions :: Public
318 ********************************************************************/
319
320 /**
321 * Controller for tinymce administrative settings.
322 */
323 function tinymce_admin($arg = NULL) {
324 $edit = $_POST['edit'];
325 $op = $_POST['op'];
326
327 $op = $arg && !$op ? $arg : $op;
328
329 switch ($op) {
330 case 'add':
331 $breadcrumb[] = array('path' => 'admin', 'title' => t('administer'));
332 $breadcrumb[] = array('path' => 'admin/settings/tinymce', 'title' => t('tinymce'));
333 $breadcrumb[] = array('path' => 'admin/settings/tinymce/add', 'title' => t('Add new tinymce profile'));
334 menu_set_location($breadcrumb);
335 $output = tinymce_profile_form($edit);
336 break;
337
338 case 'edit':
339 drupal_set_title(t('Edit tinymce profile'));
340 $output = tinymce_profile_form(tinymce_profile_load(urldecode(arg(4))));
341 break;
342
343 case 'delete':
344 tinymce_profile_delete(urldecode(arg(4)));
345 drupal_set_message(t('Deleted profile'));
346 drupal_goto('admin/settings/tinymce');
347 break;
348
349 case t('Create profile');
350 case t('Update profile');
351 if (tinymce_profile_validate($edit)) {
352 tinymce_profile_save($edit);
353 $edit['old_name'] ? drupal_set_message(t('Your tinymce profile has been updated.')) : drupal_set_message(t('Your tinymce profile has been created.'));
354 drupal_goto('admin/settings/tinymce');
355 }
356 else {
357 $output = tinymce_profile_form($edit);
358 }
359 break;
360
361 case t('Save settings'):
362 variable_set('tinymce_default_state', $edit['tinymce_default_state']);
363 drupal_set_message(t('Settings updated'));
364 drupal_goto('admin/settings/tinymce');
365 break;
366
367 default:
368 drupal_set_title(t('TinyMCE settings (%revision)', array('%revision' => '$Revision$')));
369 //Check if TinyMCE is installed.
370 $tinymce_loc = drupal_get_path('module', 'tinymce') .'/tinymce/';
371 if (!is_dir($tinymce_loc)) {
372 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' => $tinymce_loc)), 'error');
373 }
374 $output = tinymce_profile_overview();
375 }
376
377 print theme('page', $output);
378 }
379
380 /**
381 * Return an array of initial tinymce config options from the current role.
382 */
383 function tinymce_config($profile) {
384 global $base_url;
385 global $user;
386
387 $settings = $profile->settings;
388
389 // Build a default list of TinyMCE settings.
390
391 // Is tinymce on by default?
392 $status = isset($user->tinymce_status) ? $user->tinymce_status : variable_get('tinymce_default_state', 0);
393 $init['mode'] = $status == 1 ? 'exact' : 'none';
394 $init['theme'] = $settings['theme'] ? $settings['theme'] : 'simple';
395 $init['document_base_url'] = "$base_url/";
396
397 $init['verify_html'] = $settings['verify_html'] ? $settings['verify_html'] : 'false';
398 $init['auto_cleanup_word'] = $settings['msword'] ? $settings['msword'] : 'false';
399 $init['preformatted'] = $settings['preformatted'] ? $settings['preformatted'] : 'false';
400 $init['force_br_newlines'] = $settings['force_br'] ? $settings['force_br'] : 'false';
401 $init['force_p_newlines'] = $settings['force_p'] ? $settings['force_p'] : 'false';
402 if ($init['theme'] == 'advanced') {
403 $init['theme_advanced_toolbar_location'] = $settings['toolbar_loc'] ? $settings['toolbar_loc'] : 'bottom';
404 $init['theme_advanced_toolbar_align'] = $settings['toolbar_align'] ? $settings['toolbar_align'] : 'left';
405 $init['theme_advanced_path_location'] = $settings['path_loc'] ? $settings['path_loc'] : 'none';
406 $init['theme_advanced_blockformats'] = $settings['block_formats'] ? $settings['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6';
407 }
408
409 if ($edit['css_classes']) $init['theme_advanced_styles'] = $settings['css_classes'];
410 if ($settings['width']) $init['width'] = $settings['width'];
411 if ($settings['height']) $init['height'] = $settings['height'];
412
413 if ($settings['css_setting'] == 'theme') {
414 $css = drupal_get_path('theme', init_theme()) . '/style.css';
415 if (file_exists($css)) {
416 $init['content_css'] = $base_url .'/'. $css;
417 }
418 else if ($settings['css_setting'] == 'self') {
419 $init['content_css'] = $edit['css_path'];
420 }
421 }
422
423 return $init;
424 }
425
426 /**
427 * Remove a profile from the database.
428 */
429 function tinymce_profile_delete($name) {
430 db_query("DELETE FROM {tinymce_settings} WHERE name = '%s'", $name);
431 db_query("DELETE FROM {tinymce_role} WHERE name = '%s'", $name);
432 }
433
434 /**
435 * Return an HTML form for profile configuration.
436 */
437 function tinymce_profile_form($edit) {
438 $edit = array2object($edit);
439
440 // Only display the roles that currently don't have a tinymce profile. One
441 // profile per role.
442 $orig_roles = user_roles();
443 $roles = $orig_roles;
444 if (arg(3) == 'add') {
445 $result = db_query('SELECT DISTINCT(rid) FROM {tinymce_role}');
446 while ($data = db_fetch_object($result)) {
447 unset($roles[$data->rid]);
448 }
449 if (!$roles) {
450 drupal_set_message(t('You will not be allowed to create a new profile since all user roles have already been assigned profiles. First remove an existing tinymce profile from at least one role in order to create a new profile.'), 'error');
451 }
452 else if (count($orig_roles) != count($roles)) {
453 drupal_set_message(t('Not all user roles are shown since they already have tinymce profiles. You must first unassign profiles in order to add them to a new one.'));
454 }
455 $btn = t('Create profile');
456 }
457 else {
458 $output = form_hidden('old_name', $edit->name);
459 $btn = t('Update profile');
460 }
461
462 $group = form_textfield(t('Profile name'), 'name', $edit->name, 40, 128, t('Enter an unique name for this profile. This name is only visible in the tinymce administration page.'), NULL, TRUE);
463 $group .= form_checkboxes(t('Roles allowed to use this profile'), 'rids', array_keys((array) $edit->rids), $roles, t('Select at least one role.'), NULL, TRUE);
464 $group .= form_radios(t('Theme'), 'settings][theme', $edit->settings['theme'] ? $edit->settings['theme'] : 'simple', _tinymce_get_themes(), t('Select the tinymce theme. The <em>advanced</em> theme will display advanced options below.'), FALSE ,array('onclick'=>'tinymceThemeOptions(this.value);'));
465 $group .= form_radios(t('Make tinymce visible on'), 'settings][access', $edit->settings['access'], array(t('specific pages'), t('all textareas')));
466 $group .= form_textarea(t('Specific pages'), 'settings][access_pages', $edit->settings['access_pages'] ? $edit->settings['access_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."));
467 $output .= form_group(t('Basic setup'), $group);
468
469 $output .= t('<h3>Options</h3>');
470
471 $group = '';
472 $group .= form_select(t('Toolbar location'), 'settings][toolbar_loc', $edit->settings['toolbar_loc'], array('bottom' => 'bottom', 'top' => 'top'), t('Show toolbar at the top or bottom of the editor area?'));
473 $group .= form_select(t('Toolbar alignment'), 'settings][toolbar_align', $edit->settings['toolbar_align'], array('center' => 'center', 'left' => 'left', 'right' => 'right'), t('Align tool icons left, center, or right within the toolbar.'));
474 $group .= form_select(t('Path location'), 'settings][path_loc', $edit->settings['path_loc'], array('none' => 'none', 'top' => 'top', 'bottom' => 'bottom'), t('Path to html elements (i.e. "body>table>tr>td"). Show at top, bottom, or not at all.'));
475 $group .= form_textfield(t('Block formats'), 'settings][block_formats', $edit->settings['block_formats'] ? $edit->settings['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6', 40, 250, t('Comma separated list of HTML block formats. You can only remove elements, not add.'));
476 $dsp = $edit->settings['theme'] == 'advanced' ? 'block' : 'none';
477 $output .= '<div id="advancedThemeSettings" style="display:'. $dsp .';">'. form_group(('Advanced theme options'), $group).'</div>';
478
479 $group = form_textfield(t('Editor width'), 'settings][width', $edit->settings['width'], 3, 5, t('Set width of editor. Leave blank to use size of textarea being replaced.'));
480 $group .= form_textfield(t('Editor height'), 'settings][height', $edit->settings['height'], 3, 5, t('Set height of editor. Leave blank to use size of textarea being replaced.'));
481 $output .= form_group(t('Editor display'), $group);
482
483 $group = form_select(t('Auto cleanup Word'), 'settings][msword', $edit->settings['msword'], array('true' => 'true', 'false' => 'false'), t('Automatically cleanup MS Office/Word HTML will be executed automatically on paste operations. (Only works in Internet Explorer)'));
484 $group .= form_select(t('Verify HTML'), 'settings][verify_html', $edit->settings['verify_html'], array('true' => 'true', 'false' => 'false'), t('Should the HTML contents be verified or not? Verifying will strip &lt;head&gt tags, so choose false if you will be editing full page HTML.'));
485 $group .= form_select(t('Preformatted'), 'settings][preformatted', $edit->settings['preformatted'], array('false' => 'false', 'true' => 'true'), t('If this option is set to true, the editor will insert TAB characters on tab and preserve other whitespace characters just like a PRE HTML element does.'));
486 $output .= form_group(t('On save'), $group);
487
488 $group = form_select(t('Editor CSS'), 'settings][css_setting', $edit->settings['css_setting'] ? $edit->settings['css_setting'] : 'theme', array('theme' => 'use theme css', 'self' => 'define css', 'none' => 'tinyMCE default'), t('Defines the CSS to be used in the editor area.<br />use theme css - get css from current Drupal theme.<br/>define css - enter path for css file below.<br />tinyMCE default - uses default CSS from editor.'));
489 $group .= form_textfield(t('CSS path'), 'settings][css_path', $edit->settings['css_path'], 40, 255, t('Enter path to CSS file (example: "/css/editor.css"). Select "define css" above.'));
490 $group .= form_textfield(t('CSS classes'), 'settings][css_classes', $edit->settings['css_classes'], 40, 255, t('Adds CSS classes to the "styles" droplist. Format is "&lt;title&gt;=&lt;class&gt;;"<br/> Example: "Header 1=header1;Header 2=header2;Header 3=header3;"<br />Leave blank to automatically import list of CSS classes from style sheet.'));
491 $output .= form_group(t('CSS'), $group);
492
493 $group .= form_select(t('Force BR new lines'), 'settings][force_br', $edit->settings['force_br'] ? $edit->settings['force_br'] : 'false', array('true' => 'true', 'false' => 'false'), t('Use BR tags for new lines rather than P.'));
494 $group .= form_select(t('Force P new lines'), 'settings][force_p', $edit->settings['force_p'] ? $edit->settings['force_p'] : 'true', array('true' => 'true', 'false' => 'false'), t('When enabled, Mozilla/Firefox will generate P elements on Enter/Return key and BR elements on Shift+Enter/Return..'));
495 $output .= form_group(t('Formatting'), $group);
496 $output .= form_submit($btn);
497
498 $js = <<<EOD
499 <script type='text/javascript'>
500 function tinymceThemeOptions(theme) {
501 document.getElementById('advancedThemeSettings').style.display = theme == 'advanced' ? 'block' : 'none';
502 }
503 </script>
504 EOD;
505 drupal_set_html_head($js);
506
507 return form($output);
508 }
509
510 /**
511 * Load all profiles.
512 */
513 function tinymce_profile_load($name = '') {
514 static $profiles = array();
515
516 if (!$profiles) {
517 $roles = user_roles();
518 $result = db_query('SELECT * FROM {tinymce_settings}');
519 while ($data = db_fetch_object($result)) {
520 $data->settings = unserialize($data->settings);
521 $result2 = db_query("SELECT rid FROM {tinymce_role} WHERE name = '%s'", $data->name);
522 $role = array();
523 while ($r = db_fetch_object($result2)) {
524 $role[$r->rid] = $roles[$r->rid];
525 }
526 $data->rids = $role;
527
528 $profiles[$data->name] = $data;
529 }
530 }
531
532 return ($name ? $profiles[$name] : $profiles);
533 }
534
535 /**
536 * Controller for tinymce profiles.
537 */
538 function tinymce_profile_overview() {
539 $output = '';
540 $output .= t('<p><a href="%create-profile-url">Create new profile</a></p>', array('%create-profile-url' => url('admin/settings/tinymce/add')));
541
542 $profiles = tinymce_profile_load();
543 if ($profiles) {
544 $roles = user_roles();
545 $header = array(t('Profile'), t('Roles'), t('Operations'));
546 foreach ($profiles as $p) {
547 $rows[] = array(array('data' => $p->name, 'valign' => 'top'), array('data' => implode("<br />\n", $p->rids)), array('data' => l(t('edit'), 'admin/settings/tinymce/edit/'. urlencode($p->name)) . ' '. l(t('delete'), 'admin/settings/tinymce/delete/'. urlencode($p->name)), 'valign' => 'top'));
548 }
549 $output .= theme('table', $header, $rows). '<p>&nbsp;</p>';
550 }
551 else {
552 $output .= t('<p>No profiles found.</p>');
553 }
554
555 $group = form_radios(t('Default tinymce state'), 'tinymce_default_state', variable_get('tinymce_default_state', 0), array(t('Off'), t('On')), t('Should tinymce be enabled or disabled by default when it\'s first loaded from a textarea? Note: The user may override this setting in their profile.'));
556 $output .= form_group(t('Default settings'), $group);
557 $output .= form_submit(t('Save settings'));
558
559 return form($output);
560 }
561
562 /**
563 * Save a profile to the database.
564 */
565 function tinymce_profile_save($edit) {
566 db_query("DELETE FROM {tinymce_settings} WHERE name = '%s' or name = '%s'", $edit['name'], $edit['old_name']);
567 db_query("DELETE FROM {tinymce_role} WHERE name = '%s' or name = '%s'", $edit['name'], $edit['old_name']);
568 db_query("INSERT INTO {tinymce_settings} (name, settings) VALUES ('%s', '%s')", $edit['name'], serialize($edit['settings']));
569 foreach ($edit['rids'] as $rid) {
570 db_query("INSERT INTO {tinymce_role} (name, rid) VALUES ('%s', %d)", $edit['name'], $rid);
571 }
572 }
573
574 /**
575 * Profile validation.
576 */
577 function tinymce_profile_validate($edit) {
578 $errors = array();
579
580 if (!$edit['name']) {
581 $errors['name'] = t('You must give a profile name.');
582 }
583
584 if (!$edit['rids']) {
585 $errors['rids'] = t('You must select at least one role.');
586 }
587
588 foreach ($errors as $name => $message) {
589 form_set_error($name, $message);
590 }
591
592 return count($errors) == 0;
593 }
594
595 /********************************************************************
596 * Module Functions :: Private
597 ********************************************************************/
598
599 /**
600 * Determine if TinyMCE has permission to be used on the current page.
601 *
602 * @return
603 * TRUE if can render, FALSE if not allowed.
604 */
605 function _tinymce_page_match($edit) {
606 //Kill TinyMCE if we're editing a textarea with PHP in it!
607 if ($_POST['edit']['format'] == 2) {
608 return FALSE;
609 }
610 else {
611 // PHP input formats are #2 in the filters table.
612 preg_match("|^node/(\d+)(/edit)$|", $_GET['q'], $match);
613 if (intval($match[1]) > 0) {
614 if (db_result(db_query('SELECT format FROM {node} WHERE nid = %d AND format = 2', $match[1]))) {
615 return FALSE;
616 }
617 }
618 }
619
620 if ($edit->settings['access'] == 1) {
621 return TRUE;
622 }
623 else {
624 $page_match = FALSE;
625 $pages = $edit->settings['access_pages'] ? $edit->settings['access_pages'] : tinymce_help('admin/settings/tinymce#pages');
626 if ($pages) {
627 $path = drupal_get_path_alias($_GET['q']);
628 $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. variable_get('site_frontpage', 'node') .'\2'), preg_quote($pages, '/')) .')$/';
629 $page_match = preg_match($regexp, $path);
630 }
631 return $page_match;
632 }
633 }
634 ?>