// 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): 

importPackage(Packages.com.silverwrist.util);
importClass(Packages.com.silverwrist.dynamo.Namespaces);
importClass(Packages.com.silverwrist.dynamo.db.ImageStore);
importPackage(Packages.com.silverwrist.dynamo.iface);
importPackage(Packages.com.silverwrist.dynamo.util);
importClass(Packages.com.silverwrist.venice.VeniceNamespaces);
importPackage(Packages.com.silverwrist.venice.content);
importPackage(Packages.com.silverwrist.venice.iface);

req = bsf.lookupBean("request");
req_help = bsf.lookupBean("request_help");
user = vlib.getUser(req);
comm = vlib.getCommunity(req);

return_URL = "comm/admin/profile.js.vs?cc=" + comm.getCID();

// Make sure we can actually edit the profile logo.
acl = comm.getAcl();
if (!(acl.testPermission(user,VeniceNamespaces.COMMUNITY_PROFILE_NAMESPACE,"set.property")))
  dynamo.scriptReturn(vlib.stdErrorBox(req,"Security Error",
                                       "You are not permitted to modify this community's logo."));

if (req_help.isVerb("GET"))
{ // on GET, display the form
  view = new VelocityView("Set Community Logo","comm/community_logo.vm");
  view.setParameter("community",comm);
  dynamo.scriptReturn(view);

} // end if

// everything from this point on is a POST operation
if (req_help.isImageButtonClicked("cancel"))
  dynamo.scriptReturn(new Redirect("SERVLET",return_URL));

// get the image provider object
imgprov = cast.queryImageStore(req_help.getRequestObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,"images"));

// figure out which image ID we have
imgid = imgprov.findImageID(VeniceNamespaces.COMMUNITY_PROFILE_NAMESPACE,"community.logo",comm.getHostGroup());

selector = req_help.getParameterString("selector") + "";
if (selector=="none")
{ // take out the image property and the image
  if (PropertyUtils.hasProperty(comm,VeniceNamespaces.COMMUNITY_PROFILE_NAMESPACE,"url.logo"))
    comm.removeObject(user,VeniceNamespaces.COMMUNITY_PROFILE_NAMESPACE,"url.logo");
  if (imgid!=-1)
    imgprov.deleteImage(user,imgid);

} // end if
else if (selector=="set")
{ // set the image URL directly, delete the image
  s = req_help.getParameterString("url");
  if (stringutils.isEmpty(s))
    dynamo.scriptReturn(new ErrorBox(null,"No logo URL specified.",return_URL));
  comm.setObject(user,VeniceNamespaces.COMMUNITY_PROFILE_NAMESPACE,"url.logo",s);
  if (imgid!=-1)
    imgprov.deleteImage(user,imgid);

} // end else if
else if (selector=="upload")
{ // take the uploaded image, reformat it, and save it off, then set the image URL
  input_di = req_help.getDataItem("image");
  if (input_di==null)
    dynamo.scriptReturn(new ErrorBox(null,"No uploaded logo specified.",return_URL));
  mtype = input_di.getMimeType();
  if (!(mtype.startsWith("image/")))
    dynamo.scriptReturn(new ErrorBox(null,"Uploaded data item is not an image.",return_URL));
  globals = vcast.getGlobalPropertiesStore(req);
  width = cast.toInteger(globals.getObject(VeniceNamespaces.COMMUNITY_GLOBALS_NAMESPACE,"community.logo.width"));
  height = cast.toInteger(globals.getObject(VeniceNamespaces.COMMUNITY_GLOBALS_NAMESPACE,"community.logo.height"));
  di = imgprov.normalizeImage(input_di,width,height,"image/jpeg");
  if (imgid!=-1)
    imgprov.replaceImage(user,imgid,di);
  else
    imgid = imgprov.saveNewImage(VeniceNamespaces.COMMUNITY_PROFILE_NAMESPACE,"community.logo",comm.getHostGroup(),di);
  rewriter = cast.queryURLRewriter(req);
  comm.setObject(user,VeniceNamespaces.COMMUNITY_PROFILE_NAMESPACE,"url.logo",
                 rewriter.rewriteURL("IMAGEDATA",imgid + ""));

} // end else if
else  // return a "No Content" and just leave the dialog box up
  dynamo.scriptReturn(new NoContent());

vlib.forceReloadMenu(req);  // force the menu to be reloaded

// All done - bounce back to the Profile dialog
dynamo.scriptOutput(new Redirect("SERVLET",return_URL));
