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