| 1 |
<?php
|
| 2 |
// $Id: hierarchical_select.admin.inc,v 1.21 2009/03/21 20:07:46 wimleers Exp $
|
| 3 |
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* Module settings and configuration administration UI.
|
| 8 |
*/
|
| 9 |
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Form definition; admin settings.
|
| 13 |
*/
|
| 14 |
function hierarchical_select_admin_settings() {
|
| 15 |
$form['description'] = array(
|
| 16 |
'#value' => t('All settings below will be used as site-wide defaults.'),
|
| 17 |
'#prefix' => '<div>',
|
| 18 |
'#suffix' => '</div>',
|
| 19 |
);
|
| 20 |
$form['hierarchical_select_animation_delay'] = array(
|
| 21 |
'#type' => 'textfield',
|
| 22 |
'#title' => t('Animation delay'),
|
| 23 |
'#description' => t(
|
| 24 |
'The delay that will be used for the "drop in/out" effect when a
|
| 25 |
hierarchical select is being updated (in milliseconds).'
|
| 26 |
),
|
| 27 |
'#size' => 5,
|
| 28 |
'#maxlength' => 5,
|
| 29 |
'#default_value' => variable_get('hierarchical_select_animation_delay', 400),
|
| 30 |
);
|
| 31 |
$form['hierarchical_select_level_labels_style'] = array(
|
| 32 |
'#type' => 'select',
|
| 33 |
'#title' => t('Level labels style'),
|
| 34 |
'#description' => t(
|
| 35 |
'The style that will be used for level labels. This is not supported by
|
| 36 |
all browsers! If you want a consistent interface, choose to use no
|
| 37 |
style.'
|
| 38 |
),
|
| 39 |
'#options' => array(
|
| 40 |
'none' => t('No style'),
|
| 41 |
'bold' => t('Bold'),
|
| 42 |
'inversed' => t('Inversed'),
|
| 43 |
'underlined' => t('Underlined'),
|
| 44 |
),
|
| 45 |
'#default_value' => variable_get('hierarchical_select_level_labels_style', 'none'),
|
| 46 |
);
|
| 47 |
$form['hierarchical_select_js_cache_system'] = array(
|
| 48 |
'#type' => 'radios',
|
| 49 |
'#title' => t('Cache in a HTML 5 client-side database'),
|
| 50 |
'#description' => t(
|
| 51 |
'This feature only works in browsers that support the
|
| 52 |
<a href="!spec-url">HTML 5 client-side database storage specification
|
| 53 |
</a>.</br>
|
| 54 |
After enabling this, you will notice (in supporting browsers) that
|
| 55 |
refreshing the hierarchical select will not require a request to the
|
| 56 |
server when a part is being requested that has been requested before.',
|
| 57 |
array('!spec-url' => url('http://www.whatwg.org/specs/web-apps/current-work/multipage/section-sql.html'))
|
| 58 |
),
|
| 59 |
'#options' => array(
|
| 60 |
0 => t('Disabled'),
|
| 61 |
1 => t('Enabled'),
|
| 62 |
),
|
| 63 |
'#default_value' => variable_get('hierarchical_select_js_cache_system', 0),
|
| 64 |
);
|
| 65 |
|
| 66 |
|
| 67 |
return system_settings_form($form);
|
| 68 |
}
|
| 69 |
|
| 70 |
/**
|
| 71 |
* Menu callback; a table that lists all Hierarchical Select configs.
|
| 72 |
*/
|
| 73 |
function hierarchical_select_admin_configs() {
|
| 74 |
$header = array(t('Hierarchy type'), t('Hierarchy'), t('Entity type'), t('Entity'), t('Context type'), t('Context'), t('Actions'));
|
| 75 |
|
| 76 |
// Retrieve all information items
|
| 77 |
$info_items = array();
|
| 78 |
foreach (module_implements('hierarchical_select_config_info') as $module) {
|
| 79 |
$info_items = array_merge_recursive($info_items, module_invoke($module, 'hierarchical_select_config_info'));
|
| 80 |
}
|
| 81 |
|
| 82 |
// Process the retrieved information into rows.
|
| 83 |
$rows = array();
|
| 84 |
foreach ($info_items as $id => $item) {
|
| 85 |
$config_id = $item['config_id'];
|
| 86 |
|
| 87 |
$rows[$id] = array(
|
| 88 |
$item['hierarchy type'],
|
| 89 |
$item['hierarchy'],
|
| 90 |
$item['entity type'],
|
| 91 |
$item['entity'],
|
| 92 |
$item['context type'],
|
| 93 |
$item['context'],
|
| 94 |
theme('links', array(
|
| 95 |
array('title' => t('Edit'), 'href' => $item['edit link'], 'fragment' => "hierarchical-select-config-form-$config_id"),
|
| 96 |
array('title' => t('Export'), 'href' => "admin/settings/hierarchical_select/export/$config_id"),
|
| 97 |
array('title' => t('Import'), 'href' => "admin/settings/hierarchical_select/import/$config_id"),
|
| 98 |
)),
|
| 99 |
);
|
| 100 |
}
|
| 101 |
|
| 102 |
return theme('table', $header, $rows, array(), t('Overview of all Hierarchical Select configurations.'));
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Menu callback; a table that lists all Hierarchical Select implementations
|
| 107 |
* and the features they support.
|
| 108 |
*/
|
| 109 |
function hierarchical_select_admin_implementations() {
|
| 110 |
$output = '';
|
| 111 |
$header = array(t('Implementation (module)'), t('Hierarchy type'), t('Entity type'), t('Create new items'), t('Entity count'));
|
| 112 |
|
| 113 |
// Retrieve all information items
|
| 114 |
$rows = array();
|
| 115 |
foreach (module_implements('hierarchical_select_root_level') as $module) {
|
| 116 |
$filename = db_result(db_query("SELECT filename FROM {system} WHERE type = 'module' AND name = '%s'", $module));
|
| 117 |
$module_info = drupal_parse_info_file(dirname($filename) ."/$module.info");
|
| 118 |
// Try to extract the hierarchy type from the optional hook_hierarchical_select_config_info().
|
| 119 |
$hierarchy_type = $entity_type = t('unknown');
|
| 120 |
if (module_hook($module, 'hierarchical_select_implementation_info')) {
|
| 121 |
$implementation = module_invoke($module, 'hierarchical_select_implementation_info');
|
| 122 |
$hierarchy_type = $implementation['hierarchy type'];
|
| 123 |
$entity_type = $implementation['entity type'];
|
| 124 |
}
|
| 125 |
|
| 126 |
$rows[] = array(
|
| 127 |
$module_info['name'],
|
| 128 |
$hierarchy_type,
|
| 129 |
$entity_type,
|
| 130 |
(module_hook($module, 'hierarchical_select_create_item')) ? t('Yes') : t('No'),
|
| 131 |
(module_hook($module, 'hierarchical_select_entity_count')) ? t('Yes') : t('No'),
|
| 132 |
);
|
| 133 |
}
|
| 134 |
|
| 135 |
$output .= '<p>';
|
| 136 |
$output .= t('
|
| 137 |
The table below allows you to find out <strong>which Hierarchical Select
|
| 138 |
features are supported</strong> by the implementations of the Hierarchical
|
| 139 |
Select API.<br />
|
| 140 |
It is <strong>not a reflection of some settings</strong>.
|
| 141 |
');
|
| 142 |
$output .= '</p>';
|
| 143 |
|
| 144 |
$output .= theme('table', $header, $rows, array(), t('Overview of all installed Hierarchical Select implementations.'));
|
| 145 |
|
| 146 |
return $output;
|
| 147 |
}
|
| 148 |
|
| 149 |
/**
|
| 150 |
* Form definition; config export form.
|
| 151 |
*/
|
| 152 |
function hierarchical_select_admin_export(&$form_state, $config_id) {
|
| 153 |
require_once(drupal_get_path('module', 'hierarchical_select') .'/includes/common.inc');
|
| 154 |
|
| 155 |
$config = hierarchical_select_common_config_get($config_id);
|
| 156 |
$code = _hierarchical_select_create_export_code($config);
|
| 157 |
|
| 158 |
drupal_add_css(drupal_get_path('module', 'hierarchical_select') .'/hierarchical_select.css');
|
| 159 |
drupal_add_js('$(document).ready(function() { $(".hierarchical-select-code").focus(); });', 'inline');
|
| 160 |
|
| 161 |
$lines = substr_count($code, "\n") + 1;
|
| 162 |
$form['config'] = array(
|
| 163 |
'#type' => 'textarea',
|
| 164 |
'#title' => t('Hierarchical Select configuration %config_id', array('%config_id' => $config_id)),
|
| 165 |
'#default_value' => $code,
|
| 166 |
'#rows' => $lines,
|
| 167 |
'#attributes' => array('class' => 'hierarchical-select-config-code'),
|
| 168 |
);
|
| 169 |
|
| 170 |
return $form;
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* Form definition; config import form.
|
| 175 |
*/
|
| 176 |
function hierarchical_select_admin_import($config_id) {
|
| 177 |
require_once(drupal_get_path('module', 'hierarchical_select') .'/includes/common.inc');
|
| 178 |
|
| 179 |
drupal_add_css(drupal_get_path('module', 'hierarchical_select') .'/hierarchical_select.css');
|
| 180 |
drupal_add_js('$(document).ready(function() { $(".hierarchical-select-code").focus(); });', 'inline');
|
| 181 |
|
| 182 |
$form['config'] = array(
|
| 183 |
'#type' => 'textarea',
|
| 184 |
'#title' => t('Import Hierarchical Select configuration code'),
|
| 185 |
'#cols' => 60,
|
| 186 |
'#rows' => 15,
|
| 187 |
'#description' => t('Copy and paste the results of an exported
|
| 188 |
Hierarchical Select configuration here.<br />This will override the
|
| 189 |
current Hierarchical Select configuration for %config_id.',
|
| 190 |
array('%config_id' => $config_id)
|
| 191 |
),
|
| 192 |
'#attributes' => array('class' => 'hierarchical-select-config-code'),
|
| 193 |
);
|
| 194 |
$form['interpreted_config'] = array('#type' => 'value', '#value' => NULL);
|
| 195 |
$form['submit'] = array(
|
| 196 |
'#type' => 'submit',
|
| 197 |
'#value' => t("Import"),
|
| 198 |
);
|
| 199 |
$form['#redirect'] = NULL;
|
| 200 |
return $form;
|
| 201 |
}
|
| 202 |
|
| 203 |
/**
|
| 204 |
* Validate callback; config import form.
|
| 205 |
*/
|
| 206 |
function hierarchical_select_admin_import_validate($form, &$form_state) {
|
| 207 |
ob_start();
|
| 208 |
eval($form_state['values']['config']);
|
| 209 |
ob_end_clean();
|
| 210 |
|
| 211 |
form_set_value($form['interpreted_config'], serialize($config), $form_state);
|
| 212 |
|
| 213 |
if (empty($form_state['values']['config'])) {
|
| 214 |
form_error($form['config'], t('You did not enter anything.'));
|
| 215 |
}
|
| 216 |
elseif ($config == NULL) {
|
| 217 |
form_error($form['config'], t('There is a syntax error in the Hierarchical Select configuration you entered.'));
|
| 218 |
}
|
| 219 |
elseif (!isset($config['config_id']) || empty($config['config_id'])) {
|
| 220 |
form_error($form['config'], t('Unable to import this configuration, because no Hierarchical Select <em>config id</em> is set.'));
|
| 221 |
}
|
| 222 |
}
|
| 223 |
|
| 224 |
/**
|
| 225 |
* Submit callback; config import form.
|
| 226 |
*/
|
| 227 |
function hierarchical_select_admin_import_submit($form, &$form_state) {
|
| 228 |
$config = unserialize($form_state['values']['interpreted_config']);
|
| 229 |
$config_id = $config['config_id'];
|
| 230 |
hierarchical_select_common_config_set($config_id, $config);
|
| 231 |
drupal_set_message(t('Hierarchical Select configuration for %config_id imported!', array('%config_id' => $config_id)));
|
| 232 |
}
|
| 233 |
|
| 234 |
|
| 235 |
//----------------------------------------------------------------------------
|
| 236 |
// Private functions.
|
| 237 |
|
| 238 |
/**
|
| 239 |
* Given a config array, create the export code for it.
|
| 240 |
*
|
| 241 |
* @param $config
|
| 242 |
* A Hierarchical Select config array, as described in API.txt
|
| 243 |
* @return
|
| 244 |
* The code as it would appear in an editor.
|
| 245 |
*/
|
| 246 |
function _hierarchical_select_create_export_code($config) {
|
| 247 |
$output = '';
|
| 248 |
|
| 249 |
$output .= "\$config = array(\n";
|
| 250 |
$output .= " 'config_id' => '". $config['config_id'] ."',\n";
|
| 251 |
$output .= " 'save_lineage' => ". $config['save_lineage'] .",\n";
|
| 252 |
$output .= " 'enforce_deepest' => ". $config['enforce_deepest'] .",\n";
|
| 253 |
$output .= " 'entity_count' => ". $config['entity_count'] .",\n";
|
| 254 |
$output .= " 'require_entity' => ". $config['require_entity'] .",\n";
|
| 255 |
$output .= " 'resizable' => ". $config['resizable'] .",\n";
|
| 256 |
$output .= " 'level_labels' => array(\n";
|
| 257 |
$output .= " 'status' => ". $config['level_labels']['status'] .",\n";
|
| 258 |
$output .= " 'labels' => array(\n";
|
| 259 |
if (isset($config['level_labels']['labels'])) {
|
| 260 |
foreach ($config['level_labels']['labels'] as $depth => $label) {
|
| 261 |
$label = str_replace("'", "\'", $label);
|
| 262 |
$output .= " $depth => '$label',\n";
|
| 263 |
}
|
| 264 |
}
|
| 265 |
$output .= " ),\n";
|
| 266 |
$output .= " ),\n";
|
| 267 |
$output .= " 'dropbox' => array(\n";
|
| 268 |
$output .= " 'status' => ". $config['dropbox']['status'] .",\n";
|
| 269 |
$output .= " 'title' => '". str_replace("'", "\'", $config['dropbox']['title']) ."',\n";
|
| 270 |
$output .= " 'limit' => ". $config['dropbox']['limit'] .",\n";
|
| 271 |
$output .= " 'reset_hs' => ". $config['dropbox']['reset_hs'] .",\n";
|
| 272 |
$output .= " ),\n";
|
| 273 |
$output .= " 'editability' => array(\n";
|
| 274 |
$output .= " 'status' => ". $config['editability']['status'] .",\n";
|
| 275 |
$output .= " 'item_types' => array(\n";
|
| 276 |
if (isset($config['editability']['item_types'])) {
|
| 277 |
foreach ($config['editability']['item_types'] as $depth => $item_type) {
|
| 278 |
$item_type = str_replace("'", "\'", $item_type);
|
| 279 |
$output .= " $depth => '$item_type',\n";
|
| 280 |
}
|
| 281 |
}
|
| 282 |
$output .= " ),\n";
|
| 283 |
$output .= " 'allowed_levels' => array(\n";
|
| 284 |
if (isset($config['editability']['allowed_levels'])) {
|
| 285 |
foreach ($config['editability']['allowed_levels'] as $depth => $allowed_level) {
|
| 286 |
$output .= " $depth => $allowed_level,\n";
|
| 287 |
}
|
| 288 |
}
|
| 289 |
$output .= " ),\n";
|
| 290 |
$output .= " 'allow_new_levels' => ". $config['editability']['allow_new_levels'] .",\n";
|
| 291 |
$output .= " 'max_levels' => ". $config['editability']['max_levels'] .",\n";
|
| 292 |
$output .= " ),\n";
|
| 293 |
$output .= ");\n";
|
| 294 |
|
| 295 |
return $output;
|
| 296 |
}
|