* Patch #38127 by m3avrck
[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
32 case 'admin/settings/tinymce#pages':
33 return "node/*\nuser/*\ncomment/*";
34
35 case 'admin/settings/tinymce':
36 return t('<p>TinyMCE adds what-you-see-is-what-you-get (WYSIWYG) html editing to textareas. Profiles can be defined based on user roles. 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. The default profile setting uses the "simple" TinyMCE theme which just shows the most minimal buttons set (bold, italic, underline, etc), but many other settings are available by switching to "advanced". And by default, textareas on affected pages will be automatically swapped out with the rich text editor. Users may disable the editor for any textarea without reloading the page. This setting can be reversed so that pages load with conventional textarea form fields. A link below each textarea allows TinyMCE to be turned on or off "on the fly".</p><p>Be aware that things can get confusing if a user is a member of roles defined in multiple profiles. In this case, the user will receive the profile for role with lowest role id they belong to. Did I mention it was confusing?</p><p>lastly, only users with the <code>access tinymce</code> permission will be able to use TinyMCE.</p>');
37 }
38 }
39
40 /**
41 * Implementation of hook_perm().
42 */
43 function tinymce_perm() {
44 return array('administer tinymce', 'access tinymce');
45 }
46
47 /**
48 * Implementation of hook_img_assist_head().
49 */
50 function tinymce_img_assist_head() {
51 global $base_url;
52 // The tinymce docs say to include tiny_mce_popup.js, but this was killing IE!
53 $popup_path = $base_url .'/'. drupal_get_path('module', 'tinymce'). '/tinymce/jscripts/tiny_mce/tiny_mce_popup.js';
54 $img_assist_prop = $img_template = '';
55 if (module_exist('img_assist')) {
56 $img_assist_prop = $base_url .'/'. drupal_get_path('module', 'img_assist'). '/properties.js';
57 $img_assist_prop = '<script language="javascript" src="'. $img_assist_prop .'"></script>';
58
59 $img_template = variable_get('img_assist_img_html', img_assist_help('img_assist/template'));
60 $nl = "\n";
61 $img_template = preg_replace('/\n|\r|(\r\n)/m', '\\'. $nl, $img_template);
62 }
63 $clean_url = variable_get('clean_url', 0);
64
65 $output = <<<EOD
66 $img_assist_prop
67 <script language="javascript">
68 var clean_url = $clean_url;
69 var img_template = '$img_template';
70
71 function insertImage(form) {
72 if (window.opener) {
73 form['edit[filepath]'].value = window.opener.tinyMCE.convertURL(form['edit[filepath]'].value);
74 form['edit[nodePath]'].value = window.opener.tinyMCE.convertURL(form['edit[nodePath]'].value);
75
76 var img = generate_image_tag(form, 'html');
77 //img = img.replace(/\\r|\\n|\\t/g, '');
78
79 window.opener.tinyMCE.execInstanceCommand(myTextarea.name, 'mceInsertContent', false, img, true);
80 }
81 }
82 </script>
83 EOD;
84
85 return $output;
86 }
87
88 /**
89 * Implementation of hook_img_assist_on_submit().
90 */
91 function tinymce_img_assist_on_submit() {
92 return 'parent.insertImage(this.form);';
93 }
94
95 /**
96 * Implementation of hook_elements().
97 */
98 function tinymce_elements() {
99 $type['textarea'] = array('#process' => 'tinymce_process_textarea');
100 return $type;
101 }
102
103 /**
104 * Attach tinymce to a textarea
105 */
106 function tinymce_process_textarea($element) {
107 static $is_running = FALSE;
108 if (!user_access('access tinymce')) return NULL;
109
110 global $user;
111 global $base_url;
112 static $profile_name;
113
114 //$element is an array of attributes for the textarea but there is no just 'name' value, so we extract this from the #id field
115 $textarea_name = substr($element['#id'], strpos($element['#id'], '-') + 1);
116
117 // Since tinymce_config() makes a db hit, only call it when we're pretty sure
118 // we're gonna render tinymce.
119 if (!$profile_name) {
120 $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))));
121 }
122 $profile = tinymce_profile_load($profile_name);
123 $init = tinymce_config($profile);
124 $init['elements'] = 'edit['. $textarea_name .']';
125
126 if (_tinymce_page_match($profile)) {
127 // Merge user-defined TinyMCE settings.
128 $init = (array) theme('tinymce_theme', $init, $textarea_name, $init['theme'], $is_running);
129
130 // If $init array is empty no need to execute rest of code since there are no textareas to theme with TinyMCE
131 if (count($init) < 1) {
132 return $element;
133 }
134
135 $settings = array();
136 foreach ($init as $k => $v) {
137 $v = is_array($v) ? implode(',', $v) : $v;
138 // Don't wrap the JS init in quotes for boolean values or functions.
139 if (strtolower($v) != 'true' && strtolower($v) != 'false' && $v[0] != '{') {
140 $v = '"'. $v. '"';
141 }
142 $settings[] = $k. ' : '. $v;
143 }
144 $tinymce_settings = implode(",\n ", $settings);
145
146 if (module_exist('img_assist')) {
147 $img_assist_js_on = $base_url .'/'. url('img_assist/add&editor=tinymce') .'&textarea=';
148 $img_assist_js_off = $base_url .'/'. url('img_assist/add') .'&textarea=';
149 $img_assist_on = $base_url .'/'. url('img_assist/add&editor=tinymce') .'&textarea=edit['. $textarea_name .']';
150 $img_assist_off = $base_url .'/'. url('img_assist/add') .'&textarea=edit['. $textarea_name .']';
151 }
152
153 $enable = t('enable rich-text');
154 $disable = t('disable rich-text');
155
156 $tinymce_invoke = <<<EOD
157 <script language="javascript" type="text/javascript">
158 tinyMCE.init({
159 $tinymce_settings
160 });
161 </script>
162 EOD;
163
164 $js_toggle = <<<EOD
165 <script language="javascript" type="text/javascript">
166 function mceToggle(id, linkid) {
167 element = document.getElementById(id);
168 link = document.getElementById(linkid);
169 img_assist = document.getElementById('img_assist-link-'+ id);
170
171 if (tinyMCE.getEditorId(element.name) == null) {
172 tinyMCE.addMCEControl(element, element.name);
173 element.togg = 'on';
174 link.innerHTML = '$disable';
175 link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
176 if (img_assist)
177 img_assist.href = "$img_assist_js_on"+ element.name;
178 link.blur();
179 }
180 else {
181 tinyMCE.removeMCEControl(tinyMCE.getEditorId(element.name));
182 element.togg = 'off';
183 link.innerHTML = '$enable';
184 link.href = "javascript:mceToggle('" +id+ "', '" +linkid+ "');";
185 if (img_assist)
186 img_assist.href = "$img_assist_js_off"+ element.name;
187 link.blur();
188 }
189 }
190 </script>
191 EOD;
192
193 $status = isset($user->tinymce_status) ? $user->tinymce_status : variable_get('tinymce_default_state', 0);
194 $link_text = $status == 1 ? $disable : $enable;
195 $no_wysiwyg = t('Your current web browser does not support WYSIWYG editing.');
196 $wysiwyg_link = <<<EOD
197 <script language="javascript" type="text/javascript">
198 img_assist = document.getElementById('img_assist-link-edit-$textarea_name');
199 if (img_assist) {
200 img_assist.href = tinyMCE.getEditorId('edit-$textarea_name') == null ? "$img_assist_on" : "$img_assist_off";
201 }
202 if (typeof(document.execCommand) == 'undefined') {
203 img_assist.href = "$img_assist_off";
204 document.write('<div style="font-size:x-small">$no_wysiwyg<\\/div>');
205 }
206 else {
207 document.write("<div><a href=\"javascript:mceToggle('edit-$textarea_name', 'wysiwyg4$textarea_name');\" id=\"wysiwyg4$textarea_name\">$link_text<\\/div><\\/a>");
208 }
209 </script>
210 EOD;
211
212 // We only load the TinyMCE js file once per request
213 if (!$is_running) {
214 $is_running = TRUE;
215 $tinymce_mod_path = drupal_get_path('module', 'tinymce');
216 if (is_dir($tinymce_mod_path.'/imagemanager/')) {
217 // if tinymce imagemanager is installed
218 drupal_add_js(drupal_get_path('module', 'tinymce') .'/imagemanager/jscripts/mcimagemanager.js');
219 }
220 // TinyMCE Compressor
221 if (file_exists($tinymce_mod_path. 'tinymce/jscripts/tiny_mce/tiny_mce_gzip.php')) {
222 drupal_add_js($base_url. '/'. $tinymce_mod_path .'/tinymce/jscripts/tiny_mce/tiny_mce_gzip.php');
223 }
224 else {
225 // For some crazy reason IE will only load this JS file if the absolute reference is given to it.
226 drupal_add_js($base_url. '/'. $tinymce_mod_path .'/tinymce/jscripts/tiny_mce/tiny_mce.js');
227 }
228 drupal_set_html_head($js_toggle);
229 // We have to do this becuase of some unfocused CSS in certain themes. See http://drupal.org/node/18879 for details
230 drupal_set_html_head('<style type="text/css" media="all">.mceEditor img { display: inline; }</style>');
231 }
232 // Load a TinyMCE init for each textarea.
233 if ($init) drupal_set_html_head($tinymce_invoke);
234
235 // Make sure to append to #suffix so it isn't completely overwritten
236 $element['#suffix'] .= $wysiwyg_link;
237 }
238
239 return $element;
240 }
241
242 /**
243 * Implementation of hook_user().
244 */
245 function tinymce_user($type, &$edit, &$user, $category = NULL) {
246 if ($type == 'form' && $category == 'account' && user_access('access tinymce')) {
247 $user_status = $edit['tinymce_status'] != NULL ? $edit['tinymce_status'] : ($user->tinymce_status != NULL ? $user->tinymce_status : variable_get('tinymce_default_state', 0));
248 $form['tinymce'] = array('#type' => 'fieldset', '#title' => t('TinyMCE settings'), '#weight' => 5, '#collapsible' => TRUE, '#collapsed' => TRUE);
249 $form['tinymce']['tinymce_status'] = array('#type' => 'radios', '#title' => t('Default status'), '#default_value' => $user_status, '#options' => array(t('Off'), t('On')), '#description' => t('Should rich-text editing be enabled or disabled by default in textarea fields?'));
250 return array('tinymce' => $form);
251 }
252 if ($type == 'validate') {
253 return array('tinymce_status' => $edit['tinymce_status']);
254 }
255 }
256
257 /**
258 * @addtogroup themeable
259 * @{
260 */
261
262 /**
263 * Customize a TinyMCE theme.
264 *
265 * @param init
266 * An array of settings TinyMCE should invoke a theme. You may override any
267 * of the TinyMCE settings. Details here:
268 *
269 * http://tinymce.moxiecode.com/wrapper.php?url=tinymce/docs/using.htm
270 *
271 * @param textarea_name
272 * The name of the textarea TinyMCE wants to enable.
273 *
274 * @param theme_name
275 * The default tinymce theme name to be enabled for this textarea. The
276 * sitewide default is 'simple', but the user may also override this.
277 *
278 * @param is_running
279 * A boolean flag that identifies id TinyMCE is currently running for this
280 * request life cycle. It can be ignored.
281 */
282 function theme_tinymce_theme($init, $textarea_name, $theme_name, $is_running) {
283 switch ($textarea_name) {
284 // Disable tinymce for these textareas
285 case 'log': // Book log
286 case 'img_assist_pages':
287 case 'caption': // signature
288 case 'pages':
289 case 'access_pages': //TinyMCE profile settings.
290 unset($init);
291 break;
292
293 // Force the 'simple' theme for some of the smaller textareas.
294 case 'signature':
295 case 'site_mission':
296 case 'site_footer':
297 case 'access_pages':
298 $init['theme'] = 'simple';
299 foreach ($init as $k => $v) {
300 if (strstr($k, 'theme_advanced_')) unset($init[$k]);
301 }
302 break;
303 }
304
305 // Add some extra features when using the advanced theme.
306 switch ($theme_name) {
307 case 'advanced':
308 //$init['extended_valid_elements'] = array('a[href|target|name|title|onclick]');
309 break;
310 }
311
312 // Always return $init; !!
313 return $init;
314 }
315
316 /** @} End of addtogroup themeable */
317
318 /**
319 * Grab the themes available to TinyMCE.
320 *
321 * TinyMCE themes control the functionality and buttons that are available to a
322 * user. Themes are only looked for within the default TinyMCE theme directory.
323 *
324 * NOTE: This function is not used in this release. We are only using advanced theme.
325 *
326 * @return
327 * An array of theme names.
328 */
329 function _tinymce_get_themes() {
330 static $themes = array();
331
332 if (!$themes) {
333 $theme_loc = drupal_get_path('module', 'tinymce') .'/tinymce/jscripts/tiny_mce/themes/';
334 if (is_dir($theme_loc) && $dh = opendir($theme_loc)) {
335 while (($file = readdir($dh)) !== false) {
336 if (!in_array($file, array('.', '..', 'CVS')) && is_dir($theme_loc . $file)) {
337 $themes[$file] = $file;
338 }
339 }
340 closedir($dh);
341 asort($themes);
342 }
343 }
344
345 return $themes;
346 }
347
348 /**
349 * Return plugin metadata from the plugin registry.
350 *
351 * We also scrape each plugin's *.js file for the human friendly name and help
352 * text URL of each plugin.
353 *
354 * @return
355 * An array for each plugin.
356 */
357 function _tinymce_get_buttons($skip_metadata = TRUE) {
358 include_once(drupal_get_path('module', 'tinymce'). '/plugin_reg.php');
359 $plugins = _tinymce_plugins();
360 if ($skip_metadata == FALSE && is_array($plugins)) {
361 foreach ($plugins as $name => $plugin) {
362 $file = drupal_get_path('module', 'tinymce'). '/tinymce/jscripts/tiny_mce/plugins/'. $name .'/editor_plugin_src.js';
363 // Grab the plugin metadata by scanning the *.js file.
364 if (file_exists($file)) {
365 $lines = file($file);
366 $has_longname = FALSE;
367 $has_infourl = FALSE;
368 foreach ($lines as $line) {
369 if ($has_longname && $has_infourl) break;
370 if (strstr($line, 'longname')) {
371 $start = strpos($line, "'") + 1;
372 $end = strrpos($line, "'") - $start;
373 $metadata[$name]['longname'] = substr($line, $start, $end);
374 $has_longname = TRUE;
375 }
376 elseif (strstr($line, 'infourl')) {
377 $start = strpos($line, "'") + 1;
378 $end = strrpos($line, "'") - $start;
379 $metadata[$name]['infourl'] = substr($line, $start, $end);
380 $has_infourl = TRUE;
381 }
382 }
383 }
384
385 // Find out the buttons a plugin has.
386 foreach ($plugin as $k => $v) {
387 if (strstr($k, 'theme_advanced_buttons')) {
388 $metadata[$name]['buttons'] = array_merge((array) $metadata[$name]['buttons'], $plugin[$k]);
389 }
390 }
391 }
392 return $metadata;
393 }
394
395 return $plugins;
396 }
397 /********************************************************************
398 * Module Functions :: Public
399 ********************************************************************/
400
401 /**
402 * Controller for tinymce administrative settings.
403 */
404 function tinymce_admin($arg = NULL) {
405 $edit = $_POST['edit'];
406 $op = $_POST['op'];
407
408 $op = $arg && !$op ? $arg : $op;
409
410 switch ($op) {
411 case 'add':
412 $breadcrumb[] = array('path' => 'admin', 'title' => t('administer'));
413 $breadcrumb[] = array('path' => 'admin/settings/tinymce', 'title' => t('tinymce'));
414 $breadcrumb[] = array('path' => 'admin/settings/tinymce/add', 'title' => t('Add new tinymce profile'));
415 menu_set_location($breadcrumb);
416 $output = tinymce_profile_form($edit);
417 break;
418
419 case 'edit':
420 drupal_set_title(t('Edit tinymce profile'));
421 $output = tinymce_profile_form(tinymce_profile_load(urldecode(arg(4))));
422 break;
423
424 case 'delete':
425 tinymce_profile_delete(urldecode(arg(4)));
426 drupal_set_message(t('Deleted profile'));
427 drupal_goto('admin/settings/tinymce');
428 break;
429
430 case t('Create profile');
431 case t('Update profile');
432 if (tinymce_profile_validate($edit)) {
433 tinymce_profile_save($edit);
434 $edit['old_name'] ? drupal_set_message(t('Your tinymce profile has been updated.')) : drupal_set_message(t('Your tinymce profile has been created.'));
435 drupal_goto('admin/settings/tinymce');
436 }
437 else {
438 $output = tinymce_profile_form($edit);
439 }
440 break;
441
442 case t('Save settings'):
443 variable_set('tinymce_default_state', $edit['tinymce_default_state']);
444 drupal_set_message(t('Settings updated'));
445 drupal_goto('admin/settings/tinymce');
446 break;
447
448 default:
449 drupal_set_title(t('TinyMCE settings (%revision)', array('%revision' => '$Revision$')));
450 //Check if TinyMCE is installed.
451 $tinymce_loc = drupal_get_path('module', 'tinymce') .'/tinymce/';
452 if (!is_dir($tinymce_loc)) {
453 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');
454 }
455 $output = tinymce_profile_overview();
456 }
457
458 print theme('page', $output);
459 }
460
461 /**
462 * Return an array of initial tinymce config options from the current role.
463 */
464 function tinymce_config($profile) {
465 global $base_url;
466 global $user;
467 // Drupal theme path.
468 $themepath = drupal_get_path('theme', init_theme()).'/';
469 $host = $base_url.'/';
470
471 $settings = $profile->settings;
472
473 // Build a default list of TinyMCE settings.
474
475 // Is tinymce on by default?
476 $status = isset($user->tinymce_status) ? $user->tinymce_status : variable_get('tinymce_default_state', 0);
477 $init['mode'] = $status == 1 ? 'exact' : 'none';
478 $init['theme'] = $settings['theme'] ? $settings['theme'] : 'simple';
479 $init['document_base_url'] = "$base_url/";
480
481 $init['verify_html'] = $settings['verify_html'] ? $settings['verify_html'] : 'false';
482 $init['preformatted'] = $settings['preformatted'] ? $settings['preformatted'] : 'false';
483 $init['convert_fonts_to_styles'] = $settings['convert_fonts_to_styles'] ? $settings['convert_fonts_to_styles'] : 'false';
484 if ($init['theme'] == 'advanced') {
485 $init['plugins'] = array();
486 $init['theme_advanced_toolbar_location'] = $settings['toolbar_loc'] ? $settings['toolbar_loc'] : 'bottom';
487 $init['theme_advanced_toolbar_align'] = $settings['toolbar_align'] ? $settings['toolbar_align'] : 'left';
488 $init['theme_advanced_path_location'] = $settings['path_loc'] ? $settings['path_loc'] : 'none';
489 $init['theme_advanced_resizing'] = $settings['resizing'] ? $settings['resizing'] : 'false';
490 $init['theme_advanced_blockformats'] = $settings['block_formats'] ? $settings['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6';
491
492 if (is_array($settings['buttons'])) {
493 // This gives us the $plugins variable.
494 $plugins = _tinymce_get_buttons();
495
496 // Find the enabled buttons and the mce row they belong on. Also map the
497 // plugin metadata for each button.
498 $plugin_tracker = array();
499 foreach ($plugins as $rname => $rplugin) { // Plugin name
500 foreach ($rplugin as $mce_key => $mce_value) { // TinyMCE key
501 foreach ($mce_value as $k => $v) { // Buttons
502 if ($settings['buttons'][$rname . '-' . $v]) {
503 if (!in_array($rname, $plugin_tracker)) $plugin_tracker[] = $rname;
504 $init[$mce_key][] = $v;
505 }
506 }
507 }
508 }
509
510 // Add the rest of the TinyMCE config options to the $init array for each button.
511 if (is_array($plugin_tracker)) {
512 foreach ($plugin_tracker as $pname) {
513 if ($pname != 'default') $init['plugins'][] = $pname;
514 foreach ($plugins[$pname] as $mce_key => $mce_value) {
515 if (!strstr($mce_key, 'theme_advanced_buttons')) {
516 $init[$mce_key] = $mce_value;
517 }
518 }
519 }
520 }
521
522 // Cleanup
523 foreach ($init as $mce_key => $mce_value) {
524 if (is_array($mce_value)) $mce_value = array_unique($mce_value);
525 $init[$mce_key] = $mce_value;
526 }
527
528 // Shuffle buttons around so that row 1 always has the most buttons,
529 // followed by row 2, etc. Note: These rows need to be set to NULL otherwise
530 // TinyMCE loads it's own buttons inherited from the theme.
531 if (!$init['theme_advanced_buttons1']) $init['theme_advanced_buttons1'] = array();
532 if (!$init['theme_advanced_buttons2']) $init['theme_advanced_buttons2'] = array();
533 if (!$init['theme_advanced_buttons3']) $init['theme_advanced_buttons3'] = array();
534
535 $min_btns = 5; // Minimum number of buttons per row.
536 $num2 = count($init['theme_advanced_buttons2']);
537 $num3 = count($init['theme_advanced_buttons3']);
538
539 if ($num3 < $min_btns) {
540 $init['theme_advanced_buttons2'] = array_merge($init['theme_advanced_buttons2'], $init['theme_advanced_buttons3']);
541 $init['theme_advanced_buttons3'] = array();
542 }
543 if ($num2 < $min_btns) {
544 $init['theme_advanced_buttons1'] = array_merge($init['theme_advanced_buttons1'], $init['theme_advanced_buttons2']);
545 $init['theme_advanced_buttons2'] = array();
546 }
547 }
548 }
549
550 if ($edit['css_classes']) $init['theme_advanced_styles'] = $settings['css_classes'];
551
552 if ($settings['css_setting'] == 'theme') {
553 $css = $themepath . 'style.css';
554 if (file_exists($css)) {
555 $init['content_css'] = $host . $css;
556 }
557 }
558 else if ($settings['css_setting'] == 'self') {
559 $init['content_css'] = str_replace(array('%h', '%t'), array($host, $themepath), $settings['css_path']);
560 }
561
562 return $init;
563 }
564
565 /**
566 * Remove a profile from the database.
567 */
568 function tinymce_profile_delete($name) {
569 db_query("DELETE FROM {tinymce_settings} WHERE name = '%s'", $name);
570 db_query("DELETE FROM {tinymce_role} WHERE name = '%s'", $name);
571 }
572
573 /**
574 * Return an HTML form for profile configuration.
575 */
576 function tinymce_profile_form($edit) {
577 $edit = array2object($edit);
578
579 // Only display the roles that currently don't have a tinymce profile. One
580 // profile per role.
581 $orig_roles = user_roles(FALSE, 'access tinymce');
582 $roles = $orig_roles;
583 if (arg(3) == 'add') {
584 $result = db_query('SELECT DISTINCT(rid) FROM {tinymce_role}');
585 while ($data = db_fetch_object($result)) {
586 unset($roles[$data->rid]);
587 }
588 if (!$orig_roles) {
589 drupal_set_message(t('You must assign at least one role with the \'access tinymce\' permission before creating a profile.'), 'error');
590 }
591 else if (!$roles) {
592 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');
593 }
594 else if (count($orig_roles) != count($roles)) {
595 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.'));
596 }
597 $btn = t('Create profile');
598 }
599 else {
600 $form['old_name'] = array('#type' => 'hidden', '#value' => $edit->name);
601 $btn = t('Update profile');
602 }
603
604 $form['basic'] = array('#type' => 'fieldset', '#title' => t('Basic setup'), '#collapsible' => TRUE, '#collapsed' => TRUE);
605 $form['basic']['name'] = array('#type' => 'textfield', '#title' => t('Profile name'), '#default_value' => $edit->name, '#size' => 40, '#maxlength' => 128, '#description' => t('Enter a unique name for this profile. This name is only visible in the tinymce administration page.'), '#required' => TRUE);
606 $form['basic']['rids'] = array('#type' => 'checkboxes', '#title' => t('Roles allowed to use this profile'), '#default_value' => array_keys((array) $edit->rids), '#options' => $roles, '#description' => t('Select at least one role. Listed are the roles with \'access tinymce\' permission.'), '#required' => TRUE);
607 $form['basic']['theme'] = array('#type' => 'hidden', '#value' => $edit->settings['theme'] ? $edit->settings['theme'] : 'advanced');
608 $form['basic']['access'] = array('#type' => 'radios', '#title' => t('Make tinymce visible on'), '#default_value' => $edit->settings['access'], '#options' => array(t('all pages'), t('specific pages')));
609 $form['basic']['access_pages'] = array('#type' => 'textarea', '#title' => t('Specific pages'), '#default_value' => $edit->settings['access_pages'] ? $edit->settings['access_pages'] : tinymce_help('admin/settings/tinymce#pages'), '#cols' => 40, '#rows' => 5, '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '<em>blog</em>' for the blog page and '<em>blog/*</em>' for every personal blog. '<em>&lt;front&gt;</em>' is the front page."));
610
611 $form['buttons'] = array('#type' => 'fieldset', '#title' => t('Buttons'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#tree' => TRUE, '#theme' => 'tinymce_profile_form_buttons');
612
613 $metadata = _tinymce_get_buttons(FALSE);
614 // Generate the button list.
615 foreach($metadata as $name => $meta) {
616 if (is_array($meta['buttons'])) {
617 foreach ($meta['buttons'] as $button) {
618 if ($name != 'default') {
619 $img_src = drupal_get_path('module', 'tinymce'). "/tinymce/jscripts/tiny_mce/plugins/$name/images/$name.gif";
620
621 //correct for plugins that have more than one button
622 if (!file_exists($img_src)) {
623 $img_src = drupal_get_path('module', 'tinymce'). "/tinymce/jscripts/tiny_mce/plugins/$name/images/$button.gif";
624 }
625 }
626 else {
627 $img_src = drupal_get_path('module', 'tinymce'). "/tinymce/jscripts/tiny_mce/themes/advanced/images/$button.gif";
628 }
629 $b = file_exists($img_src) ? '<img src="'. $img_src .'" title="'. $button .'" style="border: 1px solid grey; vertical-align: middle;" />' : $button;
630
631 if ($name == 'default') {
632 $title = $b;
633 }
634 else {
635 $title = $metadata[$name]['longname'] ? $metadata[$name]['longname'] : $name;
636 if ($metadata[$name]['infourl']) {
637 $title = '<a href="'. $metadata[$name]['infourl'] .'" target="_blank">'. $title .'</a>';
638 }
639 $title = $b . ' &#8211; '. $title;
640 }
641 $form_value = $edit->settings['buttons'][$name . '-' . $button];
642 $form['buttons'][$name . '-' . $button] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value, '#description' => $description);
643 }
644 }
645 else {
646 $title = $metadata[$name]['longname'] ? $metadata[$name]['longname'] : $name;
647 if ($metadata[$name]['infourl']) {
648 $title = '<a href="'. $metadata[$name]['infourl'] .'" target="_blank">'. $title .'</a>';
649 }
650 $form_value = $edit->settings['buttons'][$name];
651 $form['buttons'][$name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => $form_value, '#description' => $description);
652 }
653 }
654
655 $form['appearance'] = array('#type' => 'fieldset', '#title' => t('Editor appearance'), '#collapsible' => TRUE, '#collapsed' => TRUE);
656 $form['appearance']['toolbar_loc'] = array('#type' => 'select', '#title' => t('Toolbar location'), '#default_value' => $edit->settings['toolbar_loc'], '#options' => array('bottom' => 'bottom', 'top' => 'top'), '#description' => t('Show toolbar at the top or bottom of the editor area?'));
657 $form['appearance']['toolbar_align'] = array('#type' => 'select', '#title' => t('Toolbar alignment'), '#default_value' => $edit->settings['toolbar_align'], '#options' => array('center' => 'center', 'left' => 'left', 'right' => 'right'), '#description' => t('Align tool icons left, center, or right within the toolbar.'));
658 $form['appearance']['path_loc'] = array('#type' => 'select', '#title' => t('Path location'), '#default_value' => $edit->settings['path_loc'], '#options' => array('none' => 'none', 'top' => 'top', 'bottom' => 'bottom'), '#description' => t('Path to html elements (i.e. "body>table>tr>td"). Show at top, bottom, or not at all.'));
659 $form['appearance']['resizing'] = array('#type' => 'select', '#title' => t('Enable resizing button'), '#default_value' => $edit->settings['resizing'], '#options' => array('false' => 'false', 'true' => 'true'), '#description' => t(' This option gives you the ability to enable/disable the resizing button.'));
660 $form['appearance']['block_formats'] = array('#type' => 'textfield', '#title' => t('Block formats'), '#default_value' => $edit->settings['block_formats'] ? $edit->settings['block_formats'] : 'p,address,pre,h1,h2,h3,h4,h5,h6', '#size' => 40, '#maxlength' => 250, '#description' => t('Comma separated list of HTML block formats. You can only remove elements, not add.'));
661
662 $form['output'] = array('#type' => 'fieldset', '#title' => t('Cleanup/Output'), '#collapsible' => TRUE, '#collapsed' => TRUE);
663 $form['output']['verify_html'] = array('#type' => 'select', '#title' => t('Verify HTML'), '#default_value' => $edit->settings['verify_html'], '#options' => array('true' => 'true', 'false' => 'false'), '#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.'));
664 $form['output']['preformatted'] = array('#type' => 'select', '#title' => t('Preformatted'), '#default_value' => $edit->settings['preformatted'], '#options' => array('false' => 'false', 'true' => 'true'), '#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.'));
665 $form['output']['convert_fonts_to_styles'] = array('#type' => 'select', '#title' => t('Convert &lt;font&gt; tags to styles'), '#default_value' => $edit->settings['convert_fonts_to_styles'], '#options' => array('true' => 'true', 'false' => 'false'), '#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.'));
666
667 $form['css'] = array('#type' => 'fieldset', '#title' => t('CSS'), '#collapsible' => TRUE, '#collapsed' => TRUE);
668 $form['css']['css_setting'] = array('#type' => 'select', '#title' => t('Editor CSS'), '#default_value' => $edit->settings['css_setting'] ? $edit->settings['css_setting'] : 'theme', '#options' => array('theme' => 'use theme css', 'self' => 'define css', 'none' => 'tinyMCE default'), '#description' => t('Defines the CSS to be used in the editor area.<br />use theme css - get css from current Drupal theme.<br/>define css - enter path for css file below.<br />tinyMCE default - uses default CSS from editor.'));
669 $form['css']['css_path'] = array('#type' => 'textfield', '#title' => t('CSS path'), '#default_value' => $edit->settings['css_path'], '#size' => 40, '#maxlength' => 255, '#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.'));
670 $form['css']['css_classes'] = array('#type' => 'textfield', '#title' => t('CSS classes'), '#default_value' => $edit->settings['css_classes'], '#size' => 40, '#maxlength' => 255, '#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;"<br />Leave blank to automatically import list of CSS classes from style sheet.'));
671
672 $form['submit'] = array('#type' => 'submit', '#value' => $btn);
673
674 $output .= drupal_get_form('tinymce_profile_form', $form);
675
676 return $output;
677 }
678
679 /**
680 * Layout for the buttons in the tinymce profile form
681 */
682 function theme_tinymce_profile_form_buttons($form) {
683 $buttons = array();
684
685 // Flatten forms array
686 foreach (element_children($form) as $key) {
687 $buttons[] = form_render($form[$key]);
688 }
689
690 //split checkboxes into rows with 3 columns
691 $total = count($buttons);
692 $rows = array();
693 for ($i = 0; $i < $total; $i++) {
694 $row = array();
695 $row[] = array('data' => $buttons[$i]);
696 $row[] = array('data' => $buttons[++$i]);
697 $row[] = array('data' => $buttons[++$i]);
698 $rows[] = $row;
699 }
700
701 $output = theme('table', array(), $rows, array('width' => '100%'));
702
703 return $output;
704 }
705
706 /**
707 * Load all profiles. Just load one profile if $name is passed in.
708 */
709 function tinymce_profile_load($name = '') {
710 static $profiles = array();
711
712 if (!$profiles) {
713 $roles = user_roles();
714 $result = db_query('SELECT * FROM {tinymce_settings}');
715 while ($data = db_fetch_object($result)) {
716 $data->settings = unserialize($data->settings);
717 $result2 = db_query("SELECT rid FROM {tinymce_role} WHERE name = '%s'", $data->name);
718 $role = array();
719 while ($r = db_fetch_object($result2)) {
720 $role[$r->rid] = $roles[$r->rid];
721 }
722 $data->rids = $role;
723
724 $profiles[$data->name] = $data;
725 }
726 }
727
728 return ($name ? $profiles[$name] : $profiles);
729 }
730
731 /**
732 * Controller for tinymce profiles.
733 */
734 function tinymce_profile_overview() {
735 $output = '';
736
737 $profiles = tinymce_profile_load();
738 if ($profiles) {
739 $output .= t('<p><a href="%create-profile-url">Create new profile</a></p>', array('%create-profile-url' => url('admin/settings/tinymce/add')));
740 $roles = user_roles();
741 $header = array(t('Profile'), t('Roles'), t('Operations'));
742 foreach ($profiles as $p) {
743 $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'));
744 }
745 $output .= theme('table', $header, $rows). '<p>&nbsp;</p>';
746 }
747 else {
748 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'))));
749 }
750
751 $form['settings'] = array('#type' => 'fieldset', '#title' => t('Default settings'), '#collapsible' => TRUE);
752 $form['settings']['tinymce_default_state'] = array('#type' => 'radios', '#title' => t('Default tinymce state'), '#default_value' => variable_get('tinymce_default_state', 0), '#options' => array(t('Off'), t('On')), '#description' => t('Should tinymce be enabled or disabled by default when it\'s first loaded from a textarea? Note: The user may override this setting in their profile.'));
753 $form['settings']['submit'] = array('#type' => 'submit', '#value' => t('Save settings'));
754
755 $output .= drupal_get_form('settings', $form);
756
757 return $output;
758 }
759
760 /**
761 * Save a profile to the database.
762 */
763 function tinymce_profile_save($edit) {
764 db_query("DELETE FROM {tinymce_settings} WHERE name = '%s' or name = '%s'", $edit['name'], $edit['old_name']);
765 db_query("DELETE FROM {tinymce_role} WHERE name = '%s' or name = '%s'", $edit['name'], $edit['old_name']);
766 db_query("INSERT INTO {tinymce_settings} (name, settings) VALUES ('%s', '%s')", $edit['name'], serialize($edit));
767 foreach ($edit['rids'] as $rid => $value) {
768 //if this rid has been checked, insert it
769 if ($value == 1) {
770 db_query("INSERT INTO {tinymce_role} (name, rid) VALUES ('%s', %d)", $edit['name'], $rid);
771 }
772 }
773 }
774
775 /**
776 * Profile validation.
777 */
778 function tinymce_profile_validate($edit) {
779 $errors = array();
780
781 if (!$edit['name']) {
782 $errors['name'] = t('You must give a profile name.');
783 }
784
785 if (!$edit['rids']) {
786 $errors['rids'] = t('You must select at least one role.');
787 }
788
789 foreach ($errors as $name => $message) {
790 form_set_error($name, $message);
791 }
792
793 return count($errors) == 0;
794 }
795
796 /********************************************************************
797 * Module Functions :: Private
798 ********************************************************************/
799
800 /**
801 * Determine if TinyMCE has permission to be used on the current page.
802 *
803 * @return
804 * TRUE if can render, FALSE if not allowed.
805 */
806 function _tinymce_page_match($edit) {
807 //Kill TinyMCE if we're editing a textarea with PHP in it!
808 if ($_POST['edit']['format'] == 2) {
809 return FALSE;
810 }
811 else {
812 // PHP input formats are #2 in the filters table.
813 preg_match("|^node/(\d+)(/edit)$|", $_GET['q'], $match);
814 if (intval($match[1]) > 0) {
815 if (db_result(db_query('SELECT format FROM {node_revisions} WHERE nid = %d AND vid = %d AND format = 2', $match[1], $match[1]))) {
816 return FALSE;
817 }
818 }
819 }
820
821 if ($edit->settings['access'] == 0) {
822 return TRUE;
823 }
824 else {
825 $page_match = FALSE;
826 $pages = $edit->settings['access_pages'] ? $edit->settings['access_pages'] : tinymce_help('admin/settings/tinymce#pages');
827 if ($pages) {
828 $path = drupal_get_path_alias($_GET['q']);
829 $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. variable_get('site_frontpage', 'node') .'\2'), preg_quote($pages, '/')) .')$/';
830 $page_match = preg_match($regexp, $path);
831 }
832 return $page_match;
833 }
834 }
835 ?>