From 0d6bc2a27f833ebc32f68792bb99ce47fa18cbcc Mon Sep 17 00:00:00 2001 From: Ron Shimshock Date: Wed, 12 Apr 2017 14:40:21 -0500 Subject: [PATCH] Add custom skin support --- ckeditor.module | 56 ++++++++++++++++++++++++++++++++++++++++++ includes/ckeditor.admin.inc | 59 +++++++++++++++++++++++++++++++++++++++++++++ includes/ckeditor.lib.inc | 36 +++++++++++++++++++++++---- 3 files changed, 146 insertions(+), 5 deletions(-) diff --git a/ckeditor.module b/ckeditor.module index b6b1ca0..e1bfb4d 100644 --- a/ckeditor.module +++ b/ckeditor.module @@ -541,6 +541,62 @@ function ckeditor_plugins_path($mode = 'relative', $refresh = FALSE) { } /** + * Read the CKEditor skins path from the Global profile. + * + * @return + * Path to CKEditor skins folder. + */ +function ckeditor_skins_path($mode = 'relative', $refresh = FALSE) { + static $cke_static; + + if (!isset($cke_static)) { + $cke_static = array(); + } + + if ($refresh || !isset($cke_static[$mode])) { + $global_profile = ckeditor_profile_load('CKEditor Global Profile', $refresh); + switch ($mode) { + default: + case 'relative': + if ($global_profile && isset($global_profile->settings['ckeditor_skins_path'])) { + $cke_skins_path = $global_profile->settings['ckeditor_skins_path']; + $cke_skins_path = strtr($cke_skins_path, array("%b" => ckeditor_base_path('relative'), "%m" => ckeditor_module_path('relative'), "%l" => ckeditor_library_path('relative'))); + $cke_skins_path = str_replace('\\', '/', $cke_skins_path); + $cke_skins_path = str_replace('//', '/', $cke_skins_path); + $cke_skins_path = rtrim($cke_skins_path, ' \/'); + return $cke_static[$mode] = $cke_skins_path; + } + return $cke_static[$mode] = ckeditor_module_path('relative') . '/skins'; + case 'local': + if ($global_profile) { + if (!empty($global_profile->settings['ckeditor_skins_local_path'])) { + return $cke_static[$mode] = $global_profile->settings['ckeditor_skins_local_path']; + } + if (isset($global_profile->settings['ckeditor_skins_path'])) { + $cke_skins_local_path = $global_profile->settings['ckeditor_skins_path']; + $cke_skins_local_path = strtr($cke_skins_local_path, array("%b" => ckeditor_base_path('local'), "%m" => ckeditor_module_path('local'), "%l" => ckeditor_library_path('local'))); + return $cke_static[$mode] = $cke_skins_local_path; + } + } + return $cke_static[$mode] = ckeditor_module_path('local') . '/skins'; + case 'url': + if ($global_profile && isset($global_profile->settings['ckeditor_skins_path'])) { + $cke_skins_path = $global_profile->settings['ckeditor_skins_path']; + $cke_skins_path = strtr($cke_skins_path, array("%m" => ckeditor_module_path('url'), "%l" => ckeditor_library_path('url'))); + $cke_skins_path = str_replace('\\', '/', $cke_skins_path); + $cke_skins_path = str_replace('//', '/', $cke_skins_path); + $cke_skins_path = rtrim($cke_skins_path, ' \/'); + //In D7 base path in URL mode is not needed, so we need to remove it with trailing slash (if exists) + $cke_skins_path = str_replace(array("%b/", "%b"), '', $cke_skins_path); + return $cke_static[$mode] = $cke_skins_path; + } + return $cke_static[$mode] = ckeditor_module_path('url') . '/skins'; + } + } + return $cke_static[$mode]; +} + +/** * Read the CKFinder path from the Global profile. * * @return diff --git a/includes/ckeditor.admin.inc b/includes/ckeditor.admin.inc index 9bcbefc..80a82b7 100644 --- a/includes/ckeditor.admin.inc +++ b/includes/ckeditor.admin.inc @@ -327,6 +327,41 @@ function ckeditor_admin_global_profile_form($form, $form_state, $mode = 'add') { ) ); + $form['ckeditor_advanced_settings']['ckeditor_skins_path'] = array( + '#type' => 'textfield', + '#title' => t('Path to the CKEditor skins directory'), + '#default_value' => !empty($profile->settings['ckeditor_skins_path']) ? $profile->settings['ckeditor_skins_path'] : '', + '#size' => 40, + '#maxlength' => 128, + '#description' => t('Path to the CKEditor skins directory relative to the document root.') . + '
' . + t('Available placeholders:
%b – path of the Drupal installation (@base)
%m – the base URL path where the CKEditor module is stored (@module).
%l – the base URL path to the libraries directory (@library)', array( + '@base' => $drupal_base_path, + '@module' => $module_drupal_path, + '@library' => $drupal_library_path, + ) + ) . + '
' . + t('Current path: @path', array( + '@path' => ckeditor_skins_path('relative') + ) + ) + ); + + $form['ckeditor_advanced_settings']['ckeditor_skins_local_path'] = array( + '#type' => 'textfield', + '#title' => t('Local path to the CKEditor skins directory'), + '#default_value' => isset($profile->settings['ckeditor_skins_local_path']) ? $profile->settings['ckeditor_skins_local_path'] : '', + '#size' => 40, + '#maxlength' => 128, + '#description' => t('The path to the local directory (on the server) that points to the path defined above. Enter either an absolute server path or a path relative to the index.php file. If left empty, the CKEditor module will try to find the right path.') . + '
' . + t('Current path: @path', array( + '@path' => ckeditor_skins_path('local') + ) + ) + ); + $form['ckeditor_advanced_settings']['ckfinder_path'] = array( '#type' => 'textfield', '#title' => t('Path to CKFinder'), @@ -458,7 +493,19 @@ function ckeditor_admin_global_profile_form($form, $form_state, $mode = 'add') { * Form validation for a global profile */ function ckeditor_admin_global_profile_form_validate($form, &$form_state) { + $edit = & $form_state['values']; + if (!empty($edit['skin']) && $edit['skin'] != 'kama' && $edit['skin'] != 'moono') { + $skin_js_local = ckeditor_skins_path('local') . '/' . $edit['skin'] . '/skin.js'; + if (stripos($edit['ckeditor_path'], '//cdn.ckeditor.com/', 0) !== FALSE && !file_exists($skin_js_local)) { + $skin_js_relative = ckeditor_skins_path('relative') . '/' . $edit['skin'] . '/skin.js'; + $err_msg = t('When using the CKEditor CDN and a skin other than Kama or Moono, the skin.js file must be accessible locally. To fix this issue, download the skin from CKEditor Addons, extract skin.js, and place the file in the sub-directory named for the selected skin (for example: @skin_js).', array( + '@url' => 'http://ckeditor.com/addons/skins/all', + '@skin_js' => $skin_js_relative + )); + form_set_error('ckeditor_skins_path', $err_msg); + } + } } /** @@ -514,6 +561,18 @@ function ckeditor_admin_global_profile_form_submit($form, &$form_state) { $edit['ckeditor_plugins_path'] = trim(rtrim($edit['ckeditor_plugins_path'], "/")); //strip slash from the end + if (empty($edit['ckeditor_skins_path'])) { + $edit['ckeditor_skins_path'] = ''; + } + $edit['ckeditor_skins_path'] = trim(rtrim($edit['ckeditor_skins_path'], "/")); + if ($edit['ckeditor_skins_path'] && 0 !== strpos($edit['ckeditor_skins_path'], "/") && 0 !== strpos($edit['ckeditor_skins_path'], "%")) { + //ensure that slash is at the beginning + $edit['ckeditor_skins_path'] = "/" . $edit['ckeditor_skins_path']; + } + //no slash at the end + $edit['ckeditor_skins_path'] = trim(rtrim($edit['ckeditor_skins_path'], "/")); + + //strip slash from the end if (empty($edit['ckfinder_path'])) { $edit['ckfinder_path'] = ''; } diff --git a/includes/ckeditor.lib.inc b/includes/ckeditor.lib.inc index 3370968..826e353 100644 --- a/includes/ckeditor.lib.inc +++ b/includes/ckeditor.lib.inc @@ -161,9 +161,10 @@ function ckeditor_load_toolbar_options() { */ function ckeditor_load_skin_options() { $arr = array(); + $editor_path = ckeditor_path('relative'); $editor_local_path = ckeditor_path('local'); - $skin_dir = $editor_local_path . '/skins'; - if ($editor_local_path != '' && is_dir($skin_dir)) { + $skin_dir = ckeditor_skins_path('local'); + if (($editor_local_path != '' || ($editor_path == '' && $editor_local_path == '')) && is_dir($skin_dir)) { $dh = @opendir($skin_dir); if (FALSE !== $dh) { while (($file = readdir($dh)) !== FALSE) { @@ -574,6 +575,8 @@ function ckeditor_profile_settings_compile($global_profile, $profile) { $module_drupal_local_path = ckeditor_module_path('local'); $editor_path = ckeditor_path('relative'); $editor_local_path = ckeditor_path('local'); + $skins_path = ckeditor_skins_path('relative'); + $skins_local_path = ckeditor_skins_path('local'); $toolbar = $conf['toolbar']; @@ -594,9 +597,32 @@ function ckeditor_profile_settings_compile($global_profile, $profile) { $settings['toolbarCanCollapse'] = true; } $settings['width'] = $conf['width']; - //check if skin exists, if not select default one - if (isset($global_profile->settings['skin']) && file_exists($editor_local_path . '/skins/' . $global_profile->settings['skin'])) { - $settings['skin'] = $global_profile->settings['skin']; + // check if skin exists, if not select default one + if (isset($global_profile->settings['skin'])) { + // if skin exists, check if local file is present + if (file_exists($skins_local_path . '/' . $global_profile->settings['skin'])) { + // if editor is local, set skin as profile value + if ($editor_local_path != '') { + $settings['skin'] = $global_profile->settings['skin']; + } + // if editor is remote, set skin as profile value plus local path + // example: http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-skin + else if ($editor_path == '' && $editor_local_path == '') { + $settings['skin'] = $global_profile->settings['skin'] . ',' . $skins_path . '/' . $global_profile->settings['skin'] . '/'; + } + else { + $settings['skin'] = ckeditor_default_skin(); + } + } + else { + // if no local skin file, check if editor is hosted remotely + if ($editor_path == '' && $editor_local_path == '') { + $settings['skin'] = $global_profile->settings['skin']; + } + else { + $settings['skin'] = ckeditor_default_skin(); + } + } } else { $settings['skin'] = ckeditor_default_skin(); -- 2.11.0