views_natural_sort.install 4.69 KB
Newer Older
1
<?php
2

3 4
/**
 * @file
5
 * The Views Natural Sort install file.
6 7 8
 */

/**
9
 * Implements hook_schema().
10 11 12 13 14
 */
function views_natural_sort_schema() {

  // Contains relations between two users.
  $schema['views_natural_sort'] = array(
15
    'description' => 'Compressed titles for natural sorting.',
16
    'fields' => array(
17
      'eid' => array(
18
        'description' => 'Entity id',
19 20 21 22
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
23
      'entity_type' => array(
24
        'description' => 'Entity Type',
25 26 27 28 29
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => 'node',
      ),
30
      'field' => array(
31
        'description' => 'The field name. This will be title or some cck text field, etc.',
32 33 34 35 36
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
37
      'delta' => array(
38
        'description' => 'The sequence number for this data item, used for multi-value fields',
39 40 41 42 43
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
44
      'content' => array(
45
        'description' => 'Filtered content used for sorting.',
46 47 48 49 50 51
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
52
    'primary key' => array('eid', 'entity_type', 'field', 'delta'),
53 54 55 56 57 58
  );

  return $schema;
}

/**
59
 * Implements hook_install().
60 61
 */
function views_natural_sort_install() {
62 63 64 65 66 67 68 69
  variable_set(
    'views_natural_sort_beginning_words_remove',
    array(
      t('The'),
      t('A'),
      t('An'),
      t('La'),
      t('Le'),
70
      t('Il'),
71 72 73 74 75 76 77 78 79 80 81
    )
  );
  variable_set(
    'views_natural_sort_words_remove',
    array(
      t('and'),
      t('or'),
      t('of'),
    )
  );
  variable_set('views_natural_sort_symbols_remove', "#\"'\\()[]");
82 83
  variable_set('views_natural_sort_days_of_the_week_enabled', FALSE);
  variable_set('views_natural_sort_rebuild_items_per_batch', 500);
84 85
}

86
/**
87
 * Implements hook_enable().
88 89 90 91 92 93
 */
function views_natural_sort_enable() {
  module_load_include('inc', 'views_natural_sort', 'views_natural_sort.admin');
  views_natural_sort_rebuild_index_submit();
}

94
/**
95
 * Implements hook_uninstall().
96 97
 */
function views_natural_sort_uninstall() {
98 99 100
  variable_del('views_natural_sort_beginning_words_remove');
  variable_del('views_natural_sort_words_remove');
  variable_del('views_natural_sort_symbols_remove');
101 102
  variable_del('views_natural_sort_days_of_the_week_enabled');
  variable_del('views_natural_sort_rebuild_items_per_batch');
103
}
104 105 106 107 108 109 110 111

/**
 * Rebuild the sorting index after changes made for numerical data.
 */
function views_natural_sort_update_7001() {
  module_load_include('inc', 'views_natural_sort', 'views_natural_sort.admin');
  views_natural_sort_rebuild_index_submit();
}
112 113 114 115

/**
 * Upgrade the 7.x-1.x table structure to the 7.x-2.x table structure.
 */
116 117 118 119 120 121 122 123 124 125
function views_natural_sort_update_7200() {
  db_drop_primary_key('views_natural_sort');
  db_add_field(
    'views_natural_sort',
    'entity_type',
    array(
      'description' => t('Entity Type'),
      'type' => 'varchar',
      'length' => 128,
      'not null' => TRUE,
126
      'default' => 'node',
127 128
    )
  );
129 130 131 132 133 134 135 136 137 138 139
  db_add_field(
    'views_natural_sort',
    'delta',
    array(
      'description' => t('The sequence number for this data item, used for multi-value fields'),
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    )
  );
140 141 142 143 144 145 146 147 148 149 150
  db_change_field(
    'views_natural_sort',
    'nid',
    'eid',
    array(
      'description' => t('Entity id'),
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
    ),
    array(
151
      'primary key' => array('eid', 'entity_type', 'field', 'delta'),
152 153
    )
  );
154
  module_load_include('inc', 'views_natural_sort', 'views_natural_sort.admin');
155
  views_natural_sort_rebuild_index_submit();
156
}
157 158 159 160 161

/**
 * Upgrading 1.x VNS views to 2.x VNS views.
 */
function views_natural_sort_update_7201() {
162
  foreach ($views as $view_name => $view) {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    foreach ($view->display as &$display) {
      if (!empty($display->display_options['sorts'])) {
        foreach ($display->display_options['sorts'] as &$sort) {
          if ($sort['table'] == 'views_natural_sort') {
            $sort['table'] = 'node';
            $sort['field'] = 'title';
            $sort['order'] = 'N' . $sort['order'];
            $view->save();
            drupal_set_message(t('Views Natural Sort Upgraded the view !name.', array('!name' => $view_name)), 'status');
          }
        }
      }
    }
  }
  cache_clear_all();
  return t('If you implement your views using Features, be sure to update the Featues that contain View Natural Sort Views immediatly after this upgrade and before the next revert.');
}