001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.configuration.preferences;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.OpenCms;
032import org.opencms.site.CmsSite;
033import org.opencms.workplace.CmsWorkplace;
034import org.opencms.xml.content.CmsXmlContentProperty;
035
036import java.util.Iterator;
037import java.util.List;
038import java.util.Locale;
039
040/**
041 * Preference for the start site.<p>
042 */
043public class CmsSitePreference extends CmsBuiltinPreference {
044
045    /** The nice name. */
046    private static final String NICE_NAME = "%(key."
047        + org.opencms.workplace.commons.Messages.GUI_PREF_STARTUP_SITE_0
048        + ")";
049
050    /**
051     * Creates a new instance.<p>
052     *
053     * @param name the preference name
054     */
055    public CmsSitePreference(String name) {
056
057        super(name);
058        m_basic = true;
059    }
060
061    /**
062     * Gets the options for the site selector.<p>
063     *
064     * @param cms the CMS context
065     * @param locale the locale for the select options
066     *
067     * @return the options for the site selector
068     */
069    public static String getSiteSelectOptionsStatic(CmsObject cms, Locale locale) {
070
071        List<CmsSite> sites = OpenCms.getSiteManager().getAvailableSites(
072            cms,
073            true,
074            false,
075            cms.getRequestContext().getOuFqn());
076
077        StringBuffer resultBuffer = new StringBuffer();
078        Iterator<CmsSite> i = sites.iterator();
079        int counter = 0;
080        while (i.hasNext()) {
081            CmsSite site = i.next();
082            String siteRoot = site.getSiteRoot();
083            if (!siteRoot.endsWith("/")) {
084                siteRoot += "/";
085            }
086            if (counter != 0) {
087                resultBuffer.append("|");
088            }
089            resultBuffer.append(siteRoot).append(":").append(
090                CmsWorkplace.substituteSiteTitleStatic(site.getTitle(), locale));
091            counter++;
092        }
093
094        if (sites.size() < 1) {
095            // no site found, assure that at least the current site is shown in the selector
096            String siteRoot = cms.getRequestContext().getSiteRoot();
097            CmsSite site = OpenCms.getSiteManager().getSiteForSiteRoot(siteRoot);
098            if (!siteRoot.endsWith("/")) {
099                siteRoot += "/";
100            }
101            String title = "";
102            if (site != null) {
103                title = site.getTitle();
104            }
105            resultBuffer.append(siteRoot).append(":").append(title);
106        }
107        return resultBuffer.toString();
108    }
109
110    /**
111     * @see org.opencms.configuration.preferences.CmsBuiltinPreference#getPropertyDefinition(org.opencms.file.CmsObject)
112     */
113    @Override
114    public CmsXmlContentProperty getPropertyDefinition() {
115
116        CmsXmlContentProperty prop = new CmsXmlContentProperty(
117            getName(), //name
118            "string", //type
119            null, //widget
120            null, //widgetconfig
121            null, //regex
122            null, //ruletype
123            null, //default
124            NICE_NAME, //nicename
125            null, //description
126            null, //error
127            null//preferfolder
128        );
129        return prop;
130    }
131
132    /**
133     * @see org.opencms.configuration.preferences.CmsBuiltinPreference#getPropertyDefinition(org.opencms.file.CmsObject)
134     */
135    @Override
136    public CmsXmlContentProperty getPropertyDefinition(CmsObject cms) {
137
138        Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
139        String options = getSiteSelectOptionsStatic(cms, locale);
140        CmsXmlContentProperty prop = new CmsXmlContentProperty(
141            getName(), //name
142            "string", //type
143            "select_notnull", //widget
144            options, //widgetconfig
145            null, //regex
146            null, //ruletype
147            null, //default
148            NICE_NAME, //nicename
149            null, //description
150            null, //error
151            null//preferfolder
152        );
153        return prop;
154    }
155
156}