'Calculate the export formats that can be added to the table
@param id: the unique dataTable ID
@param rfields: optional list of field selectors for exports
@param permalink: search result URL
@param base_url: the base URL of the datatable (without
method or query vars) to construct format URLs'

@staticmethod
def export_formats(rfields=None, permalink=None, base_url=None):

    T = current.T
    s3 = current.response.s3
    request = current.request
    if (base_url is None):
        base_url = request.url
    if s3.datatable_ajax_source:
        default_url = s3.datatable_ajax_source
    else:
        default_url = base_url
    default_url = re.sub('(\\/[a-zA-Z0-9_]*)(\\.[a-zA-Z]*)', '\\g<1>', default_url)
    get_vars = request.get_vars
    if get_vars:
        query = '&'.join((('%s=%s' % (k, v)) for (k, v) in get_vars.items()))
        default_url = ('%s?%s' % (default_url, query))
    icons = SPAN(_class='list_formats')
    settings = current.deployment_settings
    export_formats = settings.get_ui_export_formats()
    if export_formats:
        icons.append(('%s:' % T('Export    as')))
        formats = dict(s3.formats)
        if (('kml' not in formats) and rfields):
            kml_fields = set(['location_id', 'site_id'])
            if any(((rfield.fname in kml_fields) for rfield in rfields)):
                formats['kml'] = default_url
        default_formats = ('xml', 'rss', 'xls', 'pdf')
        EXPORT = T('Export    in    %(format)s    format')
        append_icon = icons.append
        for fmt in export_formats:
            if (fmt in default_formats):
                url = formats.get(fmt, default_url)
            else:
                url = formats.get(fmt)
            if (not url):
                continue
            if (fmt == 'map'):
                title = T('Show    on    Map')
            else:
                title = (EXPORT % dict(format=fmt.upper()))
            append_icon(DIV(_class=('dt-export    export_%s' % fmt), _title=title, data={'url': url, 'extension': fmt}))
    export_options = DIV(_class='dt-export-options')
    if (permalink is not None):
        link = A(settings.get_ui_label_permalink(), _href=permalink, _class='permalink')
        export_options.append(link)
        if len(icons):
            export_options.append('    |    ')
    export_options.append(icons)
    return export_options
