'Entry point for REST controller
@param r: the S3Request
@param attr: controller parameters for the request'

def apply_method(self, r, **attr):

    response = current.response
    settings = current.deployment_settings
    resource = r.resource
    resource.add_filter(response.s3.filter)
    get_vars = r.get_vars
    value = (get_vars.term or get_vars.value or get_vars.q or None)
    if (not value):
        r.error(400, 'No    value    provided!')
    value = s3_unicode(value).lower().strip()
    partials = value.split()[:8]
    search_fields = self.search_fields
    query = None
    for partial in partials:
        pquery = None
        for field in search_fields:
            selector = FS(field).lower()
            fquery = (selector.like(('%s%%' % partial)) | selector.like(('%%    %s%%' % partial)))
            if pquery:
                pquery |= fquery
            else:
                pquery = fquery
        if query:
            query &= pquery
        else:
            query = pquery
    if (query is not None):
        resource.add_filter(query)
    limit = int((get_vars.limit or 0))
    MAX_SEARCH_RESULTS = settings.get_search_max_results()
    if (((not limit) or (limit > MAX_SEARCH_RESULTS)) and (resource.count() > MAX_SEARCH_RESULTS)):
        msg = current.T('There    are    more    than    %(max)s    results,    please    input    more    characters.')
        output = [{'label': s3_str((msg % {'max': MAX_SEARCH_RESULTS}))}]
    else:
        fields = ['id']
        fields.extend(search_fields)
        show_hr = settings.get_pr_search_shows_hr_details()
        if show_hr:
            fields.append('human_resource.job_title_id$name')
            show_orgs = settings.get_hrm_show_organisation()
            if show_orgs:
                fields.append('human_resource.organisation_id$name')
        name_format = settings.get_pr_name_format()
        match = re.match('\\s*?%\\((?P<fname>.*?)\\)s.*', name_format)
        if match:
            orderby = ('pr_person.%s' % match.group('fname'))
        else:
            orderby = 'pr_person.first_name'
        rows = resource.select(fields=fields, start=0, limit=limit, orderby=orderby).rows
        items = []
        iappend = items.append
        for row in rows:
            name = Storage(first_name=row['pr_person.first_name'], middle_name=row['pr_person.middle_name'], last_name=row['pr_person.last_name'])
            name = s3_fullname(name)
            if ('pe_label' in search_fields):
                name = ('%s    %s' % (row['pr_person.pe_label'], name))
            item = {'id': row['pr_person.id'], 'name': name}
            if show_hr:
                job_title = row.get('hrm_job_title.name', None)
                if job_title:
                    item['job'] = job_title
                if show_orgs:
                    org = row.get('org_organisation.name', None)
                    if org:
                        item['org'] = org
            iappend(item)
        output = items
    response.headers['Content-Type'] = 'application/json'
    return json.dumps(output, separators=SEPARATORS)
