Search in sources :

Example 71 with FormItem

use of org.olat.core.gui.components.form.flexible.FormItem in project OpenOLAT by OpenOLAT.

the class ProfileFormController method updateIdentityFromFormData.

public Identity updateIdentityFromFormData(Identity identity) {
    identityToModify = identity;
    User user = identityToModify.getUser();
    for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
        FormItem formItem = formItems.get(userPropertyHandler.getName());
        if (formItem.isEnabled()) {
            userPropertyHandler.updateUserFromFormItem(user, formItem);
        }
    }
    return identityToModify;
}
Also used : User(org.olat.core.id.User) FormItem(org.olat.core.gui.components.form.flexible.FormItem) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler)

Example 72 with FormItem

use of org.olat.core.gui.components.form.flexible.FormItem in project OpenOLAT by OpenOLAT.

the class ProfileFormController method initForm.

@Override
protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) {
    User user = identityToModify.getUser();
    // show a form element for each property handler
    boolean first = true;
    for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
        if (userPropertyHandler == null) {
            continue;
        }
        // add spacer if necessary (i.e. when group name changes)
        String group = userPropertyHandler.getGroup();
        String formId = "group." + group;
        FormLayoutContainer groupContainer = (FormLayoutContainer) formLayout.getFormComponent(formId);
        if (groupContainer == null) {
            groupContainer = FormLayoutContainer.createDefaultFormLayout(formId, getTranslator());
            groupContainer.setFormTitle(translate("form.group." + group));
            if (first) {
                groupContainer.setFormContextHelp("Configuration");
                first = false;
            }
            formItems.put(formId, groupContainer);
            formLayout.add(groupContainer);
        }
        // add input field to container
        FormItem formItem = userPropertyHandler.addFormItem(getLocale(), user, usageIdentifier, isAdministrativeUser, groupContainer);
        String propertyName = userPropertyHandler.getName();
        formItems.put(propertyName, formItem);
        if (formItem instanceof TextElement) {
            // it's a text field, so get the value of this property into the text field
            TextElement textElement = (TextElement) formItem;
            textElement.setValue(user.getProperty(propertyName, getLocale()));
        } else if (formItem instanceof MultipleSelectionElement) {
            // it's a checkbox, so set the box to checked if the corresponding property is set to "true"
            MultipleSelectionElement checkbox = (MultipleSelectionElement) formItem;
            String value = user.getProperty(propertyName, getLocale());
            if (value != null) {
                checkbox.select(propertyName, value.equals("true"));
            } else {
                // assume "false" if the property is not present
                checkbox.select(propertyName, false);
            }
        }
        // special case for email field
        if (userPropertyHandler.getName().equals("email")) {
            String key = user.getProperty("emchangeKey", null);
            TemporaryKey tempKey = rm.loadTemporaryKeyByRegistrationKey(key);
            if (tempKey != null) {
                XStream xml = XStreamHelper.createXStreamInstance();
                @SuppressWarnings("unchecked") HashMap<String, String> mails = (HashMap<String, String>) xml.fromXML(tempKey.getEmailAddress());
                formItem.setExampleKey("email.change.form.info", new String[] { mails.get("changedEMail") });
            }
            if (!userModule.isEmailMandatory()) {
                formItem.setMandatory(false);
            }
        }
    }
    // add the "about me" text field.
    FormLayoutContainer groupContainer = FormLayoutContainer.createDefaultFormLayout("group.about", getTranslator());
    groupContainer.setFormTitle(translate("form.group.about"));
    groupContainer.setElementCssClass("o_user_aboutme");
    formLayout.add(groupContainer);
    HomePageConfig conf = hpcm.loadConfigFor(identityToModify.getName());
    textAboutMe = uifactory.addRichTextElementForStringData("form.text", "form.text", conf.getTextAboutMe(), 10, -1, false, null, null, groupContainer, ureq.getUserSession(), getWindowControl());
    textAboutMe.setMaxLength(10000);
    // upload image
    groupContainer = FormLayoutContainer.createDefaultFormLayout("portraitupload", getTranslator());
    groupContainer.setFormTitle(translate("ul.header"));
    formLayout.add(groupContainer);
    File portraitFile = dps.getLargestPortrait(identityToModify.getName());
    // Init upload controller
    Set<String> mimeTypes = new HashSet<>();
    mimeTypes.add("image/gif");
    mimeTypes.add("image/jpg");
    mimeTypes.add("image/jpeg");
    mimeTypes.add("image/png");
    portraitUpload = uifactory.addFileElement(getWindowControl(), "ul.select", "ul.select", groupContainer);
    portraitUpload.setMaxUploadSizeKB(10000, null, null);
    portraitUpload.setPreview(ureq.getUserSession(), true);
    portraitUpload.addActionListener(FormEvent.ONCHANGE);
    portraitUpload.setHelpTextKey("ul.select.fhelp", null);
    portraitUpload.setDeleteEnabled(true);
    if (portraitFile != null) {
        portraitUpload.setInitialFile(portraitFile);
    }
    portraitUpload.limitToMimeType(mimeTypes, null, null);
    if (logoEnabled) {
        // upload image
        groupContainer = FormLayoutContainer.createDefaultFormLayout("logoupload", getTranslator());
        groupContainer.setFormTitle(translate("logo.header"));
        formLayout.add(groupContainer);
        File logoFile = dps.getLargestLogo(identityToModify.getName());
        logoUpload = uifactory.addFileElement(getWindowControl(), "logo.select", "logo.select", groupContainer);
        logoUpload.setMaxUploadSizeKB(10000, null, null);
        logoUpload.setPreview(ureq.getUserSession(), true);
        logoUpload.addActionListener(FormEvent.ONCHANGE);
        logoUpload.setHelpTextKey("ul.select.fhelp", null);
        logoUpload.setDeleteEnabled(true);
        if (logoFile != null) {
            logoUpload.setInitialFile(logoFile);
        }
        logoUpload.limitToMimeType(mimeTypes, null, null);
    }
    // Create submit and cancel buttons
    FormLayoutContainer buttonLayoutWrappper = FormLayoutContainer.createDefaultFormLayout("buttonLayoutWrappper", getTranslator());
    formLayout.add(buttonLayoutWrappper);
    FormLayoutContainer buttonLayout = FormLayoutContainer.createButtonLayout("buttonLayoutInner", getTranslator());
    buttonLayoutWrappper.add(buttonLayout);
    uifactory.addFormSubmitButton("save", buttonLayout);
    uifactory.addFormCancelButton("cancel", buttonLayout, ureq, getWindowControl());
}
Also used : User(org.olat.core.id.User) HashMap(java.util.HashMap) FormItem(org.olat.core.gui.components.form.flexible.FormItem) TemporaryKey(org.olat.registration.TemporaryKey) XStream(com.thoughtworks.xstream.XStream) FormLayoutContainer(org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer) RichTextElement(org.olat.core.gui.components.form.flexible.elements.RichTextElement) TextElement(org.olat.core.gui.components.form.flexible.elements.TextElement) MultipleSelectionElement(org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement) File(java.io.File) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler) HashSet(java.util.HashSet)

Example 73 with FormItem

use of org.olat.core.gui.components.form.flexible.FormItem in project OpenOLAT by OpenOLAT.

the class GenericSelectionPropertyHandler method addFormItem.

/**
 * @see org.olat.user.propertyhandlers.UserPropertyHandler#addFormItem(java.util.Locale,
 *      org.olat.core.id.User, java.lang.String, boolean,
 *      org.olat.core.gui.components.form.flexible.FormItemContainer)
 */
@Override
public FormItem addFormItem(Locale locale, User user, String usageIdentifyer, boolean isAdministrativeUser, FormItemContainer formItemContainer) {
    FormItem newItem = null;
    if (isMultiselect) {
        MultipleSelectionElement mse = FormUIFactory.getInstance().addCheckboxesHorizontal(getName(), i18nFormElementLabelKey(), formItemContainer, selectionKeys, getTranslatedValues(selectionKeys, locale));
        for (String sKey : getSelectedKeys(user)) {
            mse.select(sKey, true);
        }
        newItem = mse;
    } else {
        String[] allKeys = new String[selectionKeys.length + 1];
        System.arraycopy(selectionKeys, 0, allKeys, 1, selectionKeys.length);
        allKeys[0] = NO_SEL_KEY;
        SingleSelection sse = FormUIFactory.getInstance().addDropdownSingleselect(getName(), i18nFormElementLabelKey(), formItemContainer, allKeys, getTranslatedValues(allKeys, locale), null);
        // make pre-selection of the formItem
        String internalValue = getInternalValue(user);
        if (isValidValue(user, internalValue, null, null) && internalValue != null)
            sse.select(internalValue, true);
        newItem = sse;
    }
    // enable/disable according to settings
    UserManager um = UserManager.getInstance();
    if (um.isUserViewReadOnly(usageIdentifyer, this) && !isAdministrativeUser) {
        newItem.setEnabled(false);
    }
    if (um.isMandatoryUserProperty(usageIdentifyer, this)) {
        newItem.setMandatory(true);
    }
    return newItem;
}
Also used : MultipleSelectionElement(org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement) SingleSelection(org.olat.core.gui.components.form.flexible.elements.SingleSelection) FormItem(org.olat.core.gui.components.form.flexible.FormItem) UserManager(org.olat.user.UserManager)

Example 74 with FormItem

use of org.olat.core.gui.components.form.flexible.FormItem in project OpenOLAT by OpenOLAT.

the class UsrPropContextEditController method formInnerEvent.

@Override
@SuppressWarnings("unused")
protected void formInnerEvent(UserRequest ureq, FormItem source, FormEvent event) {
    if (source instanceof FormToggle) {
        int activeCount = 0;
        FormToggle sourceToggle = (FormToggle) source;
        UserPropertyHandler handler = (UserPropertyHandler) sourceToggle.getUserObject();
        if (sourceToggle.getName().startsWith(FT_NAME_PREFIX_MAND)) {
            context.setAsMandatoryUserProperty(handler, sourceToggle.isOn());
        } else if (sourceToggle.getName().startsWith(FT_NAME_PREFIX_ADMN)) {
            context.setAsAdminstrativeUserOnly(handler, sourceToggle.isOn());
        } else if (sourceToggle.getName().startsWith(FT_NAME_PREFIX_USR)) {
            context.setAsUserViewReadOnly(handler, sourceToggle.isOn());
        } else if (sourceToggle.getName().startsWith(FT_NAME_PREFIX_INCL)) {
            if (sourceToggle.isOn()) {
                // activate the toggles on this row
                List<FormItem> rowFormItems = rowToggleButtonsMap.get(handler.getName());
                for (FormItem item : rowFormItems) {
                    item.setEnabled(true);
                    item.setVisible(true);
                }
                context.addPropertyHandler(handler);
                includedPropertiesCount++;
            } else {
                // this is removing property from context
                // deactivate the toggles on this row
                List<FormItem> rowFormItems = rowToggleButtonsMap.get(handler.getName());
                for (FormItem item : rowFormItems) {
                    item.setEnabled(false);
                    item.setVisible(false);
                }
                context.removePropertyHandler(handler);
                includedPropertiesCount--;
                if (!UsrPropCfgManager.canBeOptionalInContext(handler, contextName)) {
                    showInfo("upc.deact_infotext");
                }
            }
        }
        // something was toggled, save changes!
        usrPropCfgMng.saveUserPropertiesConfig();
        contTableFlc.contextPut("activePropCount", includedPropertiesCount);
    } else if (source instanceof FormLink) {
        // a link, (up/down)
        UserPropertyHandler handler = (UserPropertyHandler) source.getUserObject();
        if (source.getName().startsWith(FT_NAME_PREFIX_MUP)) {
            context.moveHandlerUp(handler);
            usrPropCfgMng.saveUserPropertiesConfig();
            buildPropertyHandlerListFromContext();
        } else if (source.getName().startsWith(FT_NAME_PREFIX_MDN)) {
            context.moveHandlerDown(handler);
            usrPropCfgMng.saveUserPropertiesConfig();
            buildPropertyHandlerListFromContext();
        }
    }
}
Also used : FormItem(org.olat.core.gui.components.form.flexible.FormItem) FormToggle(org.olat.core.gui.components.form.flexible.elements.FormToggle) ArrayList(java.util.ArrayList) List(java.util.List) FormLink(org.olat.core.gui.components.form.flexible.elements.FormLink) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler)

Example 75 with FormItem

use of org.olat.core.gui.components.form.flexible.FormItem in project OpenOLAT by OpenOLAT.

the class RegistrationForm2 method getFirstName.

protected String getFirstName() {
    FormItem fi = propFormItems.get("firstName");
    TextElement fn = (TextElement) fi;
    return fn.getValue().trim();
}
Also used : StaticTextElement(org.olat.core.gui.components.form.flexible.elements.StaticTextElement) TextElement(org.olat.core.gui.components.form.flexible.elements.TextElement) FormItem(org.olat.core.gui.components.form.flexible.FormItem)

Aggregations

FormItem (org.olat.core.gui.components.form.flexible.FormItem)142 UserPropertyHandler (org.olat.user.propertyhandlers.UserPropertyHandler)62 TextElement (org.olat.core.gui.components.form.flexible.elements.TextElement)34 FormLayoutContainer (org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer)30 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)22 Identity (org.olat.core.id.Identity)20 FormLink (org.olat.core.gui.components.form.flexible.elements.FormLink)16 User (org.olat.core.id.User)16 SingleSelection (org.olat.core.gui.components.form.flexible.elements.SingleSelection)14 UserManager (org.olat.user.UserManager)12 EmailProperty (org.olat.user.propertyhandlers.EmailProperty)12 HashSet (java.util.HashSet)10 Translator (org.olat.core.gui.translator.Translator)10 File (java.io.File)8 Map (java.util.Map)8 Component (org.olat.core.gui.components.Component)8 MultipleSelectionElement (org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement)8 Date (java.util.Date)6 StaticTextElement (org.olat.core.gui.components.form.flexible.elements.StaticTextElement)6