--[[ - @author Bruno Massa - @file - Functions to aid in the creation of sortable tables. - - All tables created with a call to theme('table') have the option of having - column headers that the user can click on to sort the table by that column. ]] --[[ - Format a table cell. - - Adds a class attribute to all cells in the currently active column. - - @param cell - Table, The cell to format. - @param header - Tablle, Column headers in the format described in themes.table(). - @param ts - Table, The current table sort context as returned from ajato_tablesort_init(). - @param i - Number, The index of the cell's table column. - @return - Table, A properly formatted cell, ready for themes.table_cell() ]] function ajato_tablesort_cell(cell, header, ts, i) if header and header[i] and header[i]['data'] and header[i]['data'] == ts['name'] and hv(header[i]['field']) then if type(cell) == 'table' then if cell['class'] then cell['class'] = cell['class'] ..' active' else cell['class'] = 'active' end else cell = {['data'] = cell, ['class'] = 'active'} end end return cell end --[[ - Determine the current sort criterion. - - @param headers - Table, Column headers in the format described in theme_table(). - @return - Table, The criterion, containing the keys: - - "name": The localized title of the table column. - - "sql": The name of the database field to sort on. ]] function ajato_tablesort_get_order(headers) local order = sys['get']['order'] or '' local default for _, header in pairs(headers) do if header['data'] and order == header['data'] then return {['name'] = header['data'], ['sql'] = (header['field'] or '')} end if header['sort'] and header['sort'] == 'asc' or header['sort'] == 'desc' then default = {['name'] = header['data'], ['sql'] = header['field'] or ''} end end if default then return default else -- The first column specified is initial 'order by' field unless otherwise specified if type(headers[0]) == 'table' then return {['name'] = headers[0]['data'], ['sql'] = headers[0]['field']} else return {['name'] = headers[0]} end end end --[[ - Initialize the table sort context. ]] function ajato_tablesort_init(header) local ts = ajato_tablesort_get_order(header) ts['sort'] = ajato_tablesort_get_sort(header) ts['query_string'] = ajato_tablesort_get_querystring() return ts end