Skip to content
Snippets Groups Projects
Commit b81bf6d1 authored by Eric Rasmussen's avatar Eric Rasmussen
Browse files

[gh-390] Uncouple unl sites table sort from db query

Now that Site Name and Last Access are no longer in the unl_sites db table the sort for them was broken.
Use a custom sort function for all the columns.
parent 4bf9e0b0
No related branches found
No related tags found
No related merge requests found
...@@ -120,7 +120,7 @@ function unl_add_extra_site_info($sites) { ...@@ -120,7 +120,7 @@ function unl_add_extra_site_info($sites) {
if ($row->installed != 2) { if ($row->installed != 2) {
continue; continue;
} }
// Switch to alt db connection // Switch to alt db connection
db_set_active('UNLNoPrefix'); db_set_active('UNLNoPrefix');
...@@ -147,44 +147,43 @@ function unl_add_extra_site_info($sites) { ...@@ -147,44 +147,43 @@ function unl_add_extra_site_info($sites) {
} }
/** /**
* Site List appears on admin/sites/unl, admin/sites/unl/sites * Existing sites list table appears on admin/sites/unl, admin/sites/unl/sites
*/ */
function unl_site_list($form, &$form_state) { function unl_site_list($form, &$form_state) {
$header = array( $header = array(
'uri' => array( 'uri' => array(
'data' => t('Default Path'), 'data' => t('Default Path'),
'field' => 's.uri', 'field' => 'uri',
), ),
'name' => array( 'name' => array(
'data' => t('Site Name'), 'data' => t('Site Name'),
'field' => 's.name', 'field' => 'name',
), ),
'access' => array( 'access' => array(
'data' => t('Last Access'), 'data' => t('Last Access'),
'field' => 's.access', 'field' => 'access',
), ),
'installed' => array( 'installed' => array(
'data' => t('Status'), 'data' => t('Status'),
'field' => 's.installed', 'field' => 'installed',
), ),
'operations' => t('Operations'), 'operations' => t('Operations'),
); );
$sites = db_select('unl_sites', 's') $sites = db_select('unl_sites', 's')
->fields('s', array('site_id', 'db_prefix', 'installed', 'site_path', 'uri')) ->fields('s', array('site_id', 'db_prefix', 'installed', 'site_path', 'uri'))
->extend('TableSort')
->orderByHeader($header)
->execute() ->execute()
->fetchAll(); ->fetchAll();
// In addition to the above db query, add site name and last access timestamp
unl_add_extra_site_info($sites); unl_add_extra_site_info($sites);
$options = array(); $rows = array();
foreach ($sites as $site) { foreach ($sites as $site) {
$options[$site->site_id] = array( $rows[$site->site_id] = array(
'uri' => theme('unl_site_details', array('site_path' => $site->site_path, 'uri' => $site->uri, 'db_prefix' => $site->db_prefix)), 'uri' => theme('unl_site_details', array('site_path' => $site->site_path, 'uri' => $site->uri, 'db_prefix' => $site->db_prefix)),
'site_name' => $site->name, 'name' => $site->name,
'last_access' => (isset($site->access) && $site->access > 0) ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $site->access))) : t('never'), 'access' => $site->access,
'installed' => _unl_get_install_status_text($site->installed), 'installed' => _unl_get_install_status_text($site->installed),
'operations' => array( 'operations' => array(
'data' => array( 'data' => array(
...@@ -202,6 +201,16 @@ function unl_site_list($form, &$form_state) { ...@@ -202,6 +201,16 @@ function unl_site_list($form, &$form_state) {
); );
} }
// Sort the table data accordingly with a custom sort function
$order = tablesort_get_order($header);
$sort = tablesort_get_sort($header);
$rows = unl_sites_sort($rows, $order, $sort);
// Now that the access timestamp has been used to sort, convert it to something readable
foreach ($rows as $key=>$row) {
$rows[$key]['access'] = (isset($row['access']) && $row['access'] > 0) ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $row['access']))) : t('never');
}
$form['unl_sites'] = array( $form['unl_sites'] = array(
'#type' => 'fieldset', '#type' => 'fieldset',
'#title' => t('Existing Sites: ') . count($sites), '#title' => t('Existing Sites: ') . count($sites),
...@@ -209,13 +218,83 @@ function unl_site_list($form, &$form_state) { ...@@ -209,13 +218,83 @@ function unl_site_list($form, &$form_state) {
$form['unl_sites']['site_list'] = array( $form['unl_sites']['site_list'] = array(
'#theme' => 'table', '#theme' => 'table',
'#header' => $header, '#header' => $header,
'#rows' => $options, '#rows' => $rows,
'#empty' => t('No sites available.'), '#empty' => t('No sites have been created.'),
); );
return $form; return $form;
} }
/**
* Custom sort the Existing Sites table.
*/
function unl_sites_sort($rows, $order, $sort) {
switch ($order['sql']) {
case 'uri':
if ($sort == 'asc') {
usort($rows, "unl_uri_cmp_asc");
}
else {
usort($rows, "unl_uri_cmp_desc");
}
break;
case 'name':
if ($sort == 'asc') {
usort($rows, "unl_name_cmp_asc");
}
else {
usort($rows, "unl_name_cmp_desc");
}
break;
case 'access':
if ($sort == 'asc') {
usort($rows, "unl_access_cmp_asc");
}
else {
usort($rows, "unl_access_cmp_desc");
}
break;
case 'installed':
if ($sort == 'asc') {
usort($rows, "unl_installed_cmp_asc");
}
else {
usort($rows, "unl_installed_cmp_desc");
}
break;
}
return $rows;
}
/**
* Comparison functions used in unl_sites_sort().
*/
function unl_uri_cmp_asc($a, $b) {
return strcmp($a['uri'], $b['uri']);
}
function unl_uri_cmp_desc($a, $b) {
return strcmp($b['uri'], $a['uri']);
}
function unl_name_cmp_asc($a, $b) {
return strcmp($a['name'], $b['name']);
}
function unl_name_cmp_desc($a, $b) {
return strcmp($b['name'], $a['name']);
}
function unl_access_cmp_asc($a, $b) {
return strcmp($b['access'], $a['access']);
}
function unl_access_cmp_desc($a, $b) {
return strcmp($a['access'], $b['access']);
}
function unl_installed_cmp_asc($a, $b) {
return strcmp($a['installed'], $b['installed']);
}
function unl_installed_cmp_desc($a, $b) {
return strcmp($b['installed'], $a['installed']);
}
/** /**
* Implements theme_CUSTOM() which works with unl_theme(). * Implements theme_CUSTOM() which works with unl_theme().
* This themes the Default Path column on the UNL Sites tables at admin/sites/unl * This themes the Default Path column on the UNL Sites tables at admin/sites/unl
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment