From 5218f867109497357df4d3ef67f6d92c15c599c0 Mon Sep 17 00:00:00 2001 From: mpv Date: Thu, 17 Jul 2014 16:56:25 -0300 Subject: [PATCH] Issue #2098557 by gildir, mpv, jay.dansand: Changed configuration storage to variables. --- views_distinct.install | 78 +++++++++++++++++--------------------------------- views_distinct.module | 69 ++++++++++++++++++++++---------------------- 2 files changed, 61 insertions(+), 86 deletions(-) diff --git a/views_distinct.install b/views_distinct.install index 3e801c8..87fd1bc 100644 --- a/views_distinct.install +++ b/views_distinct.install @@ -5,57 +5,33 @@ */ /** - * Implements hook_install(). + * Implements hook_uninstall(). */ -function views_distinct_schema() { - $schema['views_distinct'] = array( - 'description' => 'Store settings per View->Display->Field', - 'fields' => array( - 'view_name' => array( - 'type' => 'varchar', - 'length' => '64', - 'default' => '', - 'not null' => TRUE, - 'description' => 'View name, as found in views_view.name.', - ), - 'display_id' => array( - 'type' => 'varchar', - 'length' => '64', - 'default' => '', - 'not null' => TRUE, - 'description' => 'Display id, as found in views_display.id.', - ), - 'field_id' => array( - 'type' => 'varchar', - 'length' => '128', - 'default' => '', - 'not null' => TRUE, - 'description' => 'Machine name for the field on this display, as assigned by Views.', - ), - 'settings' => array( - 'type' => 'blob', - 'description' => 'A serialized array of settings for this View->Display->Field.', - 'serialize' => TRUE, - 'serialized default' => 'a:0:{}', - ), - ), - 'indexes' => array( - 'view' => array('view_name'), - 'field_setting' => array('view_name', 'display_id', 'field_id'), - ), - 'unique keys' => array(), - 'foreign keys' => array( - 'view_name' => array( - 'table' => 'views_view', - 'columns' => array('view_name' => 'name'), - ), - 'display_id' => array( - 'table' => 'views_display', - 'columns' => array('display_id' => 'id'), - ), - ), - 'primary key' => array('view_name', 'display_id', 'field_id'), - ); +function views_distinct_uninstall() { + // We can't delete the variables one by one because we don't know them all. + db_delete('variable') + ->condition('name', 'views_distinct:%', 'LIKE') + ->execute(); + cache_clear_all('variables', 'cache_bootstrap'); +} + +/** + * Migrate existing views_distinct settings to the variable storage. + */ +function views_distinct_update_7001() { + $result = db_select('views_distinct', 'v') + ->fields('v') + ->execute(); + + foreach ($result as $v) { + $views[$v->view_name][$v->display_id][$v->field_id] = unserialize($v->settings); + } + + foreach ($views as $view_name => $settings) { + variable_set('views_distinct:' . $view_name, $settings); + } - return $schema; + if (db_table_exists('views_distinct')) { + db_drop_table('views_distinct'); + } } diff --git a/views_distinct.module b/views_distinct.module index 685b195..321fdd9 100644 --- a/views_distinct.module +++ b/views_distinct.module @@ -353,26 +353,10 @@ function _views_distinct_field_settings_get($view_name, $display_name, $field_na // Check if this view_name has been loaded and if not, load it from the DB: if (!isset($static_cache[$view_name])) { $static_cache[$view_name] = array(); - $results = db_select('views_distinct', 'vsd') - ->fields('vsd') - ->condition('view_name', $view_name, '=') - ->execute(); - if (empty($results)) { - $results = array(); - } - foreach ($results as $result) { - $settings = unserialize($result->settings) + _views_distinct_field_settings_defaults(); - // HTML is allowed, but filter for terrible exploits in the aggregation - // joiner: + $settings = variable_get('views_distinct:'.$view_name); + if($settings != NULL) { $settings['aggregate_separator'] = filter_xss($settings['aggregate_separator']); - $static_cache[$view_name] = array_merge_recursive( - $static_cache[$view_name], - array( - $result->display_id => array( - $result->field_id => $settings, - ), - ) - ); + $static_cache[$view_name] = $settings; } } // Check for a result in static cache: @@ -406,32 +390,47 @@ function _views_distinct_field_settings_set($view_name, $display_name, $field_na if (empty($display_name)) { $display_name = 'default'; } - // Whether updating/inserting or removing, we start with removing the current - // setting, if it exists: - db_delete('views_distinct') - ->condition('view_name', $view_name, '=') - ->condition('display_id', $display_name, '=') - ->condition('field_id', $field_name, '=') - ->execute(); // Now, optionally insert a new (or updated) value: if (!empty($settings)) { // Updating the stored record. $settings = (array) $settings + _views_distinct_field_settings_defaults(); - // If !empty($settings), we're inserting/updating this setting; since we've - // already removed the setting either way, this is always a simple INSERT: - $record = array( - 'view_name' => $view_name, - 'display_id' => $display_name, - 'field_id' => $field_name, - 'settings' => $settings, - ); - drupal_write_record('views_distinct', $record); + + $view_settings = variable_get('views_distinct:'.$view_name); + if($view_settings == NULL) { + $view_settings = array($display_name => array($field_name => $settings)); + } + elseif(isset($view_settings[$display_name])) { + $view_settings[$display_name][$field_name] = $settings; + } + else { + $view_settings[$display_name] = array($field_name => $settings); + } + variable_set('views_distinct:'.$view_name, $view_settings); } else { // If we're removing settings, revert the static cache to defaults: $settings = _views_distinct_field_settings_defaults(); + + //remove field, and display if no more settings + $view_settings = variable_get('views_distinct:'.$view_name); + if(isset($view_settings[$display_name][$field_name])) { + unset($view_settings[$display_name][$field_name]); + if(count($view_settings[$display_name]) == 0) { + unset($view_settings[$display_name]); + } + } + + if($view_settings != NULL && count($view_settings) == 0) { + //delete variable if no more useful information + variable_del('views_distinct:'.$view_name); + } + else { + //update just to remove the settings + variable_set('views_distinct:'.$view_name, $view_settings); + } } + if (!isset($static_cache[$view_name])) { $static_cache[$view_name] = array(); } -- 1.9.1