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.CmsUser;
032import org.opencms.main.CmsLog;
033import org.opencms.main.OpenCms;
034import org.opencms.util.CmsStringUtil;
035import org.opencms.workplace.CmsWorkplace;
036
037import java.util.ArrayList;
038import java.util.List;
039
040import org.apache.commons.logging.Log;
041
042/**
043 * Provides a OpenCms User selection widget, for use on a widget dialog.<p>
044 *
045 * @since 6.0.0
046 */
047public class CmsUserWidget extends CmsSelectWidget {
048
049    /** Configuration parameter to set the flags of the users to display, optional. */
050    public static final String CONFIGURATION_FLAGS = "flags";
051
052    /** Configuration parameter to set the group of users to display, optional. */
053    public static final String CONFIGURATION_GROUP = "group";
054
055    private static final Log LOG = CmsLog.getLog(CmsUserWidget.class);
056
057    /** The the flags used in the popup window. */
058    private Integer m_flags;
059
060    /** The the group used in the popup window. */
061    private String m_groupName;
062
063    /**
064     * Creates a new user selection widget.<p>
065     */
066    public CmsUserWidget() {
067
068        // empty constructor is required for class registration
069        this("");
070    }
071
072    /**
073     * Creates a new user selection widget with the parameters to configure the popup window behaviour.<p>
074     *
075     * @param flags the group flags to restrict the group selection, can be <code>null</code>
076     * @param groupName the group to restrict the user selection, can be <code>null</code>
077     */
078    public CmsUserWidget(Integer flags, String groupName) {
079
080        m_flags = flags;
081        m_groupName = groupName;
082    }
083
084    /**
085     * Creates a new user selection widget with the given configuration.<p>
086     *
087     * @param configuration the configuration to use
088     */
089    public CmsUserWidget(String configuration) {
090
091        super(configuration);
092    }
093
094    /**
095     * @see org.opencms.widgets.A_CmsWidget#getConfiguration()
096     */
097    @Override
098    public String getConfiguration() {
099
100        StringBuffer result = new StringBuffer(8);
101
102        // append flags to configuration
103        if (m_flags != null) {
104            if (result.length() > 0) {
105                result.append("|");
106            }
107            result.append(CONFIGURATION_FLAGS);
108            result.append("=");
109            result.append(m_flags);
110        }
111        // append group to configuration
112        if (m_groupName != null) {
113            if (result.length() > 0) {
114                result.append("|");
115            }
116            result.append(CONFIGURATION_GROUP);
117            result.append("=");
118            result.append(m_groupName);
119        }
120
121        return result.toString();
122    }
123
124    /**
125     * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
126     */
127    @Override
128    public String getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
129
130        StringBuffer result = new StringBuffer(16);
131        result.append(getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/userselector.js"));
132        return result.toString();
133    }
134
135    /**
136     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
137     */
138    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
139
140        String id = param.getId();
141        StringBuffer result = new StringBuffer(128);
142
143        result.append("<td class=\"xmlTd\">");
144        result.append(
145            "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"maxwidth\"><tr><td style=\"width: 100%;\">");
146        result.append("<input style=\"width: 99%;\" class=\"xmlInput");
147        if (param.hasError()) {
148            result.append(" xmlInputError");
149        }
150        result.append("\" value=\"");
151        result.append(param.getStringValue(cms));
152        result.append("\" name=\"");
153        result.append(id);
154        result.append("\" id=\"");
155        result.append(id);
156        result.append("\"></td>");
157        result.append(widgetDialog.dialogHorizontalSpacer(10));
158        result.append(
159            "<td><table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
160
161        StringBuffer buttonJs = new StringBuffer(8);
162        buttonJs.append("javascript:openUserWin('");
163        buttonJs.append(OpenCms.getSystemInfo().getOpenCmsContext());
164        buttonJs.append("/system/workplace/commons/user_selection.jsp");
165        buttonJs.append("','EDITOR',  '");
166        buttonJs.append(id);
167        buttonJs.append("', document, ");
168        if (m_flags != null) {
169            buttonJs.append("'");
170            buttonJs.append(m_flags);
171            buttonJs.append("'");
172        } else {
173            buttonJs.append("null");
174        }
175        buttonJs.append(", ");
176        if (m_groupName != null) {
177            buttonJs.append("'");
178            buttonJs.append(m_groupName);
179            buttonJs.append("'");
180        } else {
181            buttonJs.append("null");
182        }
183        buttonJs.append(");");
184
185        result.append(
186            widgetDialog.button(
187                buttonJs.toString(),
188                null,
189                "user",
190                org.opencms.workplace.Messages.GUI_DIALOG_BUTTON_SEARCH_0,
191                widgetDialog.getButtonStyle()));
192        result.append("</tr></table>");
193        result.append("</td></tr></table>");
194
195        result.append("</td>");
196
197        return result.toString();
198    }
199
200    /**
201     * Returns the flags, or <code>null</code> if all.<p>
202     *
203     * @return the flags, or <code>null</code> if all
204     */
205    public Integer getFlags() {
206
207        return m_flags;
208    }
209
210    /**
211     * Returns the group name, or <code>null</code> if all.<p>
212     *
213     * @return the group name, or <code>null</code> if all
214     */
215    public String getGroupName() {
216
217        return m_groupName;
218    }
219
220    /**
221     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
222     */
223    public String getWidgetName() {
224
225        return CmsSelectWidget.class.getName();
226    }
227
228    /**
229     * @see org.opencms.widgets.I_CmsWidget#newInstance()
230     */
231    public I_CmsWidget newInstance() {
232
233        return new CmsUserWidget(getConfiguration());
234    }
235
236    /**
237     * @see org.opencms.widgets.A_CmsWidget#setConfiguration(java.lang.String)
238     */
239    @Override
240    public void setConfiguration(String configuration) {
241
242        m_groupName = null;
243        m_flags = null;
244        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(configuration)) {
245            int flagsIndex = configuration.indexOf(CONFIGURATION_FLAGS);
246            if (flagsIndex != -1) {
247                // user is given
248                String flags = configuration.substring(CONFIGURATION_FLAGS.length() + 1);
249                if (flags.indexOf('|') != -1) {
250                    // cut eventual following configuration values
251                    flags = flags.substring(0, flags.indexOf('|'));
252                }
253                try {
254                    m_flags = Integer.valueOf(flags);
255                } catch (Throwable t) {
256                    // invalid flags
257                }
258            }
259            int groupIndex = configuration.indexOf(CONFIGURATION_GROUP);
260            if (groupIndex != -1) {
261                // group is given
262                String group = configuration.substring(CONFIGURATION_GROUP.length() + 1);
263                if (group.indexOf('|') != -1) {
264                    // cut eventual following configuration values
265                    group = group.substring(0, group.indexOf('|'));
266                }
267                m_groupName = group;
268            }
269        }
270        super.setConfiguration(configuration);
271    }
272
273    /**
274     * @see org.opencms.widgets.A_CmsSelectWidget#parseSelectOptions(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
275     */
276    @Override
277    protected List<CmsSelectWidgetOption> parseSelectOptions(
278        CmsObject cms,
279        I_CmsWidgetDialog widgetDialog,
280        I_CmsWidgetParameter param) {
281
282        List<CmsSelectWidgetOption> options = new ArrayList<>();
283        options.add(new CmsSelectWidgetOption("", true, ""));
284        try {
285            List<CmsUser> users;
286            if (m_groupName != null) {
287                users = cms.getUsersOfGroup(m_groupName);
288            } else {
289                users = OpenCms.getOrgUnitManager().getUsers(cms, "/", true);
290            }
291            for (CmsUser user : users) {
292                CmsSelectWidgetOption option = new CmsSelectWidgetOption(user.getName(), false, user.getFullName());
293                options.add(option);
294            }
295        } catch (Exception e) {
296            LOG.error(e.getLocalizedMessage(), e);
297        }
298        return options;
299    }
300}