'
',
'#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' => '
',
);
$sql = "SELECT COUNT(DISTINCT(connection)) FROM {tw_tables} WHERE connection <> 'default'";
$extconns = db_result(db_query($sql));
if ($extconns >= 1) {
$connheader = array('data' => t('Connection'), 'field' => 'twt.connection', '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' => t('Delete')),
array('data' => ''),
$connheader,
$nameheader,
array('data' => t('Row count')),
),
);
} else {
$form['header'] = array(
'#type' => 'value',
'#value' => array(
array('data' => t('Delete')),
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);
$form['analyze'][$row->twtid] = array('#value' =>
l(t('Analysis'), 'admin/content/tw/analyze/'. $row->twtid, array('html' => TRUE)));
if ($extconns >= 1) {
$form['connection'][$row->twtid] = array('#value' => $row->connection);
}
$form['tablename'][$row->twtid] = array('#value' =>
l($row->tablename, 'admin/content/tw/view/'. $row->tablename, array('html' => TRUE)));
if ($last_connection != $row->connection) {
db_set_active($row->connection);
$last_connection = $row->connection;
}
$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['connection'][$twtid])) {
$row[] = drupal_render($form['connection'][$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;
}
/**
* Enter description here...
*
* @param unknown_type $form
* @param unknown_type $form_state
*/
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 connection,tablename FROM {tw_tables} WHERE twtid=%d";
$row = db_fetch_object(db_query($sql, $twtid));
tw_remove_tables($row->connection . '.' . $row->tablename);
drupal_set_message(t('Removed table %tablename (connection %connection)',
array('%tablename' => $row->tablename, '%connection' => $row->connection)));
}
}
} 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($tableset);
}
}
/**
* Add a fieldset into the import form, for identifying where to obtain source data.
*
* @return unknown
*/
function tw_tw_form() {
$fieldsets['existing'] = array(
'#type' => 'fieldset',
'#title' => t('Add existing tables'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$fieldsets['existing']['uploadhelp'] = array(
'#prefix' => '',
'#value' => t('Add tables to the Table Wizard.'),
'#suffix' => '
',
);
// Build list of tables we already manage
$excludes = array();
$sql = "SELECT * FROM {tw_tables}";
$tblresult = db_query($sql);
while ($tblrow = db_fetch_object($tblresult)) {
$tblname = $tblrow->tablename;
// Exclude the table itself...
$excludes[$tblrow->connection][$tblname] = $tblname;
}
// Add tables from any available connections
global $db_url;
if (!is_array($db_url)) {
$connlist = array('default' => $db_url);
} else {
$connlist = $db_url;
}
// We assume the default is first
foreach ($connlist as $connection => $url) {
if ($connection == 'default') {
$defaulturl = $url;
}
db_set_active($connection);
$sql = 'SHOW TABLES';
$result = db_query($sql);
$options = array();
while ($row = db_fetch_array($result)) {
foreach ($row as $tablename) {
$unprefixed = schema_unprefix_table($tablename);
// Skip those we're already managing
if (isset($excludes[$connection][$unprefixed])) {
continue;
}
// If we're in the default connection, skip tables which weren't prefixed
global $db_prefix;
if ($connection == 'default' && $db_prefix && $tablename == $unprefixed) {
continue;
}
// If we're in a connection that's not the default, but is using the
// same database as the default, skip tables which were prefixed
if ($connection != 'default' && $url == $defaulturl && $tablename != $unprefixed) {
continue;
}
// See if this table already has a views definition - skip it if it does
$views_data = views_fetch_data($unprefixed);
if (!$views_data) {
$options[$unprefixed] = $unprefixed;
}
}
}
if (!empty($options)) {
$fieldsets['existing']['lists'][$connection] = array(
'#type' => 'select',
'#title' => t('Available tables in %connection connection', array('%connection' => $connection)),
'#options' => $options,
'#multiple' => TRUE,
'#size' => min(20, count($options)),
'#description' => t('Tables which are not currently managed
by the Table Wizard or otherwise already made available to Views'),
);
}
}
db_set_active('default');
$fieldsets['existing']['existingsubmit'] = array(
'#type' => 'submit',
'#value' => t('Add tables'),
);
return $fieldsets;
}
/**
* Enter description here...
*
* @param unknown_type $values
* @return unknown
*/
function tw_tw_form_submit_existing($values) {
$tablenames = array();
foreach ($values['lists'] as $connection => $tablelist) {
foreach ($tablelist as $tablename) {
$tablename = $connection . '.' . $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
*
* @param unknown_type $form_state
* @param unknown_type $table
* @param unknown_type $flag
* @return unknown
*/
function tw_analysis($form_state, $twtid, $flag=TW_COLS_NOIGNORE) {
$row = db_fetch_object(db_query("SELECT connection, tablename FROM {tw_tables}
WHERE twtid=%d",
$twtid));
$tablename = $row->tablename;
$connection = $row->connection;
if (isset($_GET['reanalyze'])) {
tw_perform_analysis($tablename, $connection);
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']['columns'] = array(
'#prefix' => '',
'#value' => t('These are the columns 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-column 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-column primary key. A single-column primary
key is necessary to use this table as the base table for a view.',
array('%tablename' => $tablename)));
}
return $form;
}
/**
* Enter description here...
*
* @param unknown_type $form
* @return unknown
*/
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 columns to display.'),
'colspan' => count($header)));
}
$output .= theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
/**
* Implementation of hook_submit()
*
* @param unknown_type $form
* @param unknown_type $form_state
*/
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];
$newfk = $form_state['values'][$colname.'_fk'];
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'));
}
/**
* Enter description here...
*
* @return unknown
*/
function tw_relationships() {
return(drupal_get_form('_tw_relationships_form'));
}
/**
* Enter description here...
*
* @param unknown_type $form_state
* @return unknown
*/
function _tw_relationships_form($form_state) {
$form['description'] = array(
'#prefix' => '',
'#value' => t('Define relationships between tables, enabling the creation
of views joining those tables.'),
'#suffix' => '
',
);
$form['header'] = array(
'#type' => 'value',
'#value' => array(
array('data' => t('Delete')),
array('data' => t('Left column')),
array('data' => t('Right column')),
),
);
$sql = "SELECT COUNT(*) FROM {tw_tables} WHERE connection <> '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.connection leftconn, twtright.connection 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.connection
FROM {tw_columns} twc
INNER JOIN {tw_tables} twt ON twc.twtid=twt.twtid
WHERE twc.availablefk=1
ORDER BY connection, tablename, colname";
$result = db_query($sql);
while ($row = db_fetch_array($result)) {
if ($use_connection) {
$options[$row['twcid']] = $row['connection'] . '.' . $row['tablename'] . '.' . $row['colname'];
} else {
$options[$row['twcid']] = $row['tablename'] . '.' . $row['colname'];
}
}
$form['addrel']['newleftcol'] = array(
'#type' => 'select',
'#title' => t('Column to appear on the left side of the join'),
'#options' => $options,
);
$form['addrel']['newrightcol'] = array(
'#type' => 'select',
'#title' => t('Column to appear on the right side of the join'),
'#options' => $options,
);
$form['addrel']['add'] = array(
'#type' => 'submit',
'#value' => t('Add'),
);
return $form;
}
/**
* Enter description here...
*
* @param unknown_type $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;
}
/**
* Enter description here...
*
* @param unknown_type $form
* @param unknown_type $form_state
*/
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;
}