ckeditor.user.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * CKEditor - The text editor for the Internet - http://ckeditor.com
  4. * Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
  5. *
  6. * == BEGIN LICENSE ==
  7. *
  8. * Licensed under the terms of any of the following licenses of your
  9. * choice:
  10. *
  11. * - GNU General Public License Version 2 or later (the "GPL")
  12. * http://www.gnu.org/licenses/gpl.html
  13. *
  14. * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  15. * http://www.gnu.org/licenses/lgpl.html
  16. *
  17. * - Mozilla Public License Version 1.1 or later (the "MPL")
  18. * http://www.mozilla.org/MPL/MPL-1.1.html
  19. *
  20. * == END LICENSE ==
  21. *
  22. * @file
  23. * CKEditor Module for Drupal 7.x
  24. *
  25. * This module allows Drupal to replace textarea fields with CKEditor.
  26. *
  27. * CKEditor is an online rich text editor that can be embedded inside web pages.
  28. * It is a WYSIWYG (What You See Is What You Get) editor which means that the
  29. * text edited in it looks as similar as possible to the results end users will
  30. * see after the document gets published. It brings to the Web popular editing
  31. * features found in desktop word processors such as Microsoft Word and
  32. * OpenOffice.org Writer. CKEditor is truly lightweight and does not require any
  33. * kind of installation on the client computer.
  34. */
  35. function ckeditor_user_customize(&$form, &$form_state, $form_id) {
  36. module_load_include('inc', 'ckeditor', 'includes/ckeditor.lib');
  37. $data = $form['#user']->data;
  38. $default = ckeditor_user_get_setting_default();
  39. $lang_options = ckeditor_load_lang_options();
  40. // because the settings are saved as strings we need to test for the string 'true'
  41. if (user_access('customize ckeditor')) {
  42. $form['ckeditor'] = array(
  43. '#type' => 'fieldset',
  44. '#title' => t('Rich text editor settings'),
  45. '#weight' => 10,
  46. '#collapsible' => TRUE,
  47. '#collapsed' => TRUE
  48. );
  49. $form['ckeditor']['ckeditor_default'] = array(
  50. '#type' => 'radios',
  51. '#title' => t('Default state'),
  52. '#default_value' => isset($data['ckeditor_default']) ? $data['ckeditor_default'] : $default['default'],
  53. '#options' => array(
  54. 't' => t('Enabled'),
  55. 'f' => t('Disabled')
  56. ),
  57. '#description' => t('Should rich text editing be enabled or disabled by default in textarea fields? If disabled, the rich text editor may still be enabled by using toggle.'),
  58. );
  59. $form['ckeditor']['ckeditor_show_toggle'] = array(
  60. '#type' => 'radios',
  61. '#title' => t('Show the disable/enable rich text editor toggle'),
  62. '#default_value' => isset($data['ckeditor_show_toggle']) ? $data['ckeditor_show_toggle'] : $default['show_toggle'],
  63. '#options' => array(
  64. 't' => t('Yes'),
  65. 'f' => t('No')
  66. ),
  67. '#description' => t('Whether or not to show the disable/enable rich text editor toggle below the textarea.'),
  68. );
  69. $form['ckeditor']['ckeditor_width'] = array(
  70. '#type' => 'textfield',
  71. '#title' => t('Editor width'),
  72. '#default_value' => isset($data['ckeditor_width']) ? $data['ckeditor_width'] : $default['width'],
  73. '#description' => t('Editor interface width in pixels or percent.') . ' ' . t('Examples') . ': 400 ' . t('or') . ' 100%.',
  74. '#size' => 40,
  75. '#maxlength' => 128,
  76. );
  77. $form['ckeditor']['ckeditor_lang'] = array(
  78. '#type' => 'select',
  79. '#title' => t('Language'),
  80. '#default_value' => isset($data['ckeditor_lang']) ? $data['ckeditor_lang'] : $default['lang'],
  81. '#options' => $lang_options,
  82. '#description' => t('The language for the CKEditor interface.')
  83. );
  84. $form['ckeditor']['ckeditor_auto_lang'] = array(
  85. '#type' => 'radios',
  86. '#title' => t('Auto-detect language'),
  87. '#default_value' => isset($data['ckeditor_auto_lang']) ? $data['ckeditor_auto_lang'] : $default['auto_lang'],
  88. '#options' => array(
  89. 't' => t('Yes'),
  90. 'f' => t('No')
  91. ),
  92. '#description' => t('Automatically detect the user language.')
  93. );
  94. $form['#validate'][] = 'ckeditor_user_customize_form_validate';
  95. }
  96. }
  97. function ckeditor_user_customize_form_validate(&$form, &$form_state) {
  98. /*
  99. if (isset($form_state['values']['ckeditor_default'], $form_state['values']['ckeditor_popup']) && $form_state['values']['ckeditor_default'] == 't' && $form_state['values']['ckeditor_popup'] == 't') {
  100. form_set_error('ckeditor_popup', t('If CKEditor is enabled by default, the popup window must be disabled.'));
  101. }
  102. if (isset($form_state['values']['ckeditor_show_toggle'], $form_state['values']['ckeditor_popup']) && $form_state['values']['ckeditor_show_toggle'] == 't' && $form_state['values']['ckeditor_popup'] == 't') {
  103. form_set_error('ckeditor_popup', t('If toggle is enabled, the popup window must be disabled.'));
  104. }
  105. */
  106. if (isset($form_state['values']['ckeditor_width']) && !preg_match('/^\d+%?$/', $form_state['values']['ckeditor_width'])) {
  107. form_set_error('ckeditor_width', t('Enter a valid width value.') . ' ' . t('Examples:') . ': 400 ' . t('or') . ' 100%.');
  108. }
  109. }
  110. function ckeditor_user_presave(&$edit, $account, $category) {
  111. if (user_access('customize ckeditor')) {
  112. module_load_include('inc', 'ckeditor', 'includes/ckeditor.lib');
  113. $default = ckeditor_user_get_setting_default();
  114. $edit['data']['ckeditor_default'] = isset($edit['ckeditor_default']) ? $edit['ckeditor_default'] : $default['default'];
  115. $edit['data']['ckeditor_show_toggle'] = isset($edit['ckeditor_show_toggle']) ? $edit['ckeditor_show_toggle'] : $default['show_toggle'];
  116. $edit['data']['ckeditor_width'] = isset($edit['ckeditor_width']) ? $edit['ckeditor_width'] : $default['width'];
  117. $edit['data']['ckeditor_lang'] = isset($edit['ckeditor_lang']) ? $edit['ckeditor_lang'] : $default['lang'];
  118. $edit['data']['ckeditor_auto_lang'] = isset($edit['ckeditor_auto_lang']) ? $edit['ckeditor_auto_lang'] : $default['auto_lang'];
  119. }
  120. }