'Render an action link for a create-popup (used in data lists
and data tables).
@param r: the S3Request instance
@param widget: the widget definition as dict
@param list_id: the list ID
@param resource: the target resource
@param context: the context filter
@param numrows: the total number of rows in the list/table'

@staticmethod
def _create_popup(r, widget, list_id, resource, context, numrows):

    create = ''
    widget_get = widget.get
    insert = widget_get('insert', True)
    if (not insert):
        return create
    table = resource.table
    tablename = resource.tablename
    (c, f) = tablename.split('_', 1)
    create_controller = widget_get('create_controller')
    if create_controller:
        c = create_controller
    create_function = widget_get('create_function')
    if create_function:
        f = create_function
    permit = current.auth.s3_has_permission
    create_ok = permit('create', table, c=c, f=f)
    if create_ok:
        if ((not create_controller) or (not create_function)):
            create_ok = permit('update', r.table, record_id=r.id, c=c, f=f)
    if create_ok:
        widget_filter = widget_get('filter')
        if widget_filter:
            url_vars = widget_filter.serialize_url(resource)
        else:
            url_vars = Storage()
        if context:
            filters = context.serialize_url(resource)
            for selector in filters:
                url_vars[selector] = filters[selector]
        default = widget_get('default')
        if default:
            (k, v) = default.split('=', 1)
            url_vars[k] = v
        url_vars.refresh = list_id
        url_vars.profile = r.tablename
        label_create = widget_get('label_create', None)
        if label_create:
            label_create = current.T(label_create)
        else:
            label_create = S3CRUD.crud_string(tablename, 'label_create')
        component = widget_get('create_component', None)
        if component:
            args = [r.id, component, 'create.popup']
        else:
            args = ['create.popup']
        add_url = URL(c=c, f=f, args=args, vars=url_vars)
        if callable(insert):
            create = insert(r, list_id, label_create, add_url)
        elif (current.deployment_settings.ui.formstyle == 'bootstrap'):
            create = A(ICON('plus-sign', _class='small-add'), _href=add_url, _class='s3_modal', _title=label_create)
        else:
            create = A(label_create, _href=add_url, _class='action-btn    profile-add-btn    s3_modal')
        if (widget_get('type') == 'datalist'):
            multiple = widget_get('multiple', True)
            if ((not multiple) and hasattr(create, 'update')):
                if numrows:
                    create.update(_style='display:none')
                else:
                    create.update(_style='display:block')
                createid = create['_id']
                if (not createid):
                    createid = ('%s-add-button' % list_id)
                    create.update(_id=createid)
                script = ("$('#%(list_id)s').on('listUpdate',function(){\n$('#%(createid)s').css({display:$(this).datalist('getTotalItems')?'none':'block'})\n})" % dict(list_id=list_id, createid=createid))
                current.response.s3.jquery_ready.append(script)
    return create
