'JSON search method for S3OrganisationAutocompleteWidget
- searches name & acronym for both this organisation & the parent
of branches
@param r: the S3Request
@param attr: request attributes'

@staticmethod
def org_search_ac(r, **attr):

    _vars = current.request.get_vars
    value = (_vars.term or _vars.value or _vars.q or None)
    value = s3_unicode(value).lower().strip()
    if (not value):
        output = current.xml.json_message(False, 400, 'Missing    option!    Require    value')
        raise HTTP(400, body=output)
    response = current.response
    resource = r.resource
    table = resource.table
    settings = current.deployment_settings
    use_branches = settings.get_org_branches()
    search_l10n = settings.get_L10n_translate_org_organisation()
    resource.add_filter(response.s3.filter)
    query = (FS('organisation.name').lower().like((value + '%')) | FS('organisation.acronym').lower().like((value + '%')))
    if use_branches:
        query |= (FS('parent.name').lower().like((value + '%')) | FS('parent.acronym').lower().like((value + '%')))
    if search_l10n:
        query |= (FS('name.name_l10n').lower().like((value + '%')) | FS('name.acronym_l10n').lower().like((value + '%')))
    if ('link' in _vars):
        link_filter = S3EmbeddedComponentWidget.link_filter(table, _vars.link)
        if link_filter:
            query &= link_filter
    resource.add_filter(query)
    MAX_SEARCH_RESULTS = settings.get_search_max_results()
    limit = int((_vars.limit or MAX_SEARCH_RESULTS))
    if (((not limit) or (limit > MAX_SEARCH_RESULTS)) and (resource.count() > MAX_SEARCH_RESULTS)):
        output = [dict(label=str((current.T('There    are    more    than    %(max)s    results,    please    input    more    characters.') % dict(max=MAX_SEARCH_RESULTS))))]
    else:
        field = table.name
        field2 = table.acronym
        fields = ['id', 'name', 'acronym']
        if use_branches:
            fields.append('parent.name')
        if search_l10n:
            fields += ['name.name_l10n', 'name.acronym_l10n']
        rows = resource.select(fields, start=0, limit=limit, orderby=field, as_rows=True)
        output = []
        append = output.append
        for row in rows:
            acronym = ''
            if (use_branches or search_l10n):
                _row = row[table]
            else:
                _row = row
            if search_l10n:
                name = (row['org_organisation_name.name_l10n'] or _row.name)
                acronym = (row['org_organisation_name.acronym_l10n'] or _row.acronym)
            else:
                name = _row.name
                acronym = _row.acronym
            record = dict(id=_row.id, name=name)
            if acronym:
                record['acronym'] = acronym
            if ('org_parent_organisation' in row):
                parent = object.__getattribute__(row, 'org_parent_organisation')
                if (parent.name is not None):
                    record['parent'] = parent.name
            value_len = len(value)
            name_match = (s3_unicode(name)[:value_len].lower() == value)
            acronym_match = (acronym and (s3_unicode(acronym)[:value_len].lower() == value))
            if name_match:
                nextString = name[value_len:]
                if (nextString != ''):
                    record['matchString'] = name[:value_len]
                    record['nextString'] = nextString
            elif acronym_match:
                nextString = acronym[value_len:]
                if (nextString != ''):
                    record['matchString'] = acronym[:value_len]
                    record['nextString'] = nextString
                    record['match'] = 'acronym'
            append(record)
    response.headers['Content-Type'] = 'application/json'
    return json.dumps(output, separators=SEPARATORS)
