| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
define('NUMBER', 10);
|
| 5 |
|
| 6 |
/**
|
| 7 |
* Implementation of hook_field_info().
|
| 8 |
*/
|
| 9 |
function matrix_field_info() {
|
| 10 |
return array(
|
| 11 |
'matrix' => array('label' => 'Matrix Field'),
|
| 12 |
);
|
| 13 |
}
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_field_settings().
|
| 17 |
*/
|
| 18 |
function matrix_field_settings($op, $field) {
|
| 19 |
switch ($op) {
|
| 20 |
case 'form':
|
| 21 |
$form = array();
|
| 22 |
$form['size'] = array(
|
| 23 |
'#type' => 'textfield',
|
| 24 |
'#size' => 20,
|
| 25 |
'#title' => t('Size of textfields'),
|
| 26 |
'#default_value' => isset($field['size']) ? $field['size'] : 15,
|
| 27 |
);
|
| 28 |
|
| 29 |
$form['rows'] = array(
|
| 30 |
'#type' => 'fieldset',
|
| 31 |
'#collapsible' => TRUE,
|
| 32 |
'#title' => t('Rows'),
|
| 33 |
);
|
| 34 |
for ($i=1; $i<= NUMBER; $i++) {
|
| 35 |
$form['rows']["label_row_$i"] = array(
|
| 36 |
'#type' => 'textfield',
|
| 37 |
'#title' => t('Label for row %i',array('%i' => $i)),
|
| 38 |
'#default_value' => isset($field["label_row_$i"]) ? $field["label_row_$i"] : '',
|
| 39 |
);
|
| 40 |
}
|
| 41 |
|
| 42 |
$form['cols'] = array(
|
| 43 |
'#type' => 'fieldset',
|
| 44 |
'#collapsible' => TRUE,
|
| 45 |
'#title' => t('Cols'),
|
| 46 |
);
|
| 47 |
for ($i=1; $i<= NUMBER; $i++) {
|
| 48 |
$form['cols']["label_column_$i"] = array(
|
| 49 |
'#type' => 'textfield',
|
| 50 |
'#title' => t('Label for column %i',array('%i' => $i)),
|
| 51 |
'#default_value' => isset($field["label_column_$i"]) ? $field["label_column_$i"] : '',
|
| 52 |
);
|
| 53 |
}
|
| 54 |
return $form;
|
| 55 |
|
| 56 |
case 'save':
|
| 57 |
for ($i=1; $i<= NUMBER; $i++) {
|
| 58 |
$values[] = 'label_row_'.$i;
|
| 59 |
$values[] = 'label_column_'.$i;
|
| 60 |
}
|
| 61 |
$values[] = 'size';
|
| 62 |
return $values;
|
| 63 |
}
|
| 64 |
}
|
| 65 |
|
| 66 |
/**
|
| 67 |
* Implementation of hook_field().
|
| 68 |
*/
|
| 69 |
function matrix_field($op, &$node, $field, &$node_field, $teaser, $page) {
|
| 70 |
switch ($op) {
|
| 71 |
case 'load':
|
| 72 |
$result = db_query("SELECT value, row, col FROM {node_field_matrix_data} WHERE vid = %d AND field_name = '%s'", $node->vid, $field['field_name']);
|
| 73 |
$values = array();
|
| 74 |
while ($data = db_fetch_array($result)) {
|
| 75 |
$values[$data["row"]][$data["col"]] = $data['value'];
|
| 76 |
}
|
| 77 |
$additions = array($field['field_name'] => $values);
|
| 78 |
return $additions;
|
| 79 |
case 'view':
|
| 80 |
return theme('matrix_table_view',$node_field, $field);
|
| 81 |
case 'update':
|
| 82 |
db_query("DELETE FROM {node_field_matrix_data} WHERE vid = %d",$node->vid);
|
| 83 |
case 'insert':
|
| 84 |
for ($i=1;$i<= NUMBER;$i++) {
|
| 85 |
if (!empty($field["label_row_$i"])) {
|
| 86 |
for ($j=1;$j<= NUMBER;$j++) {
|
| 87 |
if (!empty($node_field[$i][$j])) {
|
| 88 |
db_query("INSERT INTO {node_field_matrix_data}
|
| 89 |
SET nid = %d, vid = %d, field_name = '%s', row = %d, col = %d, value = '%s'",
|
| 90 |
$node->nid, $node->vid, $field['field_name'], $i, $j, $node_field[$i][$j]);
|
| 91 |
}
|
| 92 |
}
|
| 93 |
}
|
| 94 |
}
|
| 95 |
break;
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Implementation of hook_widget_info().
|
| 101 |
*/
|
| 102 |
function matrix_widget_info() {
|
| 103 |
return array(
|
| 104 |
'matrix' => array(
|
| 105 |
'label' => 'Textfields',
|
| 106 |
'field types' => array('matrix'),
|
| 107 |
),
|
| 108 |
);
|
| 109 |
}
|
| 110 |
|
| 111 |
/**
|
| 112 |
* Implementation of hook_widget().
|
| 113 |
*/
|
| 114 |
function matrix_widget($op, &$node, $field, &$node_field) {
|
| 115 |
switch ($op) {
|
| 116 |
case 'form':
|
| 117 |
$form = array();
|
| 118 |
$form[$field['field_name']] = array(
|
| 119 |
'#tree' => TRUE,
|
| 120 |
'#weight' => $field['widget']['weight'],
|
| 121 |
'#prefix' => '<div class="matrix_field">',
|
| 122 |
'#suffix' => '</div>',
|
| 123 |
);
|
| 124 |
|
| 125 |
$prefix = '<div class="matrix_title">'. $field['widget']['label'] .'</div>';
|
| 126 |
$prefix .= '<table class="matrix" summary="table with textfields"><tr><th scope="col"></th>';
|
| 127 |
for ($i=1; $i<= NUMBER; $i++) {
|
| 128 |
if (!empty($field["label_column_$i"])) {
|
| 129 |
$prefix .= '<th abbr="matrix_col" scope="col">'. $field["label_column_$i"] . '</th>';
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
for ($i=1; $i<= NUMBER; $i++) {
|
| 134 |
if (!empty($field["label_row_$i"])) {
|
| 135 |
for ($j=1; $j<= NUMBER; $j++) {
|
| 136 |
if (!empty($field["label_column_$j"])) {
|
| 137 |
if ($j == 1) {
|
| 138 |
if ($i != 1) $prefix = NULL;
|
| 139 |
$prefix .= '</tr><tr><td>'. $field["label_row_$i"] .'</td><td>';
|
| 140 |
}
|
| 141 |
else {
|
| 142 |
$prefix = '<td class="matrix_row_col">';
|
| 143 |
}
|
| 144 |
$form[$field['field_name']][$i][$j] = array(
|
| 145 |
'#type' => 'textfield',
|
| 146 |
'#size' => $field['size'],
|
| 147 |
'#default_value' => isset($node_field[$i][$j]) ? $node_field[$i][$j] : '',
|
| 148 |
'#prefix' => $prefix,
|
| 149 |
'#suffix' => '</td>',
|
| 150 |
);
|
| 151 |
}
|
| 152 |
}
|
| 153 |
}
|
| 154 |
}
|
| 155 |
$form[$field['field_name']]['hidden'] = array(
|
| 156 |
'#type' => 'markup',
|
| 157 |
'#value' => '</table>',
|
| 158 |
);
|
| 159 |
return $form;
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|
| 163 |
function theme_matrix_table_view($node_field, $field) {
|
| 164 |
$header[] = '';
|
| 165 |
for ($i=1; $i<= NUMBER; $i++) {
|
| 166 |
if (!empty($field['label_column_'.$i])) {
|
| 167 |
$header[] = $field['label_column_'.$i];
|
| 168 |
}
|
| 169 |
$rows = NULL;
|
| 170 |
if (!empty($field['label_row_'.$i])) {
|
| 171 |
|
| 172 |
$rows[] = $field['label_row_'.$i];
|
| 173 |
for ($j=1; $j<= NUMBER; $j++) {
|
| 174 |
if (!empty($node_field[$i][$j])) {
|
| 175 |
$rows[] = $node_field[$i][$j];
|
| 176 |
}
|
| 177 |
elseif (!empty($field['label_column_'.$j])) {
|
| 178 |
$rows[] = '-';
|
| 179 |
}
|
| 180 |
}
|
| 181 |
}
|
| 182 |
if ($rows) $row[] = $rows;
|
| 183 |
}
|
| 184 |
return theme('table',$header,$row,array(),$field['widget']['label']);
|
| 185 |
}
|