'Custom form to manage subscriptions
@param resources: available resources config
@param filters: filter widgets'

def _manage_subscriptions(self, resources, filters):

    from gluon.sqlhtml import SQLFORM
    from gluon.validators import IS_IN_SET
    from s3 import S3GroupedOptionsWidget
    T = current.T
    labels = Storage(RESOURCES=T('Subscribe    To'), NOTIFY_ON=T('Notify    On'), FREQUENCY=T('Frequency'), NOTIFY_BY=T('Notify    By'), MORE=T('More    Options'), LESS=T('Less    Options'))
    messages = Storage(ERROR=T('Error:    could    not    update    notification    settings'), SUCCESS=T('Notification    settings    updated'))
    subscription = self._get_subscription()
    formstyle = SQLFORM.formstyles.bootstrap
    form = FORM(_id='subscription-form', hidden={'subscription-filters': ''})
    filter_form = S3FilterForm(filters, clear=False)
    fieldset = FIELDSET(filter_form.fields(None, subscription['get_vars']), _id='subscription-filter-form')
    form.append(fieldset)
    rows = []
    stable = current.s3db.pr_subscription
    selector = S3GroupedOptionsWidget(cols=1)
    rows.append(('trigger_selector__row', ('%s:' % labels.NOTIFY_ON), selector(stable.notify_on, subscription['notify_on'], _id='trigger_selector'), ''))
    switch = S3GroupedOptionsWidget(cols=1, multiple=False, sort=False)
    rows.append(('frequency_selector__row', ('%s:' % labels.FREQUENCY), switch(stable.frequency, subscription['frequency'], _id='frequency_selector'), ''))
    fieldset = formstyle(form, rows)
    fieldset.insert(0, DIV(SPAN([I(_class='icon-reorder'), labels.MORE], _class='toggle-text', _style='display:none'), SPAN([I(_class='icon-reorder'), labels.LESS], _class='toggle-text'), _id='notification-options', _class='control-group'))
    form.append(fieldset)
    row = ('submit__row', '', INPUT(_type='submit', _value='Update    Settings'), '')
    fieldset = formstyle(form, [row])
    form.append(fieldset)
    script = URL(c='static', f='scripts', args=['S3', 's3.subscriptions.js'])
    response = current.response
    response.s3.scripts.append(script)
    if form.accepts(current.request.post_vars, current.session, formname='subscription', keepvalues=True):
        formvars = form.vars
        listify = (lambda x: (None if (not x) else (x if (type(x) is list) else [x])))
        subscription['subscribe'] = [resources[0]]
        subscription['filters'] = form.request_vars.get('subscription-filters', None)
        subscription['notify_on'] = listify(formvars.notify_on)
        subscription['frequency'] = formvars.frequency
        subscription['method'] = ['EMAIL']
        success = self._update_subscription(subscription)
        if success:
            response.confirmation = messages.SUCCESS
        else:
            response.error = messages.ERROR
    return form
