'Export field options of this resource as element tree
@param component: name of the component which the options are
requested of, None for the primary table
@param fields: list of names of fields for which the options
are requested, None for all fields (which have
options)
@param as_json: convert the output into JSON
@param only_last: obtain only the latest record'

def export_options(self, component=None, fields=None, only_last=False, show_uids=False, hierarchy=False, as_json=False):

    if (component is not None):
        c = self.components.get(component)
        if c:
            tree = c.export_options(fields=fields, only_last=only_last, show_uids=show_uids, hierarchy=hierarchy, as_json=as_json)
            return tree
        else:
            raise AttributeError
    else:
        if (as_json and only_last and (len(fields) == 1)):
            default = {'option': []}
            try:
                field = self.table[fields[0]]
            except AttributeError:
                return json.dumps(default)
            requires = field.requires
            if (not isinstance(requires, (list, tuple))):
                requires = [requires]
            requires = requires[0]
            if isinstance(requires, IS_EMPTY_OR):
                requires = requires.other
            from s3validators import IS_LOCATION
            if (not isinstance(requires, (IS_ONE_OF, IS_LOCATION))):
                return json.dumps(default)
            db = current.db
            lookuptable = requires.ktable
            lookupfield = db[lookuptable][requires.kfield]
            fields = [lookupfield]
            h = None
            if hierarchy:
                from s3hierarchy import S3Hierarchy
                h = S3Hierarchy(lookuptable)
                if (not h.config):
                    h = None
                elif (h.pkey.name != lookupfield.name):
                    fields.append(h.pkey)
            row = db().select(orderby=(~ lookupfield), limitby=(0, 1), *fields).first()
            if row:
                value = row[lookupfield]
                widget = field.widget
                if (hasattr(widget, 'represent') and widget.represent):
                    represent = widget.represent(value)
                elif field.represent:
                    represent = field.represent(value)
                else:
                    represent = s3_unicode(value)
                if isinstance(represent, A):
                    represent = represent.components[0]
                item = {'@value': value, '$': represent}
                if h:
                    parent = h.parent(row[h.pkey])
                    if parent:
                        item['@parent'] = str(parent)
                result = [item]
            else:
                result = []
            return json.dumps({'option': result})
        xml = current.xml
        tree = xml.get_options(self.table, fields=fields, show_uids=show_uids, hierarchy=hierarchy)
        if as_json:
            return xml.tree2json(tree, pretty_print=False, native=True)
        else:
            return xml.tostring(tree, pretty_print=False)
