'Build the Options'

def _options(self, data):

    s3db = current.s3db
    opts = self.options
    resource = self.resource
    component_name = data['component']
    component = resource.components[component_name]
    table = component.table
    option_help = opts.get('option_help', None)
    if option_help:
        fields = ['id', 'name', option_help]
    else:
        fields = ['id', 'name']
    opt_filter = opts.get('filter', None)
    if opt_filter:
        linktable = s3db[opt_filter['linktable']]
        lkey = opt_filter['lkey']
        rkey = opt_filter['rkey']
        if ('values' in opt_filter):
            values = opt_filter['values']
        else:
            lookuptable = opt_filter.get('lookuptable', None)
            if lookuptable:
                lookupkey = opt_filter.get('lookupkey', None)
                if (not lookupkey):
                    raise
                if resource._rows:
                    _id = resource._rows[0][lookupkey]
                    _resource = s3db.resource(lookuptable, id=_id)
                else:
                    _id = None
            elif resource._ids:
                _id = resource._ids[0]
                _resource = resource
            else:
                _id = None
            if _id:
                _table = _resource.table
                if (rkey in _table.fields):
                    values = [_table[rkey]]
                else:
                    found = False
                    if lookuptable:
                        hooks = s3db.get_components(_table)
                        for alias in hooks:
                            if (hooks[alias].rkey == rkey):
                                found = True
                                _resource._attach(alias, hooks[alias])
                                _component = _resource.components[alias]
                                break
                    else:
                        components = _resource.components
                        for c in components:
                            if (components[c].rkey == rkey):
                                found = True
                                _component = components[c]
                                break
                    if found:
                        _rows = _component.select(['id'], limit=None, as_rows=True)
                        values = [r.id for r in _rows]
                    else:
                        values = []
            else:
                values = []
        rows = []
        rappend = rows.append
        fields = [table[f] for f in fields]
        fields.append(linktable[rkey])
        query = ((table.deleted == False) & current.auth.s3_accessible_query('read', table))
        srows = current.db(query).select(left=linktable.on((linktable[lkey] == table.id)), orderby=table.name, *fields)
        for r in srows:
            v = r[linktable][rkey]
            if ((not v) or (v in values)):
                _r = r[table]
                record = Storage(id=_r.id, name=_r.name)
                if option_help:
                    record[option_help] = _r[option_help]
                rappend(record)
    else:
        _resource = s3db.resource(component.tablename)
        filterby = opts.get('filterby', None)
        if filterby:
            options = filterby['options']
            filter_field = filterby['field']
            if isinstance(options, list):
                _resource.add_filter(FS(filter_field).belongs(options))
            else:
                _resource.add_filter((FS(filter_field) == options))
        rows = _resource.select(fields=fields, limit=None, orderby=table.name, as_rows=True)
    if (not rows):
        return None
    if component.link:
        component = component.link
    creatable = current.auth.s3_has_permission('create', component.tablename)
    options = OrderedDict()
    for r in rows:
        options[r.id] = dict(name=r.name, selected=False, editable=creatable)
        if option_help:
            options[r.id]['help'] = r[option_help]
    fieldname = data['field']
    items = data['data']
    prefix = component.prefix
    name = component.name
    audit = current.audit
    for i in xrange(len(items)):
        item = items[i]
        if (fieldname in item):
            if ('_delete' in item):
                continue
            _id = item[fieldname]['value']
            if ('_id' in item):
                record_id = item['_id']
                editable = (not ('_readonly' in item))
                audit('read', prefix, name, record=record_id, representation='html')
            elif ('_changed' in item):
                editable = True
                _id = int(_id)
            try:
                options[_id].update(selected=True, editable=editable)
            except:
                current.session.error = (current.T('Invalid    data:    record    %(id)s    not    accessible    in    table    %(table)s') % dict(id=_id, table=table))
                redirect(URL(args=None, vars=None))
    return options
