mcardle     2005/05/31 18:22:02 CEST

  Added files:
    src/view/jsp/include box_newslast5.inc 
  Log:
  added "last 5 news" box
  
  Revision  Changes    Path
  1.1       +449 -0    
corporate_portal_templates/src/view/jsp/include/box_newslast5.inc (new)
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/corporate_portal_templates/src/view/jsp/include/box_newslast5.inc?rev=1.1&content-type=text/plain
  
  
  
  Index: box_newslast5.inc
  ====================================================================
  <br>
      <%
      //Note: the variable 'id' is the current column number where this code is 
executed i.e. 1, 2 or 3
  
      //Containers Definitions over which to search/filter/sort over
      String[] newsContainerDefNames = new 
String[]{"newsContainer1","newsContainer2","newsContainer3","newsContainerLast5"+id};
      //The (optional) search string to search for entered by the user
      String userQueryString = 
request.getParameter(JahiaSearchConstant.CLIST_SEARCHQUERY_INPUT_PREFIX + 
"newsContainerLast5"+id); //i.e. clistsquery_newsContainerLast5
  
      //Toggle to search using Jahia only or the Lucene search index
      boolean useLucene = false;
  
      //Get current time
      TimeZone tz = TimeZone.getTimeZone("UTC");
      Calendar cal = Calendar.getInstance(tz);
      Date nowDate = cal.getTime();
      long nowLong = nowDate.getTime();
  
      
//----------------------------------------------------------------------------------
      //BEGIN - SEARCH SCOPE
      
//-----------------------------------------------------------------------------------
      //searchScope: Limit the search to the current virtual site or to all 
sites on the Jahia server
      //possible values are "-1" (all sites) or "n" (where n is the current 
site id)
      String searchScope = request.getParameter("searchScope");
  
      String currentPageID = Integer.toString(jData.params().getPageID());
  
      //The UserProperty key where the searchScope for the current page will be 
saved
      //(note that each column in each page will have its own entry - simply 
get rid of 'currentPageID' or 'id' to
      //stop this behaviour and have just one global site-wide user window size)
      String key = "LAST5NEWS_searchScope_id_" + id + "_pid_" + currentPageID;
  
      //initialize the searchScope from the user profile if the user is logged 
on
      if ( searchScope == null ){
          searchScope = "currentSite";
          if (jData.gui().isLogged()) {
              UserProperty userSearchScope = 
jParams.getUser().getUserProperty(key);
              //The user has already got a saved searchScope in his profile
              //for the current page
              if (userSearchScope != null) {
                  searchScope = userSearchScope.getValue();
              }
              //First time this user accesses this page
              //So save the searchScope in his profile
              else {
                  jParams.getUser().setProperty (key,searchScope);
              }
          }
          jParams.setParameter(key,searchScope);
      }
      //Save the searchScope value if it's different from the one stored in the 
user's profile
      //for that current page and column, or if it's null for some reason
      else if (jData.gui().isLogged()) {
          if (jParams.getUser().getUserProperty(key)==null
          || 
!jParams.getUser().getUserProperty(key).getValue().equals(searchScope)) {
              jParams.getUser().setProperty (key, searchScope);
          }
      }
      int searchScopeVal = 
(searchScope.equals("currentSite"))?jParams.getSiteID():-1;
      
//----------------------------------------------------------------------------------
      //END - SEARCH SCOPE
      
//----------------------------------------------------------------------------------
  
      
//----------------------------------------------------------------------------------
      //BEGIN - DO FILTERING
      
//----------------------------------------------------------------------------------
      //list of container filter beans
      Vector cFilterBeans = new Vector();
      //holds logically combined filter beans
      ContainerChainedFilter allfilters = null;
  
      //In live mode, create filters to return news entries without empty titles
      //These correspond to news entries in other languages.
      //Even if we don't include these filters, these entries in the current 
locale still won't be displayed
      //but the pagination includes them and this distorts item numbering.
      //so for the pagination not to be messed up, we include the following 
filters
      if (! jData.gui().isEditMode()) {
          //Get rid of news entries with blank titles in Normal Mode
          //These will correspond to entries not translated for the user's 
locale
          ContainerFilterBean containerFilter1 = new 
ContainerFilterBean("newsTitle", jParams.getEntryLoadRequest());
          containerFilter1.addClause(ContainerFilterBean.COMP_NOT_EQUAL,"");
  
          allfilters = new ContainerChainedFilter(
          new ContainerFilterInterface[]{containerFilter1},
          new int[]{});
  
          //Check the news has valid dates
          containerFilter1 = new ContainerFilterBean("newsStartDate",true, 
jParams.getEntryLoadRequest());
          
containerFilter1.addClause(ContainerFilterBean.COMP_SMALLER_OR_EQUAL,String.valueOf(nowLong));
  
          //IF the date isn't in the range, then it might also be because there 
is no date declared
          //in which case we don't want to filter out news entries with empty 
dates
          ContainerFilterBean containerFilter2 = new 
ContainerFilterBean("newsStartDate", jParams.getEntryLoadRequest());
          containerFilter2.addClause(ContainerFilterBean.COMP_EQUAL,"");
  
          ContainerChainedFilter allNewsStartDateFilters = new 
ContainerChainedFilter(
          new ContainerFilterInterface[]{containerFilter1,containerFilter2},
          new int[]{ContainerChainedFilter.OR} );
  
          allfilters = new ContainerChainedFilter(
          new ContainerFilterInterface[]{allfilters,allNewsStartDateFilters},
          new int[]{ContainerChainedFilter.AND} );
  
          containerFilter1 = new ContainerFilterBean("newsEndDate",true, 
jParams.getEntryLoadRequest());
          
containerFilter1.addClause(ContainerFilterBean.COMP_BIGGER_OR_EQUAL,String.valueOf(nowLong));
  
          //IF the date isn't in the range, then it might also be because there 
is no date declared
          //in which case we don't want to filter out news entries with empty 
dates
          containerFilter2 = new ContainerFilterBean("newsEndDate", 
jParams.getEntryLoadRequest());
          containerFilter2.addClause(ContainerFilterBean.COMP_EQUAL,"");
  
          ContainerChainedFilter allNewsEndDateFilters = new 
ContainerChainedFilter(
          new ContainerFilterInterface[]{containerFilter1,containerFilter2},
          new int[]{ContainerChainedFilter.OR} );
  
          allfilters = new ContainerChainedFilter(
          new ContainerFilterInterface[]{allfilters,allNewsEndDateFilters},
          new int[]{ContainerChainedFilter.AND} );
      }
  
      //Use Jahia for the search (i.e. a database query) instead of Lucene
      //Note userQueryString text searches are not supported here
      if (!useLucene) {
          //search for all containers of any definition name defined in 
'newsContainerDefNames'
          ContainerFilterByContainerDefinitions defFilter = new 
ContainerFilterByContainerDefinitions(
          newsContainerDefNames,jParams.getEntryLoadRequest());
  
          if (allfilters!=null)
          allfilters = new ContainerChainedFilter(new 
ContainerFilterInterface[]{allfilters,defFilter},new 
int[]{ContainerChainedFilter.AND});
          else
          allfilters = new ContainerChainedFilter(new 
ContainerFilterInterface[]{defFilter},
          new int[]{});
          //ContainerChainedFilter.OR);
      }
  
      if (allfilters!=null) {
          Vector filterBeans = new Vector();
          filterBeans.add(allfilters);
          ContainerFilters containerFilters = new ContainerFilters(filterBeans, 
searchScopeVal, null);//null means it searches over all container lists
  
          // Store the list of filters in the request object.
          // It will be used later by the container list loader.
          
request.setAttribute("newsContainerLast5"+id+"_filter_handler",containerFilters);
      }
      
//----------------------------------------------------------------------------------
      //END - DO FILTERING
      
//----------------------------------------------------------------------------------
  
      
//----------------------------------------------------------------------------------
      //BEGIN - DO LUCENE SEARCH
      
//----------------------------------------------------------------------------------
      if (useLucene) {
          StringBuffer luceneSearchString = new StringBuffer("");
  
          //include the user's search string if any
          if (userQueryString!=null && !"".equals(userQueryString.trim())) {
              luceneSearchString.append( "( ");
              luceneSearchString.append(userQueryString);
              luceneSearchString.append(" AND ");
          }
          //manually limit the search to news containers only
          luceneSearchString.append("(container_definition_name:newsContainer1 
OR container_definition_name:newsContainer2"+
          " OR container_definition_name:newsContainer3 OR 
container_definition_name:newsContainerLast5"+id+" )");
          if (userQueryString!=null && !"".equals(userQueryString.trim())) {
              luceneSearchString.append(" ) ");
          }
          //we use null for the second 'String containerDefinitionName' 
argument since we have already manually
          //specified above, not one but a SERIES of container definitionS to 
search over.
          ContainerSearcher containerTaglibSearcher = new 
ContainerSearcher(searchScopeVal,
          null,luceneSearchString.toString(),jParams.getEntryLoadRequest());
  
          // Store the list of filters in the lucene search handler.
          // It will be used later by the container list loader.
          
request.setAttribute("newsContainerLast5"+id+"_search_handler",containerTaglibSearcher);
      }
      
//----------------------------------------------------------------------------------
      //END - DO LUCENE SEARCH
      
//----------------------------------------------------------------------------------
  
      
//----------------------------------------------------------------------------------
      //BEGIN - DO SORTING
      
//----------------------------------------------------------------------------------
      //holds field names to sort over
      String newsSort[] = null;
      //Numerical sort or not?
      boolean numberSort = true;
      //holds the field type to sort over
      String newsSortReq = 
request.getParameter("newsContainerLast5"+id+"_sort");
  
      //According to the type of field we want to sort over,
      //we declare the associated field nameS that we should sort over since 
the same field type
      //might have a different field name
      if ( "newsEndDate".equals(newsSortReq) ) {
          newsSort = new String[]{ "newsEndDate"};
      }
      else if ( "newsTitle".equals(newsSortReq) ) {
          newsSort = new String[]{"newsTitle"};
          numberSort = false;
      }
      else if ( newsSortReq==null || "".equals(newsSortReq)
      || "null".equals(newsSortReq) || "newsStartDate".equals(newsSortReq) ) {
          newsSort = new String[]{ "newsStartDate"};
          newsSortReq="newsStartDate";
      }
      else logger.error("Unexpected value of newsSortReq :" + newsSortReq);
  
      //initialize multi-fieldname sorter
      ContainerSorterByContainerDefinition newsContainerLast5_sort_handler =
      new ContainerSorterByContainerDefinition(searchScopeVal,newsSort, 
newsContainerDefNames
      ,numberSort, null, jParams.getEntryLoadRequest());
  
      //retrieve the sort order i.e. descending or ascending
      String newsSortOrder = 
request.getParameter("newsContainerLast5"+id+"_sort_order");
      if (newsSortOrder==null || newsSortOrder.trim().equals("") )
      newsSortOrder = "desc";
      if ( "desc".equals(newsSortOrder) )
      newsContainerLast5_sort_handler.setDescOrdering();
  
      // Store the sort handler in the request object.
      // It will be used later by the container list loader.
      
request.setAttribute("newsContainerLast5"+id+"_sort_handler",newsContainerLast5_sort_handler);
      
//----------------------------------------------------------------------------------
      //END - DO SORTING
      
//----------------------------------------------------------------------------------
  
      
//----------------------------------------------------------------------------------
      //BEGIN - PAGINATION
      
//----------------------------------------------------------------------------------
      //Selected pagination window size
      String selWindowSize = 
request.getParameter("newsContainerLast5"+id+"_windowsize");
  
      //The UserProperty key where the window size for the current page will be 
saved
      //(note that each column in each page will have its own entry - simply 
get rid of 'currentPageID' or 'id' to
      //stop this behaviour and have just one global site-wide user window size)
      key = "LAST5NEWS_windowsize_id_" + id + "_pid_" + currentPageID;
  
      if ( selWindowSize == null ){
          selWindowSize = "5";
          if (jData.gui().isLogged()) {
              UserProperty userWindowSize = 
jParams.getUser().getUserProperty(key);
              //The user has already got a saved window sze in his profile
              //for the current page
              if (userWindowSize != null) {
                  selWindowSize = userWindowSize.getValue();
              }
              //First time this user accesses this page
              //So save the windows size in his profile
              else {
                  jParams.getUser().setProperty (key,selWindowSize);
              }
          }
          
jParams.setParameter("newsContainerLast5"+id+"_windowsize",selWindowSize);
      }
      //Save the window size value if it's different from the one stored in the 
user's profile
      //for that current page and column
      else if (jData.gui().isLogged()) {
          if 
(!jParams.getUser().getUserProperty(key).getValue().equals(selWindowSize)) {
              jParams.getUser().setProperty (key, selWindowSize);
          }
      }
  
      
//----------------------------------------------------------------------------------
      //END - PAGINATION
      
//----------------------------------------------------------------------------------
  
      JahiaContainerList newsContainerList = 
jData.containers().getContainerList( "newsContainerLast5"+id );
  
      newsContainerList.setIsContainersLoaded(false);
      //Make sure newsContainerList is initialized
      if ( newsContainerList !=null && newsContainerList.getID()==0 ) {
          ContainerListBean newsContainerListBean = new 
ContainerListBean(newsContainerList,jParams);
          
org.jahia.registries.ServicesRegistry.getInstance().getJahiaContainersService().
          saveContainerListInfo(newsContainerList, 
jParams.getContentPage().getAclID());
          newsContainerList = newsContainerListBean.getJahiaContainerList();
      }
   %>
  
  <jahia:jahiaPageForm name="jahiapageform">
  
  <%-- content:containerList name='<%="newsContainerLast5"+id%>' 
id='newsContainerListTaglib' windowSize="<%=selWindowSize%>" --%>
  <jahia:containerList name='<%="newsContainerLast5"+id%>' 
title='newsContainerListTaglib' windowSize="<%=selWindowSize%>" >
  
<%//----------------------------------------------------------------------------------------------------------------------
 %>
  <%//                                                    START - PAGINATION 
INFO                                            %>
  
<%//----------------------------------------------------------------------------------------------------------------------
 %>
  
  <jahia:cListPaginationCurrentPageScrollingValue valueOnly="false" />
  <table border="0">
  <%
  //text search is only available when using Lucene
  if (useLucene) { %>
  <tr>
      <td nowrap="nowrap" valign="top" align="left"><b><jahia:resourceBundle 
resourceBundle="jahiatemplates.Corporate_portal_templates" 
resourceName="search"/> : </b></td>
        <td valign="top" align="left">
                <input type="text" name="<jahia:ctnListSQueryInputName/>" 
value="<jahia:ctnListSQueryInputValue/>" size="25"/>  <a 
href="javascript:document.jahiapageform.submit()"><img 
src="<jahia:contextURL/>/images/search.gif" alt="" width="15" height="15" 
border="0" align="middle"/></a>
        </td>
  </tr>
  <% } %>
  
  <tr>
      <td nowrap="nowrap" align="left" valign="top"><b><jahia:resourceBundle 
resourceBundle="jahiatemplates.Corporate_portal_templates" 
resourceName="result"/> : </b></td>
      <td width="100%" align="left" valign="top">
                <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td align="left" valign="top" style="white-space: nowrap;">
                        <select class="text" 
name='<%="newsContainerLast5"+id%>_windowsize' 
onChange="javascript:document.jahiapageform.submit()">
                                <option value="5" <% if 
(selWindowSize.equals("5")){%>selected<% }%>>5</option>
                                <option value="10" <% if 
(selWindowSize.equals("10")){%>selected<% }%>>10</option>
                                <option value="20" <% if 
(selWindowSize.equals("20")){%>selected<% }%>>20</option>
                  </select> Items/Page
                  <br/><font size="2">[<jahia:cListPaginationFirstItemIndex /> 
- <jahia:cListPaginationLastItemIndex />] <jahia:resourceBundle 
resourceBundle="jahiatemplates.Corporate_portal_templates" resourceName="of"/> 
<jahia:cListPaginationTotalSize /></font>
              </td>
              <td align="right" valign="top" width="100%">
                        <jahia:previousWindowButton title='<%="&lt;&lt;" + 
getResourceBundle("previousResults","previousResults",jData)%>' method="post" 
formName="jahiapageform" />
                        <jahia:cListPagination nbStepPerPage="10" 
><jahia:cListPaginationPreviousRangeOfPages method="post" 
formName="jahiapageform" title=" .. "/>
                        
<jahia:ifCListPaginationCurrentPage><b></jahia:ifCListPaginationCurrentPage>
                        <jahia:cListPaginationPageUrl method="post" 
formName="jahiapageform" />
                        <jahia:ifCListPaginationCurrentPage></b>
                        </jahia:ifCListPaginationCurrentPage>
                        <jahia:cListPaginationNextRangeOfPages method="post" 
formName="jahiapageform" title=" .. "/></jahia:cListPagination>
                        <jahia:nextWindowButton 
title='<%=getResourceBundle("nextResults","nextResults",jData) + "&gt;&gt;"%>' 
method="post" formName="jahiapageform" />
                </td>
          </tr>
            </table>
      </td>
  </tr>
  
<%//----------------------------------------------------------------------------------------------------------------------
 %>
  <%//                                                    END - PAGINATION INFO 
                                             %>
  
<%//----------------------------------------------------------------------------------------------------------------------
 %>
  <tr>
      <td nowrap="nowrap" valign="top" align="left"><b>Scope : </b></td>
        <td valign="top" align="left">
                <input type="radio" name="searchScope" value="currentSite" <% 
if ( searchScopeVal!=-1 ){%>checked<% }%>
                 onchange="javascript:document.jahiapageform.submit()" > 
Current site only
             <br/>
                 <input type="radio" name="searchScope" value="allSites" <% if 
( searchScopeVal==-1 ){%>checked<% }%>
              onchange="javascript:document.jahiapageform.submit()"> All sites
        </td>
  </tr>
  
  
<%//----------------------------------------------------------------------------------------------------------------------
 %>
  <%//                                                    START - SORT ORDER    
                                             %>
  
<%//----------------------------------------------------------------------------------------------------------------------
 %>
  
  <% if (jData.gui().isEditMode()) { %>
     <tr align="center"> ---> <b>Filtering Disabled</b> <--- </tr>
  <% } %>
  
  <tr>
  <td nowrap="nowrap" align="left" valign="top"><b>Sort by :</b></td>
      <td width="100%" align="left" valign="top">
                <table border="0" cellpadding="0" cellspacing="0">
          <tr>
                  <a href="javascript:<%
                      
%>document.jahiapageform.<%="newsContainerLast5"+id%>_sort.value='newsStartDate';<%
                      
%>document.jahiapageform.<%="newsContainerLast5"+id%>_sort_order.value=<%
                      if (newsSortOrder.equals("asc")){%>'desc'<%} else 
{%>'asc'<%}
                      %>;document.jahiapageform.submit();" title="Select or 
toggle Start Date sort order">
                      <%if (newsSortReq.equals("newsStartDate")) {%><b>Start 
Date</b><%} else {%>Start Date<%}%>
                          <img
                          src="<%=theURL%>/images/sort_desc.gif" alt="sort" 
border="0" width="5" height="7"/><img
                          src="<%=theURL%>/images/sort_asc.gif" alt="" 
border="0" width="5" height="7"/></a>
                  <a href="javascript:<%
                      
%>document.jahiapageform.<%="newsContainerLast5"+id%>_sort.value='newsEndDate';<%
                      
%>document.jahiapageform.<%="newsContainerLast5"+id%>_sort_order.value=<%
                      if (newsSortOrder.equals("asc")){%>'desc'<%} else 
{%>'asc'<%}
                      %>;document.jahiapageform.submit();" title="Select or 
toggle End Date sort order">
                      <%if (newsSortReq.equals("newsEndDate")) {%><b>End 
Date</b><%} else {%>End Date<%}%>
                      <img
                          src="<%=theURL%>/images/sort_desc.gif" alt="sort" 
border="0" width="5" height="7"/><img
                          src="<%=theURL%>/images/sort_asc.gif" alt="" 
border="0" width="5" height="7"/></a>
                  <a href="javascript:<%
                      
%>document.jahiapageform.<%="newsContainerLast5"+id%>_sort.value='newsTitle';<%
                      
%>document.jahiapageform.<%="newsContainerLast5"+id%>_sort_order.value=<%
                      if (newsSortOrder.equals("asc")){%>'desc'<%} else 
{%>'asc'<%}
                      %>;document.jahiapageform.submit();" title="Select or 
toggle Title sort order">
                      <%if (newsSortReq.equals("newsTitle")) {%><b>Title</b><%} 
else {%>Title<%}%>
                      <img
                          src="<%=theURL%>/images/sort_desc.gif" alt="sort" 
border="0" width="5" height="7"/><img
                          src="<%=theURL%>/images/sort_asc.gif" alt="" 
border="0" width="5" height="7"/></a>
  
              </table>
          </tr>
  
<%//----------------------------------------------------------------------------------------------------------------------
 %>
  <%//                                                    END - SORT ORDER      
                                             %>
  
<%//----------------------------------------------------------------------------------------------------------------------
 %>
  </table>
  
  <input type="hidden" name='<%="newsContainerLast5"+id%>_sort' 
value="<%=newsSortReq%>"/>
  <input type="hidden" name='<%="newsContainerLast5"+id%>_sort_order' 
value="<%=newsSortOrder%>"/>
  
  </jahia:containerList>
  </jahia:jahiaPageForm>
  
  <%
  
//----------------------------------------------------------------------------------------------------------------------
  //                                                    START - DISPLAY NEWS 
ENTRIES
  
//----------------------------------------------------------------------------------------------------------------------
  Enumeration newsEnumeration = newsContainerList.getContainers();
  
  int newsCount=0;
  long cacheExpirationDelay = Long.MAX_VALUE;
  
  while (newsEnumeration.hasMoreElements()) {
      newsCount++;
      if (newsCount == 1){
          %><table border="0" cellpadding="0" cellspacing="0"><%
      }
      JahiaContainer newsContainer = 
(JahiaContainer)newsEnumeration.nextElement();
  
      //Retrieve the current fieldname postfix for the current container
      String newsTitle = newsContainer.getFieldValue("newsTitle" , "", true , 
jData.params());
      String newsDesc = newsContainer.getFieldValue("newsDesc"  , "", true , 
jData.params());
      JahiaPage myNewsLink = (JahiaPage) 
newsContainer.getFieldObject("newsLink" );
      String newsDate = (String) newsContainer.getFieldValue("newsStartDate" 
,"");
      String newsValidDate = (String) newsContainer.getFieldValue("newsEndDate" 
,"");
      String newsStartDateStr = (String) 
newsContainer.getFieldObject("newsStartDate");
      String newsEndDateStr = (String) 
newsContainer.getFieldObject("newsEndDate" );
  
      ContainerBean newsContainerBean = new 
ContainerBean(newsContainer,jData.params());
      jData.gui().html().drawBeginActionMenu(newsContainerBean , null, null, 
true, "", "jahiatemplates.Corporate_portal_templates", null, out);
          %><span class="bold"><%=newsTitle%></span><br><%
          if (! "".equals(newsDate)){
              %><i>Posted on the <%=newsDate%></i><br/><%
          }
          if (! "".equals(newsValidDate)){
              %><i>Valid until the <%=newsValidDate%></i><br/><%
          }
          %><%=newsDesc%><br/><%
          if (myNewsLink != null){
              %><a 
href="<%=myNewsLink.getUrl(jData.params())%>"><%=myNewsLink.getTitle()%></a><br><%
          }
          %><br/><br/><%
      jData.gui().html().drawEndActionMenu(newsContainerBean , null, null, 
true, "", "jahiatemplates.Corporate_portal_templates", null, out);
  }
  if (newsCount >0){
      %></table><%
  } else {
      %><content:resourceBundle 
resourceBundle="jahiatemplates.Corporate_portal_templates" 
resourceName="noNews"/><br/><%
  }
  ContainerListBean newsContainerListBean = new 
ContainerListBean(newsContainerList,jData.params());
  jData.gui().html().drawBeginActionMenu(newsContainerListBean , null, theURL + 
"/images/add.gif", false, "", "jahiatemplates.Corporate_portal_templates", 
"addNew", out);
  
  
//----------------------------------------------------------------------------------------------------------------------
  //                                                    END - DISPLAY NEWS 
ENTRIES
  
//----------------------------------------------------------------------------------------------------------------------
  %>
  
  
  
  

Reply via email to