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' => 'false', 'true' => 'true'),
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_styles'] = $settings['convert_fonts_to_styles'] ?
$settings['convert_fonts_to_styles'] : 'false';
513 $init['theme_advanced_resize_horizontal'] = 'false';
514 $init['theme_advanced_resizing_use_cookie'] = 'false';
517 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
518 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/imagemanager/') && user_access('access tinymce imagemanager')) {
519 // we probably need more security than this
520 $init['file_browser_callback'] = "mcImageManager.filebrowserCallBack";
522 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/filemanager/') && user_access('access tinymce filemanager')) {
523 // we probably need more security than this
524 $init['file_browser_callback'] = "mcImageManager.filebrowserCallBack";
527 if ($init['theme'] == 'advanced') {
528 $init['plugins'] = array();
529 $init['theme_advanced_toolbar_location'] = $settings['toolbar_loc'] ?
$settings['toolbar_loc'] : 'bottom';
530 $init['theme_advanced_toolbar_align'] = $settings['toolbar_align'] ?
$settings['toolbar_align'] : 'left';
531 $init['theme_advanced_path_location'] = $settings['path_loc'] ?
$settings['path_loc'] : 'bottom';
532 $init['theme_advanced_resizing'] = $settings['resizing'] ?
$settings['resizing'] : 'true';
533 $init['theme_advanced_blockformats'] = $settings['block_formats'] ?
$settings['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6';
535 if (is_array($settings['buttons'])) {
536 // This gives us the $plugins variable.
537 $plugins = _tinymce_get_buttons();
539 // Find the enabled buttons and the mce row they belong on. Also map the
540 // plugin metadata for each button.
541 $plugin_tracker = array();
542 foreach ($plugins as
$rname => $rplugin) { // Plugin name
543 foreach ($rplugin as
$mce_key => $mce_value) { // TinyMCE key
544 foreach ($mce_value as
$k => $v) { // Buttons
545 if ($settings['buttons'][$rname .
'-' .
$v]) {
546 // Font isn't a true plugin, rather it's buttons made available by the advanced theme
547 if (!in_array($rname, $plugin_tracker) && $rname != 'font') $plugin_tracker[] = $rname;
548 $init[$mce_key][] = $v;
552 // Some advanced plugins only have an $rname and no buttons
553 if ($settings['buttons'][$rname]) {
554 if (!in_array($rname, $plugin_tracker)) $plugin_tracker[] = $rname;
558 // Add the rest of the TinyMCE config options to the $init array for each button.
559 if (is_array($plugin_tracker)) {
560 foreach ($plugin_tracker as
$pname) {
561 if ($pname != 'default') $init['plugins'][] = $pname;
562 foreach ($plugins[$pname] as
$mce_key => $mce_value) {
563 // Don't overwrite buttons or extended_valid_elements
564 if ($mce_key == 'extended_valid_elements') {
565 // $mce_value is an array for extended_valid_elements so just grab the first element in the array (never more than one)
566 $init[$mce_key][] = $mce_value[0];
568 else if (!strstr($mce_key, 'theme_advanced_buttons')) {
569 $init[$mce_key] = $mce_value;
576 foreach ($init as
$mce_key => $mce_value) {
577 if (is_array($mce_value)) $mce_value = array_unique($mce_value);
578 $init[$mce_key] = $mce_value;
581 // Shuffle buttons around so that row 1 always has the most buttons,
582 // followed by row 2, etc. Note: These rows need to be set to NULL otherwise
583 // TinyMCE loads it's own buttons inherited from the theme.
584 if (!$init['theme_advanced_buttons1']) $init['theme_advanced_buttons1'] = array();
585 if (!$init['theme_advanced_buttons2']) $init['theme_advanced_buttons2'] = array();
586 if (!$init['theme_advanced_buttons3']) $init['theme_advanced_buttons3'] = array();
588 $min_btns = 5; // Minimum number of buttons per row.
589 $num1 = count($init['theme_advanced_buttons1']);
590 $num2 = count($init['theme_advanced_buttons2']);
591 $num3 = count($init['theme_advanced_buttons3']);
593 if ($num3 < $min_btns) {
594 $init['theme_advanced_buttons2'] = array_merge($init['theme_advanced_buttons2'], $init['theme_advanced_buttons3']);
595 $init['theme_advanced_buttons3'] = array();
596 $num2 = count($init['theme_advanced_buttons2']);
598 if ($num2 < $min_btns) {
599 $init['theme_advanced_buttons1'] = array_merge($init['theme_advanced_buttons1'], $init['theme_advanced_buttons2']);
600 // Squish the rows together, since row 2 is empty
601 $init['theme_advanced_buttons2'] = $init['theme_advanced_buttons3'];
602 $init['theme_advanced_buttons3'] = array();
603 $num1 = count($init['theme_advanced_buttons1']);
605 if ($num1 < $min_btns) {
606 $init['theme_advanced_buttons1'] = array_merge($init['theme_advanced_buttons1'], $init['theme_advanced_buttons2']);
607 // Squish the rows together, since row 2 is empty
608 $init['theme_advanced_buttons2'] = $init['theme_advanced_buttons3'];
609 $init['theme_advanced_buttons3'] = array();
615 if ($settings['css_classes']) $init['theme_advanced_styles'] = $settings['css_classes'];
617 if ($settings['css_setting'] == 'theme') {
618 $css = $themepath .
'style.css';
619 if (file_exists($css)) {
620 $init['content_css'] = $host .
$css;
623 else if ($settings['css_setting'] == 'self') {
624 $init['content_css'] = str_replace(array('%h', '%t'), array($host, $themepath), $settings['css_path']);
631 * Remove a profile from the database.
633 function tinymce_profile_delete($name) {
634 db_query("DELETE FROM {tinymce_settings} WHERE name = '%s'", $name);
635 db_query("DELETE FROM {tinymce_role} WHERE name = '%s'", $name);
639 * Return an HTML form for profile configuration.
641 function tinymce_profile_form_build($edit) {
642 $edit = (object) $edit;
644 // Only display the roles that currently don't have a tinymce profile. One
646 $orig_roles = user_roles(FALSE
, 'access tinymce');
647 $roles = $orig_roles;
648 if (arg(3) == 'add') {
649 $result = db_query('SELECT DISTINCT(rid) FROM {tinymce_role}');
650 while ($data = db_fetch_object($result)) {
651 if (!in_array($data->rid
, array_keys((array) $edit->rids
)) && !form_get_errors()){
652 unset($roles[$data->rid
]);
656 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');
659 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');
661 else if (count($orig_roles) != count($roles)) {
662 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.'));
664 $btn = t('Create profile');
667 $form['old_name'] = array('#type' => 'hidden', '#value' => $edit->name
);
668 $btn = t('Update profile');
671 $form['basic'] = array(
672 '#type' => 'fieldset',
673 '#title' => t('Basic setup'),
674 '#collapsible' => TRUE
,
678 $form['basic']['name'] = array(
679 '#type' => 'textfield',
680 '#title' => t('Profile name'),
681 '#default_value' => $edit->name
,
684 '#description' => t('Enter a name for this profile. This name is only visible within the tinymce administration page.'),
688 $form['basic']['rids'] = array(
689 '#type' => 'checkboxes',
690 '#title' => t('Roles allowed to use this profile'),
691 '#default_value' => array_keys((array) $edit->rids
),
692 '#options' => $roles,
693 '#description' => t('Check at least one role. Only roles with \'access tinymce\' permission will be shown here.'),
697 $form['basic']['default'] = array(
699 '#title' => t('Default state'),
700 '#default_value' => $edit->settings
['default'] ?
$edit->settings
['default'] : 'false',
701 '#options' => array('false' => 'false', 'true' => 'true'),
702 '#description' => t('Default editor state for users in this profile. Users will be able to override this state if the next option is enabled.'),
705 $form['basic']['user_choose'] = array(
707 '#title' => t('Allow users to choose default'),
708 '#default_value' => $edit->settings
['user_choose'] ?
$edit->settings
['user_choose'] : 'false',
709 '#options' => array('false' => 'false', 'true' => 'true'),
710 '#description' => t('If allowed, users will be able to choose their own TinyMCE default state by visiting their profile page.'),
713 $form['basic']['show_toggle'] = array(
715 '#title' => t('Show disable/enable rich text editor toggle'),
716 '#default_value' => $edit->settings
['show_toggle'] ?
$edit->settings
['show_toggle'] : 'true',
717 '#options' => array('false' => 'false', 'true' => 'true'),
718 '#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).'),
721 // This line upgrades previous versions of TinyMCE for user who previously selected a theme other than advanced.
722 if ($edit->settings
['theme'] != 'advanced') $edit->settings
['theme'] = 'advanced';
724 $form['basic']['theme'] = array(
726 '#value' => $edit->settings
['theme'] ?
$edit->settings
['theme'] : 'advanced'
729 $form['basic']['language'] = array(
731 '#title' => t('Language'),
732 '#default_value' => $edit->settings
['language'] ?
$edit->settings
['language'] : 'en',
733 '#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', 'sk', 'sv', 'th', 'zh_cn', 'zh_tw', 'zh_tw_utf8')),
734 '#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.')
737 $form['basic']['safari_message'] = array(
739 '#title' => t('Safari browser warning'),
740 '#default_value' => $edit->settings
['safari_message'] ?
$edit->settings
['safari_message'] : 'false',
741 '#options' => array('false' => 'false', 'true' => 'true'),
742 '#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.')
745 $form['visibility'] = array(
746 '#type' => 'fieldset',
747 '#title' => t('Visibility'),
748 '#collapsible' => TRUE
,
752 $access = user_access('use PHP for block visibility');
754 // 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
755 if ($edit->settings
['access'] == 2 && !$access) {
756 $form['visibility'] = array();
757 $form['visibility']['access'] = array(
761 $form['visibility']['access_pages'] = array(
763 '#value' => $edit->settings
['access_pages']
767 $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
768 $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>')));
771 $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
772 $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 ?>')));
774 $form['visibility']['access'] = array(
776 '#title' => t('Show tinymce on specific pages'),
777 '#default_value' => isset($edit->settings
['access']) ?
$edit->settings
['access'] : 1,
778 '#options' => $options
780 $form['visibility']['access_pages'] = array(
781 '#type' => 'textarea',
782 '#title' => t('Pages'),
783 '#default_value' => isset($edit->settings
['access_pages']) ?
$edit->settings
['access_pages'] : tinymce_help('admin/settings/tinymce#pages'),
784 '#description' => $description
788 $form['buttons'] = array(
789 '#type' => 'fieldset',
790 '#title' => t('Buttons and plugins'),
791 '#collapsible' => TRUE
,
792 '#collapsed' => TRUE
,
794 '#theme' => 'tinymce_profile_form_buttons'
797 $metadata = _tinymce_get_buttons(FALSE
);
798 // Generate the button list.
799 foreach($metadata as
$name => $meta) {
800 if (is_array($meta['buttons'])) {
801 foreach ($meta['buttons'] as
$button) {
802 if ($name != 'default') {
803 $img_src = drupal_get_path('module', 'tinymce').
"/tinymce/jscripts/tiny_mce/plugins/$name/images/$name.gif";
805 //correct for plugins that have more than one button
806 if (!file_exists($img_src)) {
807 $img_src = drupal_get_path('module', 'tinymce').
"/tinymce/jscripts/tiny_mce/plugins/$name/images/$button.gif";
811 $img_src = drupal_get_path('module', 'tinymce').
"/tinymce/jscripts/tiny_mce/themes/advanced/images/$button.gif";
814 $b = file_exists($img_src) ?
'<img src="'.
base_path() .
$img_src .
'" title="'.
$button .
'" style="border: 1px solid grey; vertical-align: middle;" />' : $button;
816 if ($name == 'default') {
820 $title = $metadata[$name]['longname'] ?
$metadata[$name]['longname'] : $name;
821 if ($metadata[$name]['infourl']) {
822 $title = '<a href="'.
$metadata[$name]['infourl'] .
'" target="_blank">'.
$title .
'</a>';
824 $title = $b .
' – '.
$title;
826 $form_value = $edit->settings
['buttons'][$name .
'-' .
$button];
827 $form['buttons'][$name .
'-' .
$button] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value);
831 $title = $metadata[$name]['longname'] ?
$metadata[$name]['longname'] : $name;
832 if ($metadata[$name]['infourl']) {
833 $title = '<a href="'.
$metadata[$name]['infourl'] .
'" target="_blank">'.
$title .
'</a>';
835 $form_value = $edit->settings
['buttons'][$name];
836 $form['buttons'][$name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value);
840 $form['appearance'] = array(
841 '#type' => 'fieldset',
842 '#title' => t('Editor appearance'),
843 '#collapsible' => TRUE
,
847 $form['appearance']['toolbar_loc'] = array(
849 '#title' => t('Toolbar location'),
850 '#default_value' => $edit->settings
['toolbar_loc'],
851 '#options' => array('bottom' => 'bottom', 'top' => 'top'),
852 '#description' => t('Show toolbar at the top or bottom of the editor area?')
855 $form['appearance']['toolbar_align'] = array(
857 '#title' => t('Toolbar alignment'),
858 '#default_value' => $edit->settings
['toolbar_align'],
859 '#options' => array('center' => 'center', 'left' => 'left', 'right' => 'right'),
860 '#description' => t('Align tool icons left, center, or right within the toolbar.')
863 $form['appearance']['path_loc'] = array(
865 '#title' => t('Path location'),
866 '#default_value' => $edit->settings
['path_loc'] ?
$edit->settings
['path_loc'] : 'bottom',
867 '#options' => array('none' => 'none', 'top' => 'top', 'bottom' => 'bottom'),
868 '#description' => t('Path to html elements (i.e. "body>table>tr>td"). Show at top, bottom, or not at all.')
871 $form['appearance']['resizing'] = array(
873 '#title' => t('Enable resizing button'),
874 '#default_value' => isset($edit->settings
['resizing']) ?
$edit->settings
['resizing'] : 'true',
875 '#options' => array('false' => 'false', 'true' => 'true'),
876 '#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.')
879 $form['appearance']['block_formats'] = array(
880 '#type' => 'textfield',
881 '#title' => t('Block formats'),
882 '#default_value' => $edit->settings
['block_formats'] ?
$edit->settings
['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6',
885 '#description' => t('Comma separated list of HTML block formats. You can only remove elements, not add.')
888 $form['output'] = array(
889 '#type' => 'fieldset',
890 '#title' => t('Cleanup and output'),
891 '#collapsible' => TRUE
,
895 $form['output']['verify_html'] = array(
897 '#title' => t('Verify HTML'),
898 '#default_value' => $edit->settings
['verify_html'],
899 '#options' => array('true' => 'true', 'false' => 'false'),
900 '#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.')
903 $form['output']['preformatted'] = array(
905 '#title' => t('Preformatted'),
906 '#default_value' => $edit->settings
['preformatted'],
907 '#options' => array('false' => 'false', 'true' => 'true'),
908 '#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.')
911 $form['output']['convert_fonts_to_styles'] = array(
913 '#title' => t('Convert <font> tags to styles'),
914 '#default_value' => $edit->settings
['convert_fonts_to_styles'],
915 '#options' => array('true' => 'true', 'false' => 'false'),
916 '#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.')
919 $form['css'] = array(
920 '#type' => 'fieldset',
921 '#title' => t('CSS'),
922 '#collapsible' => TRUE
,
926 $form['css']['css_setting'] = array(
928 '#title' => t('Editor CSS'),
929 '#default_value' => $edit->settings
['css_setting'] ?
$edit->settings
['css_setting'] : 'theme',
930 '#options' => array('theme' => 'use theme css', 'self' => 'define css', 'none' => 'tinyMCE default'),
931 '#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.')
934 $form['css']['css_path'] = array(
935 '#type' => 'textfield',
936 '#title' => t('CSS path'),
937 '#default_value' => $edit->settings
['css_path'],
940 '#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.')
943 $form['css']['css_classes'] = array(
944 '#type' => 'textfield',
945 '#title' => t('CSS classes'),
946 '#default_value' => $edit->settings
['css_classes'],
949 '#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.')
952 $form['submit'] = array(
961 * Return an HTML form for profile configuration.
963 function tinymce_profile_form($edit) {
965 $output .
= drupal_get_form('tinymce_profile_form_build', $edit);
971 * Layout for the buttons in the tinymce profile form
973 function theme_tinymce_profile_form_buttons($form) {
976 // Flatten forms array
977 foreach (element_children($form) as
$key) {
978 $buttons[] = drupal_render($form[$key]);
981 //split checkboxes into rows with 3 columns
982 $total = count($buttons);
984 for ($i = 0; $i < $total; $i++) {
986 $row[] = array('data' => $buttons[$i]);
987 $row[] = array('data' => $buttons[++$i]);
988 $row[] = array('data' => $buttons[++$i]);
992 $output = theme('table', array(), $rows, array('width' => '100%'));
998 * Load all profiles. Just load one profile if $name is passed in.
1000 function tinymce_profile_load($name = '') {
1001 static
$profiles = array();
1004 $roles = user_roles();
1005 $result = db_query('SELECT * FROM {tinymce_settings}');
1006 while ($data = db_fetch_object($result)) {
1007 $data->settings
= unserialize($data->settings
);
1008 $result2 = db_query("SELECT rid FROM {tinymce_role} WHERE name = '%s'", $data->name
);
1010 while ($r = db_fetch_object($result2)) {
1011 $role[$r->rid
] = $roles[$r->rid
];
1013 $data->rids
= $role;
1015 $profiles[$data->name
] = $data;
1019 return ($name ?
$profiles[$name] : $profiles);
1023 * Controller for tinymce profiles.
1025 function tinymce_profile_overview() {
1028 $profiles = tinymce_profile_load();
1030 $roles = user_roles();
1031 $header = array(t('Profile'), t('Roles'), t('Operations'));
1032 foreach ($profiles as
$p) {
1033 $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'));
1035 $output .
= theme('table', $header, $rows);
1036 $output .
= t('<p><a href="!create-profile-url">Create new profile</a></p>', array('!create-profile-url' => url('admin/settings/tinymce/add')));
1039 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'))));
1046 * Save a profile to the database.
1048 function tinymce_profile_save($edit) {
1049 db_query("DELETE FROM {tinymce_settings} WHERE name = '%s' or name = '%s'", $edit['name'], $edit['old_name']);
1050 db_query("DELETE FROM {tinymce_role} WHERE name = '%s' or name = '%s'", $edit['name'], $edit['old_name']);
1051 db_query("INSERT INTO {tinymce_settings} (name, settings) VALUES ('%s', '%s')", $edit['name'], serialize($edit));
1052 foreach ($edit['rids'] as
$rid => $value) {
1053 db_query("INSERT INTO {tinymce_role} (name, rid) VALUES ('%s', %d)", $edit['name'], $rid);
1056 // if users can't set their own defaults, make sure to remove $user->tinymce_status so their default doesn't override the main default
1057 if ($edit['user_choose'] == 'false') {
1059 user_save($user, array('tinymce_status' => NULL
));
1064 * Profile validation.
1066 function tinymce_profile_validate($edit) {
1069 if (!$edit['name']) {
1070 $errors['name'] = t('You must give a profile name.');
1073 if (!$edit['rids']) {
1074 $errors['rids'] = t('You must select at least one role.');
1077 foreach ($errors as
$name => $message) {
1078 form_set_error($name, $message);
1081 return count($errors) == 0;
1084 /********************************************************************
1085 * Module Functions :: Private
1086 ********************************************************************/
1089 * Determine if TinyMCE has permission to be used on the current page.
1092 * TRUE if can render, FALSE if not allowed.
1094 function _tinymce_page_match($edit) {
1095 $page_match = FALSE
;
1097 // Kill TinyMCE if we're editing a textarea with PHP in it!
1098 // PHP input formats are #2 in the filters table.
1099 if (is_numeric(arg(1)) && arg(2) == 'edit') {
1100 $node = node_load(arg(1));
1101 if ($node->format
== 2) {
1106 if ($edit->settings
['access_pages']) {
1107 // If the PHP option wasn't selected
1108 if ($edit->settings
['access'] < 2) {
1109 $path = drupal_get_path_alias($_GET['q']);
1110 $regexp = '/^('.
preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'.
preg_quote(variable_get('site_frontpage', 'node'), '/') .
'\2'), preg_quote($edit->settings
['access_pages'], '/')) .
')$/';
1111 $page_match = !($edit->settings
['access'] xor
preg_match($regexp, $path));
1114 $page_match = drupal_eval($edit->settings
['access_pages']);
1117 // No pages were specified to block so show on all
1125 function tinymce_user_get_profile($account) {
1126 $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
))));
1128 return tinymce_profile_load($profile_name);
1135 function tinymce_user_get_status($user, $profile){
1136 $settings = $profile->settings
;
1138 if ($settings['user_choose']) {
1139 $status = isset($user->tinymce_status
) ?
$user->tinymce_status
: (isset($settings['default']) ?
$settings['default'] : 'false');
1142 $status = isset($settings['default']) ?
$settings['default'] : 'false';