# Setup methods for ImageAttachments

from Products.CMFCore.utils import getToolByName

def setupImageAttachmentWorkflow(context):
    """Turn off workflow for the ImageAttachment type. Should be called from
    Install.py, with a context parameter that allows portal_workflow to be
    acquired.
    """
    wft = getToolByName(context, 'portal_workflow')
    wft.setChainForPortalTypes(('ImageAttachment',), ())

def setupImageAttachmentView(context):
    """Add ImageAttachment to the list of types which use the /view suffix in
    listings. Should be called from Install.py, with a context parameter that 
    allows portal_workflow to be acquired.
    """
    propsTool = getToolByName(context, 'portal_properties')
    siteProperties = getattr(propsTool, 'site_properties')
    typesUseViewActionInListings = list(siteProperties.getProperty('typesUseViewActionInListings'))
    if 'ImageAttachment' not in typesUseViewActionInListings:
        typesUseViewActionInListings.append('ImageAttachment')
    siteProperties.manage_changeProperties(typesUseViewActionInListings  = typesUseViewActionInListings)

def registerImagesFormControllerActions(context, contentType=None, template='base_edit'):
    """Register the form controller actions necessary for the widget to work.
    This should probably be called from the Install.py script. The parameter
    'context' should be the portal root or another place from which the form
    controller can be acquired. The contentType and template argument allow
    you to restrict the registration to only one content type and choose a
    template other than base_edit, if necessary.
    """
    pfc = getToolByName(context, 'portal_form_controller')
    pfc.addFormAction(template,
                      'success',
                      contentType,
                      'UploadImage',
                      'traverse_to',
                      'string:widget_imagesmanager_upload')

    pfc.addFormAction(template,
                      'success',
                      contentType,
                      'RenameImages',
                      'traverse_to',
                      'string:widget_imagesmanager_rename')

    pfc.addFormAction(template,
                      'success',
                      contentType,
                      'MoveImages',
                      'traverse_to',
                      'string:widget_imagesmanager_move')

    pfc.addFormAction(template,
                      'success',
                      contentType,
                      'DeleteImages',
                      'traverse_to',
                      'string:widget_imagesmanager_delete')
                      
# Setup methods for FileAttachments

def setupFileAttachmentWorkflow(context):
    """Turn off workflow for the FileAttachment type. Should be called from
    Install.py, with a context parameter that allows portal_workflow to be
    acquired.
    """
    wft = getToolByName(context, 'portal_workflow')
    wft.setChainForPortalTypes(('FileAttachment',), ())

def setupFileAttachmentView(context):
    """Add FileAttachment to the list of types which use the /view suffix in
    listings. Should be called from Install.py, with a context parameter that 
    allows portal_workflow to be acquired.
    """
    propsTool = getToolByName(context, 'portal_properties')
    siteProperties = getattr(propsTool, 'site_properties')
    typesUseViewActionInListings = list(siteProperties.getProperty('typesUseViewActionInListings'))
    if 'FileAttachment' not in typesUseViewActionInListings:
        typesUseViewActionInListings.append('FileAttachment')
    siteProperties.manage_changeProperties(typesUseViewActionInListings  = typesUseViewActionInListings)

def registerAttachmentsFormControllerActions(context, contentType=None, template='base_edit'):
    """Register the form controller actions necessary for the widget to work.
    This should probably be called from the Install.py script. The parameter
    'context' should be the portal root or another place from which the form
    controller can be acquired. The contentType and template argument allow
    you to restrict the registration to only one content type and choose a
    template other than base_edit, if necessary.
    """
    pfc = getToolByName(context, 'portal_form_controller')
    pfc.addFormAction(template,
                      'success',
                      contentType,
                      'UploadAttachment',
                      'traverse_to',
                      'string:widget_attachmentsmanager_upload')

    pfc.addFormAction(template,
                      'success',
                      contentType,
                      'RenameAttachments',
                      'traverse_to',
                      'string:widget_attachmentsmanager_rename')

    pfc.addFormAction(template,
                      'success',
                      contentType,
                      'MoveAttachments',
                      'traverse_to',
                      'string:widget_attachmentsmanager_move')

    pfc.addFormAction(template,
                      'success',
                      contentType,
                      'DeleteAttachments',
                      'traverse_to',
                      'string:widget_attachmentsmanager_delete')