## Script (Python) "retrieveAssocHelpText"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=topic, assoctype
##title=
##
topicmap = context.portal_topicmaps.getTopicMap()
helptype = topicmap.getTopicBySubjectIdentifier('http://psi.emnekart.no/ztm/ztmdefault/#helptext')
if not helptype is None:
    occs = assoctype.getOccurrences()
    topictypes = topic.getTypes()
    unconstrained = None
    constrained = None
    for occ in occs:
        if occ.getType(wrap=False).tm_serial == helptype.tm_serial:
            scopeset = occ.getScope()
            if not scopeset and not unconstrained:
                unconstrained = occ
                continue
            for topictype in topictypes:
                if topictype in scopeset:
                    constrained = occ
                    break
    
    #TODO: Find locator, and add editposibility
    if constrained:
        string = constrained.getData()
        occ = constrained
    elif unconstrained:
        string = unconstrained.getData()
        occ = unconstrained
    else:
        string = "Ingen hjelpetekst"
        occ = None
    
    from AccessControl import getSecurityManager
    user = getSecurityManager().getUser()
    
    if 'Manager' in user.getRolesInContext(topic):
        if occ:
            string = "%s %s"%(string, '[&nbsp;<a href="%s/%s">Rediger</a>&nbsp;]'%(occ.absolute_url(), occ.getTypeInfo().getActionById('ztmedit')) )
        else:
            string = '[&nbsp;<a href="%s/%s">Opprett hjelpetekst</a>&nbsp;]'%(assoctype.absolute_url(), 'create_helptext')
    
    return string
return ''
