'Delete record(s)
@param r: the S3Request
@param attr: dictionary of parameters for the method handler
@todo: update for link table components'

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

    output = dict()
    config = self._config
    deletable = config('deletable', True)
    delete_next = config('delete_next', None)
    if (not deletable):
        r.error(403, current.ERROR.NOT_PERMITTED, next=r.url(method=''))
    record_id = self.record_id
    authorised = self._permitted()
    if (not authorised):
        r.unauthorised()
    elif ((r.interactive or (r.representation == 'aadata')) and (r.http == 'GET') and (not record_id)):
        output = self._datatable(r, **attr)
        if isinstance(output, dict):
            form = FORM(TABLE(TR(TD(self.settings.confirm_delete, _style='color:red'), TD(INPUT(_type='submit', _value=current.T('Delete'), _style='margin-left:10px')))))
            output['form'] = form
            current.response.view = self._view(r, 'delete.html')
        else:
            return output
    elif (r.interactive and ((r.http == 'POST') or ((r.http == 'GET') and record_id))):
        numrows = self.resource.delete(format=r.representation)
        if (numrows > 1):
            message = ('%s    %s' % (numrows, current.T('records    deleted')))
        elif (numrows == 1):
            message = self.crud_string(self.tablename, 'msg_record_deleted')
        else:
            r.error(404, self.resource.error, next=r.url(method=''))
        current.response.confirmation = message
        r.http = 'DELETE'
        self.next = (delete_next or r.url(method=''))
    elif ((r.http == 'DELETE') or ((r.representation == 'json') and (r.http == 'POST') and record_id)):
        numrows = None
        resource = self.resource
        recursive = r.get_vars.get('recursive', False)
        if (recursive and (recursive.lower() in ('1', 'true'))):
            from s3hierarchy import S3Hierarchy
            h = S3Hierarchy(resource.tablename)
            if h.config:
                node_ids = None
                pkey = h.pkey
                if (str(pkey) == str(resource._id)):
                    node_ids = [record_id]
                else:
                    query = (resource._id == record_id)
                    row = current.db(query).select(pkey, limitby=(0, 1)).first()
                    if row:
                        node_ids = [row[pkey]]
                numrows = (h.delete(node_ids) if node_ids else 0)
                if (not numrows):
                    resource.error = current.T('Deletion    failed')
                    numrows = 0
        if (numrows is None):
            numrows = self.resource.delete(format=r.representation)
        if (numrows > 1):
            message = ('%s    %s' % (numrows, current.T('records    deleted')))
        elif (numrows == 1):
            message = self.crud_string(self.tablename, 'msg_record_deleted')
        else:
            r.error(404, resource.error, next=r.url(method=''))
        item = current.xml.json_message(message=message)
        current.response.view = 'xml.html'
        output.update(item=item)
    else:
        r.error(405, current.ERROR.BAD_METHOD)
    return output
