View Javadoc
1   /*
2    * Copyright (c) 2002-2015, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the description of 'Mairie de Paris' nor 'Lutece' nor the descriptions of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.piwik.web;
35  
36  import fr.paris.lutece.portal.service.content.PageData;
37  import fr.paris.lutece.portal.service.datastore.DatastoreService;
38  import fr.paris.lutece.portal.service.includes.PageInclude;
39  import fr.paris.lutece.portal.service.plugin.PluginService;
40  import fr.paris.lutece.portal.service.template.AppTemplateService;
41  import fr.paris.lutece.portal.service.util.AppPropertiesService;
42  import fr.paris.lutece.util.html.HtmlTemplate;
43  
44  import java.util.HashMap;
45  import java.util.Map;
46  
47  import javax.servlet.http.HttpServletRequest;
48  
49  
50  /**
51   * Include of the Piwik analytics code
52   */
53  public class PiwikInclude implements PageInclude
54  {
55      private static final String PROPERTY_DEFAULT_SITE_ID = "piwik.default.site.id";
56      private static final String PROPERTY_DEFAULT_SERVER_HTTP_URL = "piwik.default.server.http.url";
57      private static final String PROPERTY_DEFAULT_SERVER_HTTPS_URL = "piwik.default.server.https.url";
58      private static final String PROPERTY_DEFAULT_AUTH_TOKEN = "piwik.default.widget.auth.token";
59      private static final String DSKEY_SITE_ID = "piwik.site_property.site.id";
60      private static final String DSKEY_SERVER_HTTP_URL = "piwik.site_property.server.http.url";
61      private static final String DSKEY_SERVER_HTTPS_URL = "piwik.site_property.server.https.url";
62      private static final String DSKEY_AUTH_TOKEN = "piwik.site_property.widget.auth.token";
63      private static final String MARK_PIWIK = "piwik";
64      private static final String PLUGIN_NAME = "piwik";
65      private static final String TEMPLATE_PIWIK_INCLUDE = "skin/plugins/piwik/piwik_analytics.html";
66      private static final String MARK_SITE_ID = "site_id";
67      private static final String MARK_SERVER_HTTP_URL = "server_http_url";
68      private static final String MARK_SERVER_HTTPS_URL = "server_https_url";
69      
70      private final String _strDefaultSiteId;
71      private final String _strDefaultServerHttpUrl;
72      private final String _strDefaultServerHttpsUrl;
73      private final String _strDefaultAuthToken;
74  
75      /**
76       * Constructor
77       */
78      public PiwikInclude(  )
79      {
80          _strDefaultSiteId = AppPropertiesService.getProperty( PROPERTY_DEFAULT_SITE_ID,
81                  "<no site id provided>" );
82          _strDefaultServerHttpUrl = AppPropertiesService.getProperty( PROPERTY_DEFAULT_SERVER_HTTP_URL,
83                      "<no server http url provided>" );
84          _strDefaultServerHttpsUrl = AppPropertiesService.getProperty( PROPERTY_DEFAULT_SERVER_HTTPS_URL,
85                      "<no server https url provided>" );
86          _strDefaultAuthToken = AppPropertiesService.getProperty( PROPERTY_DEFAULT_AUTH_TOKEN, "" );
87  
88          // initialize the datastore if it has not yet been done
89          if( ! DatastoreService.existsKey( DSKEY_SITE_ID ) )
90          {
91              DatastoreService.setDataValue( DSKEY_SITE_ID, _strDefaultSiteId );
92          }
93          if( ! DatastoreService.existsKey( DSKEY_SERVER_HTTP_URL ) )
94          {
95              DatastoreService.setDataValue( DSKEY_SERVER_HTTP_URL, _strDefaultServerHttpUrl );
96          }
97          if( ! DatastoreService.existsKey( DSKEY_SERVER_HTTPS_URL ) )
98          {
99              DatastoreService.setDataValue( DSKEY_SERVER_HTTPS_URL, _strDefaultServerHttpsUrl );
100         }
101         if( ! DatastoreService.existsKey( DSKEY_AUTH_TOKEN ) )
102         {
103             DatastoreService.setDataValue( DSKEY_AUTH_TOKEN, _strDefaultAuthToken );
104         }
105     }
106 
107     /**
108      * Substitute specific Freemarker markers in the page template.
109      * @param rootModel the HashMap containing markers to substitute
110      * @param data A PageData object containing applications data
111      * @param nMode The current mode
112      * @param request The HTTP request
113      */
114     @Override
115     public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
116     {
117         if ( PluginService.isPluginEnable( PLUGIN_NAME ) && ( request != null ) )
118         {
119             Map<String, Object> model = new HashMap<String, Object>(  );
120             String strSiteId = DatastoreService.getDataValue( DSKEY_SITE_ID, _strDefaultSiteId );
121             String strServerHttpUrl = DatastoreService.getDataValue( DSKEY_SERVER_HTTP_URL, _strDefaultServerHttpUrl );
122             String strServerHttpsUrl = DatastoreService.getDataValue( DSKEY_SERVER_HTTPS_URL, _strDefaultServerHttpsUrl );
123             model.put( MARK_SITE_ID, strSiteId );
124             model.put( MARK_SERVER_HTTP_URL, strServerHttpUrl );
125             model.put( MARK_SERVER_HTTPS_URL, strServerHttpsUrl );
126 
127             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PIWIK_INCLUDE, request.getLocale(  ), model );
128 
129             rootModel.put( MARK_PIWIK, template.getHtml(  ) );
130         }
131         else
132         {
133             rootModel.put( MARK_PIWIK, "" );
134         }
135     }
136 
137 }