'Creates the form for composing the message'

def _compose_form(self):

    T = current.T
    db = current.db
    s3db = current.s3db
    request = current.request
    get_vars = request.get_vars
    mtable = s3db.msg_message
    otable = s3db.msg_outbox
    mtable.body.label = T('Message')
    mtable.body.default = self.message
    mtable.inbound.default = False
    mtable.inbound.writable = False
    resource = self.resource
    recipient_type = self.recipient_type
    if ((not recipient_type) and resource):
        recipient_type = self._config('msg_recipient_type', None)
    contact_method = self.contact_method
    if ((not contact_method) and resource):
        contact_method = self._config('msg_contact_method', 'EMAIL')
    otable.contact_method.default = contact_method
    recipient = self.recipient
    if (not recipient):
        if ('pe_id' in get_vars):
            recipient = get_vars.pe_id
        elif ('person_id' in get_vars):
            pass
        elif ('group_id' in get_vars):
            pass
        elif ('human_resource.id' in get_vars):
            pass
    if recipient:
        recipients = [recipient]
    else:
        recipients = []
        if resource:
            table = resource.table
            if ('pe_id' in table):
                field = 'pe_id'
            elif ('person_id' in table):
                field = 'person_id$pe_id'
            else:
                field = None
            if field:
                records = resource.select([field], limit=None)['rows']
                recipients = [record.values()[0] for record in records]
    pe_field = otable.pe_id
    pe_field.label = T('Recipient(s)')
    pe_field.writable = True
    if recipients:
        pe_field.requires = None
        self.recipients = recipients
        pe_field.default = recipients
        if (len(recipients) == 1):
            recipient = recipients[0]
            represent = s3db.pr_PersonEntityRepresent(show_label=False)(recipient)
            petable = s3db.pr_pentity
            entity_type = db((petable.pe_id == recipient)).select(petable.instance_type, limitby=(0, 1)).first().instance_type
            if (entity_type == 'pr_person'):
                all_contact_opts = current.msg.MSG_CONTACT_OPTS
                contact_method_opts = {}
                ctable = s3db.pr_contact
                query = ((ctable.deleted != True) & (ctable.pe_id == recipient))
                rows = db(query).select(ctable.contact_method)
                for row in rows:
                    if (row.contact_method in all_contact_opts):
                        contact_method_opts[row.contact_method] = all_contact_opts[row.contact_method]
                if (not contact_method_opts):
                    current.session.error = T('There    are    no    contacts    available    for    this    person!')
                    controller = request.controller
                    if (controller == 'hrm'):
                        url = URL(c='hrm', f='person', args='contacts', vars={'group': 'staff', 'human_resource.id': get_vars.get('human_resource.id')})
                    elif (controller == 'vol'):
                        url = URL(c='vol', f='person', args='contacts', vars={'group': 'volunteer', 'human_resource.id': get_vars.get('human_resource.id')})
                    elif (controller == 'member'):
                        url = URL(c='member', f='person', args='contacts', vars={'membership.id': get_vars.get('membership.id')})
                    else:
                        url = URL(f='index')
                    redirect(url)
                otable.contact_method.requires = IS_IN_SET(contact_method_opts, zero=None)
                if (contact_method not in contact_method_opts):
                    otable.contact_method.default = contact_method_opts.popitem()[0]
        else:
            represent = (T('%(count)s    Recipients') % dict(count=len(recipients)))
    else:
        if recipient_type:
            pe_field.requires = IS_ONE_OF(db, 'pr_pentity.pe_id', filterby='instance_type', filter_opts=(recipient_type,))
            pe_field.widget = S3PentityAutocompleteWidget(types=(recipient_type,))
        else:
            pe_field.widget = S3PentityAutocompleteWidget()
        pe_field.comment = DIV(_class='tooltip', _title=('%s|%s' % (T('Recipients'), T('Please    enter    the    first    few    letters    of    the    Person/Group    for    the    autocomplete.'))))
    sqlform = S3SQLDefaultForm()
    s3resource = s3db.resource
    logform = sqlform(request=request, resource=s3resource('msg_message'), onvalidation=self._compose_onvalidation, message='Message    Sent', format='html')
    outboxform = sqlform(request=request, resource=s3resource('msg_outbox'), message='Message    Sent', format='html')
    mailform = sqlform(request=request, resource=s3resource('msg_email'), message='Message    Sent', format='html')
    lcustom = logform.custom
    ocustom = outboxform.custom
    mcustom = mailform.custom
    pe_row = TR(TD(LABEL(ocustom.label.pe_id)), _id='msg_outbox_pe_id__row')
    if recipients:
        ocustom.widget.pe_id['_class'] = 'hide'
        pe_row.append(TD(ocustom.widget.pe_id, represent))
    else:
        pe_row.append(TD(ocustom.widget.pe_id))
        pe_row.append(TD(ocustom.comment.pe_id))
    form = DIV(lcustom.begin, TABLE(TBODY(TR(TD(LABEL(ocustom.label.contact_method)), TD(ocustom.widget.contact_method), TD(ocustom.comment.contact_method), _id='msg_outbox_contact_method__row'), pe_row, TR(TD(LABEL(mcustom.label.subject)), TD(mcustom.widget.subject), TD(mcustom.comment.subject), _id='msg_log_subject__row'), TR(TD(LABEL(lcustom.label.body)), TD(lcustom.widget.body), TD(lcustom.comment.body), _id='msg_log_message__row'), TR(TD(), TD(INPUT(_type='submit', _value=T('Send    message'), _id='dummy_submit')), _id='submit_record__row'))), lcustom.end)
    s3 = current.response.s3
    if s3.debug:
        s3.scripts.append(('/%s/static/scripts/S3/s3.msg.js' % request.application))
    else:
        s3.scripts.append(('/%s/static/scripts/S3/s3.msg.min.js' % request.application))
    script = ('i18n.none_of_the_above="%s"' % T('None    of    the    above'))
    s3.js_global.append(script)
    return form
