/** * This file is dual-licensed under both the Magnolia * Network Agreement and the Common Public Attribution License. * You may elect to use one or the other of these licenses as * indicated below. * * The contents of this file are subject to the Common Public * Attribution License Version 1.0 (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.magnolia-cms.com/cpal. * The License is based on the Mozilla Public License Version 1.1 * but Sections 14 and 15 have been added to cover use of software * over a computer network and provide for limited attribution for * the Original Developer. In addition, Exhibit A has been modified * to be consistent with Exhibit B. * * 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 Magnolia Templating Kit. * The Original Developer is the Initial Developer. * The Initial Developer of the Original Code is Magnolia International Ltd. * All portions of the code written by Initial Developer are * Copyright (c) 2008-2010 the Initial Developer. All Rights Reserved. * * Contributor(s): * * * * * Alternatively, the contents of this file may be used under the * terms of the Magnolia Network Agreement license (the MNA), in * which case the provisions of the MNA are applicable instead of * those above. As a licensee of Magnolia, you have received a signed * copy of the terms and conditions of the MNA. * * If you wish to allow use of your version of this file only under * the terms of the MNA and not to allow others to use your version * of this file under the CPAL, indicate your decision by deleting * the provisions above and replace them with the notice and other * provisions required by the MNA. If you do not delete the provisions * above, a recipient may use your version of this file under either * the CPAL or the MNA (if he is a Magnolia Enterprise Edition licensee). * */ package info.magnolia.module.templatingkit.templates; import javax.jcr.RepositoryException; import java.util.*; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.magnolia.cms.beans.config.ContentRepository; import info.magnolia.cms.core.Content; import info.magnolia.cms.i18n.I18nContentSupportFactory; import info.magnolia.cms.util.NodeDataUtil; import info.magnolia.module.templating.MagnoliaTemplatingUtilities; import info.magnolia.module.templating.RenderingModel; import info.magnolia.module.templatingkit.badge.BadgeSupport; import info.magnolia.module.templatingkit.categorization.CategorizationSupport; import info.magnolia.module.templatingkit.navigation.LinkImpl; import info.magnolia.module.templatingkit.navigation.SiteNavigationModel; import info.magnolia.module.templatingkit.paragraphs.ImageModel; import info.magnolia.module.templatingkit.paragraphs.TextImageModel; import info.magnolia.module.templatingkit.sites.Site; import info.magnolia.module.templatingkit.templates.ExtrasAreaModel; import info.magnolia.module.templatingkit.templates.InheritedContentModel; import info.magnolia.module.templatingkit.templates.STKTemplate; import info.magnolia.module.templatingkit.templates.category.TemplateCategory; import info.magnolia.module.templatingkit.templates.category.TemplateCategoryUtil; import info.magnolia.module.templatingkit.util.STKUtil; /** * @author pbracher * @version $Id: MainTemplateModel.java 19969 2008-11-17 16:37:48Z pbracher $ * */ public class STKTemplateModel extends RenderingModelImpl { private static Logger log = LoggerFactory.getLogger(STKTemplateModel.class); /** * Will be set by the stk renderer. */ private Site site; public STKTemplateModel(Content content, RD definition, RenderingModel parent) { super(content, definition, parent); } public String getBodyClass() { return getSite().getTheme().getBodyClassResolver().resolveBodyClass(this); } public String getLanguage(){ return I18nContentSupportFactory.getI18nSupport().getLocale().toString(); } public SiteNavigationModel getNavigation() { return new SiteNavigationModel(getDefinition().getNavigation(), getSiteRoot(), content); } public Collection getBreadcrumb() throws RepositoryException { List items = new ArrayList(); Content root = getSiteRoot(); Content current = content; while(current.getLevel() >= root.getLevel()) { items.add(new LinkImpl(current)); if(current.getLevel() == 0){ break; } current= current.getParent(); } Collections.reverse(items); return items; } public Content getSiteRoot() { try { Content root = TemplateCategoryUtil.findParentWithTemplateCategory(content, TemplateCategory.HOME); if(root == null){ return content.getAncestor(0); } return root; } catch (RepositoryException e) { throw new RuntimeException("Can't access site root.", e); } } public InheritedContentModel getFooter() throws RepositoryException { return STKUtil.inheritContent(content, "footer"); } public InheritedContentModel getMetaNavigation() throws RepositoryException { return STKUtil.inheritContent(content, "metaNavigation"); } public ExtrasAreaModel getExtras() throws RepositoryException{ return new ExtrasAreaModel(this); } public Collection getPromos() throws RepositoryException{ return STKUtil.collectContentByUsingInheritance(content, "promos"); } public String getLogoImageLink() throws RepositoryException { final Content root = getSiteRoot(); return STKUtil.getAssetLink(root, "logoImg"); } public String getPrintLogoImageLink() throws RepositoryException { final Content root = getSiteRoot(); return STKUtil.getAssetLink(root, "printLogoImg"); } public String getSectionTitle() throws RepositoryException { return NodeDataUtil.inheritString(content, "sectionTitle"); } /** * @deprecated since 1.2 Use {@link #getSectionText()} instead */ public String getSectionAbstract()throws RepositoryException { return getSectionText(); } public String getSectionText()throws RepositoryException { return NodeDataUtil.inheritString(content, "sectionText"); } protected Content resolveSectionPage() throws RepositoryException { return STKUtil.wrap(TemplateCategoryUtil.findParentWithTemplateCategory(content, TemplateCategory.SECTION)); } public String getHomeName() throws RepositoryException { return getSiteRoot().getName(); } public String getHomeTitle() throws RepositoryException { String title = getSiteRoot().getNodeData("title").getString(); return StringUtils.defaultIfEmpty(title, null); } public String getSiteTitle() throws RepositoryException { String siteTitle = getSiteRoot().getNodeData("siteTitle").getString(); return StringUtils.defaultIfEmpty(siteTitle, null); } public String getHomeLink() throws RepositoryException { return MagnoliaTemplatingUtilities.getInstance().createLink(getSiteRoot()); } public Collection getContentNavigation() throws RepositoryException { int maxNavLevel = this.getNavigation().getHorizontalLevel() + this.getNavigation().getVerticalLevel() + getSiteRoot().getLevel(); Collection children = content.getChildren(); Iterator childrenIterator = children.iterator(); if(content.getLevel() >= maxNavLevel && children.size() > 0) { List items = new ArrayList(); while(childrenIterator.hasNext()) { Content current = (Content) childrenIterator.next(); items.add(new LinkImpl(current)); } return items; } return null; } public String getSearchPageLink() throws RepositoryException { String searchResultPageUUID = getSiteRoot().getNodeData("searchUUID").getString(); if(StringUtils.isNotEmpty(searchResultPageUUID)) { return MagnoliaTemplatingUtilities.getInstance().createLink(ContentRepository.WEBSITE, searchResultPageUUID ); } else { log.warn("No result page defined in template home properties"); return null; } } public String getExternalLink(){ return STKUtil.getExternalLink(content, "link"); } public String getExternalLinkTitle(){ return STKUtil.getExternalLinkTitle(content, "link", "linkTitle"); } public final String getBadge(){ return BadgeSupport.getInstance().renderBadge(); } public Site getSite() { return this.site; } public void setSite(Site site) { this.site = site; } /** * just for ee users */ public List getCategories() { return CategorizationSupport.Factory.getInstance().getCategories(this.content); } /** * just for ee users */ public String getCategoryLink(String categoryName) { return CategorizationSupport.Factory.getInstance().getCategoryLink(this.content, categoryName); } /** * Gets an image for areas like page-intro */ public ImageModel getImageModel() { // we reuse the model of the text image paragraph as the same classes and names are used return new TextImageModel( content, definition, this); } }