fckeditor.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * @file
  4. * Editor integration functions for FCKeditor.
  5. */
  6. /**
  7. * Plugin implementation of hook_editor().
  8. */
  9. function wysiwyg_fckeditor_editor() {
  10. $editor['fckeditor'] = array(
  11. 'title' => 'FCKeditor',
  12. 'vendor url' => 'http://www.fckeditor.net',
  13. 'download url' => 'http://www.fckeditor.net/download',
  14. 'libraries' => array(
  15. '' => array(
  16. 'title' => 'Default',
  17. 'files' => array('fckeditor.js'),
  18. ),
  19. ),
  20. 'verified version range' => array('2.6.0', '2.6.11'),
  21. 'version callback' => 'wysiwyg_fckeditor_version',
  22. 'themes callback' => 'wysiwyg_fckeditor_themes',
  23. 'settings form callback' => 'wysiwyg_fckeditor_settings_form',
  24. 'settings callback' => 'wysiwyg_fckeditor_settings',
  25. 'plugin callback' => '_wysiwyg_fckeditor_plugins',
  26. 'plugin meta callback' => '_wysiwyg_fckeditor_plugin_meta',
  27. 'plugin settings callback' => '_wysiwyg_fckeditor_plugin_settings',
  28. 'proxy plugin' => array(
  29. 'drupal' => array(
  30. 'load' => TRUE,
  31. 'proxy' => TRUE,
  32. ),
  33. ),
  34. 'proxy plugin settings callback' => '_wysiwyg_fckeditor_proxy_plugin_settings',
  35. 'versions' => array(
  36. '2.6' => array(
  37. 'js files' => array('fckeditor-2.6.js'),
  38. ),
  39. ),
  40. );
  41. return $editor;
  42. }
  43. /**
  44. * Detect editor version.
  45. *
  46. * @param $editor
  47. * An array containing editor properties as returned from hook_editor().
  48. *
  49. * @return
  50. * The installed editor version.
  51. */
  52. function wysiwyg_fckeditor_version($editor) {
  53. $library = $editor['library path'] . '/fckeditor.js';
  54. if (!file_exists($library)) {
  55. return;
  56. }
  57. $library = fopen($library, 'r');
  58. $max_lines = 100;
  59. while ($max_lines && $line = fgets($library, 60)) {
  60. if (preg_match('@^FCKeditor.prototype.Version\s*= \'([\d\.]+)@', $line, $version)) {
  61. fclose($library);
  62. return $version[1];
  63. }
  64. $max_lines--;
  65. }
  66. fclose($library);
  67. }
  68. /**
  69. * Determine available editor themes or check/reset a given one.
  70. *
  71. * @param $editor
  72. * A processed hook_editor() array of editor properties.
  73. * @param $profile
  74. * A wysiwyg editor profile.
  75. *
  76. * @return
  77. * An array of theme names. The first returned name should be the default
  78. * theme name.
  79. */
  80. function wysiwyg_fckeditor_themes($editor, $profile) {
  81. return array('default', 'office2003', 'silver');
  82. }
  83. /**
  84. * Enhances the editor profile settings form for FCKeditor.
  85. *
  86. * @see http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Configuration/Configuration_Options
  87. */
  88. function wysiwyg_fckeditor_settings_form(&$form, &$form_state) {
  89. $profile = $form_state['wysiwyg_profile'];
  90. $settings = $profile->settings;
  91. $settings += array(
  92. 'AutoDetectPasteFromWord' => TRUE,
  93. 'ForcePasteAsPlainText' => FALSE,
  94. 'FontFormats' => 'p;address;pre;h2;h3;h4;h5;h6;div',
  95. 'FormatOutput' => TRUE,
  96. 'FormatSource' => TRUE,
  97. );
  98. $form['output']['FormatSource'] = array(
  99. '#type' => 'checkbox',
  100. '#title' => t('Apply source formatting'),
  101. '#default_value' => $settings['FormatSource'],
  102. '#return_value' => 1,
  103. '#description' => t('If enabled, the editor will re-format the HTML source code when switching to Source View.') . ' ' . t('Uses the <a href="@url">@setting</a> setting internally.', array('@setting' => 'FormatSource', '@url' => url('http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Configuration/Configuration_Options/FormatSource'))),
  104. );
  105. $form['output']['FormatOutput'] = array(
  106. '#type' => 'checkbox',
  107. '#title' => t('Apply output formatting'),
  108. '#default_value' => $settings['FormatOutput'],
  109. '#return_value' => 1,
  110. '#description' => t('If enabled, the editor will re-format the HTML source code output. Disabling this option could avoid conflicts with other input filters.') . ' ' . t('Uses the <a href="@url">@setting</a> setting internally.', array('@setting' => 'FormatOutput', '@url' => url('http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Configuration/Configuration_Options/FormatOutput'))),
  111. );
  112. $form['css']['FontFormats'] = array(
  113. '#type' => 'textfield',
  114. '#title' => t('Block formats'),
  115. '#default_value' => $settings['FontFormats'],
  116. '#size' => 40,
  117. '#maxlength' => 250,
  118. '#description' => t('Semicolon separated list of HTML block formats. Possible values: <code>@format-list</code>.', array('@format-list' => 'p;h1;h2;h3;h4;h5;h6;div;address;pre')) . ' ' . t('Uses the <a href="@url">@setting</a> setting internally.', array('@setting' => 'FontFormats', '@url' => url('http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Configuration/Configuration_Options/FontFormats'))),
  119. );
  120. $form['paste'] = array(
  121. '#type' => 'fieldset',
  122. '#title' => t('Paste plugin'),
  123. '#description' => t('Settings for the paste plugin.'),
  124. '#collapsible' => TRUE,
  125. '#collapsed' => TRUE,
  126. '#group' => 'advanced',
  127. );
  128. $form['paste']['AutoDetectPasteFromWord'] = array(
  129. '#type' => 'checkbox',
  130. '#title' => t('Auto detect paste from Word'),
  131. '#default_value' => $settings['AutoDetectPasteFromWord'],
  132. '#return_value' => 1,
  133. '#description' => t('If enabled, FCKeditor checks if pasted text comes from MS Word. If so the editor will launch the "Paste from Word" window. <strong>Only works in Internet Explorer.</strong>') . ' ' . t('Uses the <a href="@url">@setting</a> setting internally.', array('@setting' => 'AutoDetectPasteFromWord', '@url' => url('http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Configuration/Configuration_Options/AutoDetectPasteFromWord'))),
  134. );
  135. $form['paste']['ForcePasteAsPlainText'] = array(
  136. '#type' => 'checkbox',
  137. '#title' => t('Force paste as plain text'),
  138. '#default_value' => $settings['ForcePasteAsPlainText'],
  139. '#return_value' => 1,
  140. '#description' => t('If enabled, forces the editor to discard all formatting when pasting text. It will also disable the <strong>Paste from Word</strong> operation.') . ' ' . t('Uses the <a href="@url">@setting</a> setting internally.', array('@setting' => 'ForcePasteAsPlainText', '@url' => url('http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Configuration/Configuration_Options/ForcePasteAsPlainText'))),
  141. );
  142. }
  143. /**
  144. * Return runtime editor settings for a given wysiwyg profile.
  145. *
  146. * @param $editor
  147. * A processed hook_editor() array of editor properties.
  148. * @param $config
  149. * An array containing wysiwyg editor profile settings.
  150. * @param $theme
  151. * The name of a theme/GUI/skin to use.
  152. *
  153. * @return
  154. * A settings array to be populated in
  155. * Drupal.settings.wysiwyg.configs.{editor}
  156. */
  157. function wysiwyg_fckeditor_settings($editor, $config, $theme) {
  158. $settings = array(
  159. 'EditorPath' => base_path() . $editor['library path'] . '/',
  160. 'SkinPath' => base_path() . $editor['library path'] . '/editor/skins/' . $theme . '/',
  161. 'CustomConfigurationsPath' => base_path() . drupal_get_path('module', 'wysiwyg') . '/editors/js/fckeditor.config.js',
  162. 'Width' => '100%',
  163. 'LinkBrowser' => FALSE,
  164. 'LinkUpload' => FALSE,
  165. 'ImageBrowser' => FALSE,
  166. 'ImageUpload' => FALSE,
  167. 'FlashBrowser' => FALSE,
  168. 'FlashUpload' => FALSE,
  169. // By default, FCKeditor converts most characters into HTML entities. Since
  170. // it does not support a custom definition, but Drupal supports Unicode, we
  171. // disable at least the additional character sets. FCKeditor always converts
  172. // XML default characters '&', '<', '>'.
  173. // @todo Check whether completely disabling ProcessHTMLEntities is an option.
  174. 'IncludeLatinEntities' => FALSE,
  175. 'IncludeGreekEntities' => FALSE,
  176. );
  177. if (isset($config['FontFormats'])) {
  178. $settings['FontFormats'] = preg_replace('@\s+@', '', $config['FontFormats']);
  179. }
  180. $check_if_set = array(
  181. 'AutoDetectPasteFromWord',
  182. 'ForcePasteAsPlainText',
  183. 'FormatOutput',
  184. 'FormatSource',
  185. );
  186. foreach ($check_if_set as $setting_name) {
  187. if (isset($config[$setting_name])) {
  188. $settings[$setting_name] = $config[$setting_name];
  189. }
  190. }
  191. if (isset($config['css_setting'])) {
  192. if ($config['css_setting'] == 'theme') {
  193. $settings['EditorAreaCSS'] = implode(',', wysiwyg_get_css(isset($config['css_theme']) ? $config['css_theme'] : ''));
  194. }
  195. elseif ($config['css_setting'] == 'self' && isset($config['css_path'])) {
  196. $settings['EditorAreaCSS'] = strtr($config['css_path'], array(
  197. '%b' => base_path(),
  198. '%t' => drupal_get_path('theme', variable_get('theme_default', NULL)),
  199. '%q' => variable_get('css_js_query_string', ''),
  200. ));
  201. }
  202. }
  203. // Use our custom toolbar set.
  204. $settings['ToolbarSet'] = 'Wysiwyg';
  205. // Populate our custom toolbar set for fckeditor.config.js.
  206. $settings['buttons'] = array();
  207. if (!empty($config['buttons'])) {
  208. $plugins = wysiwyg_get_plugins($editor['name']);
  209. foreach ($config['buttons'] as $plugin => $buttons) {
  210. foreach ($buttons as $button => $enabled) {
  211. // Iterate separately over buttons and extensions properties.
  212. foreach (array('buttons', 'extensions') as $type) {
  213. // Skip unavailable plugins.
  214. if (!isset($plugins[$plugin][$type][$button])) {
  215. continue;
  216. }
  217. // Add buttons.
  218. if ($type == 'buttons') {
  219. $settings['buttons'][] = $button;
  220. }
  221. // Allow plugins to add or override global configuration settings.
  222. if (!empty($plugins[$plugin]['options'])) {
  223. $settings = array_merge($settings, $plugins[$plugin]['options']);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. // For now, all buttons are placed into one row.
  230. $settings['buttons'] = array($settings['buttons']);
  231. return $settings;
  232. }
  233. /**
  234. * Build a JS settings array with global metadata for native external plugins.
  235. */
  236. function _wysiwyg_fckeditor_plugin_meta($editor, $plugin) {
  237. $meta = array();
  238. // Add path for native external plugins; internal ones do not need a path.
  239. if (empty($plugin['internal']) && isset($plugin['path'])) {
  240. // All native FCKeditor plugins use the filename fckplugin.js.
  241. $meta['path'] = base_path() . $plugin['path'] . '/';
  242. }
  243. if (!empty($plugin['languages'])) {
  244. $meta['languages'] = $plugin['languages'];
  245. }
  246. return $meta;
  247. }
  248. /**
  249. * Build a JS settings array for native external plugins.
  250. */
  251. function _wysiwyg_fckeditor_plugin_settings($editor, $profile, $plugins) {
  252. $settings = array();
  253. foreach ($plugins as $name => $plugin) {
  254. if (!empty($plugin['load'])) {
  255. // Just need a list of all enabled plugins for each instance.
  256. $settings[$name] = TRUE;
  257. }
  258. }
  259. return $settings;
  260. }
  261. /**
  262. * Build a JS settings array for Drupal plugins loaded via the proxy plugin.
  263. */
  264. function _wysiwyg_fckeditor_proxy_plugin_settings($editor, $profile, $plugins) {
  265. $settings = array();
  266. foreach ($plugins as $name => $plugin) {
  267. // Just need a list of all enabled plugins for each instance.
  268. $settings[$name] = TRUE;
  269. }
  270. return $settings;
  271. }
  272. /**
  273. * Return internal plugins for this editor; semi-implementation of hook_wysiwyg_plugin().
  274. */
  275. function _wysiwyg_fckeditor_plugins($editor) {
  276. $plugins = array(
  277. 'default' => array(
  278. 'buttons' => array(
  279. 'Bold' => t('Bold'), 'Italic' => t('Italic'), 'Underline' => t('Underline'),
  280. 'StrikeThrough' => t('Strike-through'),
  281. 'JustifyLeft' => t('Align left'), 'JustifyCenter' => t('Align center'), 'JustifyRight' => t('Align right'), 'JustifyFull' => t('Justify'),
  282. 'UnorderedList' => t('Bullet list'), 'OrderedList' => t('Numbered list'),
  283. 'Outdent' => t('Outdent'), 'Indent' => t('Indent'),
  284. 'Undo' => t('Undo'), 'Redo' => t('Redo'),
  285. 'Link' => t('Link'), 'Unlink' => t('Unlink'), 'Anchor' => t('Anchor'),
  286. 'Image' => t('Image'),
  287. 'TextColor' => t('Forecolor'), 'BGColor' => t('Backcolor'),
  288. 'Superscript' => t('Superscript'), 'Subscript' => t('Subscript'),
  289. 'Blockquote' => t('Blockquote'), 'Source' => t('Source code'),
  290. 'Rule' => t('Horizontal rule'),
  291. 'Cut' => t('Cut'), 'Copy' => t('Copy'), 'Paste' => t('Paste'),
  292. 'PasteText' => t('Paste Text'), 'PasteWord' => t('Paste from Word'),
  293. 'ShowBlocks' => t('Show blocks'),
  294. 'RemoveFormat' => t('Remove format'),
  295. 'SpecialChar' => t('Character map'),
  296. 'About' => t('About'),
  297. 'FontFormat' => t('HTML block format'), 'FontName' => t('Font'), 'FontSize' => t('Font size'), 'Style' => t('Font style'),
  298. 'Table' => t('Table'),
  299. 'Find' => t('Search'), 'Replace' => t('Replace'), 'SelectAll' => t('Select all'),
  300. 'CreateDiv' => t('Create DIV container'),
  301. 'Flash' => t('Flash'), 'Smiley' => t('Smiley'),
  302. 'FitWindow' => t('FitWindow'),
  303. 'SpellCheck' => t('Check spelling'),
  304. ),
  305. 'internal' => TRUE,
  306. ),
  307. 'autogrow' => array(
  308. 'path' => $editor['library path'] . '/editor/plugins',
  309. 'extensions' => array(
  310. 'autogrow' => t('Autogrow'),
  311. ),
  312. 'options' => array(
  313. 'AutoGrowMax' => 800,
  314. ),
  315. 'internal' => TRUE,
  316. 'load' => TRUE,
  317. ),
  318. 'bbcode' => array(
  319. 'path' => $editor['library path'] . '/editor/plugins',
  320. 'extensions' => array(
  321. 'bbcode' => t('BBCode'),
  322. ),
  323. 'internal' => TRUE,
  324. 'load' => TRUE,
  325. ),
  326. 'dragresizetable' => array(
  327. 'path' => $editor['library path'] . '/editor/plugins',
  328. 'extensions' => array(
  329. 'dragresizetable' => t('Table drag/resize'),
  330. ),
  331. 'internal' => TRUE,
  332. 'load' => TRUE,
  333. ),
  334. 'tablecommands' => array(
  335. 'path' => $editor['library path'] . '/editor/plugins',
  336. 'buttons' => array(
  337. 'TableCellProp' => t('Table: Cell properties'),
  338. 'TableInsertRowAfter' => t('Table: Insert row after'),
  339. 'TableInsertColumnAfter' => t('Table: Insert column after'),
  340. 'TableInsertCellAfter' => t('Table: Insert cell after'),
  341. 'TableDeleteRows' => t('Table: Delete rows'),
  342. 'TableDeleteColumns' => t('Table: Delete columns'),
  343. 'TableDeleteCells' => t('Table: Delete cells'),
  344. 'TableMergeCells' => t('Table: Merge cells'),
  345. 'TableHorizontalSplitCell' => t('Table: Horizontal split cell'),
  346. ),
  347. 'internal' => TRUE,
  348. 'load' => TRUE,
  349. ),
  350. );
  351. return $plugins;
  352. }