View source
<?php
namespace Drupal\charts\Settings;
use Drupal\Component\Utility\Color;
use Symfony\Component\Yaml\Yaml;
class ChartsDefaultSettings {
protected $colors;
public $defaults;
public function __construct() {
$this->colors = new ChartsDefaultColors();
$this->defaults = $this
->getSettingsFromDefaultConfig();
}
private function getSettingsFromDefaultConfig() {
$path = __DIR__ . '/../../config/install/charts.settings.yml';
$default_config = Yaml::parse(file_get_contents($path))['charts_default_settings'];
$defaults = [
'type' => $default_config['type'],
'library' => $default_config['library'],
'grouping' => FALSE,
'label_field' => NULL,
'data_fields' => NULL,
'field_colors' => NULL,
'colors' => $default_config['display']['colors'],
'title' => $default_config['display']['title'],
'title_position' => $default_config['display']['title_position'],
'data_labels' => $default_config['display']['data_labels'],
'data_markers' => $default_config['display']['data_markers'],
'legend' => $default_config['display']['legend'],
'legend_position' => $default_config['display']['legend_position'],
'background' => $default_config['display']['background'],
'three_dimensional' => $default_config['display']['three_dimensional'],
'polar' => $default_config['display']['polar'],
'tooltips' => $default_config['display']['tooltips'],
'tooltips_use_html' => $default_config['display']['tooltips_use_html'],
'width' => $default_config['display']['dimensions']['width'],
'width_units' => $default_config['display']['dimensions']['width_units'],
'height' => $default_config['display']['dimensions']['height'],
'height_units' => $default_config['display']['dimensions']['height_units'],
'xaxis_title' => $default_config['xaxis']['title'],
'xaxis_labels_rotation' => $default_config['xaxis']['labels_rotation'],
'yaxis_title' => $default_config['yaxis']['title'],
'yaxis_min' => $default_config['yaxis']['min'],
'yaxis_max' => $default_config['yaxis']['max'],
'yaxis_prefix' => $default_config['yaxis']['prefix'],
'yaxis_suffix' => $default_config['yaxis']['suffix'],
'yaxis_decimal_count' => $default_config['yaxis']['decimal_count'],
'yaxis_labels_rotation' => $default_config['yaxis']['labels_rotation'],
'green_to' => $default_config['display']['gauge']['green_to'],
'green_from' => $default_config['display']['gauge']['green_from'],
'yellow_to' => $default_config['display']['gauge']['yellow_to'],
'yellow_from' => $default_config['display']['gauge']['yellow_from'],
'red_to' => $default_config['display']['gauge']['red_to'],
'red_from' => $default_config['display']['gauge']['red_from'],
'max' => $default_config['display']['gauge']['max'],
'min' => $default_config['display']['gauge']['min'],
];
return $defaults;
}
public function getDefaults($new_format = FALSE) {
$defaults = $this->defaults;
if ($new_format && empty($defaults['display'])) {
$keys_mapping = self::getLegacySettingsMappingKeys();
$keys_mapping['colors'] = 'display_colors';
$defaults = self::transformLegacySettingsToNew($defaults, $keys_mapping);
}
return $defaults;
}
public function setDefaults(array $defaults) {
$this->defaults = $defaults;
}
public static function transformLegacySettingsToNew(array &$old_settings, array $old_config_keys = []) {
$new_settings = [];
$new_settings['fields']['stacking'] = !empty($old_settings['grouping']);
$old_config_keys = $old_config_keys ?: self::getLegacySettingsMappingKeys();
foreach ($old_settings as $setting_id => $setting_value) {
$setting_key_map = isset($old_config_keys[$setting_id]) ? $old_config_keys[$setting_id] : '';
if ($setting_key_map) {
$value = self::transformBoolStringValueToBool($setting_value);
if (substr($setting_key_map, 0, 7) === 'display') {
$setting_key_map = substr($setting_key_map, 8, strlen($setting_key_map));
if (substr($setting_key_map, 0, 10) === 'dimensions') {
$setting_key_map = substr($setting_key_map, 11, strlen($setting_key_map));
$new_settings['display']['dimensions'][$setting_key_map] = $value;
}
elseif (substr($setting_key_map, 0, 5) === 'gauge') {
$setting_key_map = substr($setting_key_map, 6, strlen($setting_key_map));
$new_settings['display']['gauge'][$setting_key_map] = $value;
}
else {
$new_settings['display'][$setting_key_map] = $value;
}
}
elseif (substr($setting_key_map, 0, 5) === 'xaxis') {
$setting_key_map = substr($setting_key_map, 6, strlen($setting_key_map));
$new_settings['xaxis'][$setting_key_map] = $value;
}
elseif (substr($setting_key_map, 0, 5) === 'yaxis') {
$setting_key_map = substr($setting_key_map, 6, strlen($setting_key_map));
if (substr($setting_key_map, 0, 9) === 'secondary') {
$setting_key_map = substr($setting_key_map, 10, strlen($setting_key_map));
$new_settings['yaxis']['secondary'][$setting_key_map] = $value;
}
else {
$new_settings['yaxis'][$setting_key_map] = $value;
}
}
elseif (substr($setting_key_map, 0, 6) === 'fields') {
$setting_key_map = substr($setting_key_map, 7, strlen($setting_key_map));
if ($setting_key_map === 'data_providers' && is_array($value)) {
$data_providers = !empty($new_settings['fields']['data_providers']) ? $new_settings['fields']['data_providers'] : [];
if ($setting_id === 'data_fields' || $setting_id == 'field_colors') {
$new_settings['fields']['data_providers'] = self::getFieldsDataProviders($data_providers, $value);
}
}
else {
$new_settings['fields'][$setting_key_map] = $value;
}
}
elseif ($setting_key_map === 'grouping' && $new_settings['fields']['stacking']) {
$new_settings[$setting_key_map] = [];
}
else {
$new_settings[$setting_key_map] = $setting_key_map !== 'color' ? $value : $value[0];
}
unset($old_settings[$setting_id]);
}
}
return $new_settings;
}
public static function getLegacySettingsMappingKeys() {
return [
'library' => 'library',
'chart_library' => 'library',
'type' => 'type',
'chart_type' => 'type',
'grouping' => 'grouping',
'title' => 'display_title',
'title_position' => 'display_title_position',
'data_labels' => 'display_data_labels',
'data_markers' => 'display_data_markers',
'legend' => 'display_legend',
'legend_position' => 'display_legend_position',
'background' => 'display_background',
'three_dimensional' => 'display_three_dimensional',
'polar' => 'display_polar',
'series' => 'series',
'data' => 'data',
'color' => 'color',
'data_series' => 'data_series',
'series_label' => 'series_label',
'categories' => 'categories',
'field_colors' => 'fields_data_providers',
'tooltips' => 'display_tooltips',
'tooltips_use_html' => 'display_tooltips_use_html',
'width' => 'display_dimensions_width',
'height' => 'display_dimensions_height',
'width_units' => 'display_dimensions_width_units',
'height_units' => 'display_dimensions_height_units',
'xaxis_title' => 'xaxis_title',
'xaxis_labels_rotation' => 'xaxis_labels_rotation',
'yaxis_title' => 'yaxis_title',
'yaxis_min' => 'yaxis_min',
'yaxis_max' => 'yaxis_max',
'yaxis_prefix' => 'yaxis_prefix',
'yaxis_suffix' => 'yaxis_suffix',
'yaxis_decimal_count' => 'yaxis_decimal_count',
'yaxis_labels_rotation' => 'yaxis_labels_rotation',
'inherit_yaxis' => 'yaxis_inherit',
'secondary_yaxis_title' => 'yaxis_secondary_title',
'secondary_yaxis_min' => 'yaxis_secondary_min',
'secondary_yaxis_max' => 'yaxis_secondary_min',
'secondary_yaxis_prefix' => 'yaxis_secondary_prefix',
'secondary_yaxis_suffix' => 'yaxis_secondary_suffix',
'secondary_yaxis_decimal_count' => 'yaxis_secondary_decimal_count',
'secondary_yaxis_labels_rotation' => 'yaxis_secondary_labels_rotation',
'green_from' => 'display_gauge_green_from',
'green_to' => 'display_gauge_green_to',
'red_from' => 'display_gauge_red_from',
'red_to' => 'display_gauge_red_to',
'yellow_from' => 'display_gauge_yellow_from',
'yellow_to' => 'display_gauge_yellow_to',
'max' => 'display_gauge_max',
'min' => 'display_gauge_min',
'allow_advanced_rendering' => 'fields_allow_advanced_rendering',
'label_field' => 'fields_label',
'data_fields' => 'fields_data_providers',
];
}
public static function transformBoolStringValueToBool($value) {
if ($value === 'FALSE') {
$value = FALSE;
}
elseif ($value === 'TRUE') {
$value = TRUE;
}
return $value;
}
public static function getFieldsDataProviders(array $data_providers, array $legacy_value) {
$default_weight = 0;
foreach ($legacy_value as $field_id => $value) {
if (Color::validateHex($value)) {
$data_providers[$field_id]['color'] = $value;
}
else {
$data_providers[$field_id]['enabled'] = !empty($value);
}
$data_providers[$field_id]['weight'] = $default_weight;
$default_weight++;
}
return $data_providers;
}
}