NIHVIVO-3704 Create a Freemarker method called placeholderImageUrl(individualUri). Use that instead of hard-coding the placeholder image URLs in templates, or passing the placeholder image along from one page to the next via the controller. Remove the "extraParams" parameter to several Freemarker macros, since this is the only purpose it was used for.
This commit is contained in:
parent
af8fc9e0a9
commit
2c585755d0
6 changed files with 97 additions and 62 deletions
|
@ -19,6 +19,10 @@ import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
|||
import edu.cornell.mannlib.vitro.webapp.config.RevisionInfoBean;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Route;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.EditConfigurationConstants;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.methods.IndividualLocalNameMethod;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.methods.IndividualPlaceholderImageUrlMethod;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.methods.IndividualProfileUrlMethod;
|
||||
import freemarker.cache.ClassTemplateLoader;
|
||||
import freemarker.cache.FileTemplateLoader;
|
||||
import freemarker.cache.MultiTemplateLoader;
|
||||
|
@ -28,7 +32,6 @@ import freemarker.template.Configuration;
|
|||
import freemarker.template.DefaultObjectWrapper;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateModelException;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.EditConfigurationConstants;
|
||||
|
||||
public class FreemarkerConfiguration extends Configuration {
|
||||
|
||||
|
@ -160,8 +163,9 @@ public class FreemarkerConfiguration extends Configuration {
|
|||
|
||||
public static Map<String, Object> getMethods() {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("profileUrl", new edu.cornell.mannlib.vitro.webapp.web.methods.IndividualProfileUrlMethod());
|
||||
map.put("localName", new edu.cornell.mannlib.vitro.webapp.web.methods.IndividualLocalNameMethod());
|
||||
map.put("profileUrl", new IndividualProfileUrlMethod());
|
||||
map.put("localName", new IndividualLocalNameMethod());
|
||||
map.put("placeholderImageUrl", new IndividualPlaceholderImageUrlMethod());
|
||||
return map;
|
||||
}
|
||||
|
||||
|
|
|
@ -36,6 +36,7 @@ import edu.cornell.mannlib.vitro.webapp.filestorage.backend.FileStorageSetup;
|
|||
import edu.cornell.mannlib.vitro.webapp.filestorage.model.FileInfo;
|
||||
import edu.cornell.mannlib.vitro.webapp.filestorage.model.ImageInfo;
|
||||
import edu.cornell.mannlib.vitro.webapp.filestorage.uploadrequest.FileUploadServletRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.ImageUtil;
|
||||
|
||||
/**
|
||||
* Handle adding, replacing or deleting the main image on an Individual.
|
||||
|
@ -63,12 +64,6 @@ public class ImageUploadController extends FreemarkerHttpServlet {
|
|||
/** The form field of the uploaded file; use as a key to the FileItem map. */
|
||||
public static final String PARAMETER_UPLOADED_FILE = "datafile";
|
||||
|
||||
/**
|
||||
* The image to use as a placeholder when the individual has no image.
|
||||
* Determined by the template.
|
||||
*/
|
||||
public static final String PARAMETER_PLACEHOLDER_URL = "placeholder";
|
||||
|
||||
/** Here is the main image file. Hold on to it. */
|
||||
public static final String ACTION_UPLOAD = "upload";
|
||||
|
||||
|
@ -141,7 +136,8 @@ public class ImageUploadController extends FreemarkerHttpServlet {
|
|||
String imageUri = entity.getMainImageUri();
|
||||
|
||||
RequestedAction ra;
|
||||
if (ACTION_DELETE.equals(action) || ACTION_DELETE_EDIT.equals(action)) {
|
||||
if (ACTION_DELETE.equals(action)
|
||||
|| ACTION_DELETE_EDIT.equals(action)) {
|
||||
ra = new DropObjectPropStmt(entity.getURI(),
|
||||
VitroVocabulary.IND_MAIN_IMAGE, imageUri);
|
||||
} else if (imageUri != null) {
|
||||
|
@ -157,7 +153,7 @@ public class ImageUploadController extends FreemarkerHttpServlet {
|
|||
return Actions.UNAUTHORIZED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Parse the multi-part request, process the request, and produce the
|
||||
|
@ -405,12 +401,12 @@ public class ImageUploadController extends FreemarkerHttpServlet {
|
|||
private TemplateResponseValues showAddImagePage(VitroRequest vreq,
|
||||
Individual entity) {
|
||||
|
||||
String placeholderUrl = vreq.getParameter(PARAMETER_PLACEHOLDER_URL);
|
||||
|
||||
String formAction = (entity == null) ? "" : formAction(entity.getURI(),
|
||||
ACTION_UPLOAD, placeholderUrl);
|
||||
ACTION_UPLOAD);
|
||||
String cancelUrl = (entity == null) ? "" : exitPageUrl(vreq,
|
||||
entity.getURI());
|
||||
String placeholderUrl = (entity == null) ? "" : UrlBuilder.getUrl(ImageUtil
|
||||
.getPlaceholderImagePathForIndividual(vreq, entity.getURI()));
|
||||
|
||||
TemplateResponseValues rv = new TemplateResponseValues(TEMPLATE_NEW);
|
||||
|
||||
|
@ -437,12 +433,11 @@ public class ImageUploadController extends FreemarkerHttpServlet {
|
|||
*/
|
||||
private TemplateResponseValues showReplaceImagePage(VitroRequest vreq,
|
||||
Individual entity, ImageInfo imageInfo) {
|
||||
String placeholderUrl = vreq.getParameter(PARAMETER_PLACEHOLDER_URL);
|
||||
TemplateResponseValues rv = new TemplateResponseValues(TEMPLATE_REPLACE);
|
||||
rv.put(BODY_THUMBNAIL_URL, UrlBuilder.getUrl(imageInfo.getThumbnail()
|
||||
.getBytestreamAliasUrl()));
|
||||
rv.put(BODY_DELETE_URL, formAction(entity.getURI(), ACTION_DELETE_EDIT, placeholderUrl));
|
||||
rv.put(BODY_FORM_ACTION, formAction(entity.getURI(), ACTION_UPLOAD, placeholderUrl));
|
||||
rv.put(BODY_DELETE_URL, formAction(entity.getURI(), ACTION_DELETE_EDIT));
|
||||
rv.put(BODY_FORM_ACTION, formAction(entity.getURI(), ACTION_UPLOAD));
|
||||
rv.put(BODY_CANCEL_URL, exitPageUrl(vreq, entity.getURI()));
|
||||
rv.put(BODY_TITLE, "Replace image" + forName(entity));
|
||||
rv.put(BODY_MAX_FILE_SIZE, MAXIMUM_FILE_SIZE / (1024 * 1024));
|
||||
|
@ -468,12 +463,11 @@ public class ImageUploadController extends FreemarkerHttpServlet {
|
|||
*/
|
||||
private TemplateResponseValues showCropImagePage(VitroRequest vreq,
|
||||
Individual entity, String imageUrl, Dimensions dimensions) {
|
||||
String placeholderUrl = vreq.getParameter(PARAMETER_PLACEHOLDER_URL);
|
||||
TemplateResponseValues rv = new TemplateResponseValues(TEMPLATE_CROP);
|
||||
rv.put(BODY_MAIN_IMAGE_URL, UrlBuilder.getUrl(imageUrl));
|
||||
rv.put(BODY_MAIN_IMAGE_HEIGHT, dimensions.height);
|
||||
rv.put(BODY_MAIN_IMAGE_WIDTH, dimensions.width);
|
||||
rv.put(BODY_FORM_ACTION, formAction(entity.getURI(), ACTION_SAVE, placeholderUrl));
|
||||
rv.put(BODY_FORM_ACTION, formAction(entity.getURI(), ACTION_SAVE));
|
||||
rv.put(BODY_CANCEL_URL, exitPageUrl(vreq, entity.getURI()));
|
||||
rv.put(BODY_TITLE, "Crop Photo" + forName(entity));
|
||||
return rv;
|
||||
|
@ -518,11 +512,9 @@ public class ImageUploadController extends FreemarkerHttpServlet {
|
|||
* back to this controller, along with the desired action and the Entity
|
||||
* URI.
|
||||
*/
|
||||
private String formAction(String entityUri, String action,
|
||||
String placeholderUrl) {
|
||||
ParamMap params = new ParamMap(
|
||||
PARAMETER_ENTITY_URI, entityUri, PARAMETER_ACTION, action,
|
||||
PARAMETER_PLACEHOLDER_URL, placeholderUrl);
|
||||
private String formAction(String entityUri, String action) {
|
||||
ParamMap params = new ParamMap(PARAMETER_ENTITY_URI, entityUri,
|
||||
PARAMETER_ACTION, action);
|
||||
return UrlBuilder.getPath(URL_HERE, params);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.web.methods;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.ImageUtil;
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.template.TemplateModelException;
|
||||
|
||||
/**
|
||||
* Get a URL for the placeholder image for this Individual, based on the VClass
|
||||
* that the Individual belongs to.
|
||||
*/
|
||||
public class IndividualPlaceholderImageUrlMethod extends BaseTemplateMethodModel {
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public String exec(List args) throws TemplateModelException {
|
||||
if (args.size() != 1) {
|
||||
throw new TemplateModelException("Wrong number of arguments");
|
||||
}
|
||||
|
||||
String uri = (String) args.get(0);
|
||||
Environment env = Environment.getCurrentEnvironment();
|
||||
HttpServletRequest request = (HttpServletRequest) env.getCustomAttribute("request");
|
||||
VitroRequest vreq = new VitroRequest(request);
|
||||
String imageUrl = ImageUtil.getPlaceholderImagePathForIndividual(vreq, uri);
|
||||
return UrlBuilder.getUrl(imageUrl);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> help(String name) {
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
|
||||
map.put("return value", "The URL of the placeholder image for this individual, " +
|
||||
"based on the VClasses that the individual belongs to.");
|
||||
|
||||
List<String>params = new ArrayList<String>();
|
||||
params.add("Uri of individual");
|
||||
map.put("parameters", params);
|
||||
|
||||
List<String> examples = new ArrayList<String>();
|
||||
examples.add(name + "(individual.uri)");
|
||||
map.put("examples", examples);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
|
@ -76,15 +76,11 @@ public abstract class BaseIndividualTemplateModel extends BaseTemplateModel {
|
|||
return UrlBuilder.getIndividualProfileUrl(individual, vreq);
|
||||
}
|
||||
|
||||
// For image, we use the default list view and Individual methods to reconstruct the image
|
||||
// url from various triples. A custom list view would require that logic to be duplicated here.
|
||||
public String getImageUrl() {
|
||||
String imageUrl = individual.getImageUrl();
|
||||
return imageUrl == null ? null : getUrl(imageUrl);
|
||||
}
|
||||
|
||||
// For image, we use the default list view and Individual methods to reconstruct the image
|
||||
// url from various triples. A custom list view would require that logic to be duplicated here.
|
||||
public String getThumbUrl() {
|
||||
String thumbUrl = individual.getThumbUrl();
|
||||
return thumbUrl == null ? null : getUrl(thumbUrl);
|
||||
|
|
|
@ -9,8 +9,7 @@
|
|||
propertyGroups=propertyGroups
|
||||
namespaces=namespaces
|
||||
editable=editable
|
||||
showPlaceholder="with_add_link"
|
||||
placeholder="${urls.images}/placeholders/thumbnail.jpg" />
|
||||
showPlaceholder="with_add_link" />
|
||||
</#assign>
|
||||
|
||||
<#if ( individualImage?contains('<img class="individual-photo"') )>
|
||||
|
|
|
@ -89,8 +89,8 @@ Assumes property is non-null. -->
|
|||
<#-- Some properties usually display without a label. But if there's an add link,
|
||||
we need to also show the property label. If no label is specified, the property
|
||||
name will be used as the label. -->
|
||||
<#macro addLinkWithLabel property editable label="${property.name?capitalize}" extraParams="">
|
||||
<#local addLink><@addLink property editable label extraParams /></#local>
|
||||
<#macro addLinkWithLabel property editable label="${property.name?capitalize}">
|
||||
<#local addLink><@addLink property editable label /></#local>
|
||||
<#local verboseDisplay><@verboseDisplay property /></#local>
|
||||
<#-- Changed to display the label when user is in edit mode, even if there's no add link (due to
|
||||
displayLimitAnnot, for example). Otherwise the display looks odd, since neighboring
|
||||
|
@ -104,24 +104,15 @@ name will be used as the label. -->
|
|||
</#if>
|
||||
</#macro>
|
||||
|
||||
<#macro addLink property editable label="${property.name}" extraParams="">
|
||||
<#macro addLink property editable label="${property.name}">
|
||||
<#if editable>
|
||||
<#local url = property.addUrl>
|
||||
<#if url?has_content>
|
||||
<@showAddLink property.localName label addParamsToEditUrl(url, extraParams) />
|
||||
<@showAddLink property.localName label url />
|
||||
</#if>
|
||||
</#if>
|
||||
</#macro>
|
||||
|
||||
<#function addParamsToEditUrl url extraParams="">
|
||||
<#if extraParams?is_hash_ex>
|
||||
<#list extraParams?keys as key>
|
||||
<#local url = "${url}&${key}=${extraParams[key]?url}">
|
||||
</#list>
|
||||
</#if>
|
||||
<#return url>
|
||||
</#function>
|
||||
|
||||
<#macro showAddLink propertyLocalName label url>
|
||||
<a class="add-${propertyLocalName}" href="${url}" title="Add new ${label?lower_case} entry"><img class="add-individual" src="${urls.images}/individual/addIcon.gif" alt="add" /></a>
|
||||
</#macro>
|
||||
|
@ -138,18 +129,18 @@ name will be used as the label. -->
|
|||
</li>
|
||||
</#macro>
|
||||
|
||||
<#macro editingLinks propertyLocalName statement editable extraParams="">
|
||||
<#macro editingLinks propertyLocalName statement editable>
|
||||
<#if editable>
|
||||
<@editLink propertyLocalName statement extraParams />
|
||||
<@deleteLink propertyLocalName statement extraParams />
|
||||
<@editLink propertyLocalName statement />
|
||||
<@deleteLink propertyLocalName statement />
|
||||
|
||||
</#if>
|
||||
</#macro>
|
||||
|
||||
<#macro editLink propertyLocalName statement extraParams="">
|
||||
<#macro editLink propertyLocalName statement>
|
||||
<#local url = statement.editUrl>
|
||||
<#if url?has_content>
|
||||
<@showEditLink propertyLocalName addParamsToEditUrl(url, extraParams) />
|
||||
<@showEditLink propertyLocalName url />
|
||||
</#if>
|
||||
</#macro>
|
||||
|
||||
|
@ -157,10 +148,10 @@ name will be used as the label. -->
|
|||
<a class="edit-${propertyLocalName}" href="${url}" title="edit this entry"><img class="edit-individual" src="${urls.images}/individual/editIcon.gif" alt="edit" /></a>
|
||||
</#macro>
|
||||
|
||||
<#macro deleteLink propertyLocalName statement extraParams="">
|
||||
<#macro deleteLink propertyLocalName statement>
|
||||
<#local url = statement.deleteUrl>
|
||||
<#if url?has_content>
|
||||
<@showDeleteLink propertyLocalName addParamsToEditUrl(url, extraParams) />
|
||||
<@showDeleteLink propertyLocalName url />
|
||||
</#if>
|
||||
</#macro>
|
||||
|
||||
|
@ -191,25 +182,21 @@ name will be used as the label. -->
|
|||
|
||||
Note that this macro has a side-effect in the call to propertyGroups.pullProperty().
|
||||
-->
|
||||
<#macro image individual propertyGroups namespaces editable showPlaceholder="never" placeholder="">
|
||||
<#macro image individual propertyGroups namespaces editable showPlaceholder="never">
|
||||
<#local mainImage = propertyGroups.pullProperty("${namespaces.vitroPublic}mainImage")!>
|
||||
<#local extraParams = "">
|
||||
<#if placeholder?has_content>
|
||||
<#local extraParams = { "placeholder" : placeholder } >
|
||||
</#if>
|
||||
<#local thumbUrl = individual.thumbUrl!>
|
||||
<#-- Don't assume that if the mainImage property is populated, there is a thumbnail image (though that is the general case).
|
||||
If there's a mainImage statement but no thumbnail image, treat it as if there is no image. -->
|
||||
<#if (mainImage.statements)?has_content && thumbUrl?has_content>
|
||||
<a href="${individual.imageUrl}" title="individual photo"><img class="individual-photo" src="${thumbUrl}" title="click to view larger image" alt="${individual.name}" width="160" /></a>
|
||||
<@editingLinks "${mainImage.localName}" mainImage.first() editable extraParams />
|
||||
<a href="${individual.imageUrl}" title="individual photo">
|
||||
<img class="individual-photo" src="${thumbUrl}" title="click to view larger image" alt="${individual.name}" width="160" />
|
||||
</a>
|
||||
<@editingLinks "${mainImage.localName}" mainImage.first() editable />
|
||||
<#else>
|
||||
<#local imageLabel><@addLinkWithLabel mainImage editable "Photo" extraParams /></#local>
|
||||
<#local imageLabel><@addLinkWithLabel mainImage editable "Photo" /></#local>
|
||||
${imageLabel}
|
||||
<#if placeholder?has_content>
|
||||
<#if showPlaceholder == "always" || (showPlaceholder="with_add_link" && imageLabel?has_content)>
|
||||
<img class="individual-photo" src="${placeholder}" title = "no image" alt="placeholder image" width="160" />
|
||||
</#if>
|
||||
<#if showPlaceholder == "always" || (showPlaceholder="with_add_link" && imageLabel?has_content)>
|
||||
<img class="individual-photo" src="${placeholderImageUrl(individual.uri)}" title = "no image" alt="placeholder image" width="160" />
|
||||
</#if>
|
||||
</#if>
|
||||
</#macro>
|
||||
|
|
Loading…
Add table
Reference in a new issue