Commit 113dba1d authored by frjo's avatar frjo
Browse files

Issue #989590: Added libraries support to Colorbox module. Improved...

Issue #989590: Added libraries support to Colorbox module. Improved colorbox_get_path() and error messages for missing Colorbox plugin.
parent 2141b701
......@@ -11,7 +11,30 @@
function colorbox_admin_settings() {
drupal_add_js(drupal_get_path('module', 'colorbox') . '/js/colorbox_admin_settings.js', array('preprocess' => FALSE));
$colorbox_path = variable_get('colorbox_path', COLORBOX_PATH);
$colorbox_path = colorbox_get_path();
$form['colorbox_plugin_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Colorbox plugin settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE
);
$form['colorbox_plugin_settings']['colorbox_path'] = array(
'#type' => 'textfield',
'#title' => t('Path'),
'#default_value' => $colorbox_path,
'#description' => t('The location where Colorbox plugin is installed. Relative paths are from the Drupal root directory.'),
'#after_build' => array('_colorbox_admin_settings_check_plugin_path'),
);
$form['colorbox_plugin_settings']['colorbox_compression_type'] = array(
'#type' => 'radios',
'#title' => t('Choose Colorbox compression level'),
'#options' => array(
'min' => t('Production (Minified)'),
'none' => t('Development (Uncompressed Code)'),
),
'#default_value' => variable_get('colorbox_compression_type', 'min'),
);
if (module_exists('image')) {
$form['colorbox_imagefield'] = array(
......@@ -303,21 +326,6 @@ function colorbox_admin_settings() {
'#prefix' => '<div class="colorbox-title-trim-settings' . $js_hide . '">',
'#suffix' => '</div>',
);
$form['colorbox_advanced_settings']['colorbox_path'] = array(
'#type' => 'textfield',
'#title' => t('Path to Colorbox'),
'#default_value' => $colorbox_path,
'#description' => t('Enter the path relative to Drupal root where the Colorbox plugin directory is located. NO trailing slash!'),
);
$form['colorbox_advanced_settings']['colorbox_compression_type'] = array(
'#type' => 'radios',
'#title' => t('Choose Colorbox compression level'),
'#options' => array(
'min' => t('Production (Minified)'),
'none' => t('Development (Uncompressed Code)'),
),
'#default_value' => variable_get('colorbox_compression_type', 'min'),
);
$form['colorbox_advanced_settings']['colorbox_pages'] = array(
'#type' => 'textarea',
'#title' => t('Deactivate Colorbox on specific pages'),
......@@ -328,3 +336,19 @@ function colorbox_admin_settings() {
return system_settings_form($form);
}
/**
* Checks if the directory in $form_element exists and contains a file named
* 'jquery.colorbox.js'. If validation fails, the form element is flagged
* with an error from within the file_check_directory function.
*
* @param $form_element
* The form element containing the name of the directory to check.
*/
function _colorbox_admin_settings_check_plugin_path($form_element) {
$library_path = $form_element['#value'];
if (!is_dir($library_path) || !(file_exists($library_path .'/colorbox/jquery.colorbox.js') && file_exists($library_path .'/colorbox/jquery.colorbox-min.js'))) {
form_set_error($form_element['#parents'][0], t('You need to download the !colorbox and extract the entire contents of the archive into the %path folder of your server.', array('!colorbox' => l(t('Colorbox plugin'), 'http://colorpowered.com/colorbox/'), '%path' => $library_path)));
}
return $form_element;
}
......@@ -13,21 +13,22 @@ function colorbox_requirements($phase) {
if ($phase == 'runtime') {
$t = get_t();
$status = _colorbox_status();
$library_path = colorbox_get_path();
$colorbox_version = colorbox_get_version();
if (!$status['colorbox_plugin']) {
if (version_compare($colorbox_version, COLORBOX_MIN_PLUGIN_VERSION, '>=')) {
$requirements['colorbox_plugin'] = array(
'title' => $t('Colorbox plugin'),
'value' => $t('At least @a', array('@a' => COLORBOX_MIN_PLUGIN_VERSION)),
'severity' => REQUIREMENT_ERROR,
'description' => $t('You need to download the !colorbox and extract the entire contents of the archive into the %path folder of your server.', array('!colorbox' => l(t('Colorbox plugin'), 'http://colorpowered.com/colorbox/'), '%path' => 'sites/all/libraries')),
'severity' => REQUIREMENT_OK,
'value' => $colorbox_version,
);
}
else {
$requirements['colorbox_plugin'] = array(
'title' => $t('Colorbox plugin'),
'severity' => REQUIREMENT_OK,
'value' => colorbox_get_version(),
'value' => $t('At least @a', array('@a' => COLORBOX_MIN_PLUGIN_VERSION)),
'severity' => REQUIREMENT_ERROR,
'description' => $t('You need to download the !colorbox and extract the entire contents of the archive into the %path folder of your server.', array('!colorbox' => l(t('Colorbox plugin'), 'http://colorpowered.com/colorbox/'), '%path' => $library_path)),
);
}
}
......
......@@ -133,25 +133,6 @@ function _colorbox_form_page_access($form_id) {
return $access;
}
/**
* Check Colorbox dependencies, Colorbox plugin.
*
* @return
* array with TRUE/FALSE for each dependency.
*
* @see colorbox_requirements()
*/
function _colorbox_status() {
$status = array();
$status['colorbox_plugin'] = FALSE;
if (version_compare(colorbox_get_version(), COLORBOX_MIN_PLUGIN_VERSION, '>=')) {
$status['colorbox_plugin'] = TRUE;
}
return $status;
}
/**
* Check if Colorbox should be active for the current URL.
*
......@@ -244,7 +225,7 @@ function _colorbox_doheader() {
}
// Add and initialise the Colorbox plugin.
drupal_add_js(colorbox_get_path());
drupal_add_js(colorbox_get_js());
drupal_add_js($path . '/js/colorbox.js');
// Add JS and CSS based on selected style.
......@@ -286,30 +267,81 @@ function _colorbox_doheader() {
*
* @see version_compare()
*/
function colorbox_get_version($colorbox_path = NULL) {
function colorbox_get_version($colorbox_js = NULL) {
$version = 0;
$pattern = '#ColorBox v([0-9\.a-z]+)#';
// No file is passed in so use the default location.
if (is_null($colorbox_path)) {
$colorbox_path = colorbox_get_path();
if (is_null($colorbox_js)) {
$colorbox_js = colorbox_get_js();
}
// Return the version of Colorbox plugin.
$colorbox_plugin = file_get_contents($colorbox_path, NULL, NULL, 0, 32);
if (preg_match($pattern, $colorbox_plugin, $matches)) {
$version = $matches[1];
// Return the version of Colorbox plugin, it it exists.
if (file_exists($colorbox_js)) {
$colorbox_plugin = file_get_contents($colorbox_js, NULL, NULL, 0, 32);
if (preg_match($pattern, $colorbox_plugin, $matches)) {
$version = $matches[1];
}
}
return $version;
}
/**
* Return the JS filename for Colorbox plugin.
*
* @return
* Boolean indicating if the JS is located.
*/
function colorbox_get_js() {
$library_path = colorbox_get_path();
if (file_exists($library_path .'/colorbox/jquery.colorbox.js') && file_exists($library_path .'/colorbox/jquery.colorbox-min.js')) {
$colorbox_js_map = array('none' => 'jquery.colorbox.js', 'min' => 'jquery.colorbox-min.js');
$colorbox_js = $colorbox_js_map[variable_get('colorbox_compression_type', 'min')];
return $library_path .'/colorbox/'. $colorbox_js;
}
else {
drupal_set_message(t('You need to download the !colorbox and extract the entire contents of the archive into the %path folder of your server.', array('!colorbox' => l(t('Colorbox plugin'), 'http://colorpowered.com/colorbox/'), '%path' => $library_path)), 'error', FALSE);
return FALSE;
}
}
/**
* Return the path to the Colorbox plugin.
*/
function colorbox_get_path() {
$colorbox_file = array('none' => 'jquery.colorbox.js', 'min' => 'jquery.colorbox-min.js');
return variable_get('colorbox_path', COLORBOX_PATH) . '/colorbox/' . $colorbox_file[variable_get('colorbox_compression_type', 'min')];
static $library_path = NULL;
// Try to locate the library path in any possible setup.
if ($library_path == NULL) {
// First check the default location.
$path = variable_get('colorbox_path', COLORBOX_PATH);
if (is_dir($path .'/colorbox')) {
$library_path = $path;
}
// Ask the libraries module as a fallback.
elseif ($library_path == NULL && module_exists('libraries')) {
if ($path = libraries_get_path('colorbox')) {
$library_path = $path;
variable_set('colorbox_path', $library_path);
}
}
// HACK: If libraries api module is not installed but available, load it.
elseif ($library_path == NULL && file_exists(dirname(__FILE__) . '/../libraries/libraries.module')) {
require_once(dirname(__FILE__) . '/../libraries/libraries.module');
if ($path = libraries_get_path('colorbox')) {
$library_path = $path;
variable_set('colorbox_path', $library_path);
}
}
// If no path is found suggest the default one.
elseif ($library_path == NULL) {
$library_path = COLORBOX_PATH;
}
}
return $library_path;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment