| 1 |
<?php
|
| 2 |
// $Id: ext.module,v 1.3 2008/05/24 00:47:07 ximo Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Load the Ext JavaScript framework with Drupal.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_init().
|
| 11 |
*/
|
| 12 |
function ext_init() {
|
| 13 |
// Load the Ext library if enabled in settings.
|
| 14 |
if (variable_get('ext_state', 0)) {
|
| 15 |
ext_load_library();
|
| 16 |
}
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implementation of hook_perm().
|
| 21 |
*/
|
| 22 |
function ext_perm() {
|
| 23 |
return array('administer ext');
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Implementation of hook_menu().
|
| 28 |
*/
|
| 29 |
function ext_menu() {
|
| 30 |
$items['admin/settings/ext'] = array(
|
| 31 |
'title' => 'Ext',
|
| 32 |
'description' => 'Settings for Ext.',
|
| 33 |
'page callback' => 'drupal_get_form',
|
| 34 |
'page arguments' => array('ext_form_settings'),
|
| 35 |
'access arguments' => array('administer ext'),
|
| 36 |
);
|
| 37 |
|
| 38 |
// Add a default General settings tab.
|
| 39 |
$items['admin/settings/ext/general'] = array(
|
| 40 |
'title' => t('General'),
|
| 41 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 42 |
'weight' => -10,
|
| 43 |
);
|
| 44 |
|
| 45 |
return $items;
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Ext module settings form.
|
| 50 |
*/
|
| 51 |
function ext_form_settings() {
|
| 52 |
// Fetch the path to Ext, removing the potential trailing slash.
|
| 53 |
$ext_path = rtrim(variable_get('ext_path', drupal_get_path('module', 'ext') .'/ext'), '/');
|
| 54 |
|
| 55 |
// Path to Ext library.
|
| 56 |
$form['ext_path'] = array(
|
| 57 |
'#type' => 'textfield',
|
| 58 |
'#title' => t('Path to library'),
|
| 59 |
'#description' => t('Specify the path to where the Ext library is installed, relative to the Drupal installation directory.'),
|
| 60 |
'#default_value' => $ext_path,
|
| 61 |
);
|
| 62 |
|
| 63 |
// Retrieve all .js files from the Ext directory.
|
| 64 |
$libraries = file_scan_directory($ext_path, '\.js$', array(), 0, FALSE);
|
| 65 |
|
| 66 |
if ($libraries) {
|
| 67 |
// Add radios for all available Ext libraries.
|
| 68 |
foreach ($libraries as $library) {
|
| 69 |
$library_options[$library->basename] = $library->basename;
|
| 70 |
}
|
| 71 |
$form['ext_library'] = array(
|
| 72 |
'#type' => 'radios',
|
| 73 |
'#title' => t('Library file'),
|
| 74 |
'#description' => t('Select which Ext library file to use.'),
|
| 75 |
'#options' => $library_options,
|
| 76 |
'#default_value' => variable_get('ext_library', 'ext-all.js'),
|
| 77 |
);
|
| 78 |
|
| 79 |
// Default state of the Ext library.
|
| 80 |
$form['ext_state'] = array(
|
| 81 |
'#type' => 'radios',
|
| 82 |
'#title' => t('Default state'),
|
| 83 |
'#description' => t('Should it be running all the time (Enabled), or only when needed by another module (Disabled)?'),
|
| 84 |
'#options' => array(t('Disabled'), t('Enabled')),
|
| 85 |
'#default_value' => variable_get('ext_state', 0),
|
| 86 |
);
|
| 87 |
}
|
| 88 |
else {
|
| 89 |
// If no Ext libraries were found, produce a warning. TODO: Make this go away as soon as the problem is solved.
|
| 90 |
drupal_set_message(t('There were no Ext library files found in the specified directory. Please make sure the path leads to a directory containing the Ext library files.'), 'error', FALSE);
|
| 91 |
}
|
| 92 |
|
| 93 |
// Retrieve minified locales from the 'build/locale' directory.
|
| 94 |
$locales = file_scan_directory($ext_path .'/build/locale', '\-min.js$', array('ext-lang-en-min.js'));
|
| 95 |
|
| 96 |
if ($locales) {
|
| 97 |
// Attempt to retrieve the names of the locales.
|
| 98 |
include_once './includes/locale.inc';
|
| 99 |
$locales_list = _locale_get_predefined_list();
|
| 100 |
foreach ($locales as $locale) {
|
| 101 |
$language = strtolower(str_replace(array('ext-lang-', '-min', '_'), array('', '', '-'), $locale->name));
|
| 102 |
$locale_options[$locale->basename] = (isset($locales_list[$language])) ? t($locales_list[$language][0]) : $language;
|
| 103 |
}
|
| 104 |
natcasesort($locale_options); // Case insensitive sort
|
| 105 |
|
| 106 |
// Add the default option to the top of the list.
|
| 107 |
$locale_options = array_merge(array(0 => t('English (default)')), $locale_options);
|
| 108 |
|
| 109 |
// Add radios for all Ext locales.
|
| 110 |
$form['ext_locale'] = array(
|
| 111 |
'#type' => 'select',
|
| 112 |
'#title' => t('Locale'),
|
| 113 |
'#description' => t('Select the locale you wish to use with Ext.'),
|
| 114 |
'#options' => $locale_options,
|
| 115 |
'#default_value' => variable_get('ext_locale', 0),
|
| 116 |
);
|
| 117 |
}
|
| 118 |
|
| 119 |
return system_settings_form($form);
|
| 120 |
}
|
| 121 |
|
| 122 |
/**
|
| 123 |
* Load the Ext library.
|
| 124 |
*
|
| 125 |
* @param $library
|
| 126 |
* File name of the library to load, relative to the Ext library directory.
|
| 127 |
* Defaults to NULL (user selected library).
|
| 128 |
*/
|
| 129 |
function ext_load_library($library = NULL) {
|
| 130 |
static $loaded = FALSE;
|
| 131 |
|
| 132 |
// Make sure an Ext library is only loaded once.
|
| 133 |
if (!$loaded) {
|
| 134 |
// Fetch the path to Ext, removing the potential trailing slash. TODO: Do this when saving settings.
|
| 135 |
$ext_path = rtrim(variable_get('ext_path', drupal_get_path('module', 'ext') .'/ext'), '/');
|
| 136 |
|
| 137 |
// Load adapter.
|
| 138 |
drupal_add_js($ext_path .'/adapter/jquery/ext-jquery-adapter.js');
|
| 139 |
|
| 140 |
// Load library.
|
| 141 |
if (!$library) {
|
| 142 |
$library = variable_get('ext_library', 'ext-all.js');
|
| 143 |
}
|
| 144 |
drupal_add_js($ext_path .'/'. $library);
|
| 145 |
|
| 146 |
// Load locale, if one has been specified.
|
| 147 |
if (variable_get('ext_locale', 0)) {
|
| 148 |
drupal_add_js($ext_path .'/build/locale/'. variable_get('ext_locale', 0));
|
| 149 |
}
|
| 150 |
|
| 151 |
// Load CSS. TODO: Add settings for this.
|
| 152 |
drupal_add_css($ext_path .'/resources/css/ext-all.css');
|
| 153 |
|
| 154 |
$loaded = TRUE;
|
| 155 |
}
|
| 156 |
}
|