## Controller Python Script "send_feedback"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind state=state
##bind subpath=traverse_subpath
##parameters=
##title=Send feedback to an author
##
REQUEST=context.REQUEST

from Products.CMFCore.utils import getToolByName

mtool = getToolByName(context, 'portal_membership')
anon_tool = getToolByName(context, 'portal_anonymailer')
putils = getToolByName(context, 'plone_utils')

##
## This may change depending on the called (portal_feedback or author)
state_success = "success"
state_failure = "failure"

## make these arguments?
referer = REQUEST.get('referer', 'unknown referer')
author = REQUEST.get('author', None) # None means portal administrator

if author is None:
    send_to_address = site_properties.email_from_address
else:
    state_success = "success_author"
    state_failure = "failure_author"

state.set(status=state_success) ## until proven otherwise

sender = mtool.getAuthenticatedMember()
sender_id = "%s (%s)" % (sender.getProperty('fullname'), sender.getId())

variables = {'url'               : referer,
             'recip_id'          : author,
             'encoding'          : putils.getSiteEncoding(),
             }

state_values = anon_tool.sendMessage(REQUEST.form, variables,
                                     'author_feedback_template',
                                     state_failure)

## clear request variables so form is cleared as well
REQUEST.set('message', None)
REQUEST.set('subject', None)

state.set(**state_values)
return state
