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 'access' => user_access('administer tinymce'));
25 * Implementation of hook_help().
27 function tinymce_help($section) {
29 case
'admin/modules#description':
30 return t('The TinyMCE Javascript HTML WYSIWYG editor.');
31 case
'admin/settings/tinymce#pages':
32 return "node/*\nuser/*\ncomment/*";
33 case
'admin/settings/tinymce':
34 case
'admin/help#tinymce' :
35 return t('<p style="font-size:x-small">$Revision$ $Date$</p>' .
36 '<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>
37 <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>
38 <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/access'))
44 * Implementation of hook_perm().
46 function tinymce_perm() {
47 $array = array('administer tinymce', 'access tinymce');
48 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
50 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/imagemanager/')) {
51 $array[] = 'access tinymce imagemanager';
53 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/filemanager/')) {
54 $array[] = 'access tinymce filemanager';
61 * Implementation of hook_elements().
63 function tinymce_elements() {
66 if (user_access('access tinymce')) {
67 // Set resizable to false to avoid drupal.js resizable function from taking control of the textarea
68 $type['textarea'] = array('#process' => array('tinymce_process_textarea' => array()), '#resizable' => FALSE
);
75 * Attach tinymce to a textarea
77 function tinymce_process_textarea($element) {
78 static
$is_running = FALSE
;
82 //$element is an array of attributes for the textarea but there is no just 'name' value, so we extract this from the #id field
83 $textarea_name = substr($element['#id'], strpos($element['#id'], '-') + 1);
85 // Since tinymce_config() makes a db hit, only call it when we're pretty sure
86 // we're gonna render tinymce.
88 $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
))));
90 $profile = tinymce_profile_load($profile_name);
91 $init = tinymce_config($profile);
92 $init['elements'] = 'edit-'.
$textarea_name;
94 if (_tinymce_page_match($profile)) {
95 // Merge user-defined TinyMCE settings.
96 $init = (array) theme('tinymce_theme', $init, $textarea_name, $init['theme'], $is_running);
98 // If $init array is empty no need to execute rest of code since there are no textareas to theme with TinyMCE
99 if (count($init) < 1) {
100 // we set this textarea to use drupal resizable since tinymce won't be controling this textarea
101 $element['#resizable'] = TRUE
;
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
120 <script type
="text/javascript">
128 <script type
="text/javascript">
129 function mceToggle(id
, linkid
) {
130 element
= document.
getElementById(id
);
131 link = document.
getElementById(linkid
);
132 img_assist
= document.
getElementById('img_assist-link-'+ id
);
134 if (tinyMCE.
getEditorId(element.id
) == null
) {
135 tinyMCE.
addMCEControl(element
, element.id
);
137 link.innerHTML
= '$disable';
138 link.href
= "javascript:mceToggle('" +id
+ "', '" +linkid
+ "');";
140 img_assist.innerHTML
= '';
144 tinyMCE.
removeMCEControl(tinyMCE.
getEditorId(element.id
));
145 element.togg
= 'off';
146 link.innerHTML
= '$enable';
147 link.href
= "javascript:mceToggle('" +id
+ "', '" +linkid
+ "');";
149 img_assist.innerHTML
= img_assist_default_link
;
156 $status = tinymce_user_get_status($user, $profile);
158 // note we test for string == true because we save our settings as strings
159 $link_text = $status == 'true' ?
$disable : $enable;
160 $img_assist_link = ($status == 'true') ?
'yes' : 'no';
161 $no_wysiwyg = t('Your current web browser does not support WYSIWYG editing.');
162 $wysiwyg_link = <<<EOD
163 <script type
="text/javascript">
164 img_assist
= document.
getElementById('img_assist-link-edit-$textarea_name');
166 var img_assist_default_link
= img_assist.innerHTML
;
167 if ('$img_assist_link' == 'yes') {
168 img_assist.innerHTML
= tinyMCE.
getEditorId('edit-$textarea_name') == null ?
'' : img_assist_default_link
;
171 img_assist.innerHTML
= tinyMCE.
getEditorId('edit-$textarea_name') == null ? img_assist_default_link
: '';
174 if (typeof(document.execCommand
) == 'undefined') {
175 img_assist.innerHTML
= img_assist_default_link
;
176 document.
write('<div style="font-size:x-small">$no_wysiwyg</div>');
179 document.
write("<div><a href=\"javascript:mceToggle('edit-$textarea_name', 'wysiwyg4$textarea_name');\" id=\"wysiwyg4$textarea_name\">$link_text</a></div>");
184 // We only load the TinyMCE js file once per request
187 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
189 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/imagemanager/') && user_access('access tinymce imagemanager') ) {
190 // if tinymce imagemanager is installed
191 drupal_add_js($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/imagemanager/jscripts/mcimagemanager.js');
194 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/filemanager/') && user_access('access tinymce filemanager') ) {
195 // if tinymce filemanager is installed
196 drupal_add_js($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/filemanager/jscripts/mcfilemanager.js');
199 // TinyMCE Compressor
200 if (file_exists($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php')) {
201 drupal_add_js($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php');
204 // For some crazy reason IE will only load this JS file if the absolute reference is given to it.
205 drupal_add_js($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/tiny_mce.js');
207 drupal_set_html_head($js_toggle);
208 // We have to do this becuase of some unfocused CSS in certain themes. See http://drupal.org/node/18879 for details
209 drupal_set_html_head('<style type="text/css" media="all">.mceEditor img { display: inline; }</style>');
211 // Load a TinyMCE init for each textarea.
212 if ($init) drupal_set_html_head($tinymce_invoke);
214 //settings are saved as strings, not booleans
215 if ($profile->settings
['show_toggle'] == 'true') {
216 // Make sure to append to #suffix so it isn't completely overwritten
217 $element['#suffix'] .
= $wysiwyg_link;
221 $element['#resizable'] = TRUE
;
228 * Implementation of hook_user().
230 function tinymce_user($type, &$edit, &$user, $category = NULL
) {
231 if ($type == 'form' && $category == 'account' && user_access('access tinymce')) {
232 $profile = tinymce_user_get_profile($user);
234 // because the settings are saved as strings we need to test for the string 'true'
235 if ($profile->settings
['user_choose'] == 'true') {
236 $form['tinymce'] = array(
237 '#type' => 'fieldset',
238 '#title' => t('TinyMCE rich-text settings'),
240 '#collapsible' => TRUE
,
244 $form['tinymce']['tinymce_status'] = array(
246 '#title' => t('Default state'),
247 '#default_value' => isset($user->tinymce_status
) ?
$user->tinymce_status
: (isset($profile->settings
['default']) ?
$profile->settings
['default'] : 'false'),
248 '#options' => array('false' => 'false', 'true' => 'true'),
249 '#description' => t('Should rich-text editing be enabled or disabled by default in textarea fields?')
252 return array('tinymce' => $form);
255 if ($type == 'validate') {
256 return array('tinymce_status' => $edit['tinymce_status']);
261 * @addtogroup themeable
266 * Customize a TinyMCE theme.
269 * An array of settings TinyMCE should invoke a theme. You may override any
270 * of the TinyMCE settings. Details here:
272 * http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm
274 * @param textarea_name
275 * The name of the textarea TinyMCE wants to enable.
278 * The default tinymce theme name to be enabled for this textarea. The
279 * sitewide default is 'simple', but the user may also override this.
282 * A boolean flag that identifies id TinyMCE is currently running for this
283 * request life cycle. It can be ignored.
285 function theme_tinymce_theme($init, $textarea_name, $theme_name, $is_running) {
286 switch ($textarea_name) {
287 // Disable tinymce for these textareas
288 case
'log': // book and page log
289 case
'img_assist_pages':
290 case
'caption': // signature
292 case
'access_pages': //TinyMCE profile settings.
293 case
'user_mail_welcome_body': // user config settings
294 case
'user_mail_approval_body': // user config settings
295 case
'user_mail_pass_body': // user config settings
296 case
'synonyms': // taxonomy terms
297 case
'description': // taxonomy terms
301 // Force the 'simple' theme for some of the smaller textareas.
305 case
'site_offline_message':
307 case
'user_registration_help':
308 case
'user_picture_guidelines':
309 $init['theme'] = 'simple';
310 foreach ($init as
$k => $v) {
311 if (strstr($k, 'theme_advanced_')) unset($init[$k]);
316 /* Example, add some extra features when using the advanced theme.
318 // If $init is available, we can extend it
320 switch ($theme_name) {
322 $init['extended_valid_elements'] = array('a[href|target|name|title|onclick]');
329 // Always return $init
333 /** @} End of addtogroup themeable */
336 * Grab the themes available to TinyMCE.
338 * TinyMCE themes control the functionality and buttons that are available to a
339 * user. Themes are only looked for within the default TinyMCE theme directory.
341 * NOTE: This function is not used in this release. We are only using advanced theme.
344 * An array of theme names.
346 function _tinymce_get_themes() {
347 static
$themes = array();
350 $theme_loc = drupal_get_path('module', 'tinymce') .
'/tinymce/jscripts/tiny_mce/themes/';
351 if (is_dir($theme_loc) && $dh = opendir($theme_loc)) {
352 while (($file = readdir($dh)) !== false
) {
353 if (!in_array($file, array('.', '..', 'CVS')) && is_dir($theme_loc .
$file)) {
354 $themes[$file] = $file;
366 * Return plugin metadata from the plugin registry.
368 * We also scrape each plugin's *.js file for the human friendly name and help
369 * text URL of each plugin.
372 * An array for each plugin.
374 function _tinymce_get_buttons($skip_metadata = TRUE
) {
375 include_once(drupal_get_path('module', 'tinymce').
'/plugin_reg.php');
376 $plugins = _tinymce_plugins();
378 if ($skip_metadata == FALSE
&& is_array($plugins)) {
379 foreach ($plugins as
$name => $plugin) {
380 $file = drupal_get_path('module', 'tinymce').
'/tinymce/jscripts/tiny_mce/plugins/'.
$name .
'/editor_plugin_src.js';
381 // Grab the plugin metadata by scanning the *.js file.
382 if (file_exists($file)) {
383 $lines = file($file);
384 $has_longname = FALSE
;
385 $has_infourl = FALSE
;
386 foreach ($lines as
$line) {
387 if ($has_longname && $has_infourl) break;
388 if (strstr($line, 'longname')) {
389 $start = strpos($line, "'") + 1;
390 $end = strrpos($line, "'") - $start;
391 $metadata[$name]['longname'] = substr($line, $start, $end);
392 $has_longname = TRUE
;
394 elseif (strstr($line, 'infourl')) {
395 $start = strpos($line, "'") + 1;
396 $end = strrpos($line, "'") - $start;
397 $metadata[$name]['infourl'] = substr($line, $start, $end);
403 // Find out the buttons a plugin has.
404 foreach ($plugin as
$k => $v) {
405 if (strstr($k, 'theme_advanced_buttons')) {
406 $metadata[$name]['buttons'] = array_merge((array) $metadata[$name]['buttons'], $plugin[$k]);
415 /********************************************************************
416 * Module Functions :: Public
417 ********************************************************************/
420 * Controller for tinymce administrative settings.
422 function tinymce_admin($arg = NULL
) {
424 $edit = $_POST['edit'];
427 $op = $arg && !$op ?
$arg : $op;
431 $breadcrumb[] = array('path' => 'admin', 'title' => t('administer'));
432 $breadcrumb[] = array('path' => 'admin/settings/tinymce', 'title' => t('tinymce'));
433 $breadcrumb[] = array('path' => 'admin/settings/tinymce/add', 'title' => t('Add new TinyMCE profile'));
434 menu_set_location($breadcrumb);
435 $output = tinymce_profile_form($edit);
439 drupal_set_title(t('Edit tinymce profile'));
440 $output = tinymce_profile_form(tinymce_profile_load(urldecode(arg(4))));
444 tinymce_profile_delete(urldecode(arg(4)));
445 drupal_set_message(t('Deleted profile'));
446 drupal_goto('admin/settings/tinymce');
449 case
t('Create profile');
450 case
t('Update profile');
451 if (tinymce_profile_validate($edit)) {
452 tinymce_profile_save($edit);
453 $edit['old_name'] ?
drupal_set_message(t('Your TinyMCE profile has been updated.')) : drupal_set_message(t('Your TinyMCE profile has been created.'));
454 drupal_goto('admin/settings/tinymce');
457 $output = tinymce_profile_form($edit);
462 drupal_set_title(t('TinyMCE settings'));
463 //Check if TinyMCE is installed.
464 $tinymce_loc = drupal_get_path('module', 'tinymce') .
'/tinymce/';
465 if (!is_dir($tinymce_loc)) {
466 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');
468 $output = tinymce_profile_overview();
475 * Return an array of initial tinymce config options from the current role.
477 function tinymce_config($profile) {
480 // Drupal theme path.
481 $themepath = path_to_theme() .
'/';
484 $settings = $profile->settings
;
486 // Build a default list of TinyMCE settings.
488 // Is tinymce on by default?
489 $status = tinymce_user_get_status($user, $profile);
491 $init['mode'] = $status == 'true' ?
'exact' : 'none';
492 $init['theme'] = $settings['theme'] ?
$settings['theme'] : 'advanced';
493 $init['relative_urls'] = 'false';
494 $init['document_base_url'] = "$host";
495 $init['language'] = $settings['language'] ?
$settings['language'] : 'en';
496 $init['safari_warning'] = $settings['safari_message'] ?
$settings['safari_message'] : 'false';
497 $init['entity_encoding'] = 'raw';
498 $init['verify_html'] = $settings['verify_html'] ?
$settings['verify_html'] : 'false';
499 $init['preformatted'] = $settings['preformatted'] ?
$settings['preformatted'] : 'false';
500 $init['convert_fonts_to_styles'] = $settings['convert_fonts_to_styles'] ?
$settings['convert_fonts_to_styles'] : 'false';
502 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
503 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/imagemanager/') && user_access('access tinymce imagemanager')) {
504 // we probably need more security than this
505 $init['file_browser_callback'] = "mcImageManager.filebrowserCallBack";
507 if (is_dir($tinymce_mod_path .
'/tinymce/jscripts/tiny_mce/plugins/filemanager/') && user_access('access tinymce filemanager')) {
508 // we probably need more security than this
509 $init['file_browser_callback'] = "mcImageManager.filebrowserCallBack";
512 if ($init['theme'] == 'advanced') {
513 $init['plugins'] = array();
514 $init['theme_advanced_toolbar_location'] = $settings['toolbar_loc'] ?
$settings['toolbar_loc'] : 'bottom';
515 $init['theme_advanced_toolbar_align'] = $settings['toolbar_align'] ?
$settings['toolbar_align'] : 'left';
516 $init['theme_advanced_path_location'] = $settings['path_loc'] ?
$settings['path_loc'] : 'bottom';
517 $init['theme_advanced_resizing'] = $settings['resizing'] ?
$settings['resizing'] : 'true';
518 $init['theme_advanced_blockformats'] = $settings['block_formats'] ?
$settings['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6';
520 if (is_array($settings['buttons'])) {
521 // This gives us the $plugins variable.
522 $plugins = _tinymce_get_buttons();
524 // Find the enabled buttons and the mce row they belong on. Also map the
525 // plugin metadata for each button.
526 $plugin_tracker = array();
527 foreach ($plugins as
$rname => $rplugin) { // Plugin name
528 foreach ($rplugin as
$mce_key => $mce_value) { // TinyMCE key
529 foreach ($mce_value as
$k => $v) { // Buttons
530 if ($settings['buttons'][$rname .
'-' .
$v]) {
531 // Font isn't a true plugin, rather it's buttons made available by the advanced theme
532 if (!in_array($rname, $plugin_tracker) && $rname != 'font') $plugin_tracker[] = $rname;
533 $init[$mce_key][] = $v;
537 // Some advanced plugins only have an $rname and no buttons
538 if ($settings['buttons'][$rname]) {
539 if (!in_array($rname, $plugin_tracker)) $plugin_tracker[] = $rname;
543 // Add the rest of the TinyMCE config options to the $init array for each button.
544 if (is_array($plugin_tracker)) {
545 foreach ($plugin_tracker as
$pname) {
546 if ($pname != 'default') $init['plugins'][] = $pname;
547 foreach ($plugins[$pname] as
$mce_key => $mce_value) {
548 // Don't overwrite buttons or extended_valid_elements
549 if ($mce_key == 'extended_valid_elements') {
550 // $mce_value is an array for extended_valid_elements so just grab the first element in the array (never more than one)
551 $init[$mce_key][] = $mce_value[0];
553 else if (!strstr($mce_key, 'theme_advanced_buttons')) {
554 $init[$mce_key] = $mce_value;
561 foreach ($init as
$mce_key => $mce_value) {
562 if (is_array($mce_value)) $mce_value = array_unique($mce_value);
563 $init[$mce_key] = $mce_value;
566 // Shuffle buttons around so that row 1 always has the most buttons,
567 // followed by row 2, etc. Note: These rows need to be set to NULL otherwise
568 // TinyMCE loads it's own buttons inherited from the theme.
569 if (!$init['theme_advanced_buttons1']) $init['theme_advanced_buttons1'] = array();
570 if (!$init['theme_advanced_buttons2']) $init['theme_advanced_buttons2'] = array();
571 if (!$init['theme_advanced_buttons3']) $init['theme_advanced_buttons3'] = array();
573 $min_btns = 5; // Minimum number of buttons per row.
574 $num1 = count($init['theme_advanced_buttons1']);
575 $num2 = count($init['theme_advanced_buttons2']);
576 $num3 = count($init['theme_advanced_buttons3']);
578 if ($num3 < $min_btns) {
579 $init['theme_advanced_buttons2'] = array_merge($init['theme_advanced_buttons2'], $init['theme_advanced_buttons3']);
580 $init['theme_advanced_buttons3'] = array();
581 $num2 = count($init['theme_advanced_buttons2']);
583 if ($num2 < $min_btns) {
584 $init['theme_advanced_buttons1'] = array_merge($init['theme_advanced_buttons1'], $init['theme_advanced_buttons2']);
585 // Squish the rows together, since row 2 is empty
586 $init['theme_advanced_buttons2'] = $init['theme_advanced_buttons3'];
587 $init['theme_advanced_buttons3'] = array();
588 $num1 = count($init['theme_advanced_buttons1']);
590 if ($num1 < $min_btns) {
591 $init['theme_advanced_buttons1'] = array_merge($init['theme_advanced_buttons1'], $init['theme_advanced_buttons2']);
592 // Squish the rows together, since row 2 is empty
593 $init['theme_advanced_buttons2'] = $init['theme_advanced_buttons3'];
594 $init['theme_advanced_buttons3'] = array();
600 if ($settings['css_classes']) $init['theme_advanced_styles'] = $settings['css_classes'];
602 if ($settings['css_setting'] == 'theme') {
603 $css = $themepath .
'style.css';
604 if (file_exists($css)) {
605 $init['content_css'] = $host .
$css;
608 else if ($settings['css_setting'] == 'self') {
609 $init['content_css'] = str_replace(array('%h', '%t'), array($host, $themepath), $settings['css_path']);
616 * Remove a profile from the database.
618 function tinymce_profile_delete($name) {
619 db_query("DELETE FROM {tinymce_settings} WHERE name = '%s'", $name);
620 db_query("DELETE FROM {tinymce_role} WHERE name = '%s'", $name);
624 * Return an HTML form for profile configuration.
626 function tinymce_profile_form($edit) {
627 $edit = (object) $edit;
629 // Only display the roles that currently don't have a tinymce profile. One
631 $orig_roles = user_roles(FALSE
, 'access tinymce');
632 $roles = $orig_roles;
633 if (arg(3) == 'add') {
634 $result = db_query('SELECT DISTINCT(rid) FROM {tinymce_role}');
635 while ($data = db_fetch_object($result)) {
636 if (!in_array($data->rid
, array_keys((array) $edit->rids
)) && !form_get_errors()){
637 unset($roles[$data->rid
]);
641 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/access'))), 'error');
644 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');
646 else if (count($orig_roles) != count($roles)) {
647 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.'));
649 $btn = t('Create profile');
652 $form['old_name'] = array('#type' => 'hidden', '#value' => $edit->name
);
653 $btn = t('Update profile');
656 $form['basic'] = array(
657 '#type' => 'fieldset',
658 '#title' => t('Basic setup'),
659 '#collapsible' => TRUE
,
663 $form['basic']['name'] = array(
664 '#type' => 'textfield',
665 '#title' => t('Profile name'),
666 '#default_value' => $edit->name
,
669 '#description' => t('Enter a name for this profile. This name is only visible within the tinymce administration page.'),
673 $form['basic']['rids'] = array(
674 '#type' => 'checkboxes',
675 '#title' => t('Roles allowed to use this profile'),
676 '#default_value' => array_keys((array) $edit->rids
),
677 '#options' => $roles,
678 '#description' => t('Check at least one role. Only roles with \'access tinymce\' permission will be shown here.'),
682 $form['basic']['default'] = array(
684 '#title' => t('Default state'),
685 '#default_value' => $edit->settings
['default'] ?
$edit->settings
['default'] : 'false',
686 '#options' => array('false' => 'false', 'true' => 'true'),
687 '#description' => t('Default editor state for users in this profile. Users will be able to override this state if the next option is enabled.'),
690 $form['basic']['user_choose'] = array(
692 '#title' => t('Allow users to choose default'),
693 '#default_value' => $edit->settings
['user_choose'] ?
$edit->settings
['user_choose'] : 'false',
694 '#options' => array('false' => 'false', 'true' => 'true'),
695 '#description' => t('If allowed, users will be able to choose their own TinyMCE default state by visiting their profile page.'),
698 $form['basic']['show_toggle'] = array(
700 '#title' => t('Show disable/enable rich text editor toggle'),
701 '#default_value' => $edit->settings
['show_toggle'] ?
$edit->settings
['show_toggle'] : 'true',
702 '#options' => array('false' => 'false', 'true' => 'true'),
703 '#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).'),
706 // This line upgrades previous versions of TinyMCE for user who previously selected a theme other than advanced.
707 if ($edit->settings
['theme'] != 'advanced') $edit->settings
['theme'] = 'advanced';
709 $form['basic']['theme'] = array(
711 '#value' => $edit->settings
['theme'] ?
$edit->settings
['theme'] : 'advanced'
714 $form['basic']['language'] = array(
716 '#title' => t('Language'),
717 '#default_value' => $edit->settings
['language'] ?
$edit->settings
['language'] : 'en',
718 '#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')),
719 '#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.')
722 $form['basic']['safari_message'] = array(
724 '#title' => t('Safari browser warning'),
725 '#default_value' => $edit->settings
['safari_message'] ?
$edit->settings
['safari_message'] : 'false',
726 '#options' => array('false' => 'false', 'true' => 'true'),
727 '#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.')
730 $form['visibility'] = array(
731 '#type' => 'fieldset',
732 '#title' => t('Visibility'),
733 '#collapsible' => TRUE
,
737 $access = user_access('use PHP for block visibility');
739 // 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
740 if ($edit->settings
['access'] == 2 && !$access) {
741 $form['visibility'] = array();
742 $form['visibility']['access'] = array(
746 $form['visibility']['access_pages'] = array(
748 '#value' => $edit->settings
['access_pages']
752 $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
753 $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>')));
756 $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
757 $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 ?>')));
759 $form['visibility']['access'] = array(
761 '#title' => t('Show tinymce on specific pages'),
762 '#default_value' => isset($edit->settings
['access']) ?
$edit->settings
['access'] : 1,
763 '#options' => $options
765 $form['visibility']['access_pages'] = array(
766 '#type' => 'textarea',
767 '#title' => t('Pages'),
768 '#default_value' => isset($edit->settings
['access_pages']) ?
$edit->settings
['access_pages'] : tinymce_help('admin/settings/tinymce#pages'),
769 '#description' => $description
773 $form['buttons'] = array(
774 '#type' => 'fieldset',
775 '#title' => t('Buttons and plugins'),
776 '#collapsible' => TRUE
,
777 '#collapsed' => TRUE
,
779 '#theme' => 'tinymce_profile_form_buttons'
782 $metadata = _tinymce_get_buttons(FALSE
);
783 // Generate the button list.
784 foreach($metadata as
$name => $meta) {
785 if (is_array($meta['buttons'])) {
786 foreach ($meta['buttons'] as
$button) {
787 if ($name != 'default') {
788 $img_src = drupal_get_path('module', 'tinymce').
"/tinymce/jscripts/tiny_mce/plugins/$name/images/$name.gif";
790 //correct for plugins that have more than one button
791 if (!file_exists($img_src)) {
792 $img_src = drupal_get_path('module', 'tinymce').
"/tinymce/jscripts/tiny_mce/plugins/$name/images/$button.gif";
796 $img_src = drupal_get_path('module', 'tinymce').
"/tinymce/jscripts/tiny_mce/themes/advanced/images/$button.gif";
799 $b = file_exists($img_src) ?
'<img src="'.
base_path() .
$img_src .
'" title="'.
$button .
'" style="border: 1px solid grey; vertical-align: middle;" />' : $button;
801 if ($name == 'default') {
805 $title = $metadata[$name]['longname'] ?
$metadata[$name]['longname'] : $name;
806 if ($metadata[$name]['infourl']) {
807 $title = '<a href="'.
$metadata[$name]['infourl'] .
'" target="_blank">'.
$title .
'</a>';
809 $title = $b .
' – '.
$title;
811 $form_value = $edit->settings
['buttons'][$name .
'-' .
$button];
812 $form['buttons'][$name .
'-' .
$button] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value);
816 $title = $metadata[$name]['longname'] ?
$metadata[$name]['longname'] : $name;
817 if ($metadata[$name]['infourl']) {
818 $title = '<a href="'.
$metadata[$name]['infourl'] .
'" target="_blank">'.
$title .
'</a>';
820 $form_value = $edit->settings
['buttons'][$name];
821 $form['buttons'][$name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value);
825 $form['appearance'] = array(
826 '#type' => 'fieldset',
827 '#title' => t('Editor appearance'),
828 '#collapsible' => TRUE
,
832 $form['appearance']['toolbar_loc'] = array(
834 '#title' => t('Toolbar location'),
835 '#default_value' => $edit->settings
['toolbar_loc'],
836 '#options' => array('bottom' => 'bottom', 'top' => 'top'),
837 '#description' => t('Show toolbar at the top or bottom of the editor area?')
840 $form['appearance']['toolbar_align'] = array(
842 '#title' => t('Toolbar alignment'),
843 '#default_value' => $edit->settings
['toolbar_align'],
844 '#options' => array('center' => 'center', 'left' => 'left', 'right' => 'right'),
845 '#description' => t('Align tool icons left, center, or right within the toolbar.')
848 $form['appearance']['path_loc'] = array(
850 '#title' => t('Path location'),
851 '#default_value' => $edit->settings
['path_loc'] ?
$edit->settings
['path_loc'] : 'bottom',
852 '#options' => array('none' => 'none', 'top' => 'top', 'bottom' => 'bottom'),
853 '#description' => t('Path to html elements (i.e. "body>table>tr>td"). Show at top, bottom, or not at all.')
856 $form['appearance']['resizing'] = array(
858 '#title' => t('Enable resizing button'),
859 '#default_value' => isset($edit->settings
['resizing']) ?
$edit->settings
['resizing'] : 'true',
860 '#options' => array('false' => 'false', 'true' => 'true'),
861 '#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.')
864 $form['appearance']['block_formats'] = array(
865 '#type' => 'textfield',
866 '#title' => t('Block formats'),
867 '#default_value' => $edit->settings
['block_formats'] ?
$edit->settings
['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6',
870 '#description' => t('Comma separated list of HTML block formats. You can only remove elements, not add.')
873 $form['output'] = array(
874 '#type' => 'fieldset',
875 '#title' => t('Cleanup and output'),
876 '#collapsible' => TRUE
,
880 $form['output']['verify_html'] = array(
882 '#title' => t('Verify HTML'),
883 '#default_value' => $edit->settings
['verify_html'],
884 '#options' => array('true' => 'true', 'false' => 'false'),
885 '#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.')
888 $form['output']['preformatted'] = array(
890 '#title' => t('Preformatted'),
891 '#default_value' => $edit->settings
['preformatted'],
892 '#options' => array('false' => 'false', 'true' => 'true'),
893 '#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.')
896 $form['output']['convert_fonts_to_styles'] = array(
898 '#title' => t('Convert <font> tags to styles'),
899 '#default_value' => $edit->settings
['convert_fonts_to_styles'],
900 '#options' => array('true' => 'true', 'false' => 'false'),
901 '#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.')
904 $form['css'] = array(
905 '#type' => 'fieldset',
906 '#title' => t('CSS'),
907 '#collapsible' => TRUE
,
911 $form['css']['css_setting'] = array(
913 '#title' => t('Editor CSS'),
914 '#default_value' => $edit->settings
['css_setting'] ?
$edit->settings
['css_setting'] : 'theme',
915 '#options' => array('theme' => 'use theme css', 'self' => 'define css', 'none' => 'tinyMCE default'),
916 '#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.')
919 $form['css']['css_path'] = array(
920 '#type' => 'textfield',
921 '#title' => t('CSS path'),
922 '#default_value' => $edit->settings
['css_path'],
925 '#description' => t('Enter path to CSS file (example: "css/editor.css").<br />Macros: %h (host name: http://www.example.com/), %t (path to theme: theme/yourtheme/)<br />Be sure to select "define css" above.')
928 $form['css']['css_classes'] = array(
929 '#type' => 'textfield',
930 '#title' => t('CSS classes'),
931 '#default_value' => $edit->settings
['css_classes'],
934 '#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.')
937 $form['submit'] = array(
942 $output .
= drupal_get_form('tinymce_profile_form', $form);
948 * Layout for the buttons in the tinymce profile form
950 function theme_tinymce_profile_form_buttons($form) {
953 // Flatten forms array
954 foreach (element_children($form) as
$key) {
955 $buttons[] = form_render($form[$key]);
958 //split checkboxes into rows with 3 columns
959 $total = count($buttons);
961 for ($i = 0; $i < $total; $i++) {
963 $row[] = array('data' => $buttons[$i]);
964 $row[] = array('data' => $buttons[++$i]);
965 $row[] = array('data' => $buttons[++$i]);
969 $output = theme('table', array(), $rows, array('width' => '100%'));
975 * Load all profiles. Just load one profile if $name is passed in.
977 function tinymce_profile_load($name = '') {
978 static
$profiles = array();
981 $roles = user_roles();
982 $result = db_query('SELECT * FROM {tinymce_settings}');
983 while ($data = db_fetch_object($result)) {
984 $data->settings
= unserialize($data->settings
);
985 $result2 = db_query("SELECT rid FROM {tinymce_role} WHERE name = '%s'", $data->name
);
987 while ($r = db_fetch_object($result2)) {
988 $role[$r->rid
] = $roles[$r->rid
];
992 $profiles[$data->name
] = $data;
996 return ($name ?
$profiles[$name] : $profiles);
1000 * Controller for tinymce profiles.
1002 function tinymce_profile_overview() {
1005 $profiles = tinymce_profile_load();
1007 $roles = user_roles();
1008 $header = array(t('Profile'), t('Roles'), t('Operations'));
1009 foreach ($profiles as
$p) {
1010 $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'));
1012 $output .
= theme('table', $header, $rows);
1013 $output .
= t('<p><a href="%create-profile-url">Create new profile</a></p>', array('%create-profile-url' => url('admin/settings/tinymce/add')));
1016 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'))));
1023 * Save a profile to the database.
1025 function tinymce_profile_save($edit) {
1026 db_query("DELETE FROM {tinymce_settings} WHERE name = '%s' or name = '%s'", $edit['name'], $edit['old_name']);
1027 db_query("DELETE FROM {tinymce_role} WHERE name = '%s' or name = '%s'", $edit['name'], $edit['old_name']);
1028 db_query("INSERT INTO {tinymce_settings} (name, settings) VALUES ('%s', '%s')", $edit['name'], serialize($edit));
1029 foreach ($edit['rids'] as
$rid => $value) {
1030 db_query("INSERT INTO {tinymce_role} (name, rid) VALUES ('%s', %d)", $edit['name'], $rid);
1033 // if users can't set their own defaults, make sure to remove $user->tinymce_status so their default doesn't override the main default
1034 if ($edit['user_choose'] == 'false') {
1036 user_save($user, array('tinymce_status' => NULL
));
1041 * Profile validation.
1043 function tinymce_profile_validate($edit) {
1046 if (!$edit['name']) {
1047 $errors['name'] = t('You must give a profile name.');
1050 if (!$edit['rids']) {
1051 $errors['rids'] = t('You must select at least one role.');
1054 foreach ($errors as
$name => $message) {
1055 form_set_error($name, $message);
1058 return count($errors) == 0;
1061 /********************************************************************
1062 * Module Functions :: Private
1063 ********************************************************************/
1066 * Determine if TinyMCE has permission to be used on the current page.
1069 * TRUE if can render, FALSE if not allowed.
1071 function _tinymce_page_match($edit) {
1072 $page_match = FALSE
;
1074 // Kill TinyMCE if we're editing a textarea with PHP in it!
1075 // PHP input formats are #2 in the filters table.
1076 if (is_numeric(arg(1)) && arg(2) == 'edit') {
1077 $node = node_load(arg(1));
1078 if ($node->format
== 2) {
1083 if ($edit->settings
['access_pages']) {
1084 // If the PHP option wasn't selected
1085 if ($edit->settings
['access'] < 2) {
1086 $path = drupal_get_path_alias($_GET['q']);
1087 $regexp = '/^('.
preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'.
preg_quote(variable_get('site_frontpage', 'node'), '/') .
'\2'), preg_quote($edit->settings
['access_pages'], '/')) .
')$/';
1088 $page_match = !($edit->settings
['access'] xor
preg_match($regexp, $path));
1091 $page_match = drupal_eval($edit->settings
['access_pages']);
1094 // No pages were specified to block so show on all
1102 function tinymce_user_get_profile($account) {
1103 $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
))));
1105 return tinymce_profile_load($profile_name);
1112 function tinymce_user_get_status($user, $profile){
1113 $settings = $profile->settings
;
1115 if ($settings['user_choose']) {
1116 $status = isset($user->tinymce_status
) ?
$user->tinymce_status
: (isset($settings['default']) ?
$settings['default'] : 'false');
1119 $status = isset($settings['default']) ?
$settings['default'] : 'false';