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 GmbH & Co. KG, 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.widgets;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.i18n.CmsMessages;
033import org.opencms.main.OpenCms;
034import org.opencms.ui.dialogs.permissions.CmsPrincipalSelect;
035import org.opencms.ui.dialogs.permissions.CmsPrincipalSelectDialog;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.workplace.CmsWorkplace;
038import org.opencms.xml.content.I_CmsXmlContentHandler.DisplayType;
039import org.opencms.xml.types.A_CmsXmlContentValue;
040
041import java.util.List;
042import java.util.Locale;
043
044/**
045 * Provides a OpenCms Group selection widget, for use on a widget dialog.<p>
046 *
047 * @since 6.0.0
048 */
049public class CmsGroupWidget extends A_CmsWidget implements I_CmsADEWidget {
050
051    /** Configuration parameter to set the flags of the groups to display, optional. */
052    public static final String CONFIGURATION_FLAGS = "flags";
053
054    /** Configuration parameter to set the organizational unit of the groups to display, optional. */
055    public static final String CONFIGURATION_OUFQN = "oufqn";
056
057    /** Configuration parameter to set the user of the groups to display, optional. */
058    public static final String CONFIGURATION_USER = "user";
059
060    /** The the flags used in the popup window. */
061    private Integer m_flags;
062
063    /** The the organizational unit used in the popup window. */
064    private String m_ouFqn;
065
066    /** The the user used in the popup window. */
067    private String m_userName;
068
069    /**
070     * Creates a new group selection widget.<p>
071     */
072    public CmsGroupWidget() {
073
074        // empty constructor is required for class registration
075        this("");
076    }
077
078    /**
079     * Creates a new group selection widget with the parameters to configure the popup window behaviour.<p>
080     *
081     * @param flags the group flags to restrict the group selection, can be <code>null</code>
082     * @param userName the user to restrict the group selection, can be <code>null</code>
083     */
084    public CmsGroupWidget(Integer flags, String userName) {
085
086        m_flags = flags;
087        m_userName = userName;
088        m_ouFqn = null;
089    }
090
091    /**
092     * Creates a new group selection widget with the parameters to configure the popup window behaviour.<p>
093     *
094     * @param flags the group flags to restrict the group selection, can be <code>null</code>
095     * @param userName the user to restrict the group selection, can be <code>null</code>
096     * @param ouFqn the organizational unit to restrict the group selection, can be <code>null</code>
097     */
098    public CmsGroupWidget(Integer flags, String userName, String ouFqn) {
099
100        m_flags = flags;
101        m_userName = userName;
102        m_ouFqn = ouFqn;
103    }
104
105    /**
106     * Creates a new group selection widget with the given configuration.<p>
107     *
108     * @param configuration the configuration to use
109     */
110    public CmsGroupWidget(String configuration) {
111
112        super(configuration);
113    }
114
115    /**
116     * @see org.opencms.widgets.A_CmsWidget#getConfiguration()
117     */
118    @Override
119    public String getConfiguration() {
120
121        StringBuffer result = new StringBuffer(8);
122
123        // append flags to configuration
124        if (m_flags != null) {
125            if (result.length() > 0) {
126                result.append("|");
127            }
128            result.append(CONFIGURATION_FLAGS);
129            result.append("=");
130            result.append(m_flags);
131        }
132        // append user to configuration
133        if (m_userName != null) {
134            if (result.length() > 0) {
135                result.append("|");
136            }
137            result.append(CONFIGURATION_USER);
138            result.append("=");
139            result.append(m_userName);
140        }
141        // append oufqn to configuration
142        if (m_ouFqn != null) {
143            if (result.length() > 0) {
144                result.append("|");
145            }
146            result.append(CONFIGURATION_OUFQN);
147            result.append("=");
148            result.append(m_ouFqn);
149        }
150        return result.toString();
151    }
152
153    /**
154     * @see org.opencms.widgets.I_CmsADEWidget#getConfiguration(org.opencms.file.CmsObject, org.opencms.xml.types.A_CmsXmlContentValue, org.opencms.i18n.CmsMessages, org.opencms.file.CmsResource, java.util.Locale)
155     */
156    public String getConfiguration(
157        CmsObject cms,
158        A_CmsXmlContentValue schemaType,
159        CmsMessages messages,
160        CmsResource resource,
161        Locale contentLocale) {
162
163        return CmsPrincipalSelectDialog.PARAM_TYPE + "=" + CmsPrincipalSelect.WidgetType.groupwidget.name();
164    }
165
166    /**
167     * @see org.opencms.widgets.I_CmsADEWidget#getCssResourceLinks(org.opencms.file.CmsObject)
168     */
169    public List<String> getCssResourceLinks(CmsObject cms) {
170
171        return null;
172    }
173
174    /**
175     * @see org.opencms.widgets.I_CmsADEWidget#getDefaultDisplayType()
176     */
177    public DisplayType getDefaultDisplayType() {
178
179        return DisplayType.singleline;
180    }
181
182    /**
183     * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
184     */
185    @Override
186    public String getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
187
188        StringBuffer result = new StringBuffer(16);
189        result.append(getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/groupselector.js"));
190        return result.toString();
191    }
192
193    /**
194     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
195     */
196    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
197
198        String id = param.getId();
199        StringBuffer result = new StringBuffer(128);
200
201        result.append("<td class=\"xmlTd\">");
202        result.append(
203            "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"maxwidth\"><tr><td style=\"width: 100%;\">");
204        result.append("<input style=\"width: 99%;\" class=\"xmlInput");
205        if (param.hasError()) {
206            result.append(" xmlInputError");
207        }
208        result.append("\" value=\"");
209        result.append(param.getStringValue(cms));
210        result.append("\" name=\"");
211        result.append(id);
212        result.append("\" id=\"");
213        result.append(id);
214        result.append("\"></td>");
215        result.append(widgetDialog.dialogHorizontalSpacer(10));
216        result.append(
217            "<td><table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
218
219        StringBuffer buttonJs = new StringBuffer(8);
220        buttonJs.append("javascript:openGroupWin('");
221        buttonJs.append(OpenCms.getSystemInfo().getOpenCmsContext());
222        buttonJs.append("/system/workplace/commons/group_selection.jsp");
223        buttonJs.append("','EDITOR',  '");
224        buttonJs.append(id);
225        buttonJs.append("', document, ");
226        if (m_flags != null) {
227            buttonJs.append("'");
228            buttonJs.append(m_flags);
229            buttonJs.append("'");
230        } else {
231            buttonJs.append("null");
232        }
233        buttonJs.append(", ");
234        if (m_userName != null) {
235            buttonJs.append("'");
236            buttonJs.append(m_userName);
237            buttonJs.append("'");
238        } else {
239            buttonJs.append("null");
240        }
241        buttonJs.append(", ");
242        if (m_ouFqn != null) {
243            buttonJs.append("'");
244            buttonJs.append(m_ouFqn);
245            buttonJs.append("'");
246        } else {
247            buttonJs.append("null");
248        }
249        buttonJs.append(");");
250
251        result.append(
252            widgetDialog.button(
253                buttonJs.toString(),
254                null,
255                "group",
256                org.opencms.workplace.Messages.GUI_DIALOG_BUTTON_SEARCH_0,
257                widgetDialog.getButtonStyle()));
258        result.append("</tr></table>");
259        result.append("</td></tr></table>");
260
261        result.append("</td>");
262
263        return result.toString();
264    }
265
266    /**
267     * Returns the flags, or <code>null</code> if all.<p>
268     *
269     * @return the flags, or <code>null</code> if all
270     */
271    public Integer getFlags() {
272
273        return m_flags;
274    }
275
276    /**
277     * @see org.opencms.widgets.I_CmsADEWidget#getInitCall()
278     */
279    public String getInitCall() {
280
281        return null;
282    }
283
284    /**
285     * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject)
286     */
287    public List<String> getJavaScriptResourceLinks(CmsObject cms) {
288
289        return null;
290    }
291
292    /**
293     * Returns the organizational unit fqn, or <code>null</code> if all.<p>
294     *
295     * @return the organizational unit fqn
296     */
297    public String getOufqn() {
298
299        return m_ouFqn;
300    }
301
302    /**
303     * Returns the user name, or <code>null</code> if all.<p>
304     *
305     * @return the user name
306     */
307    public String getUserName() {
308
309        return m_userName;
310    }
311
312    /**
313     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
314     */
315    public String getWidgetName() {
316
317        return CmsPrincipalWidget.class.getName();
318    }
319
320    /**
321     * @see org.opencms.widgets.I_CmsADEWidget#isInternal()
322     */
323    public boolean isInternal() {
324
325        return true;
326    }
327
328    /**
329     * @see org.opencms.widgets.I_CmsWidget#newInstance()
330     */
331    public I_CmsWidget newInstance() {
332
333        return new CmsGroupWidget(getConfiguration());
334    }
335
336    /**
337     * @see org.opencms.widgets.A_CmsWidget#setConfiguration(java.lang.String)
338     */
339    @Override
340    public void setConfiguration(String configuration) {
341
342        m_userName = null;
343        m_flags = null;
344        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(configuration)) {
345            int flagsIndex = configuration.indexOf(CONFIGURATION_FLAGS);
346            if (flagsIndex != -1) {
347                // user is given
348                String flags = configuration.substring(CONFIGURATION_FLAGS.length() + 1);
349                if (flags.indexOf('|') != -1) {
350                    // cut eventual following configuration values
351                    flags = flags.substring(0, flags.indexOf('|'));
352                }
353                try {
354                    m_flags = Integer.valueOf(flags);
355                } catch (Throwable t) {
356                    // invalid flags
357                }
358            }
359            int groupIndex = configuration.indexOf(CONFIGURATION_USER);
360            if (groupIndex != -1) {
361                // user is given
362                String user = configuration.substring(groupIndex + CONFIGURATION_USER.length() + 1);
363                if (user.indexOf('|') != -1) {
364                    // cut eventual following configuration values
365                    user = user.substring(0, user.indexOf('|'));
366                }
367                m_userName = user;
368            }
369            int oufqnIndex = configuration.indexOf(CONFIGURATION_OUFQN);
370            if (oufqnIndex != -1) {
371                // user is given
372                String oufqn = configuration.substring(oufqnIndex + CONFIGURATION_OUFQN.length() + 1);
373                if (oufqn.indexOf('|') != -1) {
374                    // cut eventual following configuration values
375                    oufqn = oufqn.substring(0, oufqn.indexOf('|'));
376                }
377                m_ouFqn = oufqn;
378            }
379        }
380        super.setConfiguration(configuration);
381    }
382}