',
'#value' => t('Tables managed by the Table Wizard module are listed here, each with the name of the
table used to store the data, and statistics on the amount of data. Click the Analysis
link to view and edit information on the fields available in the table. Click on the table
name to view the data in the table.'),
'#suffix' => '
',
);
// To add the "select all" checkbox to the following tables, we call
// theme_table_select_header_cell.
// This will include the tableselect.js file. We don't need the output.
theme('table_select_header_cell');
$sql = "SELECT COUNT(DISTINCT(dbconnection)) FROM {tw_tables} WHERE dbconnection <> 'default'";
$extconns = db_result(db_query($sql));
if ($extconns >= 1) {
$connheader = array('data' => t('Connection'), 'field' => 'twt.dbconnection', 'sort' => 'asc');
$nameheader = array('data' => t('Table name'), 'field' => 'twt.tablename');
}
else {
$nameheader = array('data' => t('Table name'), 'field' => 'twt.tablename', 'sort' => 'asc');
}
if ($extconns >= 1) {
$form['header'] = array(
'#type' => 'value',
'#value' => array(
array('data' => '', 'class' => 'select-all'),
array('data' => ''),
$connheader,
$nameheader,
array('data' => t('Row count')),
),
);
}
else {
$form['header'] = array(
'#type' => 'value',
'#value' => array(
array('data' => t('Delete'), 'class' => 'select-all'),
array('data' => ''),
$nameheader,
array('data' => t('Row count')),
),
);
}
$sql = "SELECT * FROM {tw_tables} twt";
if ($extconns >= 1) {
$tablesort = tablesort_sql(array($connheader, $nameheader));
}
else {
$tablesort = tablesort_sql(array($nameheader));
}
$result = db_query($sql . $tablesort);
$checks = array();
$last_connection = 'default';
while ($row = db_fetch_object($result)) {
$checks[$row->twtid] = '';
$form['twtid'][$row->twtid] = array('#value' => $row->twtid);
// Make sure we're using the default connection - l() may query the alias table
if ($last_connection != 'default') {
db_set_active('default');
$last_connection = 'default';
}
$form['analyze'][$row->twtid] = array('#value' =>
l(t('Analysis'), 'admin/content/tw/analyze/' . $row->twtid, array('html' => TRUE)));
if ($extconns >= 1) {
$form['dbconnection'][$row->twtid] = array('#value' => $row->dbconnection);
}
$form['tablename'][$row->twtid] = array('#value' =>
l($row->tablename, 'admin/content/tw/view/' . $row->tablename, array('html' => TRUE)));
if ($last_connection != $row->dbconnection) {
db_set_active($row->dbconnection);
$last_connection = $row->dbconnection;
}
$prefixed = db_prefix_tables('{' . $row->tablename . '}');
if ($prefixed != $row->tablename && !db_table_exists($row->tablename)) {
drupal_set_message(t('Table %tablename is subject to Drupal prefixing, but
the physical table does not have a prefix. To work properly with Table Wizard,
add an element to $db_prefix setting the prefix for this table to an empty string.',
array('%tablename' => $row->tablename)));
$rowcount = t('N/A');
}
else {
$sql = 'SELECT COUNT(*) FROM {' . $row->tablename . '}';
$rowcount = db_result(db_query($sql));
}
$form['rowcount'][$row->twtid] = array('#value' => $rowcount);
}
if ($last_connection != 'default') {
db_set_active('default');
}
$form['checks'] = array('#type' => 'checkboxes', '#options' => $checks);
$form['#theme'] = 'tw_sources';
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Remove selected tables'),
);
// Keep each type's fields apart
$form['#tree'] = TRUE;
// Note that fieldset names ('existing', 'delimited', ...) are the source type names
$sourcefieldsets = module_invoke_all('tw_form');
$form = array_merge($form, $sourcefieldsets);
// Support file uploads
$form['#attributes'] = array('enctype' => 'multipart/form-data');
return $form;
}
function theme_tw_sources($form) {
$output = drupal_render($form['description']);
if (isset($form['twtid']) && is_array($form['twtid'])) {
foreach (element_children($form['twtid']) as $twtid) {
$row = array();
// Don't show the table id
$null = drupal_render($form['twtid']);
$row[] = drupal_render($form['checks'][$twtid]);
$row[] = drupal_render($form['analyze'][$twtid]);
if (isset($form['dbconnection'][$twtid])) {
$row[] = drupal_render($form['dbconnection'][$twtid]);
}
$row[] = drupal_render($form['tablename'][$twtid]);
$row[] = drupal_render($form['rowcount'][$twtid]);
$rows[] = $row;
}
}
$header = $form['header']['#value'];
if (!isset($rows)) {
$rows[] = array(array('data' => t('No tables have been added..'),
'colspan' => count($header)));
}
$output .= theme('table', $header, $rows);
$output .= drupal_render($form['delete']);
$output .= drupal_render($form);
return $output;
}
/**
* Sources form submit handler.
*/
function tw_sources_form_submit($form, &$form_state) {
$type = $form_state['clicked_button']['#parents'][0];
if ($type == 'delete') {
foreach ($form_state['values']['checks'] as $twtid => $value) {
if ($value) {
$sql = "SELECT dbconnection,tablename FROM {tw_tables} WHERE twtid=%d";
$row = db_fetch_object(db_query($sql, $twtid));
tw_remove_tables($row->dbconnection . '.' . $row->tablename);
drupal_set_message(t('Removed table %tablename (connection %connection)',
array('%tablename' => $row->tablename, '%connection' => $row->dbconnection)));
}
}
}
else {
// Submit hooks return arrays of tablenames they're bringing in
$values = $form_state['values'][$type];
$tableset = module_invoke_all("tw_form_submit_$type", $values);
tw_add_tables_batch($tableset);
}
}
/**
* Add a fieldset into the import form, for identifying where to obtain source data.
*/
function tw_tw_form() {
$fieldsets['existing'] = array(
'#type' => 'fieldset',
'#title' => t('Add existing tables'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$fieldsets['existing']['uploadhelp'] = array(
'#prefix' => '.
*/
function tw_tw_form_submit_existing($values) {
$tablenames = array();
foreach ($values['lists'] as $dbconnection => $tablelist) {
foreach ($tablelist as $tablename) {
$tablename = $dbconnection . '.' . $tablename;
$tablenames[] = $tablename;
drupal_set_message(t('Added table %tablename to the Table Wizard.',
array('%tablename' => $tablename)));
}
}
return $tablenames;
}
/**
* Menu callback function.
* TODO: Flag any available FKs that are not indexed.
*/
function tw_analysis($form_state, $twtid, $flag=TW_COLS_NOIGNORE) {
$row = db_fetch_object(db_query("SELECT dbconnection, tablename FROM {tw_tables}
WHERE twtid=%d",
$twtid));
$tablename = $row->tablename;
$dbconnection = $row->dbconnection;
if (isset($_GET['reanalyze'])) {
tw_perform_analysis($tablename, $dbconnection);
drupal_set_message(t('%tablename analyzed', array('%tablename' => $tablename)));
}
$form['twtid'] = array('#type' => 'value', '#value' => $twtid);
$form['tablename'] = array('#type' => 'value', '#value' => $tablename);
switch ($flag) {
case TW_COLS_ALL:
$flaglabel = t('All fields');
break;
case TW_COLS_IGNORE:
$flaglabel = t('Ignored fields only');
break;
case TW_COLS_NOIGNORE:
$flaglabel = t('Non-ignored fields only');
break;
}
$form['help'] = array(
'#type' => 'fieldset',
'#title' => t('Help'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['help']['help1'] = array(
'#prefix' => '',
'#value' => t('Fields in the table are listed below, with some information
on the range and type of values each field contains. Comments may be entered to
document each field individually, and any fields deemed unnecessary can be marked
Ignore to omit them from views based on this table.'),
'#suffix' => '
',
);
$form['help']['help2'] = array(
'#prefix' => '',
'#value' => t('The first time this page is visited for a file that has been
uploaded (including the first time after a new copy has been imported), the entire
contents are read and analyzed - this can take quite a while. The results of the analysis
are saved so later visits to the analysis page are quicker. Sometimes, if changes have
been made to the table behind the scenes, it\'s useful to force the full analysis
by clicking Reanalyze.'),
'#suffix' => '
',
);
$form['help']['help3'] = array(
'#prefix' => '',
'#value' => t('View table contents goes to the Table View page for this table.
You may find it helpful to open this link in a new window, to browse the data in context
while figuring out its meaning for the comments here.'),
'#suffix' => '
',
);
$form['help']['fields'] = array(
'#prefix' => '',
'#value' => t('These are the fields in the table below:'),
'#suffix' => '
',
);
$form['help']['fieldname'] = array(
'#prefix' => '',
'#value' => t('Field name - The name of the field in the database table.'),
'#suffix' => '
',
);
$form['help']['ignore'] = array(
'#prefix' => '',
'#value' => t('Ignore - Checking this box for a field will omit that field from the
table view. A primary key field cannot be ignored - it is needed for the view to work.'),
'#suffix' => '
',
);
$form['help']['empty'] = array(
'#prefix' => '',
'#value' => t('Empty - Indicates whether this field is empty for all rows of the table.'),
'#suffix' => '
',
);
$form['help']['pk'] = array(
'#prefix' => '',
'#value' => t('PK - Indicates that this field is the primary key of the table.'),
'#suffix' => '
',
);
$form['help']['fk'] = array(
'#prefix' => '',
'#value' => t('Available FK - Check this box to make the field available for use
in table relationships.'),
'#suffix' => '
',
);
$form['help']['type'] = array(
'#prefix' => '',
'#value' => t('Type - The Drupal schema type of the field.'),
'#suffix' => '
',
);
$form['help']['strlength'] = array(
'#prefix' => '',
'#value' => t('Text length - The range of text lengths in this field, if it\'s
a text field.'),
'#suffix' => '
',
);
$form['help']['range'] = array(
'#prefix' => '',
'#value' => t('Range of values - The range of values in the field, from lowest to highest'),
'#suffix' => '
',
);
$form['help']['comments'] = array(
'#prefix' => '',
'#value' => t('Comments - Description of the field.'),
'#suffix' => '
',
);
$form['title'] = array('#value' => t('Analysis of %table', array('%table' => $tablename)));
$form['header'] = array(
'#type' => 'value',
'#value' => array(
array('data' => t('Field name')),
array('data' => t('Ignore')),
array('data' => t('Empty')),
array('data' => t('PK')),
array('data' => t('Available FK')),
array('data' => t('Type')),
array('data' => t('Text length')),
array('data' => t('Range')),
array('data' => t('Comments')),
),
);
$colcount = 0;
$emptycount = 0;
$result = db_query("SELECT *
FROM {tw_columns}
WHERE twtid=%d
ORDER BY weight",
$twtid);
$availablefks = array();
$availablefkenable = array();
$ignoredcols = array();
$ignoreenable = array();
$cols = array();
$pks = array();
while ($row = db_fetch_object($result)) {
$colname = $row->colname;
$colcount++;
if ($row->isempty) {
$emptycount++;
}
// Display this column if:
// Asked to see all columns, or
// Asked to see ignored columns, and the column is ignored, or
// Ask to see non-ignored columns (the default), and the column is not being ignored
if (($flag == TW_COLS_ALL) ||
(($flag == TW_COLS_IGNORE) && $row->ignorecol) ||
(($flag == TW_COLS_NOIGNORE) && !$row->ignorecol)) {
$cols[] = $colname;
$form['colname'][$colname] = array('#value' => $colname);
$ignoredcols[$colname] = '';
if ($row->ignorecol) {
$ignoreenable[] = $colname;
}
$form['empty'][$colname] = array('#value' => $row->isempty ? t('Yes') : '');
$form['primarykey'][$colname] = array('#value' => $row->primarykey ? t('Yes') : '');
if ($row->primarykey) {
$pks[] = $colname;
}
$availablefks[$colname] = '';
if ($row->availablefk) {
$availablefkenable[] = $colname;
}
$form['currtype'][$colname] = array('#value' => $row->coltype);
if (tw_column_type($row->coltype) == 'text') {
$form['lengths'][$colname] = array('#value' =>
$row->maxlength ? $row->minlength . '-' . $row->maxlength : ''
);
$form['values'][$colname] = array(
'#attributes' => array('style' => 'white-space: normal'),
'#prefix' => '',
'#value' =>
$row->maxlength ? substr(check_plain($row->minstring), 0, 40) . '
' .
substr(check_plain($row->maxstring), 0, 40) : '',
'#suffix' => '',
);
}
else {
$form['lengths'][$colname] = array('#value' => '');
$form['values'][$colname] = array('#value' =>
$row->maxvalue ? $row->minvalue . '-' . $row->maxvalue : ''
);
}
$form['comments'][$colname] = array(
'#type' => 'textarea',
'#default_value' => $row->comments,
'#cols' => 45,
'#rows' => 3,
'#resizable' => FALSE,
);
}
}
$form['availablefk'] = array(
'#type' => 'checkboxes',
'#options' => $availablefks,
'#default_value' => $availablefkenable,
);
$form['ignorecol'] = array(
'#type' => 'checkboxes',
'#options' => $ignoredcols,
'#default_value' => $ignoreenable,
);
$form['cols'] = array(
'#type' => 'value',
'#value' => $cols,
);
$form['colcounts'] = array('#value' =>
t('%emptycount of %colcount fields have no data for any row.',
array('%emptycount' => $emptycount, '%colcount' => $colcount)));
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit changes'),
);
if (count($pks) == 0) {
drupal_set_message(t('%tablename has no primary key defined. A single-field primary
key is necessary to use this table as the base table for a view.',
array('%tablename' => $tablename)));
}
elseif (count($pks) > 1) {
drupal_set_message(t('%tablename has a multiple-field primary key. A single-field primary
key is necessary to use this table as the base table for a view.',
array('%tablename' => $tablename)));
}
else {
if (strlen($tablename) > 32) {
drupal_set_message(t('%tablename is more than 32 characters long. Although the default
view for this table works, in versions of Views up through at least 2.5 you cannot
create your own views based on this table. See
http://drupal.org/node/437070 for
more information.',
array('%tablename' => $tablename, '@issuelink' => 'http://drupal.org/node/437070')));
}
}
return $form;
}
/**
* Theme analysis form.
*/
function theme_tw_analysis($form) {
$title = drupal_render($form['title']);
$twtid = $form['twtid']['#value'];
$tablename = $form['tablename']['#value'];
drupal_set_title($title);
$output = '';
$output .= 'Operations on this table: ';
$output .= l(t('Reanalyze'),
"admin/content/tw/analyze/$twtid/",
array('html' => TRUE, 'query' => 'reanalyze=1')) .' | ';
$output .= l(t('View table contents'),
"admin/content/tw/view/$tablename/",
array('html' => TRUE));
$output .= '
';
$output .= '';
$output .= 'Filter: ';
$output .= l(t('Show all fields'),
"admin/content/tw/analyze/$twtid/". TW_COLS_ALL,
array('html' => TRUE)) .' | ';
$output .= l(t('Show only ignored fields'),
"admin/content/tw/analyze/$twtid/". TW_COLS_IGNORE,
array('html' => TRUE)) .' | ';
$output .= l(t('Show only fields which are not being ignored'),
"admin/content/tw/analyze/$twtid/". TW_COLS_NOIGNORE,
array('html' => TRUE));
$output .= '
';
$output .= '' . drupal_render($form['colcounts']) . '
';
$output .= drupal_render($form['help']);
if (isset($form['colname']) && is_array($form['colname'])) {
foreach (element_children($form['colname']) as $colname) {
$row = array();
$row[] = drupal_render($form['colname'][$colname]);
// Remove ignore toggle for the primary key
if ($form['primarykey'][$colname]['#value']) {
unset($form['ignorecol'][$colname]);
}
$row[] = drupal_render($form['ignorecol'][$colname]);
$row[] = drupal_render($form['empty'][$colname]);
$row[] = drupal_render($form['primarykey'][$colname]);
$row[] = drupal_render($form['availablefk'][$colname]);
$row[] = drupal_render($form['currtype'][$colname]);
$row[] = drupal_render($form['lengths'][$colname]);
$row[] = drupal_render($form['values'][$colname]);
$row[] = drupal_render($form['comments'][$colname]);
$rows[] = $row;
}
}
$header = $form['header']['#value'];
if (!isset($rows)) {
$rows[] = array(array('data' => t('There are no fields to display.'),
'colspan' => count($header)));
}
$output .= theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
/**
* Implementation of hook_submit().
*/
function tw_analysis_submit($form, &$form_state) {
$twtid = $form_state['values']['twtid'];
foreach ($form_state['values']['cols'] as $key => $colname) {
$newignore = $form_state['values']['ignorecol'][$colname] ? 1 : 0;
$newavailablefk = $form_state['values']['availablefk'][$colname] ? 1 : 0;
$newcomment = $form_state['values'][$colname];
db_query("UPDATE {tw_columns}
SET ignorecol=%d, availablefk=%d, comments='%s'
WHERE twtid=%d AND colname='%s'",
$newignore, $newavailablefk, $newcomment, $twtid, $colname);
}
drupal_flush_all_caches();
drupal_set_message(t('Your changes have been saved'));
}
/**
* Page callback for relationships form.
*/
function tw_relationships() {
return drupal_get_form('tw_relationships_form');
}
/**
* Relationships form definition.
*/
function tw_relationships_form($form_state) {
$form['description'] = array(
'#prefix' => '',
'#value' => t('Define relationships between tables, enabling the creation
of views joining those tables. Each relationship is based on matching a field
in an initial (base) table to a field in a related table.'),
'#suffix' => '
',
);
// To add the "select all" checkbox to the following tables, we call
// theme_table_select_header_cell.
// This will include the tableselect.js file. We don't need the output.
theme('table_select_header_cell');
$form['header'] = array(
'#type' => 'value',
'#value' => array(
array('data' => '', 'class' => 'select-all'),
array('data' => t('Base table field')),
array('data' => t('Related table field')),
),
);
$sql = "SELECT COUNT(*) FROM {tw_tables} WHERE dbconnection <> 'default'";
$use_connection = db_result(db_query($sql));
$sql = "SELECT twr.twrid,CONCAT(twtleft.tablename, '.', twcleft.colname) leftcol,
CONCAT(twtright.tablename, '.', twcright.colname) rightcol,
twtleft.dbconnection leftconn, twtright.dbconnection rightconn
FROM {tw_relationships} twr
INNER JOIN {tw_columns} twcleft ON twr.leftcol=twcleft.twcid
INNER JOIN {tw_tables} twtleft ON twcleft.twtid=twtleft.twtid
INNER JOIN {tw_columns} twcright ON twr.rightcol=twcright.twcid
INNER JOIN {tw_tables} twtright ON twcright.twtid=twtright.twtid
ORDER BY leftconn, leftcol, rightconn, rightcol";
$result = db_query($sql);
$checks = array();
while ($row = db_fetch_object($result)) {
$checks[$row->twrid] = '';
$form['twrid'][$row->twrid] = array('#value' => $row->twrid);
if ($use_connection) {
$row->leftcol = $row->leftconn . '.' . $row->leftcol;
}
$form['leftcol'][$row->twrid] = array('#value' => $row->leftcol);
if ($use_connection) {
$row->rightcol = $row->rightconn . '.' . $row->rightcol;
}
$form['rightcol'][$row->twrid] = array('#value' => $row->rightcol);
}
$form['checks'] = array('#type' => 'checkboxes', '#options' => $checks);
$form['#theme'] = 'tw_relationships';
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete selected relationships'),
);
$form['addrel'] = array(
'#type' => 'fieldset',
'#title' => t('Add a relationship'),
'#collapsible' => TRUE,
);
$sql = "SELECT twc.twcid, twc.colname, twt.tablename, twt.dbconnection
FROM {tw_columns} twc
INNER JOIN {tw_tables} twt ON twc.twtid=twt.twtid
WHERE twc.availablefk=1
ORDER BY dbconnection, tablename, colname";
$result = db_query($sql);
while ($row = db_fetch_array($result)) {
if ($use_connection) {
$options[$row['twcid']] = $row['dbconnection'] . '.' . $row['tablename'] .'.'. $row['colname'];
}
else {
$options[$row['twcid']] = $row['tablename'] . '.' . $row['colname'];
}
}
$form['addrel']['newleftcol'] = array(
'#type' => 'select',
'#title' => t('Field from the base table of the relationship'),
'#options' => $options,
);
$form['addrel']['newrightcol'] = array(
'#type' => 'select',
'#title' => t('Corresponding field from a table to be joined to the base table'),
'#options' => $options,
);
$form['addrel']['add'] = array(
'#type' => 'submit',
'#value' => t('Add'),
);
return $form;
}
/**
* Theme relationships form.
*/
function theme_tw_relationships($form) {
$output = drupal_render($form['description']);
if (isset($form['twrid']) && is_array($form['twrid'])) {
foreach (element_children($form['twrid']) as $twrid) {
$row = array();
// Don't show the row ID
$null = drupal_render($form['twrid']);
$row[] = drupal_render($form['checks'][$twrid]);
$row[] = drupal_render($form['leftcol'][$twrid]);
$row[] = drupal_render($form['rightcol'][$twrid]);
$rows[] = $row;
}
}
$header = $form['header']['#value'];
if (!isset($rows)) {
$rows[] = array(array('data' => t('No relationships have been defined.'),
'colspan' => count($header)));
}
$output .= theme('table', $header, $rows);
$output .= drupal_render($form['delete']);
$output .= drupal_render($form['addrel']);
$output .= drupal_render($form);
return $output;
}
/**
* Submit handler for tw_relationships_form().
*/
function tw_relationships_form_submit($form, &$form_state) {
if ($form_state['clicked_button']['#parents'][0] == 'delete') {
$count = 0;
foreach ($form_state['values']['checks'] as $twrid => $value) {
if ($value) {
tw_delete_relationship($twrid);
$count++;
}
}
if ($count) {
drupal_set_message(format_plural($count, '1 relationship deleted', '@count relationships deleted'));
}
}
else {
$leftcol = $form_state['values']['newleftcol'];
$rightcol = $form_state['values']['newrightcol'];
tw_add_relationship($leftcol, $rightcol);
}
return;
}