b64e4c813f590c9bd3d4db3904f1075c73b3f87e
[project/tinymce.git] / tinymce.module
1 <?php
2 // $Id$
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>
5
6 /**
7 * @file
8 * Integrate the TinyMCE editor (http://tinymce.moxiecode.com/) into Drupal.
9 */
10
11 /**
12 * Implementation of hook_menu().
13 */
14 function tinymce_menu($may_cache) {
15 $items = array();
16 if ($may_cache) {
17 $items[] = array('path' => 'admin/settings/tinymce', 'title' => t('TinyMCE'),
18 'callback' => 'tinymce_admin',
19 'description' => t('Configure the rich editor.'),
20 'access' => user_access('administer tinymce'));
21 }
22 return $items;
23 }
24
25 /**
26 * Implementation of hook_help().
27 */
28 function tinymce_help($section) {
29 switch ($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'))
38 );
39 }
40 }
41
42 /**
43 * Implementation of hook_perm().
44 */
45 function tinymce_perm() {
46 $array = array('administer tinymce', 'access tinymce');
47 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
48
49 if (is_dir($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/imagemanager/')) {
50 $array[] = 'access tinymce imagemanager';
51 }
52 if (is_dir($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/filemanager/')) {
53 $array[] = 'access tinymce filemanager';
54 }
55
56 return $array;
57 }
58
59 /**
60 * Implementation of hook_elements().
61 */
62 function tinymce_elements() {
63 $type = array();
64
65 if (user_access('access tinymce')) {
66 // Let TinyMCE potentially process each textarea.
67 $type['textarea'] = array('#process' => array('tinymce_process_textarea' => array()));
68 }
69
70 return $type;
71 }
72
73 /**
74 * Attach tinymce to a textarea
75 */
76 function tinymce_process_textarea($element) {
77 static $is_running = FALSE;
78 global $user;
79 static $profile_name;
80
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);
83
84 // Since tinymce_config() makes a db hit, only call it when we're pretty sure
85 // we're gonna render tinymce.
86 if (!$profile_name) {
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))));
88 if (!$profile_name) {
89 return $element;
90 }
91 }
92 $profile = tinymce_profile_load($profile_name);
93 $init = tinymce_config($profile);
94 $init['elements'] = 'edit-'. $textarea_name;
95
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);
99
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) {
102 return $element;
103 }
104
105 $settings = array();
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] != '{') {
110 $v = '"'. $v. '"';
111 }
112 $settings[] = $k. ' : '. $v;
113 }
114 $tinymce_settings = implode(",\n ", $settings);
115
116 $enable = t('enable rich-text');
117 $disable = t('disable rich-text');
118
119 $tinymce_invoke = <<<EOD
120
121 tinyMCE.init({
122 $tinymce_settings
123 });
124
125 EOD;
126
127 $tinymce_gz_invoke = <<<EOD
128
129 tinyMCE_GZ.init({
130 $tinymce_settings
131 });
132
133 EOD;
134
135 $js_toggle = <<<EOD
136
137 function mceToggle(id, linkid) {
138 element = document.getElementById(id);
139 link = document.getElementById(linkid);
140 img_assist = document.getElementById('img_assist-link-'+ id);
141
142 if (tinyMCE.getEditorId(element.id) == null) {
143 tinyMCE.addMCEControl(element, element.id);
144 element.togg = 'on';
145 link.innerHTML = '$disable';
146 link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
147 if (img_assist)
148 img_assist.innerHTML = '';
149 link.blur();
150 }
151 else {
152 tinyMCE.removeMCEControl(tinyMCE.getEditorId(element.id));
153 element.togg = 'off';
154 link.innerHTML = '$enable';
155 link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
156 if (img_assist)
157 img_assist.innerHTML = img_assist_default_link;
158 link.blur();
159 }
160 }
161
162 EOD;
163
164 $status = tinymce_user_get_status($user, $profile);
165
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');
173 if (img_assist) {
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;
177 }
178 else {
179 img_assist.innerHTML = tinyMCE.getEditorId('edit-$textarea_name') == null ? img_assist_default_link : '';
180 }
181 }
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>');
185 }
186 else {
187 document.write("<div><a href=\"javascript:mceToggle('edit-$textarea_name', 'wysiwyg4$textarea_name');\" id=\"wysiwyg4$textarea_name\">$link_text</a></div>");
188 }
189 </script>
190 EOD;
191
192 // We only load the TinyMCE js file once per request
193 if (!$is_running) {
194 $is_running = TRUE;
195 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
196
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');
200 }
201
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');
205 }
206
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');
211 }
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');
215 }
216 else {
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');
219 }
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>');
223 }
224 // Load a TinyMCE init for each textarea.
225 if ($init) drupal_add_js($tinymce_invoke, 'inline');
226
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;
231 }
232 // Set resizable to false to avoid drupal.js resizable function from taking control of the textarea
233 $element['#resizable'] = FALSE;
234 }
235
236 return $element;
237 }
238
239 /**
240 * Implementation of hook_user().
241 */
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);
245
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'),
251 '#weight' => 10,
252 '#collapsible' => TRUE,
253 '#collapsed' => TRUE
254 );
255
256 $form['tinymce']['tinymce_status'] = array(
257 '#type' => 'select',
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?')
262 );
263
264 return array('tinymce' => $form);
265 }
266 }
267 if ($type == 'validate') {
268 return array('tinymce_status' => $edit['tinymce_status']);
269 }
270 }
271
272 /**
273 * @addtogroup themeable
274 * @{
275 */
276
277 /**
278 * Customize a TinyMCE theme.
279 *
280 * @param init
281 * An array of settings TinyMCE should invoke a theme. You may override any
282 * of the TinyMCE settings. Details here:
283 *
284 * http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm
285 *
286 * @param textarea_name
287 * The name of the textarea TinyMCE wants to enable.
288 *
289 * @param theme_name
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.
292 *
293 * @param is_running
294 * A boolean flag that identifies id TinyMCE is currently running for this
295 * request life cycle. It can be ignored.
296 */
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
303 case 'pages':
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
310 unset($init);
311 break;
312
313 // Force the 'simple' theme for some of the smaller textareas.
314 case 'signature':
315 case 'site_mission':
316 case 'site_footer':
317 case 'site_offline_message':
318 case 'page_help':
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]);
324 }
325 break;
326 }
327
328 /* Example, add some extra features when using the advanced theme.
329
330 // If $init is available, we can extend it
331 if (isset($init)) {
332 switch ($theme_name) {
333 case 'advanced':
334 $init['extended_valid_elements'] = array('a[href|target|name|title|onclick]');
335 break;
336 }
337 }
338
339 */
340
341 // Always return $init
342 return $init;
343 }
344
345 /** @} End of addtogroup themeable */
346
347 /**
348 * Grab the themes available to TinyMCE.
349 *
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.
352 *
353 * NOTE: This function is not used in this release. We are only using advanced theme.
354 *
355 * @return
356 * An array of theme names.
357 */
358 function _tinymce_get_themes() {
359 static $themes = array();
360
361 if (!$themes) {
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;
367 }
368 }
369 closedir($dh);
370 asort($themes);
371 }
372 }
373
374 return $themes;
375 }
376
377 /**
378 * Return plugin metadata from the plugin registry.
379 *
380 * We also scrape each plugin's *.js file for the human friendly name and help
381 * text URL of each plugin.
382 *
383 * @return
384 * An array for each plugin.
385 */
386 function _tinymce_get_buttons($skip_metadata = TRUE) {
387 include_once(drupal_get_path('module', 'tinymce'). '/plugin_reg.php');
388 $plugins = _tinymce_plugins();
389
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;
405 }
406 elseif (strstr($line, 'infourl')) {
407 $start = strpos($line, "'") + 1;
408 $end = strrpos($line, "'") - $start;
409 $metadata[$name]['infourl'] = substr($line, $start, $end);
410 $has_infourl = TRUE;
411 }
412 }
413 }
414
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]);
419 }
420 }
421 }
422 return $metadata;
423 }
424
425 return $plugins;
426 }
427 /********************************************************************
428 * Module Functions :: Public
429 ********************************************************************/
430
431 /**
432 * Controller for tinymce administrative settings.
433 */
434 function tinymce_admin($arg = NULL) {
435
436 $edit = $_POST;
437 $op = $_POST['op'];
438
439 $op = $arg && !$op ? $arg : $op;
440
441 switch ($op) {
442 case 'add':
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);
448 break;
449
450 case 'edit':
451 drupal_set_title(t('Edit tinymce profile'));
452 $output = tinymce_profile_form(tinymce_profile_load(urldecode(arg(4))));
453 break;
454
455 case 'delete':
456 tinymce_profile_delete(urldecode(arg(4)));
457 drupal_set_message(t('Deleted profile'));
458 drupal_goto('admin/settings/tinymce');
459 break;
460
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');
467 }
468 else {
469 $output = tinymce_profile_form($edit);
470 }
471 break;
472
473 default:
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');
479 }
480 $output = tinymce_profile_overview();
481 }
482
483 return $output;
484 }
485
486 /**
487 * Return an array of initial tinymce config options from the current role.
488 */
489 function tinymce_config($profile) {
490 global $user;
491
492 // Drupal theme path.
493 $themepath = path_to_theme() . '/';
494 $host = base_path();
495
496 $settings = $profile->settings;
497
498 // Build a default list of TinyMCE settings.
499
500 // Is tinymce on by default?
501 $status = tinymce_user_get_status($user, $profile);
502
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';
515
516
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";
521 }
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";
525 }
526
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';
534
535 if (is_array($settings['buttons'])) {
536 // This gives us the $plugins variable.
537 $plugins = _tinymce_get_buttons();
538
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;
549 }
550 }
551 }
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;
555 }
556 }
557
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];
567 }
568 else if (!strstr($mce_key, 'theme_advanced_buttons')) {
569 $init[$mce_key] = $mce_value;
570 }
571 }
572 }
573 }
574
575 // Cleanup
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;
579 }
580
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();
587
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']);
592
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']);
597 }
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']);
604 }
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();
610 }
611
612 }
613 }
614
615 if ($settings['css_classes']) $init['theme_advanced_styles'] = $settings['css_classes'];
616
617 if ($settings['css_setting'] == 'theme') {
618 $css = $themepath . 'style.css';
619 if (file_exists($css)) {
620 $init['content_css'] = $host . $css;
621 }
622 }
623 else if ($settings['css_setting'] == 'self') {
624 $init['content_css'] = str_replace(array('%h', '%t'), array($host, $themepath), $settings['css_path']);
625 }
626
627 return $init;
628 }
629
630 /**
631 * Remove a profile from the database.
632 */
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);
636 }
637
638 /**
639 * Return an HTML form for profile configuration.
640 */
641 function tinymce_profile_form_build($edit) {
642 $edit = (object) $edit;
643
644 // Only display the roles that currently don't have a tinymce profile. One
645 // profile per role.
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]);
653 }
654 }
655 if (!$orig_roles) {
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');
657 }
658 else if (!$roles) {
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');
660 }
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.'));
663 }
664 $btn = t('Create profile');
665 }
666 else {
667 $form['old_name'] = array('#type' => 'hidden', '#value' => $edit->name);
668 $btn = t('Update profile');
669 }
670
671 $form['basic'] = array(
672 '#type' => 'fieldset',
673 '#title' => t('Basic setup'),
674 '#collapsible' => TRUE,
675 '#collapsed' => TRUE
676 );
677
678 $form['basic']['name'] = array(
679 '#type' => 'textfield',
680 '#title' => t('Profile name'),
681 '#default_value' => $edit->name,
682 '#size' => 40,
683 '#maxlength' => 128,
684 '#description' => t('Enter a name for this profile. This name is only visible within the tinymce administration page.'),
685 '#required' => TRUE
686 );
687
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.'),
694 '#required' => TRUE
695 );
696
697 $form['basic']['default'] = array(
698 '#type' => 'select',
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.'),
703 );
704
705 $form['basic']['user_choose'] = array(
706 '#type' => 'select',
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.'),
711 );
712
713 $form['basic']['show_toggle'] = array(
714 '#type' => 'select',
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).'),
719 );
720
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';
723
724 $form['basic']['theme'] = array(
725 '#type' => 'hidden',
726 '#value' => $edit->settings['theme'] ? $edit->settings['theme'] : 'advanced'
727 );
728
729 $form['basic']['language'] = array(
730 '#type' => 'select',
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.')
735 );
736
737 $form['basic']['safari_message'] = array(
738 '#type' => 'select',
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.')
743 );
744
745 $form['visibility'] = array(
746 '#type' => 'fieldset',
747 '#title' => t('Visibility'),
748 '#collapsible' => TRUE,
749 '#collapsed' => TRUE
750 );
751
752 $access = user_access('use PHP for block visibility');
753
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(
758 '#type' => 'value',
759 '#value' => 2
760 );
761 $form['visibility']['access_pages'] = array(
762 '#type' => 'value',
763 '#value' => $edit->settings['access_pages']
764 );
765 }
766 else {
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>')));
769
770 if ($access) {
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 ?>')));
773 }
774 $form['visibility']['access'] = array(
775 '#type' => 'radios',
776 '#title' => t('Show tinymce on specific pages'),
777 '#default_value' => isset($edit->settings['access']) ? $edit->settings['access'] : 1,
778 '#options' => $options
779 );
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
785 );
786 }
787
788 $form['buttons'] = array(
789 '#type' => 'fieldset',
790 '#title' => t('Buttons and plugins'),
791 '#collapsible' => TRUE,
792 '#collapsed' => TRUE,
793 '#tree' => TRUE,
794 '#theme' => 'tinymce_profile_form_buttons'
795 );
796
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";
804
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";
808 }
809 }
810 else {
811 $img_src = drupal_get_path('module', 'tinymce'). "/tinymce/jscripts/tiny_mce/themes/advanced/images/$button.gif";
812 }
813
814 $b = file_exists($img_src) ? '<img src="'. base_path() . $img_src .'" title="'. $button .'" style="border: 1px solid grey; vertical-align: middle;" />' : $button;
815
816 if ($name == 'default') {
817 $title = $b;
818 }
819 else {
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>';
823 }
824 $title = $b . ' &#8211; '. $title;
825 }
826 $form_value = $edit->settings['buttons'][$name . '-' . $button];
827 $form['buttons'][$name . '-' . $button] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value);
828 }
829 }
830 else {
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>';
834 }
835 $form_value = $edit->settings['buttons'][$name];
836 $form['buttons'][$name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value);
837 }
838 }
839
840 $form['appearance'] = array(
841 '#type' => 'fieldset',
842 '#title' => t('Editor appearance'),
843 '#collapsible' => TRUE,
844 '#collapsed' => TRUE
845 );
846
847 $form['appearance']['toolbar_loc'] = array(
848 '#type' => 'select',
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?')
853 );
854
855 $form['appearance']['toolbar_align'] = array(
856 '#type' => 'select',
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.')
861 );
862
863 $form['appearance']['path_loc'] = array(
864 '#type' => 'select',
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.')
869 );
870
871 $form['appearance']['resizing'] = array(
872 '#type' => 'select',
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.')
877 );
878
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',
883 '#size' => 40,
884 '#maxlength' => 250,
885 '#description' => t('Comma separated list of HTML block formats. You can only remove elements, not add.')
886 );
887
888 $form['output'] = array(
889 '#type' => 'fieldset',
890 '#title' => t('Cleanup and output'),
891 '#collapsible' => TRUE,
892 '#collapsed' => TRUE
893 );
894
895 $form['output']['verify_html'] = array(
896 '#type' => 'select',
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 &lt;head&gt tags, so choose false if you will be editing full page HTML.')
901 );
902
903 $form['output']['preformatted'] = array(
904 '#type' => 'select',
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.')
909 );
910
911 $form['output']['convert_fonts_to_styles'] = array(
912 '#type' => 'select',
913 '#title' => t('Convert &lt;font&gt; 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.')
917 );
918
919 $form['css'] = array(
920 '#type' => 'fieldset',
921 '#title' => t('CSS'),
922 '#collapsible' => TRUE,
923 '#collapsed' => TRUE
924 );
925
926 $form['css']['css_setting'] = array(
927 '#type' => 'select',
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.')
932 );
933
934 $form['css']['css_path'] = array(
935 '#type' => 'textfield',
936 '#title' => t('CSS path'),
937 '#default_value' => $edit->settings['css_path'],
938 '#size' => 40,
939 '#maxlength' => 255,
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.')
941 );
942
943 $form['css']['css_classes'] = array(
944 '#type' => 'textfield',
945 '#title' => t('CSS classes'),
946 '#default_value' => $edit->settings['css_classes'],
947 '#size' => 40,
948 '#maxlength' => 255,
949 '#description' => t('Adds CSS classes to the "styles" droplist. Format is: &lt;title&gt;=&lt;class&gt;;<br/> Example: Header 1=header1;Header 2=header2;Header 3=header3 (note: no trailing \';\')<br />Leave blank to automatically import list of CSS classes from style sheet.')
950 );
951
952 $form['submit'] = array(
953 '#type' => 'submit',
954 '#value' => $btn
955 );
956
957 return $form;
958 }
959
960 /**
961 * Return an HTML form for profile configuration.
962 */
963 function tinymce_profile_form($edit) {
964
965 $output .= drupal_get_form('tinymce_profile_form_build', $edit);
966
967 return $output;
968 }
969
970 /**
971 * Layout for the buttons in the tinymce profile form
972 */
973 function theme_tinymce_profile_form_buttons($form) {
974 $buttons = array();
975
976 // Flatten forms array
977 foreach (element_children($form) as $key) {
978 $buttons[] = drupal_render($form[$key]);
979 }
980
981 //split checkboxes into rows with 3 columns
982 $total = count($buttons);
983 $rows = array();
984 for ($i = 0; $i < $total; $i++) {
985 $row = array();
986 $row[] = array('data' => $buttons[$i]);
987 $row[] = array('data' => $buttons[++$i]);
988 $row[] = array('data' => $buttons[++$i]);
989 $rows[] = $row;
990 }
991
992 $output = theme('table', array(), $rows, array('width' => '100%'));
993
994 return $output;
995 }
996
997 /**
998 * Load all profiles. Just load one profile if $name is passed in.
999 */
1000 function tinymce_profile_load($name = '') {
1001 static $profiles = array();
1002
1003 if (!$profiles) {
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);
1009 $role = array();
1010 while ($r = db_fetch_object($result2)) {
1011 $role[$r->rid] = $roles[$r->rid];
1012 }
1013 $data->rids = $role;
1014
1015 $profiles[$data->name] = $data;
1016 }
1017 }
1018
1019 return ($name ? $profiles[$name] : $profiles);
1020 }
1021
1022 /**
1023 * Controller for tinymce profiles.
1024 */
1025 function tinymce_profile_overview() {
1026 $output = '';
1027
1028 $profiles = tinymce_profile_load();
1029 if ($profiles) {
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'));
1034 }
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')));
1037 }
1038 else {
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'))));
1040 }
1041
1042 return $output;
1043 }
1044
1045 /**
1046 * Save a profile to the database.
1047 */
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);
1054 }
1055
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') {
1058 global $user;
1059 user_save($user, array('tinymce_status' => NULL));
1060 }
1061 }
1062
1063 /**
1064 * Profile validation.
1065 */
1066 function tinymce_profile_validate($edit) {
1067 $errors = array();
1068
1069 if (!$edit['name']) {
1070 $errors['name'] = t('You must give a profile name.');
1071 }
1072
1073 if (!$edit['rids']) {
1074 $errors['rids'] = t('You must select at least one role.');
1075 }
1076
1077 foreach ($errors as $name => $message) {
1078 form_set_error($name, $message);
1079 }
1080
1081 return count($errors) == 0;
1082 }
1083
1084 /********************************************************************
1085 * Module Functions :: Private
1086 ********************************************************************/
1087
1088 /**
1089 * Determine if TinyMCE has permission to be used on the current page.
1090 *
1091 * @return
1092 * TRUE if can render, FALSE if not allowed.
1093 */
1094 function _tinymce_page_match($edit) {
1095 $page_match = FALSE;
1096
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) {
1102 return FALSE;
1103 }
1104 }
1105
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));
1112 }
1113 else {
1114 $page_match = drupal_eval($edit->settings['access_pages']);
1115 }
1116 }
1117 // No pages were specified to block so show on all
1118 else {
1119 $page_match = TRUE;
1120 }
1121
1122 return $page_match;
1123 }
1124
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))));
1127 if ($profile_name){
1128 return tinymce_profile_load($profile_name);
1129 }
1130 else {
1131 return FALSE;
1132 }
1133 }
1134
1135 function tinymce_user_get_status($user, $profile){
1136 $settings = $profile->settings;
1137
1138 if ($settings['user_choose']) {
1139 $status = isset($user->tinymce_status) ? $user->tinymce_status : (isset($settings['default']) ? $settings['default'] : 'false');
1140 }
1141 else {
1142 $status = isset($settings['default']) ? $settings['default'] : 'false';
1143 }
1144
1145 return $status;
1146 }