added danish translation
[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 'access' => user_access('administer tinymce'));
20 }
21 return $items;
22 }
23
24 /**
25 * Implementation of hook_help().
26 */
27 function tinymce_help($section) {
28 switch ($section) {
29 case 'admin/modules#description':
30 return t('The TinyMCE Javascript HTML WYSIWYG editor.');
31 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'))
39 );
40 }
41 }
42
43 /**
44 * Implementation of hook_perm().
45 */
46 function tinymce_perm() {
47 $array = array('administer tinymce', 'access tinymce');
48 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
49
50 if (is_dir($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/imagemanager/')) {
51 $array[] = 'access tinymce imagemanager';
52 }
53 if (is_dir($tinymce_mod_path . '/tinymce/jscripts/tiny_mce/plugins/filemanager/')) {
54 $array[] = 'access tinymce filemanager';
55 }
56
57 return $array;
58 }
59
60 /**
61 * Implementation of hook_elements().
62 */
63 function tinymce_elements() {
64 $type = array();
65
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);
69 }
70
71 return $type;
72 }
73
74 /**
75 * Attach tinymce to a textarea
76 */
77 function tinymce_process_textarea($element) {
78 static $is_running = FALSE;
79 global $user;
80 static $profile_name;
81
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);
84
85 // Since tinymce_config() makes a db hit, only call it when we're pretty sure
86 // we're gonna render tinymce.
87 if (!$profile_name) {
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))));
89 }
90 $profile = tinymce_profile_load($profile_name);
91 $init = tinymce_config($profile);
92 $init['elements'] = 'edit-'. $textarea_name;
93
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);
97
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;
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 <script type="text/javascript">
121 tinyMCE.init({
122 $tinymce_settings
123 });
124 </script>
125 EOD;
126
127 $js_toggle = <<<EOD
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);
133
134 if (tinyMCE.getEditorId(element.id) == null) {
135 tinyMCE.addMCEControl(element, element.id);
136 element.togg = 'on';
137 link.innerHTML = '$disable';
138 link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
139 if (img_assist)
140 img_assist.innerHTML = '';
141 link.blur();
142 }
143 else {
144 tinyMCE.removeMCEControl(tinyMCE.getEditorId(element.id));
145 element.togg = 'off';
146 link.innerHTML = '$enable';
147 link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
148 if (img_assist)
149 img_assist.innerHTML = img_assist_default_link;
150 link.blur();
151 }
152 }
153 </script>
154 EOD;
155
156 $status = tinymce_user_get_status($user, $profile);
157
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');
165 if (img_assist) {
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;
169 }
170 else {
171 img_assist.innerHTML = tinyMCE.getEditorId('edit-$textarea_name') == null ? img_assist_default_link : '';
172 }
173 }
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>');
177 }
178 else {
179 document.write("<div><a href=\"javascript:mceToggle('edit-$textarea_name', 'wysiwyg4$textarea_name');\" id=\"wysiwyg4$textarea_name\">$link_text</a></div>");
180 }
181 </script>
182 EOD;
183
184 // We only load the TinyMCE js file once per request
185 if (!$is_running) {
186 $is_running = TRUE;
187 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
188
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');
192 }
193
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');
197 }
198
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');
202 }
203 else {
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');
206 }
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>');
210 }
211 // Load a TinyMCE init for each textarea.
212 if ($init) drupal_set_html_head($tinymce_invoke);
213
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;
218 }
219 }
220 else {
221 $element['#resizable'] = TRUE;
222 }
223
224 return $element;
225 }
226
227 /**
228 * Implementation of hook_user().
229 */
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);
233
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'),
239 '#weight' => 10,
240 '#collapsible' => TRUE,
241 '#collapsed' => TRUE
242 );
243
244 $form['tinymce']['tinymce_status'] = array(
245 '#type' => 'select',
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?')
250 );
251
252 return array('tinymce' => $form);
253 }
254 }
255 if ($type == 'validate') {
256 return array('tinymce_status' => $edit['tinymce_status']);
257 }
258 }
259
260 /**
261 * @addtogroup themeable
262 * @{
263 */
264
265 /**
266 * Customize a TinyMCE theme.
267 *
268 * @param init
269 * An array of settings TinyMCE should invoke a theme. You may override any
270 * of the TinyMCE settings. Details here:
271 *
272 * http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm
273 *
274 * @param textarea_name
275 * The name of the textarea TinyMCE wants to enable.
276 *
277 * @param theme_name
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.
280 *
281 * @param is_running
282 * A boolean flag that identifies id TinyMCE is currently running for this
283 * request life cycle. It can be ignored.
284 */
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
291 case 'pages':
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
298 unset($init);
299 break;
300
301 // Force the 'simple' theme for some of the smaller textareas.
302 case 'signature':
303 case 'site_mission':
304 case 'site_footer':
305 case 'site_offline_message':
306 case 'page_help':
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]);
312 }
313 break;
314 }
315
316 /* Example, add some extra features when using the advanced theme.
317
318 // If $init is available, we can extend it
319 if (isset($init)) {
320 switch ($theme_name) {
321 case 'advanced':
322 $init['extended_valid_elements'] = array('a[href|target|name|title|onclick]');
323 break;
324 }
325 }
326
327 */
328
329 // Always return $init
330 return $init;
331 }
332
333 /** @} End of addtogroup themeable */
334
335 /**
336 * Grab the themes available to TinyMCE.
337 *
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.
340 *
341 * NOTE: This function is not used in this release. We are only using advanced theme.
342 *
343 * @return
344 * An array of theme names.
345 */
346 function _tinymce_get_themes() {
347 static $themes = array();
348
349 if (!$themes) {
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;
355 }
356 }
357 closedir($dh);
358 asort($themes);
359 }
360 }
361
362 return $themes;
363 }
364
365 /**
366 * Return plugin metadata from the plugin registry.
367 *
368 * We also scrape each plugin's *.js file for the human friendly name and help
369 * text URL of each plugin.
370 *
371 * @return
372 * An array for each plugin.
373 */
374 function _tinymce_get_buttons($skip_metadata = TRUE) {
375 include_once(drupal_get_path('module', 'tinymce'). '/plugin_reg.php');
376 $plugins = _tinymce_plugins();
377
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;
393 }
394 elseif (strstr($line, 'infourl')) {
395 $start = strpos($line, "'") + 1;
396 $end = strrpos($line, "'") - $start;
397 $metadata[$name]['infourl'] = substr($line, $start, $end);
398 $has_infourl = TRUE;
399 }
400 }
401 }
402
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]);
407 }
408 }
409 }
410 return $metadata;
411 }
412
413 return $plugins;
414 }
415 /********************************************************************
416 * Module Functions :: Public
417 ********************************************************************/
418
419 /**
420 * Controller for tinymce administrative settings.
421 */
422 function tinymce_admin($arg = NULL) {
423
424 $edit = $_POST['edit'];
425 $op = $_POST['op'];
426
427 $op = $arg && !$op ? $arg : $op;
428
429 switch ($op) {
430 case 'add':
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);
436 break;
437
438 case 'edit':
439 drupal_set_title(t('Edit tinymce profile'));
440 $output = tinymce_profile_form(tinymce_profile_load(urldecode(arg(4))));
441 break;
442
443 case 'delete':
444 tinymce_profile_delete(urldecode(arg(4)));
445 drupal_set_message(t('Deleted profile'));
446 drupal_goto('admin/settings/tinymce');
447 break;
448
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');
455 }
456 else {
457 $output = tinymce_profile_form($edit);
458 }
459 break;
460
461 default:
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');
467 }
468 $output = tinymce_profile_overview();
469 }
470
471 return $output;
472 }
473
474 /**
475 * Return an array of initial tinymce config options from the current role.
476 */
477 function tinymce_config($profile) {
478 global $user;
479
480 // Drupal theme path.
481 $themepath = path_to_theme() . '/';
482 $host = base_path();
483
484 $settings = $profile->settings;
485
486 // Build a default list of TinyMCE settings.
487
488 // Is tinymce on by default?
489 $status = tinymce_user_get_status($user, $profile);
490
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';
501
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";
506 }
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";
510 }
511
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';
519
520 if (is_array($settings['buttons'])) {
521 // This gives us the $plugins variable.
522 $plugins = _tinymce_get_buttons();
523
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;
534 }
535 }
536 }
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;
540 }
541 }
542
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];
552 }
553 else if (!strstr($mce_key, 'theme_advanced_buttons')) {
554 $init[$mce_key] = $mce_value;
555 }
556 }
557 }
558 }
559
560 // Cleanup
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;
564 }
565
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();
572
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']);
577
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']);
582 }
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']);
589 }
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();
595 }
596
597 }
598 }
599
600 if ($settings['css_classes']) $init['theme_advanced_styles'] = $settings['css_classes'];
601
602 if ($settings['css_setting'] == 'theme') {
603 $css = $themepath . 'style.css';
604 if (file_exists($css)) {
605 $init['content_css'] = $host . $css;
606 }
607 }
608 else if ($settings['css_setting'] == 'self') {
609 $init['content_css'] = str_replace(array('%h', '%t'), array($host, $themepath), $settings['css_path']);
610 }
611
612 return $init;
613 }
614
615 /**
616 * Remove a profile from the database.
617 */
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);
621 }
622
623 /**
624 * Return an HTML form for profile configuration.
625 */
626 function tinymce_profile_form($edit) {
627 $edit = (object) $edit;
628
629 // Only display the roles that currently don't have a tinymce profile. One
630 // profile per role.
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]);
638 }
639 }
640 if (!$orig_roles) {
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');
642 }
643 else if (!$roles) {
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');
645 }
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.'));
648 }
649 $btn = t('Create profile');
650 }
651 else {
652 $form['old_name'] = array('#type' => 'hidden', '#value' => $edit->name);
653 $btn = t('Update profile');
654 }
655
656 $form['basic'] = array(
657 '#type' => 'fieldset',
658 '#title' => t('Basic setup'),
659 '#collapsible' => TRUE,
660 '#collapsed' => TRUE
661 );
662
663 $form['basic']['name'] = array(
664 '#type' => 'textfield',
665 '#title' => t('Profile name'),
666 '#default_value' => $edit->name,
667 '#size' => 40,
668 '#maxlength' => 128,
669 '#description' => t('Enter a name for this profile. This name is only visible within the tinymce administration page.'),
670 '#required' => TRUE
671 );
672
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.'),
679 '#required' => TRUE
680 );
681
682 $form['basic']['default'] = array(
683 '#type' => 'select',
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.'),
688 );
689
690 $form['basic']['user_choose'] = array(
691 '#type' => 'select',
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.'),
696 );
697
698 $form['basic']['show_toggle'] = array(
699 '#type' => 'select',
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).'),
704 );
705
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';
708
709 $form['basic']['theme'] = array(
710 '#type' => 'hidden',
711 '#value' => $edit->settings['theme'] ? $edit->settings['theme'] : 'advanced'
712 );
713
714 $form['basic']['language'] = array(
715 '#type' => 'select',
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.')
720 );
721
722 $form['basic']['safari_message'] = array(
723 '#type' => 'select',
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.')
728 );
729
730 $form['visibility'] = array(
731 '#type' => 'fieldset',
732 '#title' => t('Visibility'),
733 '#collapsible' => TRUE,
734 '#collapsed' => TRUE
735 );
736
737 $access = user_access('use PHP for block visibility');
738
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(
743 '#type' => 'value',
744 '#value' => 2
745 );
746 $form['visibility']['access_pages'] = array(
747 '#type' => 'value',
748 '#value' => $edit->settings['access_pages']
749 );
750 }
751 else {
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>')));
754
755 if ($access) {
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 ?>')));
758 }
759 $form['visibility']['access'] = array(
760 '#type' => 'radios',
761 '#title' => t('Show tinymce on specific pages'),
762 '#default_value' => isset($edit->settings['access']) ? $edit->settings['access'] : 1,
763 '#options' => $options
764 );
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
770 );
771 }
772
773 $form['buttons'] = array(
774 '#type' => 'fieldset',
775 '#title' => t('Buttons and plugins'),
776 '#collapsible' => TRUE,
777 '#collapsed' => TRUE,
778 '#tree' => TRUE,
779 '#theme' => 'tinymce_profile_form_buttons'
780 );
781
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";
789
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";
793 }
794 }
795 else {
796 $img_src = drupal_get_path('module', 'tinymce'). "/tinymce/jscripts/tiny_mce/themes/advanced/images/$button.gif";
797 }
798
799 $b = file_exists($img_src) ? '<img src="'. base_path() . $img_src .'" title="'. $button .'" style="border: 1px solid grey; vertical-align: middle;" />' : $button;
800
801 if ($name == 'default') {
802 $title = $b;
803 }
804 else {
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>';
808 }
809 $title = $b . ' &#8211; '. $title;
810 }
811 $form_value = $edit->settings['buttons'][$name . '-' . $button];
812 $form['buttons'][$name . '-' . $button] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value);
813 }
814 }
815 else {
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>';
819 }
820 $form_value = $edit->settings['buttons'][$name];
821 $form['buttons'][$name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value);
822 }
823 }
824
825 $form['appearance'] = array(
826 '#type' => 'fieldset',
827 '#title' => t('Editor appearance'),
828 '#collapsible' => TRUE,
829 '#collapsed' => TRUE
830 );
831
832 $form['appearance']['toolbar_loc'] = array(
833 '#type' => 'select',
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?')
838 );
839
840 $form['appearance']['toolbar_align'] = array(
841 '#type' => 'select',
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.')
846 );
847
848 $form['appearance']['path_loc'] = array(
849 '#type' => 'select',
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.')
854 );
855
856 $form['appearance']['resizing'] = array(
857 '#type' => 'select',
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.')
862 );
863
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',
868 '#size' => 40,
869 '#maxlength' => 250,
870 '#description' => t('Comma separated list of HTML block formats. You can only remove elements, not add.')
871 );
872
873 $form['output'] = array(
874 '#type' => 'fieldset',
875 '#title' => t('Cleanup and output'),
876 '#collapsible' => TRUE,
877 '#collapsed' => TRUE
878 );
879
880 $form['output']['verify_html'] = array(
881 '#type' => 'select',
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 &lt;head&gt tags, so choose false if you will be editing full page HTML.')
886 );
887
888 $form['output']['preformatted'] = array(
889 '#type' => 'select',
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.')
894 );
895
896 $form['output']['convert_fonts_to_styles'] = array(
897 '#type' => 'select',
898 '#title' => t('Convert &lt;font&gt; 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.')
902 );
903
904 $form['css'] = array(
905 '#type' => 'fieldset',
906 '#title' => t('CSS'),
907 '#collapsible' => TRUE,
908 '#collapsed' => TRUE
909 );
910
911 $form['css']['css_setting'] = array(
912 '#type' => 'select',
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.')
917 );
918
919 $form['css']['css_path'] = array(
920 '#type' => 'textfield',
921 '#title' => t('CSS path'),
922 '#default_value' => $edit->settings['css_path'],
923 '#size' => 40,
924 '#maxlength' => 255,
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.')
926 );
927
928 $form['css']['css_classes'] = array(
929 '#type' => 'textfield',
930 '#title' => t('CSS classes'),
931 '#default_value' => $edit->settings['css_classes'],
932 '#size' => 40,
933 '#maxlength' => 255,
934 '#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.')
935 );
936
937 $form['submit'] = array(
938 '#type' => 'submit',
939 '#value' => $btn
940 );
941
942 $output .= drupal_get_form('tinymce_profile_form', $form);
943
944 return $output;
945 }
946
947 /**
948 * Layout for the buttons in the tinymce profile form
949 */
950 function theme_tinymce_profile_form_buttons($form) {
951 $buttons = array();
952
953 // Flatten forms array
954 foreach (element_children($form) as $key) {
955 $buttons[] = form_render($form[$key]);
956 }
957
958 //split checkboxes into rows with 3 columns
959 $total = count($buttons);
960 $rows = array();
961 for ($i = 0; $i < $total; $i++) {
962 $row = array();
963 $row[] = array('data' => $buttons[$i]);
964 $row[] = array('data' => $buttons[++$i]);
965 $row[] = array('data' => $buttons[++$i]);
966 $rows[] = $row;
967 }
968
969 $output = theme('table', array(), $rows, array('width' => '100%'));
970
971 return $output;
972 }
973
974 /**
975 * Load all profiles. Just load one profile if $name is passed in.
976 */
977 function tinymce_profile_load($name = '') {
978 static $profiles = array();
979
980 if (!$profiles) {
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);
986 $role = array();
987 while ($r = db_fetch_object($result2)) {
988 $role[$r->rid] = $roles[$r->rid];
989 }
990 $data->rids = $role;
991
992 $profiles[$data->name] = $data;
993 }
994 }
995
996 return ($name ? $profiles[$name] : $profiles);
997 }
998
999 /**
1000 * Controller for tinymce profiles.
1001 */
1002 function tinymce_profile_overview() {
1003 $output = '';
1004
1005 $profiles = tinymce_profile_load();
1006 if ($profiles) {
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'));
1011 }
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')));
1014 }
1015 else {
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'))));
1017 }
1018
1019 return $output;
1020 }
1021
1022 /**
1023 * Save a profile to the database.
1024 */
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);
1031 }
1032
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') {
1035 global $user;
1036 user_save($user, array('tinymce_status' => NULL));
1037 }
1038 }
1039
1040 /**
1041 * Profile validation.
1042 */
1043 function tinymce_profile_validate($edit) {
1044 $errors = array();
1045
1046 if (!$edit['name']) {
1047 $errors['name'] = t('You must give a profile name.');
1048 }
1049
1050 if (!$edit['rids']) {
1051 $errors['rids'] = t('You must select at least one role.');
1052 }
1053
1054 foreach ($errors as $name => $message) {
1055 form_set_error($name, $message);
1056 }
1057
1058 return count($errors) == 0;
1059 }
1060
1061 /********************************************************************
1062 * Module Functions :: Private
1063 ********************************************************************/
1064
1065 /**
1066 * Determine if TinyMCE has permission to be used on the current page.
1067 *
1068 * @return
1069 * TRUE if can render, FALSE if not allowed.
1070 */
1071 function _tinymce_page_match($edit) {
1072 $page_match = FALSE;
1073
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) {
1079 return FALSE;
1080 }
1081 }
1082
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));
1089 }
1090 else {
1091 $page_match = drupal_eval($edit->settings['access_pages']);
1092 }
1093 }
1094 // No pages were specified to block so show on all
1095 else {
1096 $page_match = TRUE;
1097 }
1098
1099 return $page_match;
1100 }
1101
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))));
1104 if ($profile_name){
1105 return tinymce_profile_load($profile_name);
1106 }
1107 else {
1108 return FALSE;
1109 }
1110 }
1111
1112 function tinymce_user_get_status($user, $profile){
1113 $settings = $profile->settings;
1114
1115 if ($settings['user_choose']) {
1116 $status = isset($user->tinymce_status) ? $user->tinymce_status : (isset($settings['default']) ? $settings['default'] : 'false');
1117 }
1118 else {
1119 $status = isset($settings['default']) ? $settings['default'] : 'false';
1120 }
1121
1122 return $status;
1123 }