from Products.ZTM2 import ZTopicMap, ZTopic, ZAssociation, ZAssociationRole, \
                          ZOccurrence, ZBaseName

from Products.CMFCore.TypesTool import ContentFactoryMetadata

def install(self):
    """ Add relevant types to portal_types. """
    typestool = self.portal_types
    
    try:
        from Products.BTreeFolder2 import CMFBTreeFolder
    except ImportError:
        raise AssertionError, "You have not installed the required BTreeFolder2 product."
    
    ztmtypes = ( ZTopicMap.factory_type_information
                + ZTopic.factory_type_information
                + ZAssociation.factory_type_information
                + ZAssociationRole.factory_type_information
                + ZOccurrence.factory_type_information
                + ZBaseName.factory_type_information
                )
    
    # Rename the TypeInformation for CMFTopic.
    if getattr(typestool, 'Topic', None) and typestool.Topic.Metatype() == 'Portal Topic':
        typestool.manage_renameObjects(ids=['Topic'], new_ids=['Query'])

    # Check if CMF BTree Folder is registered
    if not getattr(typestool, 'CMF BTree Folder', None):
        ztmtypes = ztmtypes + CMFBTreeFolder.factory_type_information
    
    for fti in ztmtypes:
        if not hasattr(typestool, fti['id']) and fti['id']!='default':
            cfm = apply(ContentFactoryMetadata, (), fti)
            typestool._setObject(fti['id'], cfm)

    typestool.Topic.allowed_content_types = ['Occurrence', 'BaseName']
    typestool.TopicMap.allowed_content_types = self.folderishtypes
    mappe = getattr(typestool, 'CMF BTree Folder')
    mappe.allowed_content_types = [ 'File'
                                  , 'CMF BTree Folder'
                                  , 'Association'
                                  , 'Topic'
                                  ]

    # Disable unused CMF portal types
    cmf_types = [ 'Discussion Item'
                , 'Document'
                , 'Favorite'
                , 'File'
                , 'Folder'
                , 'Image'
                , 'Link'
                , 'News Item']
    for portal_type in cmf_types:
        fti = getattr(typestool, portal_type, None)
        if fti:
            fti.global_allow = False

    norwegian = { 'Topic'           : 'Emne',
                  'TopicMap'        : 'Emnekart',
                  'Occurrence'      : 'Forekomst',
                  'BaseName'        : 'Emnenavn',
                  'AssociationRole' : 'Assosiasjonsrolle',
                  'Association'     : 'Assosiasjon',
                  'CMF BTree Folder': 'Mappe',
                  'Folder'          : 'Mappe',
                }
    if self._language == 'no':
        #TODO: Set TI descriptions
        for portal_type, title in norwegian.items():
            ti = getattr(typestool, portal_type)
            ti.title = title
    
    
    
