3 // A collaborative project by Matt Westgate <drupal at asitis dot org>,
4 // Richard Bennett <richard.b@ at ritechnologies dot com> and Jeff Robbins <robbins at jjeff dot com>
8 * Integrate the TinyMCE editor (http://tinymce.moxiecode.com/) into Drupal.
12 * Implementation of hook_menu().
14 function tinymce_menu($may_cache) {
17 $items[] = array('path' => 'admin/settings/tinymce', 'title' => t('TinyMCE'),
18 'callback' => 'tinymce_admin',
19 'description' => t('Configure the rich editor.'),
20 'access' => user_access('administer tinymce'));
26 * Implementation of hook_help().
28 function tinymce_help($section) {
30 case
'admin/settings/tinymce#pages':
31 return "node/*\nuser/*\ncomment/*";
32 case
'admin/settings/tinymce':
33 case
'admin/help#tinymce' :
34 return t('<p style="font-size:x-small">$Revision$ $Date$</p>' .
35 '<p>TinyMCE adds what-you-see-is-what-you-get (WYSIWYG) html editing to textareas. This editor can be enabled/disabled without reloading the page by clicking a link below each textarea.</p>
36 <p>Profiles can be defined based on user roles. A 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.</p>
37 <p>Lastly, only users with the <code>access tinymce</code> <a href="!url">permission</a> will be able to use TinyMCE.</p>', array('!url' => url('admin/user/access'))
43 * Implementation of hook_perm().
45 function tinymce_perm() {
46 $array = array('administer tinymce', 'access tinymce');
47 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
49 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/imagemanager/')) {
50 $array[] = 'access tinymce imagemanager';
52 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/filemanager/')) {
53 $array[] = 'access tinymce filemanager';
60 * Implementation of hook_elements().
62 function tinymce_elements() {
65 if (user_access('access tinymce')) {
66 // Let TinyMCE potentially process each textarea.
67 $type['textarea'] = array('#process' => array('tinymce_process_textarea' => array()));
74 * Attach tinymce to a textarea
76 function tinymce_process_textarea($element) {
77 static
$is_running = FALSE
;
81 //$element is an array of attributes for the textarea but there is no just 'name' value, so we extract this from the #id field
82 $textarea_name = substr($element['#id'], strpos($element['#id'], '-') + 1);
84 // Since tinymce_config() makes a db hit, only call it when we're pretty sure
85 // we're gonna render tinymce.
87 $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
))));
92 $profile = tinymce_profile_load($profile_name);
93 $init = tinymce_config($profile);
94 $init['elements'] = 'edit-'.
$textarea_name;
96 if (_tinymce_page_match($profile)) {
97 // Merge user-defined TinyMCE settings.
98 $init = (array) theme('tinymce_theme', $init, $textarea_name, $init['theme'], $is_running);
100 // If $init array is empty no need to execute rest of code since there are no textareas to theme with TinyMCE
101 if (count($init) < 1) {
106 foreach ($init as
$k => $v) {
107 $v = is_array($v) ?
implode(',', $v) : $v;
108 // Don't wrap the JS init in quotes for boolean values or functions.
109 if (strtolower($v) != 'true' && strtolower($v) != 'false' && $v[0] != '{') {
112 $settings[] = $k.
' : '.
$v;
114 $tinymce_settings = implode(",\n ", $settings);
116 $enable = t('enable rich-text');
117 $disable = t('disable rich-text');
119 $tinymce_invoke = <<<EOD
127 $tinymce_gz_invoke = <<<EOD
137 function mceToggle(id
, linkid
) {
138 element
= document.
getElementById(id
);
139 link = document.
getElementById(linkid
);
140 img_assist
= document.
getElementById('img_assist-link-'+ id
);
142 if (tinyMCE.
getEditorId(element.id
) == null
) {
143 tinyMCE.
addMCEControl(element
, element.id
);
145 link.innerHTML
= '$disable';
146 link.href
= "javascript:mceToggle('" +id
+ "', '" +linkid
+ "');";
148 img_assist.innerHTML
= '';
152 tinyMCE.
removeMCEControl(tinyMCE.
getEditorId(element.id
));
153 element.togg
= 'off';
154 link.innerHTML
= '$enable';
155 link.href
= "javascript:mceToggle('" +id
+ "', '" +linkid
+ "');";
157 img_assist.innerHTML
= img_assist_default_link
;
164 $status = tinymce_user_get_status($user, $profile);
166 // note we test for string == true because we save our settings as strings
167 $link_text = $status == 'true' ?
$disable : $enable;
168 $img_assist_link = ($status == 'true') ?
'yes' : 'no';
169 $no_wysiwyg = t('Your current web browser does not support WYSIWYG editing.');
170 $wysiwyg_link = <<<EOD
171 <script type
="text/javascript">
172 img_assist
= document.
getElementById('img_assist-link-edit-$textarea_name');
174 var img_assist_default_link
= img_assist.innerHTML
;
175 if ('$img_assist_link' == 'yes') {
176 img_assist.innerHTML
= tinyMCE.
getEditorId('edit-$textarea_name') == null ?
'' : img_assist_default_link
;
179 img_assist.innerHTML
= tinyMCE.
getEditorId('edit-$textarea_name') == null ? img_assist_default_link
: '';
182 if (typeof(document.execCommand
) == 'undefined') {
183 img_assist.innerHTML
= img_assist_default_link
;
184 document.
write('<div style="font-size:x-small">$no_wysiwyg</div>');
187 document.
write("<div><a href=\"javascript:mceToggle('edit-$textarea_name', 'wysiwyg4$textarea_name');\" id=\"wysiwyg4$textarea_name\">$link_text</a></div>");
192 // We only load the TinyMCE js file once per request
195 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
197 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/imagemanager/') && user_access('access tinymce imagemanager') ) {
198 // if tinymce imagemanager is installed
199 drupal_add_js($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/imagemanager/jscripts/mcimagemanager.js');
202 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/filemanager/') && user_access('access tinymce filemanager') ) {
203 // if tinymce filemanager is installed
204 drupal_add_js($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/filemanager/jscripts/mcfilemanager.js');
207 // TinyMCE Compressor 1.0.9 and greater
208 if (file_exists($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/tiny_mce_gzip.js')) {
209 drupal_add_js($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/tiny_mce_gzip.js');
210 drupal_add_js($tinymce_gz_invoke, 'inline');
212 // TinyMCE Compressor (versions < 1.0.9)
213 elseif (file_exists($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php')) {
214 drupal_add_js($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php');
217 // For some crazy reason IE will only load this JS file if the absolute reference is given to it.
218 drupal_add_js($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/tiny_mce.js');
220 drupal_add_js($js_toggle, 'inline');
221 // We have to do this becuase of some unfocused CSS in certain themes. See http://drupal.org/node/18879 for details
222 drupal_set_html_head('<style type="text/css" media="all">.mceEditor img { display: inline; }</style>');
224 // Load a TinyMCE init for each textarea.
225 if ($init) drupal_add_js($tinymce_invoke, 'inline');
227 //settings are saved as strings, not booleans
228 if ($profile->settings
['show_toggle'] == 'true') {
229 // Make sure to append to #suffix so it isn't completely overwritten
230 $element['#suffix'] .
= $wysiwyg_link;
232 // Set resizable to false to avoid drupal.js resizable function from taking control of the textarea
233 $element['#resizable'] = FALSE
;
240 * Implementation of hook_user().
242 function tinymce_user($type, &$edit, &$user, $category = NULL
) {
243 if ($type == 'form' && $category == 'account' && user_access('access tinymce')) {
244 $profile = tinymce_user_get_profile($user);
246 // because the settings are saved as strings we need to test for the string 'true'
247 if ($profile->settings
['user_choose'] == 'true') {
248 $form['tinymce'] = array(
249 '#type' => 'fieldset',
250 '#title' => t('TinyMCE rich-text settings'),
252 '#collapsible' => TRUE
,
256 $form['tinymce']['tinymce_status'] = array(
258 '#title' => t('Default state'),
259 '#default_value' => isset($user->tinymce_status
) ?
$user->tinymce_status
: (isset($profile->settings
['default']) ?
$profile->settings
['default'] : 'false'),
260 '#options' => array('false' => t('disabled'), 'true' => t('enabled')),
261 '#description' => t('Should rich-text editing be enabled or disabled by default in textarea fields?')
264 return array('tinymce' => $form);
267 if ($type == 'validate') {
268 return array('tinymce_status' => $edit['tinymce_status']);
273 * @addtogroup themeable
278 * Customize a TinyMCE theme.
281 * An array of settings TinyMCE should invoke a theme. You may override any
282 * of the TinyMCE settings. Details here:
284 * http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm
286 * @param textarea_name
287 * The name of the textarea TinyMCE wants to enable.
290 * The default tinymce theme name to be enabled for this textarea. The
291 * sitewide default is 'simple', but the user may also override this.
294 * A boolean flag that identifies id TinyMCE is currently running for this
295 * request life cycle. It can be ignored.
297 function theme_tinymce_theme($init, $textarea_name, $theme_name, $is_running) {
298 switch ($textarea_name) {
299 // Disable tinymce for these textareas
300 case
'log': // book and page log
301 case
'img_assist_pages':
302 case
'caption': // signature
304 case
'access_pages': //TinyMCE profile settings.
305 case
'user_mail_welcome_body': // user config settings
306 case
'user_mail_approval_body': // user config settings
307 case
'user_mail_pass_body': // user config settings
308 case
'synonyms': // taxonomy terms
309 case
'description': // taxonomy terms
313 // Force the 'simple' theme for some of the smaller textareas.
317 case
'site_offline_message':
319 case
'user_registration_help':
320 case
'user_picture_guidelines':
321 $init['theme'] = 'simple';
322 foreach ($init as
$k => $v) {
323 if (strstr($k, 'theme_advanced_')) unset($init[$k]);
328 /* Example, add some extra features when using the advanced theme.
330 // If $init is available, we can extend it
332 switch ($theme_name) {
334 $init['extended_valid_elements'] = array('a[href|target|name|title|onclick]');
341 // Always return $init
345 /** @} End of addtogroup themeable */
348 * Grab the themes available to TinyMCE.
350 * TinyMCE themes control the functionality and buttons that are available to a
351 * user. Themes are only looked for within the default TinyMCE theme directory.
353 * NOTE: This function is not used in this release. We are only using advanced theme.
356 * An array of theme names.
358 function _tinymce_get_themes() {
359 static
$themes = array();
362 $theme_loc = drupal_get_path('module', 'tinymce') .
'/tinymce/jscripts/tiny_mce/themes/';
363 if (is_dir($theme_loc) && $dh = opendir($theme_loc)) {
364 while (($file = readdir($dh)) !== false
) {
365 if (!in_array($file, array('.', '..', 'CVS')) && is_dir($theme_loc .
$file)) {
366 $themes[$file] = $file;
378 * Return plugin metadata from the plugin registry.
380 * We also scrape each plugin's *.js file for the human friendly name and help
381 * text URL of each plugin.
384 * An array for each plugin.
386 function _tinymce_get_buttons($skip_metadata = TRUE
) {
387 include_once(drupal_get_path('module', 'tinymce').
'/plugin_reg.php');
388 $plugins = _tinymce_plugins();
390 if ($skip_metadata == FALSE
&& is_array($plugins)) {
391 foreach ($plugins as
$name => $plugin) {
392 $file = drupal_get_path('module', 'tinymce').
'/tinymce/jscripts/tiny_mce/plugins/'.
$name .
'/editor_plugin_src.js';
393 // Grab the plugin metadata by scanning the *.js file.
394 if (file_exists($file)) {
395 $lines = file($file);
396 $has_longname = FALSE
;
397 $has_infourl = FALSE
;
398 foreach ($lines as
$line) {
399 if ($has_longname && $has_infourl) break;
400 if (strstr($line, 'longname')) {
401 $start = strpos($line, "'") + 1;
402 $end = strrpos($line, "'") - $start;
403 $metadata[$name]['longname'] = substr($line, $start, $end);
404 $has_longname = TRUE
;
406 elseif (strstr($line, 'infourl')) {
407 $start = strpos($line, "'") + 1;
408 $end = strrpos($line, "'") - $start;
409 $metadata[$name]['infourl'] = substr($line, $start, $end);
415 // Find out the buttons a plugin has.
416 foreach ($plugin as
$k => $v) {
417 if (strstr($k, 'theme_advanced_buttons')) {
418 $metadata[$name]['buttons'] = array_merge((array) $metadata[$name]['buttons'], $plugin[$k]);
427 /********************************************************************
428 * Module Functions :: Public
429 ********************************************************************/
432 * Controller for tinymce administrative settings.
434 function tinymce_admin($arg = NULL
) {
439 $op = $arg && !$op ?
$arg : $op;
443 $breadcrumb[] = array('path' => 'admin', 'title' => t('administer'));
444 $breadcrumb[] = array('path' => 'admin/settings/tinymce', 'title' => t('tinymce'));
445 $breadcrumb[] = array('path' => 'admin/settings/tinymce/add', 'title' => t('Add new TinyMCE profile'));
446 menu_set_location($breadcrumb);
447 $output = tinymce_profile_form($edit);
451 drupal_set_title(t('Edit tinymce profile'));
452 $output = tinymce_profile_form(tinymce_profile_load(urldecode(arg(4))));
456 tinymce_profile_delete(urldecode(arg(4)));
457 drupal_set_message(t('Deleted profile'));
458 drupal_goto('admin/settings/tinymce');
461 case
t('Create profile');
462 case
t('Update profile');
463 if (tinymce_profile_validate($edit)) {
464 tinymce_profile_save($edit);
465 $edit['old_name'] ?
drupal_set_message(t('Your TinyMCE profile has been updated.')) : drupal_set_message(t('Your TinyMCE profile has been created.'));
466 drupal_goto('admin/settings/tinymce');
469 $output = tinymce_profile_form($edit);
474 drupal_set_title(t('TinyMCE settings'));
475 //Check if TinyMCE is installed.
476 $tinymce_loc = drupal_get_path('module', 'tinymce') .
'/tinymce/';
477 if (!is_dir($tinymce_loc)) {
478 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');
480 $output = tinymce_profile_overview();
487 * Return an array of initial tinymce config options from the current role.
489 function tinymce_config($profile) {
492 // Drupal theme path.
493 $themepath = path_to_theme() .
'/';
496 $settings = $profile->settings
;
498 // Build a default list of TinyMCE settings.
500 // Is tinymce on by default?
501 $status = tinymce_user_get_status($user, $profile);
503 $init['mode'] = $status == 'true' ?
'exact' : 'none';
504 $init['theme'] = $settings['theme'] ?
$settings['theme'] : 'advanced';
505 $init['relative_urls'] = 'false';
506 $init['document_base_url'] = "$host";
507 $init['language'] = $settings['language'] ?
$settings['language'] : 'en';
508 $init['safari_warning'] = $settings['safari_message'] ?
$settings['safari_message'] : 'false';
509 $init['entity_encoding'] = 'raw';
510 $init['verify_html'] = $settings['verify_html'] ?
$settings['verify_html'] : 'false';
511 $init['preformatted'] = $settings['preformatted'] ?
$settings['preformatted'] : 'false';
512 $init['convert_fonts_to_spans'] = $settings['convert_fonts_to_spans'] ?
$settings['convert_fonts_to_spans'] : 'false';
513 $init['remove_linebreaks'] = $settings['remove_linebreaks'] ?
$settings['remove_linebreaks'] : 'true';
514 $init['apply_source_formatting'] = $settings['apply_source_formatting'] ?
$settings['apply_source_formatting'] : 'true';
515 $init['theme_advanced_resize_horizontal'] = 'false';
516 $init['theme_advanced_resizing_use_cookie'] = 'false';
519 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
520 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/imagemanager/') && user_access('access tinymce imagemanager')) {
521 // we probably need more security than this
522 $init['file_browser_callback'] = "mcImageManager.filebrowserCallBack";
524 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/filemanager/') && user_access('access tinymce filemanager')) {
525 // we probably need more security than this
526 $init['file_browser_callback'] = "mcImageManager.filebrowserCallBack";
529 if ($init['theme'] == 'advanced') {
530 $init['plugins'] = array();
531 $init['theme_advanced_toolbar_location'] = $settings['toolbar_loc'] ?
$settings['toolbar_loc'] : 'bottom';
532 $init['theme_advanced_toolbar_align'] = $settings['toolbar_align'] ?
$settings['toolbar_align'] : 'left';
533 $init['theme_advanced_path_location'] = $settings['path_loc'] ?
$settings['path_loc'] : 'bottom';
534 $init['theme_advanced_resizing'] = $settings['resizing'] ?
$settings['resizing'] : 'true';
535 $init['theme_advanced_blockformats'] = $settings['block_formats'] ?
$settings['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6';
537 if (is_array($settings['buttons'])) {
538 // This gives us the $plugins variable.
539 $plugins = _tinymce_get_buttons();
541 // Find the enabled buttons and the mce row they belong on. Also map the
542 // plugin metadata for each button.
543 $plugin_tracker = array();
544 foreach ($plugins as
$rname => $rplugin) { // Plugin name
545 foreach ($rplugin as
$mce_key => $mce_value) { // TinyMCE key
546 foreach ($mce_value as
$k => $v) { // Buttons
547 if ($settings['buttons'][$rname .
'-' .
$v]) {
548 // Font isn't a true plugin, rather it's buttons made available by the advanced theme
549 if (!in_array($rname, $plugin_tracker) && $rname != 'font') $plugin_tracker[] = $rname;
550 $init[$mce_key][] = $v;
554 // Some advanced plugins only have an $rname and no buttons
555 if ($settings['buttons'][$rname]) {
556 if (!in_array($rname, $plugin_tracker)) $plugin_tracker[] = $rname;
560 // Add the rest of the TinyMCE config options to the $init array for each button.
561 if (is_array($plugin_tracker)) {
562 foreach ($plugin_tracker as
$pname) {
563 if ($pname != 'default') $init['plugins'][] = $pname;
564 foreach ($plugins[$pname] as
$mce_key => $mce_value) {
565 // Don't overwrite buttons or extended_valid_elements
566 if ($mce_key == 'extended_valid_elements') {
567 // $mce_value is an array for extended_valid_elements so just grab the first element in the array (never more than one)
568 $init[$mce_key][] = $mce_value[0];
570 else if (!strstr($mce_key, 'theme_advanced_buttons')) {
571 $init[$mce_key] = $mce_value;
578 foreach ($init as
$mce_key => $mce_value) {
579 if (is_array($mce_value)) $mce_value = array_unique($mce_value);
580 $init[$mce_key] = $mce_value;
583 // Shuffle buttons around so that row 1 always has the most buttons,
584 // followed by row 2, etc. Note: These rows need to be set to NULL otherwise
585 // TinyMCE loads it's own buttons inherited from the theme.
586 if (!$init['theme_advanced_buttons1']) $init['theme_advanced_buttons1'] = array();
587 if (!$init['theme_advanced_buttons2']) $init['theme_advanced_buttons2'] = array();
588 if (!$init['theme_advanced_buttons3']) $init['theme_advanced_buttons3'] = array();
590 $min_btns = 5; // Minimum number of buttons per row.
591 $num1 = count($init['theme_advanced_buttons1']);
592 $num2 = count($init['theme_advanced_buttons2']);
593 $num3 = count($init['theme_advanced_buttons3']);
595 if ($num3 < $min_btns) {
596 $init['theme_advanced_buttons2'] = array_merge($init['theme_advanced_buttons2'], $init['theme_advanced_buttons3']);
597 $init['theme_advanced_buttons3'] = array();
598 $num2 = count($init['theme_advanced_buttons2']);
600 if ($num2 < $min_btns) {
601 $init['theme_advanced_buttons1'] = array_merge($init['theme_advanced_buttons1'], $init['theme_advanced_buttons2']);
602 // Squish the rows together, since row 2 is empty
603 $init['theme_advanced_buttons2'] = $init['theme_advanced_buttons3'];
604 $init['theme_advanced_buttons3'] = array();
605 $num1 = count($init['theme_advanced_buttons1']);
607 if ($num1 < $min_btns) {
608 $init['theme_advanced_buttons1'] = array_merge($init['theme_advanced_buttons1'], $init['theme_advanced_buttons2']);
609 // Squish the rows together, since row 2 is empty
610 $init['theme_advanced_buttons2'] = $init['theme_advanced_buttons3'];
611 $init['theme_advanced_buttons3'] = array();
617 if ($settings['css_classes']) $init['theme_advanced_styles'] = $settings['css_classes'];
619 if ($settings['css_setting'] == 'theme') {
620 $css = $themepath .
'style.css';
621 if (file_exists($css)) {
622 $init['content_css'] = $host .
$css;
625 else if ($settings['css_setting'] == 'self') {
626 $init['content_css'] = str_replace(array('%h', '%t'), array($host, $themepath), $settings['css_path']);
633 * Remove a profile from the database.
635 function tinymce_profile_delete($name) {
636 db_query("DELETE FROM {tinymce_settings} WHERE name = '%s'", $name);
637 db_query("DELETE FROM {tinymce_role} WHERE name = '%s'", $name);
641 * Return an HTML form for profile configuration.
643 function tinymce_profile_form_build($edit) {
644 $edit = (object) $edit;
646 // Only display the roles that currently don't have a tinymce profile. One
648 $orig_roles = user_roles(FALSE
, 'access tinymce');
649 $roles = $orig_roles;
650 if (arg(3) == 'add') {
651 $result = db_query('SELECT DISTINCT(rid) FROM {tinymce_role}');
652 while ($data = db_fetch_object($result)) {
653 if (!in_array($data->rid
, array_keys((array) $edit->rids
)) && !form_get_errors()){
654 unset($roles[$data->rid
]);
658 drupal_set_message(t('You must <a href="!access-control-url">assign</a> at least one role with the \'access tinymce\' permission before creating a profile.', array('!access-control-url' => url('admin/user/access'))), 'error');
661 drupal_set_message(t('You will not be allowed to create a new profile since all user roles have already been assigned profiles. Either remove an existing tinymce profile from at least one role or assign another role the \'access tinymce\' permission.'), 'error');
663 else if (count($orig_roles) != count($roles)) {
664 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.'));
666 $btn = t('Create profile');
669 $form['old_name'] = array('#type' => 'hidden', '#value' => $edit->name
);
670 $btn = t('Update profile');
673 $form['basic'] = array(
674 '#type' => 'fieldset',
675 '#title' => t('Basic setup'),
676 '#collapsible' => TRUE
,
680 $form['basic']['name'] = array(
681 '#type' => 'textfield',
682 '#title' => t('Profile name'),
683 '#default_value' => $edit->name
,
686 '#description' => t('Enter a name for this profile. This name is only visible within the tinymce administration page.'),
690 $form['basic']['rids'] = array(
691 '#type' => 'checkboxes',
692 '#title' => t('Roles allowed to use this profile'),
693 '#default_value' => array_keys((array) $edit->rids
),
694 '#options' => $roles,
695 '#description' => t('Check at least one role. Only roles with \'access tinymce\' permission will be shown here.'),
699 $form['basic']['default'] = array(
701 '#title' => t('Default state'),
702 '#default_value' => $edit->settings
['default'] ?
$edit->settings
['default'] : 'false',
703 '#options' => array('false' => t('disabled'), 'true' => t('enabled')),
704 '#description' => t('Default editor state for users in this profile. Users will be able to override this state if the next option is enabled.'),
707 $form['basic']['user_choose'] = array(
709 '#title' => t('Allow users to choose default'),
710 '#default_value' => $edit->settings
['user_choose'] ?
$edit->settings
['user_choose'] : 'false',
711 '#options' => array('false' => t('false'), 'true' => t('true')),
712 '#description' => t('If allowed, users will be able to choose their own TinyMCE default state by visiting their profile page.'),
715 $form['basic']['show_toggle'] = array(
717 '#title' => t('Show disable/enable rich text editor toggle'),
718 '#default_value' => $edit->settings
['show_toggle'] ?
$edit->settings
['show_toggle'] : 'true',
719 '#options' => array('false' => t('false'), 'true' => t('true')),
720 '#description' => t('Whether or not to show the disable/enable rich text editor toggle below the textarea. If false, editor defaults to the global default or user default (see above).'),
723 // This line upgrades previous versions of TinyMCE for user who previously selected a theme other than advanced.
724 if ($edit->settings
['theme'] != 'advanced') $edit->settings
['theme'] = 'advanced';
726 $form['basic']['theme'] = array(
728 '#value' => $edit->settings
['theme'] ?
$edit->settings
['theme'] : 'advanced'
731 $form['basic']['language'] = array(
733 '#title' => t('Language'),
734 '#default_value' => $edit->settings
['language'] ?
$edit->settings
['language'] : 'en',
735 '#options' => drupal_map_assoc(array('ar', 'ca', 'cs', 'cy', 'da', 'de', 'el', 'en', 'es', 'fa', 'fi', 'fr', 'fr_ca', 'he', 'hu', 'is', 'it', 'ja', 'ko', 'nb', 'nl', 'nn', 'pl', 'pt', 'pt_br', 'ru', 'ru_KOI8-R', 'ru_UTF-8', 'si', 'sk', 'sv', 'th', 'zh_cn', 'zh_tw', 'zh_tw_utf8')),
736 '#description' => t('The language for the TinyMCE interface. Language codes based on the <a href="http://www.loc.gov/standards/iso639-2/englangn.html">ISO-639-2</a> format.')
739 $form['basic']['safari_message'] = array(
741 '#title' => t('Safari browser warning'),
742 '#default_value' => $edit->settings
['safari_message'] ?
$edit->settings
['safari_message'] : 'false',
743 '#options' => array('false' => t('false'), 'true' => t('true')),
744 '#description' => t('TinyMCE support for the Safari web browser is experimental and a warning message is displayed when that browser is detected. You can disable this message here.')
747 $form['visibility'] = array(
748 '#type' => 'fieldset',
749 '#title' => t('Visibility'),
750 '#collapsible' => TRUE
,
754 $access = user_access('use PHP for block visibility');
756 // If the visibility is set to PHP mode but the user doesn't have this block permission, don't allow them to edit nor see this PHP code
757 if ($edit->settings
['access'] == 2 && !$access) {
758 $form['visibility'] = array();
759 $form['visibility']['access'] = array(
763 $form['visibility']['access_pages'] = array(
765 '#value' => $edit->settings
['access_pages']
769 $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
770 $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '!blog' for the blog page and !blog-wildcard for every personal blog. !front is the front page.", array('!blog' => theme('placeholder', 'blog'), '!blog-wildcard' => theme('placeholder', 'blog/*'), '!front' => theme('placeholder', '<front>')));
773 $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
774 $description .
= t('If the PHP-mode is chosen, enter PHP code between !php. Note that executing incorrect PHP-code can break your Drupal site.', array('!php' => theme('placeholder', '<?php ?>')));
776 $form['visibility']['access'] = array(
778 '#title' => t('Show tinymce on specific pages'),
779 '#default_value' => isset($edit->settings
['access']) ?
$edit->settings
['access'] : 1,
780 '#options' => $options
782 $form['visibility']['access_pages'] = array(
783 '#type' => 'textarea',
784 '#title' => t('Pages'),
785 '#default_value' => isset($edit->settings
['access_pages']) ?
$edit->settings
['access_pages'] : tinymce_help('admin/settings/tinymce#pages'),
786 '#description' => $description
790 $form['buttons'] = array(
791 '#type' => 'fieldset',
792 '#title' => t('Buttons and plugins'),
793 '#collapsible' => TRUE
,
794 '#collapsed' => TRUE
,
796 '#theme' => 'tinymce_profile_form_buttons'
799 $metadata = _tinymce_get_buttons(FALSE
);
800 // Generate the button list.
801 foreach($metadata as
$name => $meta) {
802 if (is_array($meta['buttons'])) {
803 foreach ($meta['buttons'] as
$button) {
804 if ($name != 'default') {
805 $img_src = drupal_get_path('module', 'tinymce').
"/tinymce/jscripts/tiny_mce/plugins/$name/images/$name.gif";
807 //correct for plugins that have more than one button
808 if (!file_exists($img_src)) {
809 $img_src = drupal_get_path('module', 'tinymce').
"/tinymce/jscripts/tiny_mce/plugins/$name/images/$button.gif";
813 $img_src = drupal_get_path('module', 'tinymce').
"/tinymce/jscripts/tiny_mce/themes/advanced/images/$button.gif";
816 $b = file_exists($img_src) ?
'<img src="'.
base_path() .
$img_src .
'" title="'.
$button .
'" style="border: 1px solid grey; vertical-align: middle;" />' : $button;
818 if ($name == 'default') {
822 $title = $metadata[$name]['longname'] ?
$metadata[$name]['longname'] : $name;
823 if ($metadata[$name]['infourl']) {
824 $title = '<a href="'.
$metadata[$name]['infourl'] .
'" target="_blank">'.
$title .
'</a>';
826 $title = $b .
' – '.
$title;
828 $form_value = $edit->settings
['buttons'][$name .
'-' .
$button];
829 $form['buttons'][$name .
'-' .
$button] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value);
833 $title = $metadata[$name]['longname'] ?
$metadata[$name]['longname'] : $name;
834 if ($metadata[$name]['infourl']) {
835 $title = '<a href="'.
$metadata[$name]['infourl'] .
'" target="_blank">'.
$title .
'</a>';
837 $form_value = $edit->settings
['buttons'][$name];
838 $form['buttons'][$name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value);
842 $form['appearance'] = array(
843 '#type' => 'fieldset',
844 '#title' => t('Editor appearance'),
845 '#collapsible' => TRUE
,
849 $form['appearance']['toolbar_loc'] = array(
851 '#title' => t('Toolbar location'),
852 '#default_value' => $edit->settings
['toolbar_loc'],
853 '#options' => array('bottom' => t('bottom'), 'top' => t('top')),
854 '#description' => t('Show toolbar at the top or bottom of the editor area?')
857 $form['appearance']['toolbar_align'] = array(
859 '#title' => t('Toolbar alignment'),
860 '#default_value' => $edit->settings
['toolbar_align'],
861 '#options' => array('center' => t('center'), 'left' => t('left'), 'right' => t('right')),
862 '#description' => t('Align tool icons left, center, or right within the toolbar.')
865 $form['appearance']['path_loc'] = array(
867 '#title' => t('Path location'),
868 '#default_value' => $edit->settings
['path_loc'] ?
$edit->settings
['path_loc'] : 'bottom',
869 '#options' => array('none' => t('none'), 'top' => t('top'), 'bottom' => t('bottom')),
870 '#description' => t('Path to html elements (i.e. "body>table>tr>td"). Show at top, bottom, or not at all.')
873 $form['appearance']['resizing'] = array(
875 '#title' => t('Enable resizing button'),
876 '#default_value' => isset($edit->settings
['resizing']) ?
$edit->settings
['resizing'] : 'true',
877 '#options' => array('false' => t('false'), 'true' => t('true')),
878 '#description' => t(' This option gives you the ability to enable/disable the resizing button. If enabled the <strong>Path location toolbar</strong> must be set to "top" or "bottom" in order to display the resize icon.')
881 $form['appearance']['block_formats'] = array(
882 '#type' => 'textfield',
883 '#title' => t('Block formats'),
884 '#default_value' => $edit->settings
['block_formats'] ?
$edit->settings
['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6',
887 '#description' => t('Comma separated list of HTML block formats. You can only remove elements, not add.')
890 $form['output'] = array(
891 '#type' => 'fieldset',
892 '#title' => t('Cleanup and output'),
893 '#collapsible' => TRUE
,
897 $form['output']['verify_html'] = array(
899 '#title' => t('Verify HTML'),
900 '#default_value' => $edit->settings
['verify_html'],
901 '#options' => array('false' => t('false'), 'true' => t('true')),
902 '#description' => t('Should the HTML contents be verified or not? Verifying will strip <head> tags, so choose false if you will be editing full page HTML.')
905 $form['output']['preformatted'] = array(
907 '#title' => t('Preformatted'),
908 '#default_value' => $edit->settings
['preformatted'],
909 '#options' => array('false' => t('false'), 'true' => t('true')),
910 '#description' => 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.')
913 $form['output']['convert_fonts_to_spans'] = array(
915 '#title' => t('Convert <font> tags to styles'),
916 '#default_value' => $edit->settings
['convert_fonts_to_spans'],
917 '#options' => array('true' => t('true'), 'false' => t('false')),
918 '#description' => t('If you set this option to true, font size, font family, font color and font background color will be replaced by inline styles.')
921 $form['output']['remove_linebreaks'] = array(
923 '#title' => t('Remove linebreaks'),
924 '#default_value' => $edit->settings
['remove_linebreaks'],
925 '#options' => array('true' => 'true', 'false' => 'false'),
926 '#description' => t('Set this option to false to prevent TinyMCE from removing linebreaks from existing nodes. True avoids conflicts with some filters.')
929 $form['output']['apply_source_formatting'] = array(
931 '#title' => t('Apply source formatting'),
932 '#default_value' => $edit->settings
['apply_source_formatting'],
933 '#options' => array('true' => 'true', 'false' => 'false'),
934 '#description' => t('This option makes TinyMCE apply source formatting. Set this to true for a cleaner HTML source. Choose false to avoid conflicts with some filters.')
937 $form['css'] = array(
938 '#type' => 'fieldset',
939 '#title' => t('CSS'),
940 '#collapsible' => TRUE
,
944 $form['css']['css_setting'] = array(
946 '#title' => t('Editor CSS'),
947 '#default_value' => $edit->settings
['css_setting'] ?
$edit->settings
['css_setting'] : 'theme',
948 '#options' => array('theme' => t('use theme css'), 'self' => t('define css'), 'none' => t('tinyMCE default')),
949 '#description' => t('Defines the CSS to be used in the editor area.<br />use theme css - load style.css from current site theme.<br/>define css - enter path for css file below.<br />tinyMCE default - uses default CSS from editor.')
952 $form['css']['css_path'] = array(
953 '#type' => 'textfield',
954 '#title' => t('CSS path'),
955 '#default_value' => $edit->settings
['css_path'],
958 '#description' => t('Enter path to CSS file (<em>example: "css/editor.css"</em>) or a list of css files seperated by a comma (<em>example: /themes/garland/style.css,http://domain.com/customMCE.css</em>).<br />Macros: %h (host name: http://www.example.com/), %t (path to theme: theme/yourtheme/)<br />Be sure to select "define css" above.')
961 $form['css']['css_classes'] = array(
962 '#type' => 'textfield',
963 '#title' => t('CSS classes'),
964 '#default_value' => $edit->settings
['css_classes'],
967 '#description' => t('Adds CSS classes to the "styles" droplist. Format is: <title>=<class>;<br/> Example: Header 1=header1;Header 2=header2;Header 3=header3 (note: no trailing \';\')<br />Leave blank to automatically import list of CSS classes from style sheet.')
970 $form['submit'] = array(
979 * Return an HTML form for profile configuration.
981 function tinymce_profile_form($edit) {
983 $output .
= drupal_get_form('tinymce_profile_form_build', $edit);
989 * Layout for the buttons in the tinymce profile form
991 function theme_tinymce_profile_form_buttons($form) {
994 // Flatten forms array
995 foreach (element_children($form) as
$key) {
996 $buttons[] = drupal_render($form[$key]);
999 //split checkboxes into rows with 3 columns
1000 $total = count($buttons);
1002 for ($i = 0; $i < $total; $i++) {
1004 $row[] = array('data' => $buttons[$i]);
1005 $row[] = array('data' => $buttons[++$i]);
1006 $row[] = array('data' => $buttons[++$i]);
1010 $output = theme('table', array(), $rows, array('width' => '100%'));
1016 * Load all profiles. Just load one profile if $name is passed in.
1018 function tinymce_profile_load($name = '') {
1019 static
$profiles = array();
1022 $roles = user_roles();
1023 $result = db_query('SELECT * FROM {tinymce_settings}');
1024 while ($data = db_fetch_object($result)) {
1025 $data->settings
= unserialize($data->settings
);
1026 $result2 = db_query("SELECT rid FROM {tinymce_role} WHERE name = '%s'", $data->name
);
1028 while ($r = db_fetch_object($result2)) {
1029 $role[$r->rid
] = $roles[$r->rid
];
1031 $data->rids
= $role;
1033 $profiles[$data->name
] = $data;
1037 return ($name ?
$profiles[$name] : $profiles);
1041 * Controller for tinymce profiles.
1043 function tinymce_profile_overview() {
1046 $profiles = tinymce_profile_load();
1048 $roles = user_roles();
1049 $header = array(t('Profile'), t('Roles'), t('Operations'));
1050 foreach ($profiles as
$p) {
1051 $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'));
1053 $output .
= theme('table', $header, $rows);
1054 $output .
= t('<p><a href="!create-profile-url">Create new profile</a></p>', array('!create-profile-url' => url('admin/settings/tinymce/add')));
1057 drupal_set_message(t('No profiles found. Click here to <a href="!create-profile-url">create a new profile</a>.', array('!create-profile-url' => url('admin/settings/tinymce/add'))));
1064 * Save a profile to the database.
1066 function tinymce_profile_save($edit) {
1067 db_query("DELETE FROM {tinymce_settings} WHERE name = '%s' or name = '%s'", $edit['name'], $edit['old_name']);
1068 db_query("DELETE FROM {tinymce_role} WHERE name = '%s' or name = '%s'", $edit['name'], $edit['old_name']);
1069 db_query("INSERT INTO {tinymce_settings} (name, settings) VALUES ('%s', '%s')", $edit['name'], serialize($edit));
1070 foreach ($edit['rids'] as
$rid => $value) {
1071 db_query("INSERT INTO {tinymce_role} (name, rid) VALUES ('%s', %d)", $edit['name'], $rid);
1074 // if users can't set their own defaults, make sure to remove $user->tinymce_status so their default doesn't override the main default
1075 if ($edit['user_choose'] == 'false') {
1077 user_save($user, array('tinymce_status' => NULL
));
1082 * Profile validation.
1084 function tinymce_profile_validate($edit) {
1087 if (!$edit['name']) {
1088 $errors['name'] = t('You must give a profile name.');
1091 if (!$edit['rids']) {
1092 $errors['rids'] = t('You must select at least one role.');
1095 foreach ($errors as
$name => $message) {
1096 form_set_error($name, $message);
1099 return count($errors) == 0;
1102 /********************************************************************
1103 * Module Functions :: Private
1104 ********************************************************************/
1107 * Determine if TinyMCE has permission to be used on the current page.
1110 * TRUE if can render, FALSE if not allowed.
1112 function _tinymce_page_match($edit) {
1113 $page_match = FALSE
;
1115 // Kill TinyMCE if we're editing a textarea with PHP in it!
1116 // PHP input formats are #2 in the filters table.
1117 if (is_numeric(arg(1)) && arg(2) == 'edit') {
1118 $node = node_load(arg(1));
1119 if ($node->format
== 2) {
1124 if ($edit->settings
['access_pages']) {
1125 // If the PHP option wasn't selected
1126 if ($edit->settings
['access'] < 2) {
1127 $path = drupal_get_path_alias($_GET['q']);
1128 $regexp = '/^('.
preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'.
preg_quote(variable_get('site_frontpage', 'node'), '/') .
'\2'), preg_quote($edit->settings
['access_pages'], '/')) .
')$/';
1129 $page_match = !($edit->settings
['access'] xor
preg_match($regexp, $path));
1132 $page_match = drupal_eval($edit->settings
['access_pages']);
1135 // No pages were specified to block so show on all
1143 function tinymce_user_get_profile($account) {
1144 $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($account->roles
))));
1146 return tinymce_profile_load($profile_name);
1153 function tinymce_user_get_status($user, $profile){
1154 $settings = $profile->settings
;
1156 if ($settings['user_choose']) {
1157 $status = isset($user->tinymce_status
) ?
$user->tinymce_status
: (isset($settings['default']) ?
$settings['default'] : 'false');
1160 $status = isset($settings['default']) ?
$settings['default'] : 'false';