From 58f8435d33b2b2a2f2e0bfafcffc676bfaa90cf7 Mon Sep 17 00:00:00 2001
From: Bruno Massa <brmassa@67164.no-reply.drupal.org>
Date: Thu, 14 May 2009 21:05:31 +0000
Subject: [PATCH] Internals: * Settings attributes are again with the # sign.
 It makes the chart  display a lot easier.

---
 charts.admin.inc | 52 ++++++++++++++++++++++++--------------------
 charts.inc       | 56 ++++++++++++++++++++++++------------------------
 charts.install   | 15 -------------
 3 files changed, 57 insertions(+), 66 deletions(-)

diff --git a/charts.admin.inc b/charts.admin.inc
index b9070ec..0574a99 100644
--- a/charts.admin.inc
+++ b/charts.admin.inc
@@ -24,25 +24,25 @@ function _charts_color_form_complete(&$form, $default) {
   $form['color']['color_palettes'] = array(
     '#type'           => 'select',
     '#title'          => t('Color palettes'),
-    '#options'        => $default['color_palettes'],
-    '#default_value'  => $default['color_palette'],
+    '#options'        => $default['#color_palettes'],
+    '#default_value'  => $default['#color_palette'],
   );
   $form['color']['background'] = array(
     '#type'           => $field_type,
     '#title'          => t('Background'),
-    '#default_value'  => $default['color']['background'],
+    '#default_value'  => $default['#color']['background'],
 //     '#colorpicker'    => 'colorpicker',
   );
   $form['color']['text'] = array(
     '#type'           => $field_type,
     '#title'          => t('Text'),
-    '#default_value'  => $default['color']['text'],
+    '#default_value'  => $default['#color']['text'],
   );
   for ($series = 0; $series < 8; $series++) {
     $form['color'][$series] = array(
       '#type'           => $field_type,
       '#title'          => t('Series %serie', array('%serie' => $series + 1)),
-      '#default_value'  => $default['color'][$series],
+      '#default_value'  => $default['#color'][$series],
     );
   }
 
@@ -60,8 +60,8 @@ function _charts_color_form_simple(&$form, $settings) {
   $form['color_palettes'] = array(
     '#type'           => 'select',
     '#title'          => t('Color palettes'),
-    '#options'        => $settings['color_palettes'],
-    '#default_value'  => $settings['color_palette'],
+    '#options'        => $settings['#color_palettes'],
+    '#default_value'  => $settings['#color_palette'],
   );
 }
 
@@ -73,8 +73,8 @@ function _charts_color_form_simple(&$form, $settings) {
  * @return
  *   HTML. The generic Chart example
  */
-function _charts_example($data = array()) {
-  if (empty($data)) {
+function _charts_example() {
+//   if (empty($data)) {
     // This will hold the example chart
     // Since the chart is an example, we should provide
     // and example data.
@@ -92,7 +92,7 @@ function _charts_example($data = array()) {
       10, 20, 55, 72, 45, 50
     );
     $data['#title'] = 'Testing Chart';
-  }
+//   }
   return charts_chart($data);
 }
 
@@ -130,39 +130,40 @@ function _charts_module_invoke_all() {
 function _charts_settings_form(&$form, $default = array(), $options = array()) {
   module_load_include('inc', 'charts');
   $default = $default + _charts_settings();
+
   $options = array_fill_keys($options, TRUE);
 
   if (!empty($options['example'])) {
     $form['chart_example'] = array(
-      '#value'          => _charts_example($default),
+      '#value'          => _charts_example(),
     );
   }
 
-  asort($default['plugins']);
+  asort($default['#plugins']);
   $form['plugin'] = array(
-    '#default_value'  => $default['plugin'],
-    '#options'        => $default['plugins'],
+    '#default_value'  => $default['#plugin'],
+    '#options'        => $default['#plugins'],
     '#type'           => 'select',
     '#title'          => t('Chart plugin'),
   );
 
-  asort($default['types']);
+  asort($default['#types']);
   $form['type'] = array(
-    '#default_value'  => $default['type'],
-    '#options'        => $default['types'],
+    '#default_value'  => $default['#type'],
+    '#options'        => $default['#types'],
     '#type'           => 'radios',
     '#title'          => t('Chart type'),
   );
 
   $form['width'] = array(
-    '#default_value'  => $default['width'],
+    '#default_value'  => $default['#width'],
     '#description'    => t('The chart width, in pixels'),
     '#size'           => 8,
     '#type'           => 'textfield',
     '#title'          => t('Width'),
   );
   $form['height'] = array(
-    '#default_value'  => $default['height'],
+    '#default_value'  => $default['#height'],
     '#description'    => t('The chart height, in pixels'),
     '#size'           => 8,
     '#type'           => 'textfield',
@@ -216,10 +217,15 @@ function _charts_settings_page_submit(&$form, &$form_state) {
   // Save the Color palette. Firts, filter the data, cluster the results
   // into a single string. Finally, unset all field values, since they
   // are useless
-  $settings['color_palette'] = implode(',', $settings['palette']);
-  unset($settings['palette']);
-  unset($settings['scheme']);
+  $settings['color_palette'] = implode(',', $settings['color']);
+
+  // Include a # sign in all attributes, because it will make the
+  // merge between the chart data and the defaults easier on every
+  // chart display.
+  foreach ($settings as $index => $value) {
+    $default['#'. $index] = $value;
+  }
 
   // Save the data into database
-  variable_set('charts_settings', $settings);
+  variable_set('charts_settings', $default);
 }
diff --git a/charts.inc b/charts.inc
index 4734b51..8f2e245 100644
--- a/charts.inc
+++ b/charts.inc
@@ -18,34 +18,34 @@
  */
 function _charts_chart(&$data) {
   // Get the previously saved data from database
-  $settings = _charts_settings();
+  $data = $data + _charts_settings();
 
-  if (empty($data['#plugin']) and empty($settings['plugin'])) {
+  if (empty($data['#plugin']) and empty($default['#plugin'])) {
     return '';
   }
 
   // Split the color palette data into inidividual values
-  $color_palette = explode(',', ereg_replace('#', '', $settings['color_palette']));
+  $color_palette = explode(',', ereg_replace('#', '', $default['#color_palette']));
 
   // Check if the Chart will use the color palette for individual values
   // instead for series, like Pie Charts
   $options = array('pie2D' => TRUE, 'pie3D' => TRUE);
   if ((!empty($data['#type']) and !empty($options[$data['#type']]) ) or
-      (!empty($settings['type']) and !empty($options[$settings['type']]) ) ) {
+      (!empty($default['#type']) and !empty($options[$default['#type']]) ) ) {
     $individual_color_palette = TRUE;
   }
 
   // Merge all series option to the main data array,
   foreach (element_children($data) as $series) {
-    if (!empty($settings[$series])) {
-      $data[$series] = array_merge($settings[$series], $data[$series]);
+    if (!empty($default[$series])) {
+      $data[$series] = array_merge($default[$series], $data[$series]);
     }
-    unset($settings[$series]);
+    unset($default[$series]);
 
     // Apply the Color Palette: normally, apply one color to each series.
     // But for some types of charts, is one color to each value into the series
     if (empty($individual_color_palette) and empty($data[$series]['#color'])) {
-      $data[$series]['color'] = $color_palette[$series];
+      $data[$series]['#color'] = $color_palette[$series];
     }
     elseif (!empty($individual_color_palette)) {
       foreach (element_children($data[$series]) as $values) {
@@ -86,46 +86,46 @@ function _charts_chart(&$data) {
  */
 function _charts_settings() {
   // Get the previously saved data from Data Base
-  static $settings, $filtered = array();
+  static $default = array();
 
-  if (empty($settings)) {
-    $settings = variable_get('charts_settings', array());
+  if (empty($default)) {
+    $default = variable_get('charts_settings', array());
 
     // Plugin
     $charts_info = module_invoke_all('charts_info', 'list');
     foreach ($charts_info as $chart_code => $chart) {
-      $settings['plugins'][$chart_code] = $chart['name'];
+      $default['#plugins'][$chart_code] = $chart['name'];
     }
-    asort($settings['plugins']);
-    $settings['plugin'] = empty($settings['plugin']) ? current(array_keys($settings['plugins'])) : $settings['plugin'];
+    asort($default['#plugins']);
+    $default['#plugin'] = empty($default['#plugin']) ? current(array_keys($default['#plugins'])) : $default['#plugin'];
 
     // Type
-    $settings['types'] = module_invoke_all('chart_types');
-    $ctypes_allowed = array_fill_keys($charts_info[$settings['plugin']]['types'], TRUE);
-    foreach (array_keys($settings['types']) as $ctype_code) {
+    $default['#types'] = module_invoke_all('chart_types');
+    $ctypes_allowed = array_fill_keys($charts_info[$default['#plugin']]['types'], TRUE);
+    foreach (array_keys($default['#types']) as $ctype_code) {
       if (empty($ctypes_allowed[$ctype_code])) {
-        unset($settings['types'][$ctype_code]);
+        unset($default['#types'][$ctype_code]);
       }
     }
-    $settings['type']  = empty($settings['type']) ? current(array_keys($settings['types'])) : $settings['type'];
+    $default['#type']  = empty($default['#type']) ? current(array_keys($default['#types'])) : $default['#type'];
 
     // Width and Height
-    $settings['width']  = empty($settings['width'])  ? 400 : $settings['width'];
-    $settings['height'] = empty($settings['height']) ? 200 : $settings['height'];
+    $default['#width']  = empty($default['#width'])  ? 400 : $default['#width'];
+    $default['#height'] = empty($default['#height']) ? 200 : $default['#height'];
 
     // Color Palette
-    $settings['color_palettes'] = _charts_settings_color_palette();
-    if (empty($settings['color_palette'])) {
-      $settings['color_palette'] = current(array_keys($settings['color_palettes']));
+    $default['#color_palettes'] = _charts_settings_color_palette();
+    if (empty($default['#color_palette'])) {
+      $default['#color_palette'] = current(array_keys($default['#color_palettes']));
     }
 
     // Color
-    $settings['color'] = explode( ',', $settings['color_palette']);
-    $settings['color']['background'] = array_shift($settings['color']);
-    $settings['color']['text']       = array_shift($settings['color']);
+    $default['#color'] = explode( ',', $default['#color_palette']);
+    $default['#color']['background'] = array_shift($default['#color']);
+    $default['#color']['text']       = array_shift($default['#color']);
   }
 
-  return $settings;
+  return $default;
 }
 
 /**
diff --git a/charts.install b/charts.install
index f67093d..0a76ad0 100644
--- a/charts.install
+++ b/charts.install
@@ -12,18 +12,3 @@
 function charts_uninstall() {
   variable_del('charts_settings');
 }
-
-/**
- * Implementation of hook_update().
- *
- * Convert all settings into 'index' => value instead '#index' => value
- */
-function charts_update_6001() {
-  $settings = variable_get('charts_settings', array());
-  foreach ($settings as $index => $value) {
-    $settings_new[substr($index, 1)] = $value;
-  }
-  variable_set('charts_settings', $settings);
-
-  return array();
-}
-- 
GitLab