'Emergency Contact Information SubForm
@param pe_id: the pe_id
@param allow_create: allow adding of new contacts
@todo: make inline-editable this one too'

def emergency(self, pe_id, allow_create=False):

    if (not current.deployment_settings.get_pr_show_emergency_contacts()):
        return None
    T = current.T
    s3db = current.s3db
    resource = s3db.resource('pr_contact_emergency', filter=(FS('pe_id') == pe_id))
    table = resource.table
    fields = [f for f in ('name', 'relationship', 'address', 'phone', 'comments') if table[f].readable]
    fields.insert(0, 'id')
    rows = resource.select(fields).rows
    form = DIV(H2(T('Emergency    Contacts')), _class='pr-emergency-contacts')
    if (allow_create and current.auth.s3_has_permission('create', table)):
        add_btn = DIV(A(T('Add'), _class='contact-add-btn    action-btn'), DIV(_class='throbber    hide'))
        form.append(add_btn)
    readable_fields = fields[1:]
    action_buttons = self.action_buttons
    inline_edit_hint = T('Click    to    edit')
    for row in rows:
        contact_id = row['pr_contact_emergency.id']
        data = {'id': contact_id}
        description = TAG[''](SPAN(row['pr_contact_emergency.name'], _title=inline_edit_hint, _class='pr-emergency-name'))
        append = description.append
        for fieldname in ('relationship', 'address', 'phone'):
            if (fieldname in readable_fields):
                value = row[('pr_contact_emergency.%s' % fieldname)]
                if value:
                    data[fieldname] = s3_unicode(value)
                    editable = SPAN(value, _title=inline_edit_hint, _class=('pr-emergency-%s' % fieldname))
                    if (fieldname == 'relationship'):
                        append('    (')
                        append(editable)
                        append(')')
                    else:
                        append(',    ')
                        append(editable)
        if ('comments' in readable_fields):
            comments = (row['pr_contact_emergency.comments'] or '')
            if comments:
                data['comments'] = s3_unicode(comments)
        else:
            comments = ''
        actions = action_buttons(table, contact_id)
        form.append(DIV(DIV(DIV(description, _class='pr-contact-title'), DIV(SPAN(comments, _title=inline_edit_hint, _class='pr-emergency-comments'), _class='pr-contact-subtitle'), _class='pr-emergency-data    medium-9    columns'), actions, data=data, _class='pr-emergency-contact    row'))
    return form
