View source
<?php
define('JQMULTI_JQUERY_GROUP', -51);
define('JQMULTI_FILES_GROUP', -50);
define('JQMULTI_SWITCH_GROUP', -49);
function jqmulti_menu() {
$items = array();
$items['admin/config/system/jqmulti'] = array(
'title' => t('jQuery Multi Settings'),
'description' => t('Settings for jQuery Multi module'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'jqmulti_admin_form',
),
'access arguments' => array(
'administer jqmulti',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'jqmulti.admin.inc',
);
return $items;
}
function jqmulti_permission() {
return array(
'administer jqmulti' => array(
'title' => t('Administer jQuery Multi'),
),
);
}
function jqmulti_js_alter(&$javascript) {
$jquery_path = jqmulti_jquery_path();
if (!$jquery_path) {
return;
}
$files = jqmulti_get_files();
if (!variable_get('jqmulti_load_files_always', FALSE)) {
$files = array_intersect_key($javascript, $files);
}
if (empty($files)) {
if (!variable_get('jqmulti_load_always', FALSE)) {
return;
}
}
foreach ($files as $file => $options) {
$files[$file]['group'] = JQMULTI_FILES_GROUP;
}
$jquery = array(
$jquery_path => array(
'weight' => -100,
'group' => JQMULTI_JQUERY_GROUP,
),
);
$switch = array(
jqmulti_switch_path() => array(
'weight' => 100,
'group' => JQMULTI_SWITCH_GROUP,
),
);
$files = array_merge($jquery, $files, $switch);
foreach ($files as $file => $options) {
if (!array_key_exists($file, $javascript)) {
$javascript[$file] = drupal_js_defaults();
$javascript[$file]['data'] = $file;
}
if (isset($options['group'])) {
$javascript[$file]['group'] = $options['group'];
}
if (isset($options['weight'])) {
$javascript[$file]['weight'] = $options['weight'];
}
}
}
function jqmulti_get_files($reset = FALSE) {
if (!$reset && ($cache = cache_get('jqmulti_files'))) {
return $cache->data;
}
$files = module_invoke_all('jqmulti_files');
$libraries = module_invoke_all('jqmulti_libraries');
$ui_libs = variable_get('jqmulti_libraries', array());
foreach ($ui_libs as $ui_lib => $on) {
if ($on && !in_array($ui_lib, $libraries)) {
$libraries[] = $ui_lib;
}
}
foreach ($libraries as $library) {
$lib_files = jqmulti_get_library_files($library);
foreach ($lib_files as $lib_file) {
$files[] = $lib_file;
}
}
$temp_files = array();
foreach ($files as $key => $value) {
if (is_array($value)) {
$temp_files[$key] = $value;
}
else {
$temp_files[$value] = array();
}
}
$files = $temp_files;
cache_set('jqmulti_files', $files);
return $files;
}
function jqmulti_get_library_files($library, $reset = FALSE) {
$base_path = 'sites/all/libraries';
$path = $base_path . '/' . $library;
if (!$reset && ($cache = cache_get('jqmulti_files_' . $library))) {
return $cache->data;
}
$files = jqmulti_find_all_js_files($path);
cache_set('jqmulti_files_' . $library, $files);
return $files;
}
function jqmulti_get_available_libraries() {
$libs = libraries_get_libraries();
unset($libs['jquery']);
foreach ($libs as $key => $value) {
$libs[$key] = $key;
}
return $libs;
}
function jqmulti_switch_path() {
return drupal_get_path('module', 'jqmulti') . '/js/switch.js';
}
function jqmulti_jquery_path() {
$version = jqmulti_get_version();
if ($version) {
return libraries_get_path('jquery') . '/jquery-' . $version . '.min.js';
}
return FALSE;
}
function jqmulti_find_all_js_files($dir) {
$result = array();
$root = scandir($dir);
foreach ($root as $value) {
if ($value === '.' || $value === '..') {
continue;
}
$path = "{$dir}/{$value}";
if (is_file($path)) {
if (preg_match('/.js$/', $value)) {
$result[] = $path;
}
continue;
}
foreach (jqmulti_find_all_js_files($path) as $value) {
$result[] = $value;
}
}
return $result;
}
function jqmulti_get_version($reset = FALSE) {
if (!$reset && ($cache = cache_get('jqmulti_version'))) {
return $cache->data;
}
$libraries = libraries_get_libraries();
if (isset($libraries['jquery'])) {
$files = jqmulti_get_library_files('jquery', $reset);
if ($files) {
foreach ($files as $file) {
$version = jqmulti_jquery_version_from_path($file);
if ($version) {
cache_set('jqmulti_version', $version);
return $version;
}
}
}
}
return FALSE;
}
function jqmulti_jquery_version_from_path($path) {
$parts = explode('/', $path);
$filename = array_pop($parts);
$matches = array();
preg_match('/jquery-(.*)\\.min\\.js/', $filename, $matches);
if (count($matches) == 2) {
return $matches[1];
}
else {
return FALSE;
}
}