| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Administrative page callbacks for the Field Indexer module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Menu callback for configuring the module.
|
| 11 |
*/
|
| 12 |
function field_indexer_admin($form_state) {
|
| 13 |
// Find all the fields available for indexing.
|
| 14 |
$fields = field_indexer_find_fields();
|
| 15 |
|
| 16 |
if (empty($fields)) {
|
| 17 |
$form['message'] = array(
|
| 18 |
'#type' => 'markup',
|
| 19 |
'#value' => t('No fields available for indexing.'),
|
| 20 |
);
|
| 21 |
return $form;
|
| 22 |
}
|
| 23 |
|
| 24 |
// Find which fields were already configured.
|
| 25 |
$known_fields = field_indexer_load_fields();
|
| 26 |
|
| 27 |
// Assign fiid and status to fields.
|
| 28 |
foreach ($fields as $key => $field) {
|
| 29 |
if (isset($known_fields[$key])) {
|
| 30 |
// Preserve the known fiid and status.
|
| 31 |
$fields[$key]['fiid'] = $known_fields[$key]['fiid'];
|
| 32 |
$fields[$key]['status'] = $known_fields[$key]['status'];
|
| 33 |
// Remove from array so at the end we have a list of fields that no longer
|
| 34 |
// exist on the system.
|
| 35 |
unset($known_fields[$key]);
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
// Build the list of options.
|
| 40 |
$options = array();
|
| 41 |
$defaults = array();
|
| 42 |
foreach ($fields as $key => $field) {
|
| 43 |
// TODO: Format field label and help in a nice table instead.
|
| 44 |
$help = check_plain($field['namespace']) . (empty($field['help']) ? '' : ' / '. check_plain($field['help']));
|
| 45 |
$options[$key] = check_plain($field['label']) .' ('. $help .')';
|
| 46 |
if ($field['status']) {
|
| 47 |
$defaults[] = $key;
|
| 48 |
}
|
| 49 |
}
|
| 50 |
|
| 51 |
// Build the settings form.
|
| 52 |
$form['selector'] = array(
|
| 53 |
'#type' => 'checkboxes',
|
| 54 |
'#title' => t('Fields to index'),
|
| 55 |
'#description' => t('Check the fields that need to be indexed.'),
|
| 56 |
'#options' => $options,
|
| 57 |
'#default_value' => $defaults,
|
| 58 |
);
|
| 59 |
$form['fields'] = array(
|
| 60 |
'#type' => 'value',
|
| 61 |
'#value' => $fields,
|
| 62 |
);
|
| 63 |
$form['lost_fields'] = array(
|
| 64 |
'#type' => 'value',
|
| 65 |
'#value' => $known_fields,
|
| 66 |
);
|
| 67 |
$form['submit'] = array(
|
| 68 |
'#type' => 'submit',
|
| 69 |
'#value' => t('Save configuration'),
|
| 70 |
);
|
| 71 |
|
| 72 |
return $form;
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Submit handler for the settings.
|
| 77 |
*/
|
| 78 |
function field_indexer_admin_submit($form, &$form_state) {
|
| 79 |
$enabled = FALSE;
|
| 80 |
|
| 81 |
// Update or insert field selection.
|
| 82 |
foreach ($form_state['values']['fields'] as $key => $field) {
|
| 83 |
$field['status'] = $form_state['values']['selector'][$key] ? 1 : 0;
|
| 84 |
if (isset($field['fiid'])) {
|
| 85 |
// Determine whether the field needs an update.
|
| 86 |
if (db_result(db_query("SELECT COUNT(*) FROM {field_indexer_map} WHERE fiid = %d AND (status <> %d OR label <> '%s')", $field['fiid'], $field['status'], $field['label']))) {
|
| 87 |
// Update the existing field.
|
| 88 |
drupal_write_record('field_indexer_map', $field, 'fiid');
|
| 89 |
|
| 90 |
if ($field['status']) {
|
| 91 |
$enabled = TRUE;
|
| 92 |
}
|
| 93 |
else {
|
| 94 |
// Field is disabled, delete its index entries.
|
| 95 |
field_indexer_wipe($field['fiid']);
|
| 96 |
}
|
| 97 |
}
|
| 98 |
}
|
| 99 |
else {
|
| 100 |
// Insert newly discovered field.
|
| 101 |
drupal_write_record('field_indexer_map', $field);
|
| 102 |
|
| 103 |
if ($field['status']) {
|
| 104 |
$enabled = TRUE;
|
| 105 |
}
|
| 106 |
}
|
| 107 |
}
|
| 108 |
|
| 109 |
// Delete obsolete fields.
|
| 110 |
foreach ($form_state['values']['lost_fields'] as $field) {
|
| 111 |
// Delete obsolete field data.
|
| 112 |
db_query('DELETE FROM {field_indexer_map} WHERE fiid = %d', $field['fiid']);
|
| 113 |
|
| 114 |
// Delete index entries for the field.
|
| 115 |
field_indexer_wipe($field['fiid']);
|
| 116 |
}
|
| 117 |
|
| 118 |
if ($enabled) {
|
| 119 |
field_indexer_reindex(); // Re-index all nodes.
|
| 120 |
drupal_set_message(t('All nodes have been marked for re-indexing to ensure the indexing of the newly enabled fields. Indexing will start on the next <a href="@cron">cron</a> run.', array('@cron' => url('admin/reports/status/run-cron'))));
|
| 121 |
}
|
| 122 |
|
| 123 |
drupal_set_message(t('The configuration options have been saved.'));
|
| 124 |
}
|
| 125 |
|
| 126 |
|