Re: jahia_calendar default value

Philippe Vollenweider <[email protected]>
Newsgroups gmane.comp.cms.jahia.template
Message-ID <[email protected]>
Hello,

You can use an eventlistner to set a default value if no value has 
been set in the engine. Here is an example where the value set is the 
actual time.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<%@ page import="org.jahia.content.*" %>
<%@ page import="org.jahia.data.*" %>
<%@ page import="org.jahia.data.containers.*" %>
<%@ page import="org.jahia.data.fields.*" %>
<%@ page import="org.jahia.data.events.*" %>
<%@ page import="org.jahia.engines.*" %>
<%@ page import="org.jahia.engines.categories.*" %>
<%@ page import="org.jahia.engines.selectpage.*" %>
<%@ page import="org.jahia.engines.shared.*" %>
<%@ page import="org.jahia.params.*" %>
<%@ page import="org.jahia.registries.*" %>
<%@ page import="org.jahia.services.categories.*" %>
<%@ page import="org.jahia.services.containers.*" %>
<%@ page import="org.jahia.services.database.*" %>
<%@ page import="org.jahia.services.fields.*" %>
<%@ page import="org.jahia.services.pages.*" %>
<%@ page import="org.jahia.services.version.*" %>
<jsp:useBean id="eventsToTrap" class="java.util.Vector" scope="application"/>
<%// loads the events to trap
if (eventsToTrap.size() == 0) {
         eventsToTrap.add("addContainerEngineAfterInit");
         eventsToTrap.add("addContainerEngineBeforeSave");
         eventsToTrap.add("containerAdded");
         eventsToTrap.add("containerUpdated");
}

String eventName = (String) request.getAttribute("eventName");
JahiaEvent jahiaEvent = (JahiaEvent) request.getAttribute("jahiaEvent");
ParamBean jParams = jahiaEvent.getParams();

if ( eventsToTrap.contains(eventName)){
         HashMap engineMap =
             (HashMap) jParams.getSession().getAttribute( 
"jahia_session_engineMap" );
         if (engineMap == null) { return; }
         String languageCode = (String)engineMap.get("languageCode");
         HashSet updatedFields = (HashSet)engineMap.get("updated.fields");
     if (updatedFields == null) {
             updatedFields = new HashSet();
             engineMap.put("updated.fields", updatedFields);
         }
         JahiaContainer theContainer = (JahiaContainer)jahiaEvent.getObject();

     if ( "yourContainer".equals(theContainer.getDefinition().getName()) ){
         if ( "addContainerEngineAfterInit".equals(eventName) ) {
             JahiaField theField = theContainer.getField("yourDate");
             if (theField.getObject() == null) {
                 Calendar cal = 
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                 long now = cal.getTime().getTime();
                 String nowStr = String.valueOf(now);
                 theField.setObject(nowStr);
                 updatedFields.add(new Integer(theField.getID()));
             }
         }
     }
}
%>

Cheers,

Philippe.

At 21.12.2005 14:53, you wrote:

>Hi all,
>
>This example has been tried with Jahia 4.0.4.
>I'm trying to set a default value to a jahia calendar as follows:
>
>         java.text.DateFormat dateFormat = new
>java.text.SimpleDateFormat("dd MMM yyyy / HH:mm");
>         String publicatieDatum = "<jahia_calendar[dd mon yyyy / HH:MM]>"
>+ dateFormat.format(new Date());
>
>So, the string will look like:
>
>"<jahia_calendar[dd mon yyyy / HH:MM]>21 Dec 2005 / 14:48"
>
>This should be possible according to the following description:
>
>A special tag ("<jahia_calendar[DateFormat]>") to set the date format,
>followed by a default value.
>
>In fact, I *do* see the default value in the date field, however, it is
>not stored (the date is the publication date of an article).
>
>Do I do something wrong, or is this a bug in Jahia?
>
>
>thanks,
>Hilbert

-------=[ pvollenweider at jahia dot com ]=---------
Jahia : A collaborative source CMS and Portal Server
www.jahia.org Community and product web site
www.jahia.com Commercial services company
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.