'Read the request.vars & prepare for a record insert/update
Note: This is also used by IS_SITE_SELECTOR()'

def _process_values(self):

    post_vars = current.request.post_vars
    lat = post_vars.get('gis_location_lat', None)
    lon = post_vars.get('gis_location_lon', None)
    if lat:
        try:
            lat = float(lat)
        except ValueError:
            self.errors['lat'] = current.T('Latitude    is    Invalid!')
    if lon:
        try:
            lon = float(lon)
        except ValueError:
            self.errors['lon'] = current.T('Longitude    is    Invalid!')
    if self.errors:
        return None
    L0 = post_vars.get('gis_location_L0', None)
    db = current.db
    table = db.gis_location
    auth = current.auth
    def permitted_to_create():
        if (not auth.s3_has_permission('create', table)):
            self.errors['location_id'] = auth.messages.access_denied
            return False
        return True
    s3db = current.s3db
    if auth.s3_has_role(current.session.s3.system_roles.MAP_ADMIN):
        L1_allowed = True
        L2_allowed = True
        L3_allowed = True
        L4_allowed = True
        L5_allowed = True
    else:
        if L0:
            htable = s3db.gis_hierarchy
            query = (htable.location_id == L0)
            config = db(query).select(htable.edit_L1, htable.edit_L2, htable.edit_L3, htable.edit_L4, htable.edit_L5, limitby=(0, 1)).first()
        if (L0 and config):
            L1_allowed = config.edit_L1
            L2_allowed = config.edit_L2
            L3_allowed = config.edit_L3
            L4_allowed = config.edit_L4
            L5_allowed = config.edit_L5
        else:
            L1_allowed = True
            L2_allowed = True
            L3_allowed = True
            L4_allowed = True
            L5_allowed = True
    onaccept = current.gis.update_location_tree
    L1 = post_vars.get('gis_location_L1', None)
    L2 = post_vars.get('gis_location_L2', None)
    L3 = post_vars.get('gis_location_L3', None)
    L4 = post_vars.get('gis_location_L4', None)
    L5 = post_vars.get('gis_location_L5', None)
    if L1:
        try:
            L1 = int(L1)
            if L0:
                location = db((table.id == L1)).select(table.name, table.parent, limitby=(0, 1)).first()
                if (location and (location.parent != int(L0))):
                    db(query).update(parent=L0)
                    location['level'] = 'L1'
                    location['id'] = L1
                    onaccept(location)
        except:
            query = ((table.name == L1) & (table.level == 'L1'))
            if L0:
                query &= (table.parent == L0)
            location = db(query).select(table.id, limitby=(0, 1)).first()
            if location:
                L1 = location.id
            elif L1_allowed:
                if permitted_to_create():
                    if L0:
                        f = dict(name=L1, level='L1', parent=L0)
                        L1 = table.insert(**f)
                        f['id'] = L1
                        onaccept(f)
                    else:
                        f = dict(name=L1, level='L1')
                        L1 = table.insert(**f)
                        f['id'] = L1
                        onaccept(f)
                else:
                    return None
            else:
                L1 = None
    if L2:
        try:
            L2 = int(L2)
            if L1:
                location = db((table.id == L2)).select(table.name, table.parent, limitby=(0, 1)).first()
                if (location and (location.parent != L1)):
                    db(query).update(parent=L1)
                    location['level'] = 'L2'
                    location['id'] = L2
                    onaccept(location)
        except:
            query = ((table.name == L2) & (table.level == 'L2'))
            if L1:
                query &= (table.parent == L1)
            location = db(query).select(table.id, limitby=(0, 1)).first()
            if location:
                L2 = location.id
            elif L2_allowed:
                if permitted_to_create():
                    if L1:
                        f = dict(name=L2, level='L2', parent=L1)
                        L2 = table.insert(**f)
                        f['id'] = L2
                        onaccept(f)
                    elif L0:
                        f = dict(name=L2, level='L2', parent=L0)
                        L2 = table.insert(**f)
                        f['id'] = L2
                        onaccept(f)
                    else:
                        f = dict(name=L2, level='L2')
                        L2 = table.insert(**f)
                        f['id'] = L2
                        onaccept(f)
                else:
                    return None
            else:
                L2 = None
    if L3:
        try:
            L3 = int(L3)
            if L2:
                location = db((table.id == L3)).select(table.name, table.parent, limitby=(0, 1)).first()
                if (location and (location.parent != L2)):
                    db(query).update(parent=L2)
                    location['level'] = 'L3'
                    location['id'] = L3
                    onaccept(location)
        except:
            query = ((table.name == L3) & (table.level == 'L3'))
            if L2:
                query &= (table.parent == L2)
            location = db(query).select(table.id, limitby=(0, 1)).first()
            if location:
                L3 = location.id
            elif L3_allowed:
                if permitted_to_create():
                    if L2:
                        f = dict(name=L3, level='L3', parent=L2)
                        L3 = table.insert(**f)
                        f['id'] = L3
                        onaccept(f)
                    elif L1:
                        f = dict(name=L3, level='L3', parent=L1)
                        L3 = table.insert(**f)
                        f['id'] = L3
                        onaccept(f)
                    elif L0:
                        f = dict(name=L3, level='L3', parent=L0)
                        L3 = table.insert(**f)
                        f['id'] = L3
                        onaccept(f)
                    else:
                        f = dict(name=L3, level='L3')
                        L3 = table.insert(**f)
                        f['id'] = L3
                        onaccept(f)
                else:
                    return None
            else:
                L3 = None
    if L4:
        try:
            L4 = int(L4)
            if L3:
                location = db((table.id == L4)).select(table.name, table.parent, limitby=(0, 1)).first()
                if (location and (location.parent != L3)):
                    db(query).update(parent=L3)
                    location['level'] = 'L4'
                    location['id'] = L4
                    onaccept(location)
        except:
            query = ((table.name == L4) & (table.level == 'L4'))
            if L3:
                query &= (table.parent == L3)
            location = db(query).select(table.id, limitby=(0, 1)).first()
            if location:
                L4 = location.id
            elif L4_allowed:
                if permitted_to_create():
                    if L3:
                        f = dict(name=L4, level='L4', parent=L3)
                        L4 = table.insert(**f)
                        f['id'] = L4
                        onaccept(f)
                    elif L2:
                        f = dict(name=L4, level='L4', parent=L2)
                        L4 = table.insert(**f)
                        f['id'] = L4
                        onaccept(f)
                    elif L1:
                        f = dict(name=L4, level='L4', parent=L1)
                        L4 = table.insert(**f)
                        f['id'] = L4
                        onaccept(f)
                    elif L0:
                        f = dict(name=L4, level='L4', parent=L0)
                        L4 = table.insert(**f)
                        f['id'] = L4
                        onaccept(f)
                    else:
                        f = dict(name=L4, level='L4')
                        L4 = table.insert(**f)
                        f['id'] = L4
                        onaccept(f)
                else:
                    return None
            else:
                L4 = None
    if L5:
        try:
            L5 = int(L5)
            if L4:
                location = db((table.id == L5)).select(table.name, table.parent, limitby=(0, 1)).first()
                if (location and (location.parent != L4)):
                    db(query).update(parent=L4)
                    location['level'] = 'L5'
                    location['id'] = L5
                    onaccept(location)
        except:
            query = ((table.name == L5) & (table.level == 'L5'))
            if L4:
                query &= (table.parent == L4)
            location = db(query).select(table.id, limitby=(0, 1)).first()
            if location:
                L5 = location.id
            elif L5_allowed:
                if permitted_to_create():
                    if L4:
                        f = dict(name=L5, level='L5', parent=L4)
                        L5 = table.insert(**f)
                        f['id'] = L5
                        onaccept(f)
                    elif L3:
                        f = dict(name=L5, level='L5', parent=L3)
                        L5 = table.insert(**f)
                        f['id'] = L5
                        onaccept(f)
                    elif L2:
                        f = dict(name=L5, level='L5', parent=L2)
                        L5 = table.insert(**f)
                        f['id'] = L5
                        onaccept(f)
                    elif L1:
                        f = dict(name=L5, level='L5', parent=L1)
                        L5 = table.insert(**f)
                        f['id'] = L5
                        onaccept(f)
                    elif L0:
                        f = dict(name=L5, level='L5', parent=L1)
                        L5 = table.insert(**f)
                        f['id'] = L5
                        onaccept(f)
                    else:
                        f = dict(name=L5, level='L5')
                        L5 = table.insert(**f)
                        f['id'] = L5
                        onaccept(f)
                else:
                    return None
            else:
                L5 = None
    name = post_vars.get('gis_location_name', None)
    wkt = post_vars.get('gis_location_wkt', None)
    street = post_vars.get('gis_location_street', None)
    postcode = post_vars.get('gis_location_postcode', None)
    parent = (L5 or L4 or L3 or L2 or L1 or L0 or None)
    form = Storage()
    form.errors = dict()
    form.vars = Storage()
    form_vars = form.vars
    form_vars.lat = lat
    form_vars.lon = lon
    form_vars.wkt = wkt
    if wkt:
        form_vars.gis_feature_type = '3'
    else:
        form_vars.gis_feature_type = '1'
    form_vars.parent = parent
    if self.id:
        form.record = db((table.id == self.id)).select(table.inherited, table.lat, table.lon, limitby=(0, 1)).first()
    s3db.gis_location_onvalidation(form)
    if form.errors:
        self.errors = form.errors
        return None
    location = Storage(name=name, lat=form_vars.lat, lon=form_vars.lon, inherited=form_vars.inherited, street=street, postcode=postcode, parent=parent, wkt=form_vars.wkt, gis_feature_type=form_vars.gis_feature_type, lon_min=form_vars.lon_min, lon_max=form_vars.lon_max, lat_min=form_vars.lat_min, lat_max=form_vars.lat_max)
    return location
