views_natural_sort_text_field.module 4.57 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

function views_natural_sort_text_field_views_api() {
  return array(
    'api' => 2.0,
  );
}

function views_natural_sort_text_field_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
10 11
  //TODO: Inform the user that this field is being used in views.
  $form['field']['views_natural_sort_enable_sort'] = array(
12 13 14
    '#type' => 'checkbox',
    '#title' => t('Enable natural sorting.'),
    '#description' => t('Selecting this option will process and index this field\'s value for natural sorting in views.'),
15
    '#default_value' => empty($form['#field']['views_natural_sort_enable_sort']) ? 0 : $form['#field']['views_natural_sort_enable_sort'],
16
  );
17
  $form['#submit'][] = 'views_natural_sort_text_field_form_field_ui_field_edit_form_alter_submit';
18
}
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
function views_natural_sort_text_field_form_field_ui_field_edit_form_alter_submit($form, $form_state) {
  // Rebuild index for this field.
  if($form_state['input']['field']['views_natural_sort_enable_sort'] != 1) {
  //TODO: Revert views back to regular sort ASC & DESC.
    db_delete('views_natural_sort')
      ->condition('field', $form['#field']['field_name'])
      ->execute();
  }
  else {
    module_load_include('inc', 'views_natural_sort', 'views_natural_sort.admin');
    views_natural_sort_rebuild_index_batch_set(views_natural_sort_text_field_views_natural_sort_get_entry_types(array($form['#field'])));
  }
}

function views_natural_sort_text_field_views_natural_sort_get_entry_types($fields = array()) {
  if (empty($fields)) {
    $fields = views_natural_sort_text_field_get_configured_fields();
  }
  $entry_types = array();
  foreach($fields as $field) {
    $entry_types += views_natural_sort_text_field_get_entry_types_from_field($field);
  }
  return $entry_types;
}

function views_natural_sort_text_field_get_entry_types_from_field($field) {
  $entry_types = array();
  foreach($field['bundles'] as $entity_type => $bundles) {
    $entry_types[] = array(
      'entity_type' => $entity_type,
      'field' => $field['field_name'],
    );
  }
  return $entry_types;
}

function views_natural_sort_text_field_views_natural_sort_get_rebuild_data($entry_type) {
  if (!in_array($entry_type,views_natural_sort_text_field_views_natural_sort_get_entry_types())) {
    return array();
  }
  $field = field_info_field($entry_type['field']);
61
  $data = array();
62 63 64 65 66
  $entities = entity_load($entry_type['entity_type']);
  foreach ($entities as $entity) {
    $entries = _views_natural_sort_text_field_to_vns($entry_type['entity_type'], $entity, $field);
    if (!empty($entries)) {
      $data += $entries;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    }
  }
  return $data;
}

function views_natural_sort_text_field_field_attach_insert($entity_type, $entity) {
  $configured_fields = views_natural_sort_text_field_get_configured_fields();
  foreach ($configured_fields as $field) {
    if (in_array($entity_type, array_keys($field['bundles']))){
      _views_natural_sort_text_field_store($entity_type, $entity, $field);
    }
  }
}

function views_natural_sort_text_field_field_attach_update($entity_type, $entity) {
  $configured_fields = views_natural_sort_text_field_get_configured_fields();
  foreach ($configured_fields as $field) {
    if (in_array($entity_type, array_keys($field['bundles']))){
      _views_natural_sort_text_field_store($entity_type, $entity, $field);
    }
  }
}

function views_natural_sort_text_field_get_configured_fields(){
  $all_fields = field_info_fields();
  $returned_fields = array();
  foreach($all_fields as $field_name => $field) {
    if ($field['module'] == 'text' &&
      $field['storage']['type'] == 'field_sql_storage' &&
96 97
      $field['columns']['value']['type'] == 'varchar' &&
      $field['views_natural_sort_enable_sort']) {
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      $returned_fields[$field['id']] = $field;
    }
  }
  return $returned_fields;
}
function _views_natural_sort_text_field_store($entity_type, $entity, $field) {
  views_natural_sort_store_multiple(_views_natural_sort_text_field_to_vns($entity_type, $entity, $field));
}
function _views_natural_sort_text_field_to_vns($entity_type, $entity, $field) {
  $entity_info = entity_get_info($entity_type);
  $entity_id_property = $entity_info['entity keys']['id'];
  $field_name = $field['field_name'];

  //TODO: add support for Field Language
  $entries = array();
generalredneck's avatar
generalredneck committed
113 114 115 116 117 118 119 120 121 122 123
  $field_items = field_get_items($entity_type, $entity, $field_name) 
  if (!empty($field_items)) {
    foreach($field_items as $delta => $row) {
      $entries[] = array(
        'eid' => $entity->$entity_id_property,
        'entity_type' => $entity_type,
        'field' => $field_name,
        'delta' => $delta,
        'content' => $row['value']
      );
    }
124 125 126
  }
  return $entries;
}