'When an Incident is updated
- set correct event_id for all relevant components
- check for closure'

@staticmethod
def incident_update_onaccept(form):

    db = current.db
    s3db = current.s3db
    form_vars = form.vars
    incident_id = form_vars.id
    closed = form_vars.get('closed')
    event_id = form_vars.get('event_id', False)
    if ((event_id is False) or (closed is None)):
        itable = s3db.event_incident
        record = db((itable.id == incident_id)).select(itable.closed, itable.event_id, limitby=(0, 1)).first()
        closed = record.closed
        event_id = record.event_id
    if event_id:
        for tablename in ('event_event_impact', 'event_response', 'event_target', 'event_organisation', 'event_post', 'event_request', 'event_sitrep', 'event_task', 'event_team'):
            table = s3db.table(tablename)
            if table:
                db((table.incident_id == incident_id)).update(event_id=event_id)
    if closed:
        s3 = current.session.s3
        if (s3.incident == incident_id):
            s3.incident = None
        db = current.db
        ltable = current.s3db.event_post
        table = db.cms_post
        rows = db((ltable.incident_id == incident_id)).select(ltable.post_id)
        for row in rows:
            db((table.id == row.post_id)).update(expired=True)
