'Update Style for the PoIs layer
@ToDo: deployment_setting to add a new Feature Layer for new PoI types'

@staticmethod
def gis_poi_type_onaccept(form):

    db = current.db
    s3db = current.s3db
    ptable = s3db.gis_poi_type
    mtable = s3db.gis_marker
    query = ((ptable.deleted == False) & (mtable.id == ptable.marker_id))
    rows = db(query).select(ptable.name, mtable.image)
    style = []
    sappend = style.append
    for row in rows:
        marker = row['gis_marker.image']
        if marker:
            cat = {'prop': 'poi_type_id', 'cat': row['gis_poi_type.name'], 'externalGraphic': ('img/markers/%s' % marker)}
            sappend(cat)
    ltable = s3db.gis_layer_feature
    query = ((((ltable.controller == 'gis') & (ltable.function == 'poi')) & (ltable.filter == None)) & (ltable.deleted == False))
    row = db(query).select(ltable.layer_id, limitby=(0, 1)).first()
    if row:
        layer_id = row.layer_id
    else:
        f_id = ltable.insert(controller='gis', function='poi', attr_fields=['name', 'poi_type_id'], name='PoIs')
        record = dict(id=f_id)
        s3db.update_super(ltable, record)
        layer_id = record['layer_id']
    stable = s3db.gis_style
    query = ((stable.layer_id == layer_id) & (stable.record_id == None))
    styles = db(query).select(stable.id, stable.config_id)
    none_excluded = False
    if (len(styles) > 1):
        config = current.gis.get_config()
        config_id = config.id
        styles.exclude((lambda row: ((row.config_id == config_id) & (row.config_id != None))))
    if (len(styles) > 1):
        styles.exclude((lambda row: (row.config_id == None)))
        none_excluded = True
    if (len(styles) == 1):
        _style = styles.first()
        _style.update_record(style=style)
    elif (len(styles) == 0):
        data = dict(layer_id=layer_id, popup_format='{name}    ({poi_type_id})', style=style)
        if none_excluded:
            data['config_id'] = config_id
        stable.insert(**data)
    else:
        current.log.warning('Unable    to    update    GIS    PoI    Style    as    there    are    multiple    possible')
