// The contents of this file are subject to the Mozilla Public License Version 1.1
// (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
// WARRANTY OF ANY KIND, either express or implied. See the License for the specific
// language governing rights and limitations under the License.
// 
// The Original Code is the Venice Web Communities System.
// 
// The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
// for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
// Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
// 
// Contributor(s): 

// Parameters to this script are passed in the request with a namespace of "/util/sysadmin/edit-email.js":
//   namespace = Namespace of the subject property and message text block
//   subject.property = Name of property where the subject text is stored
//   body.block = Name of block where message text is stored
//   caller = SERVLET URL of caller, so form can POST back to it
//   returnto = SERVLET URL of page to bounce back to after update completed
//   title = Title of the message being edited
//   varmap = A Map which contains the variable replacement definitions to display below the body text box (optional)

importClass(Packages.com.silverwrist.dynamo.Namespaces);
importPackage(Packages.com.silverwrist.dynamo.except);
importPackage(Packages.com.silverwrist.dynamo.iface);
importPackage(Packages.com.silverwrist.dynamo.security);
importPackage(Packages.com.silverwrist.dynamo.util);
importPackage(Packages.com.silverwrist.venice.content);
 
req = bsf.lookupBean("request");           // get request
rhelp = bsf.lookupBean("request_help");    // get request helper
user = vlib.getUser(req);

// Get the namespace and name parameters.
my_ns = "/util/sysadmin/edit-email.js";
namespace = req.getObject(my_ns,"namespace");
subject_property = req.getObject(my_ns,"subject.property");
body_block = req.getObject(my_ns,"body.block");
return_URL = req.getObject(my_ns,"returnto");
vlib.setOnError(req,return_URL);
vlib.setOnErrorType(req,"SERVLET");

// Make sure we are permitted to be here.
srm = cast.querySecurityReferenceMonitor(rhelp.getRequestObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,"srm"));
acl = srm.getGlobalAcl();
if (!(acl.testPermission(user,namespace,"set.property") && acl.testPermission(user,namespace,"set.block")))
  dynamo.scriptReturn(vlib.stdErrorBox(req,"Security Error",
                                       "You are not permitted to edit the system E-mail messages."));

// Get the global properties store.
globals = vcast.getGlobalPropertiesStore(req);
blocks = vcast.getGlobalBlocksStore(req);

error_message = null;
rc = null;
subject_text = "";
body_text = "";

if (rhelp.isVerb("GET"))
{ // load the initial values of the text fields
  subject_text = globals.getObject(namespace,subject_property);
  body_text = blocks.getObject(namespace,body_block);

} // end if
else
{ // did they click Cancel?
  if (rhelp.isImageButtonClicked("cancel"))
    rc = new Redirect("SERVLET",return_URL);
  else
  { // retrieve the form parameters
    subject_text = rhelp.getParameterString("subject");
    body_text = rhelp.getParameterString("body");

    if (stringutils.isEmpty(subject_text))
      error_message = "Subject text not specified.";
    else if (stringutils.isEmpty(body_text))
      error_message = "Body text not specified.";
    else if (body_text.length()>65535)
      error_message = "Body text is too long.";

    if (error_message==null)
    { // attempt to set the properties!
      try
      { // set the two properties
        globals.setObject(user,namespace,subject_property,subject_text.toString());
        blocks.setObject(user,namespace,body_block,body_text.toString());

        // all done - bounce back to return location
        rc = new Redirect("SERVLET",return_URL);

      } // end try
      catch (e)
      { // thrown an exception from the above process - what sort?
        etype = dynamo.exceptionType(e) + "";
        logger.error("Caught exception of type " + etype);
        if (etype.match(/DatabaseException/))
          rc = new ErrorBox("Database Error",e,"SERVLET",return_URL);
        else if (etype.match(/DynamoSecurityException/))
          rc = new ErrorBox("Security Error",e,"SERVLET",return_URL);
        else
          rc = new ErrorBox("Unknown Exception",e,"SERVLET",return_URL);

      } // end catch

    } // end if

  } // end else

} // end else

if (rc==null)
{ // generate the form as a Velocity object
  rc = new VelocityView("Edit E-Mail Message","sysadmin/edit-email.vm");
  rc.setParameter("msgtitle",req.getObject(my_ns,"title"));
  rc.setParameter("action",req.getObject(my_ns,"caller"));
  rc.setParameter("subject",subject_text);
  rc.setParameter("body",body_text);
  tmp = PropertyUtils.getPropertyNoErr(req,my_ns,"varmap");
  if (tmp!=null)
    rc.setParameter("varmap",tmp);
  if (error_message!=null)
    rc.setParameter("error_message",error_message);

} // end if

dynamo.scriptOutput(rc);
