From 372f88fabf5c3c06f4ae02a3667870f9fe6a45d6 Mon Sep 17 00:00:00 2001
From: Heiko Henning <h.henning@educa.ch>
Date: Thu, 11 Nov 2010 17:36:52 +0100
Subject: [PATCH 1/2] add possibility for define collum width

---
 tablefield.module |  141 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 133 insertions(+), 8 deletions(-)

diff --git a/tablefield.module b/tablefield.module
index d66c7a7..9603829 100755
--- a/tablefield.module
+++ b/tablefield.module
@@ -28,6 +28,7 @@ function tablefield_theme() {
         'nid'   => NULL,
         'delta' => NULL,
         'type_of_header' => NULL,
+        'td_width' => NULL,
       )
     ),
     'tablefield_view_table' => array(
@@ -36,6 +37,7 @@ function tablefield_theme() {
         'attributes' => NULL,
         'caption'    => NULL,
         'type_of_header' => NULL,
+        'td_width'   => NULL,
       ),
     ),
 
@@ -100,6 +102,7 @@ function tablefield_field_settings($op, $field) {
  */
 function tablefield_field($op, &$node, $field, &$items, $teaser, $page) {
   $type_of_header = 'top';
+  $td_width       = array();
 
   switch ($op) {
     case 'presave':
@@ -132,6 +135,7 @@ function tablefield_field($op, &$node, $field, &$items, $teaser, $page) {
           $data = unserialize($table['value']);
           $tabledata = tablefield_rationalize_table($data);
           $type_of_header = $data['type_of_header'];
+          $td_width       = $data['td_width'];
         }
 
         // Multivalue fields will have one row in the db, so make sure that it isn't empty
@@ -150,7 +154,7 @@ function tablefield_field($op, &$node, $field, &$items, $teaser, $page) {
               }
             }
           }
-          $items[$delta]['value'] = theme('tablefield_view', $tabledata, $node->nid, $delta, $type_of_header);
+          $items[$delta]['value'] = theme('tablefield_view', $tabledata, $node->nid, $delta, $type_of_header, $td_width);
         }
       }
 
@@ -167,6 +171,8 @@ function tablefield_content_is_empty($item, $field) {
   unset($item['tablefield']['count_rows']);
   unset($item['tablefield']['rebuild']);
   unset($item['tablefield']['type_of_header']);// -> educa-customized-field
+  unset($item['tablefield']['td_width']);// -> educa-customized-field
+
   if (!empty($item['tablefield'])) {
     foreach ($item['tablefield'] as $cell) {
       if (!empty($cell)) {
@@ -208,9 +214,9 @@ function theme_tablefield($element) {
 /**
  * Theme function for table view
  */
-function theme_tablefield_view($rows, $nid, $delta, $type_of_header='top') {
+function theme_tablefield_view($rows, $nid, $delta, $type_of_header='top', $td_width=array()) {
   $html  = '<div id="tablefield-wrapper-' . $nid . '-' . $delta . '" class="tablefield-wrapper">';
-  $html .=  theme('tablefield_view_table', $rows, array('id' => 'tablefield-' . $nid . '-' . $delta, 'class' => 'tablefield'), NULL, $type_of_header);
+  $html .=  theme('tablefield_view_table', $rows, array('id' => 'tablefield-' . $nid . '-' . $delta, 'class' => 'tablefield'), NULL, $type_of_header, $td_width);
   $html .= '</div>';
 
   return $html;
@@ -219,7 +225,7 @@ function theme_tablefield_view($rows, $nid, $delta, $type_of_header='top') {
 /**
  * Theme function for table creation
  */
-function theme_tablefield_view_table($rows, $attributes = array(), $caption = NULL, $type_of_header='top') {
+function theme_tablefield_view_table($rows, $attributes = array(), $caption = NULL, $type_of_header='top', $td_width=array()) {
 
   // Add sticky headers, if applicable.
   if (empty($type_of_header) && $type_of_header!='no') {
@@ -235,6 +241,9 @@ function theme_tablefield_view_table($rows, $attributes = array(), $caption = NU
     $output .= '<caption>'. $caption ."</caption>\n";
   }
 
+  sort($td_width);
+  $last_width = 0;
+
   $rid = 0;
   foreach($rows AS $row) {
    $cid = 0;
@@ -260,6 +269,17 @@ function theme_tablefield_view_table($rows, $attributes = array(), $caption = NU
      $td_is_header = true;
     }
 
+    if($rid==0 && !empty($td_width[$cid])) {
+     $cur_width = $td_width[$cid]-$last_width;
+
+     if ($cur_width<2) {
+      $cur_width = 2;
+     } 
+
+     $cell['width'] = $cur_width.'%';
+     $last_width = $td_width[$cid];
+    }
+
     $output .= _theme_table_cell($cell, $td_is_header);
 
     $cid++;
@@ -350,10 +370,10 @@ function tablefield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  * Process the tablefield
  */
 function tablefield_process($element, $edit, $form_state, $form) {
-
   $type_of_header = 'top';
-
-  $delta = $element['#delta'];
+  $td_width = array();
+  
+  $delta = rand(0, 999999);  #$element['#delta'];
   $field = $form['#field_info'][$element['#field_name']];
 
   if (isset($element['#value']['tablefield'])) {
@@ -369,6 +389,7 @@ function tablefield_process($element, $edit, $form_state, $form) {
     // Default from database
     $default_value  = unserialize($element['#default_value']['value']);
     $type_of_header = $default_value['type_of_header'];
+    $td_width       = $default_value['td_width'];
 
     $default_value  = tablefield_rationalize_table($default_value);
   }
@@ -380,7 +401,6 @@ function tablefield_process($element, $edit, $form_state, $form) {
 
   $element['tablefield'] = array(
     '#title' => $field['widget']['label'],
-    '#description' => t('The first row will appear as the table header.'),
     '#attributes' => array('id' => 'node-tablefield-' . $delta, 'class' => 'node-tablefield'),
     '#type' => 'fieldset',
     '#tree' => TRUE,
@@ -426,6 +446,64 @@ function tablefield_process($element, $edit, $form_state, $form) {
   if (!empty($edit['tablefield']['type_of_header'])) {
     $type_of_header = $edit['tablefield']['type_of_header'];
   }
+  if (!empty($edit['tablefield']['td_width'])) {
+    $td_width = $edit['tablefield']['td_width'];
+  }
+  $td_width = tablefield_calc_td_width($td_width, $count_cols);
+
+  // add slider for tbl width
+  $element['tablefield']['hl_1'] = array(
+    '#type'  => 'markup',
+    '#value' => t('Drag slider to define column width.'),
+  );
+
+
+  // Enable JQuery plugins
+  $jq_ui_files = array('ui.slider');
+  jquery_ui_add($jq_ui_files);
+
+  for($i=0; $i<($count_cols-1) ; $i++) {
+   $element['tablefield']['td_width'][$i] = array(
+    '#type'  => 'hidden',
+    '#value' => $td_width[$i],
+    '#attributes' => array('td_width_id' => $delta . '_' . $i),
+   );
+  }
+
+  $element['tablefield']['td_width_slider'] = array(
+      '#type'  => 'markup',
+      '#value' => '<div id="tablefield_' . $delta . '_td_width_slider">'
+  );
+  for($i=0; $i<($count_cols-1) ; $i++) {
+   $element['tablefield']['td_width_slider']['#value'] .= '<div class="ui-slider-handle" id="handle_'.$i.'"></div>';
+  }
+  $element['tablefield']['td_width_slider']['#value'] .= '</div>';
+
+  $element['tablefield']['td_width_js'] = array(
+      '#type'  => 'markup',
+      '#title' => t('Slide to define row width'),
+      '#value' => '<script type="text/javascript">
+      Drupal.behaviors.tablefield = function() {
+        $("#tablefield_' . $delta . '_td_width_slider").slider({
+                step: 1,
+                min: 0,
+                max: 100,
+                orientation: "horizontal",
+                values: ['.implode(',', $td_width).'],
+                change: function(event, ui) {
+                 var thisHandle = jQuery(ui.handle);
+
+                 $("input[td_width_id='.$delta.'_"+thisHandle.attr("id").split("handle_")[1]+"]").val(ui.value);
+                }
+            });
+      };
+    </script>'
+  );
+
+  $element['tablefield']['hl_2'] = array(
+    '#type'  => 'markup',
+    '#value' => t('The first row will appear as the table header.'),
+  );
 
   // Render the form table
   $i = 0;
@@ -509,6 +587,11 @@ function tablefield_process($element, $edit, $form_state, $form) {
   return $element;
 }
 
+/**
+ * convert data from saving format to working format
+ * @param array $tablefield
+ * @return array
+ */
 function tablefield_rationalize_table($tablefield) {
   $count_cols = $tablefield['count_cols'];
   $count_rows = $tablefield['count_rows'];
@@ -528,3 +611,45 @@ function tablefield_rationalize_table($tablefield) {
 
   return $tabledata;
 }
+
+/**
+ * this functions check width of all table if it match to cound of celles and remove doubles
+ * @param array $td_width
+ * @param int $count_cols
+ * @return array
+ */
+function tablefield_calc_td_width($td_width, $count_cols) {
+
+ $td_width = array_unique($td_width);
+
+ if ($count_cols<=2) {
+  return $td_width;  
+ }
+
+ if (empty($td_width)) {
+  $td_width = array();
+
+  $per_row = floor(100/$count_cols);
+
+  for($i=0 ; $i<=($count_cols-2) ; $i++) {
+   $td_width[$i] = $per_row*($i+1);
+  }
+ }
+
+ sort($td_width);
+
+ if (count($td_width) < ($count_cols-1)) {
+  $to_fill_up = ($count_cols-1)-count($td_width);
+  $last_row   = end($td_width);
+
+  $per_row = floor((100-$last_row)/$to_fill_up);
+
+  $x = 1;
+  for($i=count($td_width) ; $i<=($count_cols-2) ; $i++) {
+   $td_width[$i] = $per_row*($x);
+   $x++;
+  }
+ }
+
+ return $td_width;
+}
\ No newline at end of file
-- 
1.7.0.4

