--- field_table.inc.old 2005-11-03 17:38:51.000000000 +0000 +++ field_table.inc 2006-03-03 19:37:53.750000000 +0000 @@ -5,26 +5,31 @@ } function flexinode_field_table_form($field, $node) { - + // prepare default values for the table $fieldname = 'flexinode_'. $field->field_id; - if ($node->$fieldname) { - $table=$node->$fieldname; - $rows=count($table)+1; - $cols=count($table[0])+1; - } - else { - $table=array(); - $rows=8; - $cols=5; - } + $table = ($node->$fieldname) ? $node->$fieldname : array(); + $rows = ($node->$fieldname) ? count($table)+1 : $field->rows; + $options_cols = $field->options[1]; + // prepare table + $title = t($field->label); $output = ''; - $output .= form_hidden($fieldname.'_rows',$rows); + $output .= form_hidden($fieldname.'_rows', $rows); $output .= '
'; $output .= ''; - $title = t($field->label); + // header rows + $colcount = 0; + $output .= ''; + foreach ($options_cols as $column) { + if ($column != '') { + $output .= ""; + $colcount++; + } + } + $output .= ''; + // value rows for ($i = 0; $i < $rows; $i++) { $output .= ''; - for ($j = 0; $j < $cols; $j++) { + for ($j = 0; $j < $colcount; $j++) { $output .= '
$column
'; $value = (isset($table[$i]) && isset($table[$i][$j])) ? $table[$i][$j] : ''; $output .= form_textarea('', $fieldname.'_'.$i.'][', $value, 10, 3); @@ -37,43 +42,41 @@ } function flexinode_field_table_validate($field, $node) { - $fieldname = 'flexinode_'. $field->field_id; if (!isset($_POST['edit'])) { return array('value' => $node->$fieldname); } $rows = $_POST['edit'][$fieldname.'_rows']; $value = array(); - $deleted_cols = array(); - foreach ($_POST['edit'][$fieldname.'_0'] as $key=>$val) { - if (empty($val) && $key) { - $deleted_cols[] = $key; - } - } + + // foreach row for ($i = 0; $i <= $rows; $i++) { - if ($i && empty($_POST['edit'][$fieldname.'_'.$i][0])) { - continue; - } $newrow = $_POST['edit'][$fieldname.'_'.$i]; - foreach ($deleted_cols as $key) - unset($newrow[$key]); - $value[] = array_values($newrow); // reindex from 0 + // validate row + $hasvalues = false; + foreach($newrow as $col) { + if (!empty($col)) + $hasvalues = true; + } + // if the row has some values, keep it + if ($hasvalues) + $value[] = array_values($newrow); // reindex from 0 } + return (array('value' => $value)); } -function flexinode_field_table_insert($field, $node) { +function flexinode_field_table_db_select($field) { $fieldname = 'flexinode_'. $field->field_id; - db_query("INSERT INTO {flexinode_data} (nid, field_id, serialized_data) VALUES (%d, %d, '%s')", $node->nid, $field->field_id, serialize($node->$fieldname)); + return $fieldname .'.serialized_data AS '. $fieldname; } -function flexinode_field_table_db_select($field) { +function flexinode_field_table_insert($field, $node) { $fieldname = 'flexinode_'. $field->field_id; - return $fieldname .'.serialized_data AS '. $fieldname; + db_query("INSERT INTO {flexinode_data} (nid, field_id, serialized_data) VALUES (%d, %d, '%s')", $node->nid, $field->field_id, serialize($node->$fieldname)); } function flexinode_field_table_load($field, $node) { - $fieldname = 'flexinode_'. $field->field_id; if (isset($table_phase)) $table_phase++; else $table_phase=0; return unserialize($node->$fieldname); @@ -82,21 +85,71 @@ function flexinode_field_table_format($field, $node, $brief = 0) { $fieldname = 'flexinode_'. $field->field_id; - $table = $node->$fieldname; - $output = ""; - $i=0; - foreach ($table as $row) { - $output .= ''; - $i=1-$i; + $rows = unserialize($node->$fieldname); + + // header (use only non empty columns) + $header = array(); + foreach ($field->options[1] as $column) { + if ($column != '') { + $header[] = "$column"; + } } - $output .= "
'.implode('',$row).'
"; + + // format table + $output = ''; + if (count($rows) > 0) { + $output = theme_table($header, $rows, array('id' => "$fieldname")); + } + return $output; } +function flexinode_field_table_config($field, $edit) { + // default number of rows that will be available in the table + $form .= form_textfield(t('Default number of rows'), 'rows', $edit['rows'], 60, 10); + + // options[1] has the array of columns available to the table + $addmore = 2; + if ($edit['options']) { + foreach ($edit['options'][1] as $column) { + $column_fields .= form_textfield('', 'options][1][', $column, 60, 128); + $addmore += ($column == '' ? -1 : 1); + } + } + for($i = 0; $i < $addmore; $i++) { + $column_fields .= form_textfield('', 'options][1][', '', 60, 128); + } + $column_fields .= form_submit(t('More')); + $form .= form_group(t('Columns'), $column_fields, t('Definition of the table columns. For more columns, click "More".')); + + // return this custom options + return $form; +} + +/** + * @addtogroup themeable + * @{ + */ + +/** + * Format a single-line text field for display in a node. + * + * @param field_id + * Which field is being displayed (useful when overriding this function + * if you want to style one particular field differently). + * @param label + * The label for the field as displayed on the node form. + * @param value + * The value that the user entered for the field. + * @param formatted_value + * The value that the user entered for the field as pre-formatted by the module. + */ function theme_flexinode_table($field_id, $label, $value, $formatted_value) { $output = theme('form_element', $label, $formatted_value); $output = '
'. $output .'
'; return $output; } +/** @} End of addtogroup themeable */ + ?>