User preferences (v3.2+)

Définition

Since versions 3.2, the CMS handles user preferences.
There are two types of user preferences :

  • user preferences of back-office (for contributors)
  • user preferences of front office (for visitors)

How to add user preferences ?

A user preference is an extension point :

  • For back-office user preferences, the extension point id is org.ametys.runtime.plugins.core.userpref.UserPreferencesExtensionPoint
  • For front office user preferences, the extension point id is org.ametys.runtime.plugins.core.userpref.UserPreferencesExtensionPoint.FO

It is composed by :

  • a unique id without spaces or special characters
  • a type among string, date, double, long, boolean or password
  • a (i18n) label
  • a (i18n) full description
  • a (i18n) group to classified them
  • a optional order to ordered them
  • a optional widget
  • a optional validator

It is declared in plugin.xml file like the following example:

<extension point="org.ametys.runtime.plugins.core.userpref.UserPreferencesExtensionPoint.FO"
           id="cms.project.user.prefs">
     <param id="lastname" type="string">
         <label i18n="true">USER_PREFS_LASTNAME_LABEL</label>
         <description i18n="true">USER_PREFS_LASTNAME_DESC</description>
         <group i18n="true">USER_PREFS_MY_DETAILS_GROUP</group>
         <order>10</order>
     </param>
     <param id="birthdate" type="date">
         <label i18n="true">USER_PREFS_BIRTHDATE_LABEL</label>
         <description i18n="true">USER_PREFS_BIRTHDATE_DESC</description>
         <group i18n="true">USER_PREFS_MY_DETAILS_GROUP</group>
         <order>20</order>
     </param>
     <param id="email" type="string">
          <label i18n="true">USER_PREFS_MAIL_LABEL</label>
          <description i18n="true">USER_PREFS_MAIL_DESC</description>
          <group i18n="true">USER_PREFS_MY_DETAILS_GROUP</group>
          <order>25</order>
          <validation>
               <mandatory/>
               <regexp>.*@.*\..*</regexp>
          </validation>
     </param>
</extension>

How to use BO user preferences ?

in js you can use the following DAO (org.ametys.dao.userpref.UserPrefDAO) to access user preferences.
See jsdoc (to come soon) to use it.

This DAO use a local cache.
Each time a value is asked, it is set in the cache. You may need to resetCache.

Samples

Simple use

Get a value (single or multivalued)

org.ametys.dao.userpref.UserPrefDAO.getInstance().getValue("my-user-pref");
org.ametys.dao.userpref.UserPrefDAO.getInstance().getValues("my-user-pref");
Asynchronous use

Get a value (single or multivalued)

function myfunc(value)
{
     alert("my-user-pref is " + (value || ""));
}

function myfunc(values)
{
     var v = "";
     if (values != null)
     {
          for (var i = 0; i < values.length; i++)
               v += values[i] + " ";
     }
     alert("my-user-pref is " + v);
}

org.ametys.dao.userpref.UserPrefDAO.getInstance().getValue("my-user-pref", myfunc);
org.ametys.dao.userpref.UserPrefDAO.getInstance().getValues("my-user-pref", myfunc2);