NIHVIVO-1957 Pass placeholder image from template to ImageUploadController

This commit is contained in:
rjy7 2011-03-03 17:56:40 +00:00
parent 52856261ce
commit f0fe94bdd0
2 changed files with 61 additions and 37 deletions

View file

@ -49,9 +49,6 @@ public class ImageUploadController extends FreemarkerHttpServlet {
.getLog(ImageUploadController.class); .getLog(ImageUploadController.class);
private static final String ATTRIBUTE_REFERRING_PAGE = "ImageUploadController.referringPage"; private static final String ATTRIBUTE_REFERRING_PAGE = "ImageUploadController.referringPage";
public static final String DUMMY_THUMBNAIL_PERSON_URL = "/images/placeholders/person.thumbnail.jpg";
public static final String DUMMY_THUMBNAIL_NON_PERSON_URL = "/images/placeholders/non.person.thumbnail.jpg";
/** Limit file size to 6 megabytes. */ /** Limit file size to 6 megabytes. */
public static final int MAXIMUM_FILE_SIZE = 6 * 1024 * 1024; public static final int MAXIMUM_FILE_SIZE = 6 * 1024 * 1024;
@ -68,6 +65,11 @@ public class ImageUploadController extends FreemarkerHttpServlet {
/** The form field of the uploaded file; use as a key to the FileItem map. */ /** The form field of the uploaded file; use as a key to the FileItem map. */
public static final String PARAMETER_UPLOADED_FILE = "datafile"; 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. */ /** Here is the main image file. Hold on to it. */
public static final String ACTION_UPLOAD = "upload"; public static final String ACTION_UPLOAD = "upload";
@ -381,23 +383,18 @@ public class ImageUploadController extends FreemarkerHttpServlet {
*/ */
private TemplateResponseValues showAddImagePage(VitroRequest vreq, private TemplateResponseValues showAddImagePage(VitroRequest vreq,
Individual entity) { Individual entity) {
String placeholderUrl = (String) vreq.getParameter(PARAMETER_PLACEHOLDER_URL);
String formAction = (entity == null) ? "" : formAction(entity.getURI(), String formAction = (entity == null) ? "" : formAction(entity.getURI(),
ACTION_UPLOAD); ACTION_UPLOAD, placeholderUrl);
String cancelUrl = (entity == null) ? "" : exitPageUrl(vreq, String cancelUrl = (entity == null) ? "" : exitPageUrl(vreq,
entity.getURI()); entity.getURI());
TemplateResponseValues rv = new TemplateResponseValues(TEMPLATE_NEW); TemplateResponseValues rv = new TemplateResponseValues(TEMPLATE_NEW);
// rjy7 We should not be referencing particular ontology values here, and ideally
// the template would add the placeholder url to the edit link, since it already rv.put(BODY_THUMBNAIL_URL, placeholderUrl);
// knows which placeholder it's using. However, this requires a significantly more
// complex implementation, so keeping it simple for now.
boolean isPerson = (entity != null)
&& entity.isVClass("http://xmlns.com/foaf/0.1/Person");
String dummyThumbnailUrl = isPerson ? DUMMY_THUMBNAIL_PERSON_URL
: DUMMY_THUMBNAIL_NON_PERSON_URL;
rv.put(BODY_THUMBNAIL_URL, UrlBuilder.getUrl(dummyThumbnailUrl));
rv.put(BODY_FORM_ACTION, formAction); rv.put(BODY_FORM_ACTION, formAction);
rv.put(BODY_CANCEL_URL, cancelUrl); rv.put(BODY_CANCEL_URL, cancelUrl);
rv.put(BODY_TITLE, "Upload image" + forName(entity)); rv.put(BODY_TITLE, "Upload image" + forName(entity));
@ -417,11 +414,12 @@ public class ImageUploadController extends FreemarkerHttpServlet {
*/ */
private TemplateResponseValues showReplaceImagePage(VitroRequest vreq, private TemplateResponseValues showReplaceImagePage(VitroRequest vreq,
Individual entity, ImageInfo imageInfo) { Individual entity, ImageInfo imageInfo) {
String placeholderUrl = (String) vreq.getParameter(PARAMETER_PLACEHOLDER_URL);
TemplateResponseValues rv = new TemplateResponseValues(TEMPLATE_REPLACE); TemplateResponseValues rv = new TemplateResponseValues(TEMPLATE_REPLACE);
rv.put(BODY_THUMBNAIL_URL, UrlBuilder.getUrl(imageInfo.getThumbnail() rv.put(BODY_THUMBNAIL_URL, UrlBuilder.getUrl(imageInfo.getThumbnail()
.getBytestreamAliasUrl())); .getBytestreamAliasUrl()));
rv.put(BODY_DELETE_URL, formAction(entity.getURI(), ACTION_DELETE_EDIT)); rv.put(BODY_DELETE_URL, formAction(entity.getURI(), ACTION_DELETE_EDIT, placeholderUrl));
rv.put(BODY_FORM_ACTION, formAction(entity.getURI(), ACTION_UPLOAD)); rv.put(BODY_FORM_ACTION, formAction(entity.getURI(), ACTION_UPLOAD, placeholderUrl));
rv.put(BODY_CANCEL_URL, exitPageUrl(vreq, entity.getURI())); rv.put(BODY_CANCEL_URL, exitPageUrl(vreq, entity.getURI()));
rv.put(BODY_TITLE, "Replace image" + forName(entity)); rv.put(BODY_TITLE, "Replace image" + forName(entity));
return rv; return rv;
@ -444,11 +442,12 @@ public class ImageUploadController extends FreemarkerHttpServlet {
*/ */
private TemplateResponseValues showCropImagePage(VitroRequest vreq, private TemplateResponseValues showCropImagePage(VitroRequest vreq,
Individual entity, String imageUrl, Dimensions dimensions) { Individual entity, String imageUrl, Dimensions dimensions) {
String placeholderUrl = (String) vreq.getParameter(PARAMETER_PLACEHOLDER_URL);
TemplateResponseValues rv = new TemplateResponseValues(TEMPLATE_CROP); TemplateResponseValues rv = new TemplateResponseValues(TEMPLATE_CROP);
rv.put(BODY_MAIN_IMAGE_URL, UrlBuilder.getUrl(imageUrl)); rv.put(BODY_MAIN_IMAGE_URL, UrlBuilder.getUrl(imageUrl));
rv.put(BODY_MAIN_IMAGE_HEIGHT, dimensions.height); rv.put(BODY_MAIN_IMAGE_HEIGHT, dimensions.height);
rv.put(BODY_MAIN_IMAGE_WIDTH, dimensions.width); rv.put(BODY_MAIN_IMAGE_WIDTH, dimensions.width);
rv.put(BODY_FORM_ACTION, formAction(entity.getURI(), ACTION_SAVE)); rv.put(BODY_FORM_ACTION, formAction(entity.getURI(), ACTION_SAVE, placeholderUrl));
rv.put(BODY_CANCEL_URL, exitPageUrl(vreq, entity.getURI())); rv.put(BODY_CANCEL_URL, exitPageUrl(vreq, entity.getURI()));
rv.put(BODY_TITLE, "Crop Photo" + forName(entity)); rv.put(BODY_TITLE, "Crop Photo" + forName(entity));
return rv; return rv;
@ -493,9 +492,9 @@ public class ImageUploadController extends FreemarkerHttpServlet {
* back to this controller, along with the desired action and the Entity * back to this controller, along with the desired action and the Entity
* URI. * URI.
*/ */
private String formAction(String entityUri, String action) { private String formAction(String entityUri, String action, String placeholderUrl) {
UrlBuilder.ParamMap params = new UrlBuilder.ParamMap( UrlBuilder.ParamMap params = new UrlBuilder.ParamMap(
PARAMETER_ENTITY_URI, entityUri, PARAMETER_ACTION, action); PARAMETER_ENTITY_URI, entityUri, PARAMETER_ACTION, action, PARAMETER_PLACEHOLDER_URL, placeholderUrl);
return UrlBuilder.getPath(URL_HERE, params); return UrlBuilder.getPath(URL_HERE, params);
} }

View file

@ -35,12 +35,12 @@
<#macro objectProperty property editable template=property.template> <#macro objectProperty property editable template=property.template>
<#if property.collatedBySubclass> <#-- collated --> <#if property.collatedBySubclass> <#-- collated -->
<@p.collatedObjectPropertyList property editable template /> <@collatedObjectPropertyList property editable template />
<#else> <#-- uncollated --> <#else> <#-- uncollated -->
<#-- We pass property.statements and property.template even though we are also <#-- We pass property.statements and property.template even though we are also
passing property, because objecctPropertyList can get other values, and passing property, because objecctPropertyList can get other values, and
doesn't necessarily use property.statements and property.template --> doesn't necessarily use property.statements and property.template -->
<@p.objectPropertyList property editable property.statements template /> <@objectPropertyList property editable property.statements template />
</#if> </#if>
</#macro> </#macro>
@ -67,7 +67,7 @@ Assumes property is non-null. -->
<#local localName = property.localName> <#local localName = property.localName>
<h2 id="${localName}">${property.name?capitalize} <@addLink property editable /> <@verboseDisplay property /></h2> <h2 id="${localName}">${property.name?capitalize} <@addLink property editable /> <@verboseDisplay property /></h2>
<ul id="individual-${localName}" role="list"> <ul id="individual-${localName}" role="list">
<@p.objectProperty property editable /> <@objectProperty property editable />
</ul> </ul>
</#macro> </#macro>
@ -80,22 +80,35 @@ Assumes property is non-null. -->
<#-- Some properties usually display without a label. But if there's an add link, <#-- 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 we need to also show the property label. If no label is specified, the property
name will be used as the label. --> name will be used as the label. -->
<#macro addLinkWithLabel property editable label="${property.name?capitalize}"> <#macro addLinkWithLabel property editable label="${property.name?capitalize}" extraParams="">
<#local addLink><@addLink property editable label /></#local> <#local addLink><@addLink property editable label extraParams /></#local>
<#if addLink?has_content> <#if addLink?has_content>
<h2 id="${property.localName}">${label} ${addLink} <@verboseDisplay property /></h2> <h2 id="${property.localName}">${label} ${addLink} <@verboseDisplay property /></h2>
</#if> </#if>
</#macro> </#macro>
<#macro addLink property editable label="${property.name}"> <#macro addLink property editable label="${property.name}" extraParams="">
<#if editable> <#if editable>
<#local url = property.addUrl> <#local url = property.addUrl>
<#if url?has_content> <#if url?has_content>
<a class="add-${property.localName}" href="${url}" title="Add new ${label?lower_case} entry"><img class="add-individual" src="${urls.images}/individual/addIcon.gif" alt="add" /></a> <@showAddLink property.localName label addParamsToEditUrl(url, extraParams) />
</#if> </#if>
</#if> </#if>
</#macro> </#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>
<#macro propertyLabel property label="${property.name?capitalize}"> <#macro propertyLabel property label="${property.name?capitalize}">
<h2 id="${property.localName}">${label} <@verboseDisplay property /></h2> <h2 id="${property.localName}">${label} <@verboseDisplay property /></h2>
</#macro> </#macro>
@ -107,27 +120,35 @@ name will be used as the label. -->
</li> </li>
</#macro> </#macro>
<#macro editingLinks propertyLocalName statement editable> <#macro editingLinks propertyLocalName statement editable extraParams="">
<#if editable> <#if editable>
<@editLink propertyLocalName statement /> <@editLink propertyLocalName statement extraParams />
<@deleteLink propertyLocalName statement /> <@deleteLink propertyLocalName statement extraParams />
</#if> </#if>
</#macro> </#macro>
<#macro editLink propertyLocalName statement> <#macro editLink propertyLocalName statement extraParams="">
<#local url = statement.editUrl> <#local url = statement.editUrl>
<#if url?has_content> <#if url?has_content>
<a class="edit-${propertyLocalName}" href="${url}" title="edit this entry"><img class="edit-individual" src="${urls.images}/individual/editIcon.gif" alt="edit" /></a> <@showEditLink propertyLocalName addParamsToEditUrl(url, extraParams) />
</#if> </#if>
</#macro> </#macro>
<#macro deleteLink propertyLocalName statement> <#macro showEditLink propertyLocalName url>
<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="">
<#local url = statement.deleteUrl> <#local url = statement.deleteUrl>
<#if url?has_content> <#if url?has_content>
<a class="delete-${propertyLocalName}" href="${url}" title="delete this entry"><img class="delete-individual" src="${urls.images}/individual/deleteIcon.gif" alt="delete" /></a> <@showDeleteLink propertyLocalName addParamsToEditUrl(url, extraParams) />
</#if> </#if>
</#macro> </#macro>
<#macro showDeleteLink propertyLocalName url>
<a class="delete-${propertyLocalName}" href="${url}" title="delete this entry"><img class="delete-individual" src="${urls.images}/individual/deleteIcon.gif" alt="delete" /></a>
</#macro>
<#macro verboseDisplay property> <#macro verboseDisplay property>
<#local verboseDisplay = property.verboseDisplay!> <#local verboseDisplay = property.verboseDisplay!>
<#if verboseDisplay?has_content> <#if verboseDisplay?has_content>
@ -188,14 +209,18 @@ name will be used as the label. -->
--> -->
<#macro image individual propertyGroups namespaces editable showPlaceholder="never" placeholder=""> <#macro image individual propertyGroups namespaces editable showPlaceholder="never" placeholder="">
<#local mainImage = propertyGroups.getPropertyAndRemoveFromList("${namespaces.vitroPublic}mainImage")!> <#local mainImage = propertyGroups.getPropertyAndRemoveFromList("${namespaces.vitroPublic}mainImage")!>
<#local extraParams = "">
<#if placeholder?has_content>
<#local extraParams = { "placeholder" : placeholder } >
</#if>
<#local thumbUrl = individual.thumbUrl!> <#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). <#-- 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 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> <#if (mainImage.statements)?has_content && thumbUrl?has_content>
<a href="${individual.imageUrl}"><img class="individual-photo" src="${thumbUrl}" title="click to view larger image" alt="${individual.name}" width="160" /></a> <a href="${individual.imageUrl}"><img class="individual-photo" src="${thumbUrl}" title="click to view larger image" alt="${individual.name}" width="160" /></a>
<@p.editingLinks "${mainImage.localName}" mainImage.first editable /> <@editingLinks "${mainImage.localName}" mainImage.first editable extraParams />
<#else> <#else>
<#local imageLabel><@p.addLinkWithLabel mainImage editable "Photo" /></#local> <#local imageLabel><@addLinkWithLabel mainImage editable "Photo" extraParams /></#local>
${imageLabel} ${imageLabel}
<#if placeholder?has_content> <#if placeholder?has_content>
<#if showPlaceholder == "always" || (showPlaceholder="with_add_link" && imageLabel?has_content)> <#if showPlaceholder == "always" || (showPlaceholder="with_add_link" && imageLabel?has_content)>
@ -209,5 +234,5 @@ name will be used as the label. -->
<#macro label individual editable> <#macro label individual editable>
<#local label = individual.nameStatement> <#local label = individual.nameStatement>
${label.value} ${label.value}
<@p.editingLinks "label" label editable /> <@editingLinks "label" label editable />
</#macro> </#macro>