'namedb') : array();
$settings = array(
'autocomplete_source' => array(
'title' => $src,
'given' => $src,
'middle' => $src,
'family' => $src,
'generational' => $src,
'credentials' => $src,
),
'autocomplete_separator' => array(
'title' => ' ',
'given' => ' -',
'middle' => ' -',
'family' => ' -',
'generational' => ' ',
'credentials' => ', ',
),
);
}
$action = array();
switch ($target) {
case 'name':
$action['components'] = drupal_map_assoc(array('given', 'middle', 'family'));
break;
case 'name-all':
$action['components'] = drupal_map_assoc($all_components);
break;
case 'title':
case 'given':
case 'middle':
case 'family':
case 'credentials':
case 'generational':
$action['components'] = array($target => $target);
break;
default:
$action['components'] = array();
foreach (explode('-', $target) as $component) {
if (in_array($component, array('title', 'given', 'middle', 'family', 'credentials', 'generational'))) {
$action['components'][$component] = $component;
}
}
break;
}
$action['source'] = array(
// 'vocab-123' => array(), etc
'namedb' => array(),
'title' => array(),
'generational' => array(),
// 'data' => array(),
);
$action['separater'] = '';
foreach ($action['components'] as $component) {
if (empty($settings['autocomplete_source'][$component])) {
unset($action['components'][$component]);
}
else {
$sep = (string)$settings['autocomplete_separator'][$component];
if (empty($sep)) {
$sep = ' ';
}
for ($i = 0; $i <= count($sep); $i++) {
if (strpos($action['separater'], $sep{$i}) === FALSE) {
$action['separater'] .= $sep{$i};
}
}
$found_source = FALSE;
foreach ((array)$settings['autocomplete_source'][$component] as $src) {
if ($src == 'namedb' && !$namedb_exists) {
continue;
}
if ($src == 'data' && !$field) {
continue;
}
if ($src == 'title' || $src == 'generational') {
if (!$field || $component != $src) {
continue;
}
}
$found_source = TRUE;
$action['source'][$src][] = $component;
}
if (!$found_source) {
unset($action['components'][$component]);
}
}
}
$pieces = preg_split('/[' . preg_quote($action['separater']) . ']+/', $string);
// We should have nice clean parameters to query.
if (!empty($pieces) && !empty($action['components'])) {
$test_string = drupal_strtolower(array_pop($pieces));
$base_string = drupal_substr($string, 0, drupal_strlen($string) - drupal_strlen($test_string));
// Query order is: vocab, namedb, title, generational, data
if (count($action['source']['namedb'])) {
$attributes = array(
'status' => 1,
'banned' => 0,
'limit' => $limit,
);
$results = namedb_query_names($test_string, $action['source']['namedb'], $attributes);
foreach ($results as $name => $safe_name) {
$matches[$base_string . $name] = $safe_name;
$limit--;
}
}
if ($limit > 0 && count($action['source']['title'])) {
$options = name_field_get_options($field, 'title');
foreach ($options as $key => $option) {
if (strpos(drupal_strtolower($key), $test_string) === 0 || strpos(drupal_strtolower($option), $test_string) === 0) {
$matches[$base_string . $key] = $key;
$limit--;
}
}
}
if ($limit > 0 && count($action['source']['generational'])) {
$options = name_field_get_options($field, 'generational');
foreach ($options as $key => $option) {
if (strpos(drupal_strtolower($key), $test_string) === 0 || strpos(drupal_strtolower($option), $test_string) === 0) {
$matches[$base_string . $key] = $key;
$limit--;
}
}
}
if ($limit > 0 && isset($action['source']['data']) && count($action['source']['data'])) {
}
}
}
drupal_json_output($matches);
}
/**
* Form builder function for module settings.
*/
function name_admin_settings_form($form, &$form_state) {
$settings = name_settings();
$form['#validate'][] = 'name_admin_settings_form_validate';
$form['name_settings'] = array('#tree' => TRUE);
$form['name_settings']['default_format'] = array(
'#type' => 'textfield',
'#title' => t('Default format'),
'#default_value' => $settings['default_format'],
'#description' => t('See help on drupal.org for more info.'),
'#required' => TRUE,
);
$form['name_settings']['sep1'] = array(
'#type' => 'textfield',
'#title' => t('Separator 1 replacement token'),
'#default_value' => $settings['sep1'],
);
$form['name_settings']['sep2'] = array(
'#type' => 'textfield',
'#title' => t('Separator 2 replacement token'),
'#default_value' => $settings['sep2'],
);
$form['name_settings']['sep3'] = array(
'#type' => 'textfield',
'#title' => t('Separator 3 replacement token'),
'#default_value' => $settings['sep3'],
);
// As the fieldset does not have the #input flag, this is not saved.
$form['name_format_help'] = _name_get_name_format_help_form();
return system_settings_form($form);
}
function name_admin_settings_form_validate($form, &$form_state) {
$default_format = trim($form_state['values']['name_settings']['default_format']);
if (empty($default_format) && !strlen($default_format)) {
form_set_error('name_settings][default_format', t('%title field is required.', array('%title' => $form['name_settings']['default_format']['#title'])));
}
}
/**
* Helper function to provide a list of example name components.
*
* @param array $excluded_components
* This will empty (set to "") any specified values.
*/
function name_example_names($excluded_components = array(), $field_name = NULL) {
$example_names = array(
1 => array(
'title' => 'Mr',
'given' => 'Joe',
'middle' => 'John Peter Mark',
'family' => 'Doe',
'generational' => 'Jnr.',
'credentials' => 'B.Sc., Ph.D.',
),
2 => array(
'title' => '',
'given' => 'JOAN',
'middle' => 'SUE',
'family' => 'DOE',
'generational' => '',
'credentials' => '',
),
3 => array(
'title' => '',
'given' => 'Prince',
'middle' => '',
'family' => '',
'generational' => '',
'credentials' => '',
),
);
$example_names = variable_get('name_example_names', $example_names);
if (isset($field_name)) {
$example_names = variable_get('name_example_names_' . $field_name, $example_names);
}
foreach ($example_names as $delta => $example_name) {
foreach ($example_name as $component => $value) {
if (in_array($component, $excluded_components)) {
$example_names[$delta][$component] = '';
}
}
}
return $example_names;
}
/**
* Lists the known custom formats.
*/
function name_list_custom_formats() {
$header = array(t('Name'), t('System code'), t('Format'), t('Examples'), t('Actions'));
$rows = array();
$example_names = name_example_names();
$default_format = new stdClass();
$default_format->ncfid = 0;
$default_format->name = t('Default');
$default_format->machine_name = 'default';
$default_format->format = name_settings('default_format');
$custom_formats = array('0' => $default_format) + name_get_custom_formats();
foreach ($custom_formats as $ncfid => $tag) {
$row = array();
$row[] = l($tag->name, 'admin/config/regional/name/' . ($ncfid ? $ncfid : 'settings'));
$row[] = $tag->machine_name;
$row[] = check_plain($tag->format);
$examples = array();
foreach ($example_names as $index => $example_name) {
$formatted = check_plain(name_format($example_name, $tag->format));
if (empty($formatted)) {
$formatted = '<<empty>>';
}
$examples[] = $formatted . " {$index}";
}
$row[] = implode('
', $examples);
if ($ncfid) {
$links = array();
$links[] = l(t('Edit'), 'admin/config/regional/name/' . $ncfid);
$links[] = l(t('Delete'), 'admin/config/regional/name/' . $ncfid . '/delete');
$row[] = implode(' ', $links);
}
else {
$row[] = l(t('Edit'), 'admin/config/regional/name/settings');
}
$rows[] = array('data' => $row, 'id' => 'name-' . $ncfid);
}
$help = '
' . t('The three examples are for the following users:') . '
';
$help_items = array();
foreach ($example_names as $example_name) {
// TODO make the labels generic
$help_items[] = t('The example %user has the following components; title is "%title", given is "%given", middle is "%middle", family is "%family", generational is "%generational", credentials is "%credentials"',
array(
'%user' => name_format($example_name, 't+ g+ m+ f+ s+ c'),
'%title' => $example_name['title'] ? $example_name['title'] : '<',
'#suffix' => '
',
'#weight' => 1,
'thead' => array(
'#prefix' => ' ',
'#weight' => 0,
),
'tbody' => array(
'#prefix' => '',
'#suffix' => '',
'#weight' => 1,
'title_display' => array(
'#prefix' => '' . t('Field') . ' ',
'#suffix' => ' ',
'#weight' => 1,
),
'field_type' => array(
'#prefix' => '' . t('Title display') . '1 ',
'#suffix' => ' ',
'#weight' => 2,
),
'size' => array(
'#prefix' => '' . t('Field type') . '2 ',
'#suffix' => ' ',
'#weight' => 3,
),
'inline_css' => array(
'#prefix' => '' . t('HTML size') . '3 ',
'#suffix' => ' ',
'#weight' => 4,
),
),
'tfoot' => array(
'#markup' => '' . t('Inline styles') . '4 ',
'#suffix' => ' ',
'#weight' => 2,
),
'extra_fields' => array(
'#weight' => 3,
),
);
$i = 0;
foreach (_name_translations() as $key => $title) {
// Adds the table header for the particullar field.
$form['styled_settings']['thead'][$key]['#markup'] = ''
. '
' . $title . ' ';
$form['styled_settings']['thead'][$key]['#weight'] = ++$i;
// Strip the title & description.
unset($form['instance_size'][$key]['#description']);
unset($form['instance_size'][$key]['#title']);
$form['instance_size'][$key]['#size'] = 5;
unset($form['instance_title_display'][$key]['#description']);
unset($form['instance_title_display'][$key]['#title']);
unset($form['instance_field_type'][$key]['#description']);
unset($form['instance_field_type'][$key]['#title']);
unset($form['instance_inline_css'][$key]['#description']);
unset($form['instance_inline_css'][$key]['#title']);
// Moves the size element into the table.
$form['styled_settings']['tbody']['size'][$key] = $form['instance_size'][$key];
$form['styled_settings']['tbody']['size'][$key]['#prefix'] = '';
$form['styled_settings']['tbody']['size'][$key]['#suffix'] = ' ';
$form['styled_settings']['tbody']['size'][$key]['#weight'] = $i;
$form['styled_settings']['tbody']['title_display'][$key] = $form['instance_title_display'][$key];
$form['styled_settings']['tbody']['title_display'][$key]['#prefix'] = '';
$form['styled_settings']['tbody']['title_display'][$key]['#suffix'] = ' ';
$form['styled_settings']['tbody']['title_display'][$key]['#weight'] = $i;
$form['styled_settings']['tbody']['field_type'][$key] = $form['instance_field_type'][$key];
$form['styled_settings']['tbody']['field_type'][$key]['#prefix'] = '';
$form['styled_settings']['tbody']['field_type'][$key]['#suffix'] = ' ';
$form['styled_settings']['tbody']['field_type'][$key]['#weight'] = $i;
$form['styled_settings']['tbody']['inline_css'][$key] = $form['instance_inline_css'][$key];
$form['styled_settings']['tbody']['inline_css'][$key]['#prefix'] = '';
$form['styled_settings']['tbody']['inline_css'][$key]['#suffix'] = ' ';
$form['styled_settings']['tbody']['inline_css'][$key]['#weight'] = $i;
// Clean up the leftovers.
unset($form['instance_size'][$key]);
$form['instance_size']['#access'] = FALSE;
unset($form['instance_title_display'][$key]);
$form['instance_title_display']['#access'] = FALSE;
unset($form['instance_field_type'][$key]);
$form['instance_field_type']['#access'] = FALSE;
unset($form['instance_inline_css'][$key]);
$form['instance_inline_css']['#access'] = FALSE;
}
return $form;
}
/**
* Themes the global field settings of the name component into a nice table,
* rather than a long list of individual elements.
*/
function _name_field_settings_pre_render($form) {
// $warning = t('Warning! Changing this setting after data has been created could result in the loss of data!');
$extra_max_info = '',
'#suffix' => '
',
'#weight' => 1,
'thead' => array(
'#prefix' => ' ',
'#weight' => 0,
),
'tbody' => array(
'#prefix' => '',
'#suffix' => '',
'#weight' => 1,
'components' => array(
'#prefix' => '' . t('Field') . ' ',
'#suffix' => ' ',
'#weight' => 1,
),
'minimum_components' => array(
'#prefix' => '' . t('Components') . '1 ',
'#suffix' => ' ',
'#weight' => 2,
),
'max_length' => array(
'#prefix' => '' . t('Minimum components') . '2 ',
'#suffix' => ' ',
'#weight' => 3,
),
'labels' => array(
'#prefix' => '' . t('Maximum length') . '3 ',
'#suffix' => ' ',
'#weight' => 4,
),
'sort_options' => array(
'#prefix' => '' . t('Labels') . '4 ',
'#suffix' => ' ',
'#weight' => 5,
),
'autocomplete_source' => array(
'#prefix' => '' . t('Sort options') . '5 ',
'#suffix' => ' ',
'#weight' => 6,
),
'autocomplete_separator' => array(
'#prefix' => '' . t('Autocomplete sources') . '6 ',
'#suffix' => ' ',
'#weight' => 7,
),
),
'tfoot' => array(
'#markup' => '' . t('Autocomplete separator') . '7 ',
'#suffix' => ' ',
'#weight' => 2,
),
);
$i = 0;
foreach (_name_translations() as $key => $title) {
// Adds the table header for the particullar field.
$form['field_properties']['thead'][$key]['#markup'] = ''
. '
' . $title . ' ';
$form['field_properties']['thead'][$key]['#weight'] = ++$i;
// Strip the title & description.
unset($form['components'][$key]['#description']);
unset($form['components'][$key]['#title']);
unset($form['minimum_components'][$key]['#description']);
unset($form['minimum_components'][$key]['#title']);
unset($form['max_length'][$key]['#description']);
unset($form['max_length'][$key]['#title']);
$form['max_length'][$key]['#size'] = 10;
unset($form['labels'][$key]['#description']);
unset($form['labels'][$key]['#title']);
$form['labels'][$key]['#size'] = 10;
if (isset($form['sort_options'][$key])) {
unset($form['sort_options'][$key]['#description']);
unset($form['sort_options'][$key]['#title']);
}
unset($form['autocomplete_source'][$key]['#description']);
unset($form['autocomplete_source'][$key]['#title']);
unset($form['autocomplete_separator'][$key]['#description']);
unset($form['autocomplete_separator'][$key]['#title']);
// Moves the elements into the table.
$form['field_properties']['tbody']['components'][$key] = $form['components'][$key];
$form['field_properties']['tbody']['components'][$key]['#prefix'] = '';
$form['field_properties']['tbody']['components'][$key]['#suffix'] = ' ';
$form['field_properties']['tbody']['components'][$key]['#weight'] = $i;
$form['field_properties']['tbody']['minimum_components'][$key] = $form['minimum_components'][$key];
$form['field_properties']['tbody']['minimum_components'][$key]['#prefix'] = '';
$form['field_properties']['tbody']['minimum_components'][$key]['#suffix'] = ' ';
$form['field_properties']['tbody']['minimum_components'][$key]['#weight'] = $i;
$form['field_properties']['tbody']['max_length'][$key] = $form['max_length'][$key];
$form['field_properties']['tbody']['max_length'][$key]['#prefix'] = '';
$form['field_properties']['tbody']['max_length'][$key]['#suffix'] = ' ';
$form['field_properties']['tbody']['max_length'][$key]['#weight'] = $i;
$form['field_properties']['tbody']['labels'][$key] = $form['labels'][$key];
$form['field_properties']['tbody']['labels'][$key]['#prefix'] = '';
$form['field_properties']['tbody']['labels'][$key]['#suffix'] = ' ';
$form['field_properties']['tbody']['labels'][$key]['#weight'] = $i;
if (isset($form['sort_options'][$key])) {
$form['field_properties']['tbody']['sort_options'][$key] = $form['sort_options'][$key];
}
else {
$form['field_properties']['tbody']['sort_options'][$key] = array('#markup' => ' ');
}
$form['field_properties']['tbody']['sort_options'][$key]['#prefix'] = '';
$form['field_properties']['tbody']['sort_options'][$key]['#suffix'] = ' ';
$form['field_properties']['tbody']['sort_options'][$key]['#weight'] = $i;
$form['field_properties']['tbody']['autocomplete_source'][$key] = $form['autocomplete_source'][$key];
$form['field_properties']['tbody']['autocomplete_source'][$key]['#prefix'] = '';
$form['field_properties']['tbody']['autocomplete_source'][$key]['#suffix'] = ' ';
$form['field_properties']['tbody']['autocomplete_source'][$key]['#weight'] = $i;
$form['field_properties']['tbody']['autocomplete_separator'][$key] = $form['autocomplete_separator'][$key];
$form['field_properties']['tbody']['autocomplete_separator'][$key]['#prefix'] = '';
$form['field_properties']['tbody']['autocomplete_separator'][$key]['#suffix'] = ' ';
$form['field_properties']['tbody']['autocomplete_separator'][$key]['#weight'] = $i;
// Clean up the leftovers.
unset($form['components'][$key]);
$form['components']['#access'] = FALSE;
unset($form['minimum_components'][$key]);
$form['minimum_components']['#access'] = FALSE;
unset($form['max_length'][$key]);
$form['max_length']['#access'] = FALSE;
unset($form['labels'][$key]);
$form['labels']['#access'] = FALSE;
if (isset($form['sort_options'][$key])) {
unset($form['sort_options'][$key]);
$form['sort_options']['#access'] = FALSE;
}
unset($form['autocomplete_source'][$key]);
$form['autocomplete_source']['#access'] = FALSE;
unset($form['autocomplete_separator'][$key]);
$form['autocomplete_separator']['#access'] = FALSE;
}
// Move the additional options under the table.
$form['extra_fields'] = array(
'#weight' => 2,
);
$form['title_options']['#weight'] = 0;
$form['generational_options']['#weight'] = 1;
$form['extra_fields']['title_options'] = $form['title_options'];
$form['extra_fields']['generational_options'] = $form['generational_options'];
unset($form['title_options']);
unset($form['generational_options']);
return $form;
}