View Javadoc
1   /*
2    * Copyright (c) 2002-2019, 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.matomo.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   * Include of the Matomo analytics code
51   */
52  public class MatomoInclude implements PageInclude
53  {
54      private static final String PROPERTY_DEFAULT_SITE_ID = "matomo.default.site.id";
55      private static final String PROPERTY_DEFAULT_SERVER_HTTP_URL = "matomo.default.server.http.url";
56      private static final String PROPERTY_DEFAULT_SERVER_HTTPS_URL = "matomo.default.server.https.url";
57      private static final String PROPERTY_DEFAULT_AUTH_TOKEN = "matomo.default.widget.auth.token";
58      private static final String DSKEY_SITE_ID = "matomo.site_property.site.id";
59      private static final String DSKEY_SERVER_HTTP_URL = "matomo.site_property.server.http.url";
60      private static final String DSKEY_SERVER_HTTPS_URL = "matomo.site_property.server.https.url";
61      private static final String DSKEY_AUTH_TOKEN = "matomo.site_property.widget.auth.token";
62      private static final String MARK_MATOMO = "matomo";
63      private static final String PLUGIN_NAME = "matomo";
64      private static final String TEMPLATE_MATOMO_INCLUDE = "skin/plugins/matomo/matomo_analytics.html";
65      private static final String MARK_SITE_ID = "site_id";
66      private static final String MARK_SERVER_HTTP_URL = "server_http_url";
67      private static final String MARK_SERVER_HTTPS_URL = "server_https_url";
68  
69      private final String _strDefaultSiteId;
70      private final String _strDefaultServerHttpUrl;
71      private final String _strDefaultServerHttpsUrl;
72      private final String _strDefaultAuthToken;
73  
74      /**
75       * Constructor
76       */
77      public MatomoInclude( )
78      {
79          _strDefaultSiteId = AppPropertiesService.getProperty( PROPERTY_DEFAULT_SITE_ID, "<no site id provided>" );
80          _strDefaultServerHttpUrl = AppPropertiesService.getProperty( PROPERTY_DEFAULT_SERVER_HTTP_URL, "<no server http url provided>" );
81          _strDefaultServerHttpsUrl = AppPropertiesService.getProperty( PROPERTY_DEFAULT_SERVER_HTTPS_URL, "<no server https url provided>" );
82          _strDefaultAuthToken = AppPropertiesService.getProperty( PROPERTY_DEFAULT_AUTH_TOKEN, "" );
83  
84          // initialize the datastore if it has not yet been done
85          if ( !DatastoreService.existsKey( DSKEY_SITE_ID ) )
86          {
87              DatastoreService.setDataValue( DSKEY_SITE_ID, _strDefaultSiteId );
88          }
89          if ( !DatastoreService.existsKey( DSKEY_SERVER_HTTP_URL ) )
90          {
91              DatastoreService.setDataValue( DSKEY_SERVER_HTTP_URL, _strDefaultServerHttpUrl );
92          }
93          if ( !DatastoreService.existsKey( DSKEY_SERVER_HTTPS_URL ) )
94          {
95              DatastoreService.setDataValue( DSKEY_SERVER_HTTPS_URL, _strDefaultServerHttpsUrl );
96          }
97          if ( !DatastoreService.existsKey( DSKEY_AUTH_TOKEN ) )
98          {
99              DatastoreService.setDataValue( DSKEY_AUTH_TOKEN, _strDefaultAuthToken );
100         }
101     }
102 
103     /**
104      * Substitute specific Freemarker markers in the page template.
105      * 
106      * @param rootModel
107      *            the HashMap containing markers to substitute
108      * @param data
109      *            A PageData object containing applications data
110      * @param nMode
111      *            The current mode
112      * @param request
113      *            The HTTP request
114      */
115     @Override
116     public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
117     {
118         if ( PluginService.isPluginEnable( PLUGIN_NAME ) && ( request != null ) )
119         {
120             Map<String, Object> model = new HashMap<String, Object>( );
121             String strSiteId = DatastoreService.getDataValue( DSKEY_SITE_ID, _strDefaultSiteId );
122             String strServerHttpUrl = DatastoreService.getDataValue( DSKEY_SERVER_HTTP_URL, _strDefaultServerHttpUrl );
123             String strServerHttpsUrl = DatastoreService.getDataValue( DSKEY_SERVER_HTTPS_URL, _strDefaultServerHttpsUrl );
124             model.put( MARK_SITE_ID, strSiteId );
125             model.put( MARK_SERVER_HTTP_URL, strServerHttpUrl );
126             model.put( MARK_SERVER_HTTPS_URL, strServerHttpsUrl );
127 
128             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MATOMO_INCLUDE, request.getLocale( ), model );
129 
130             rootModel.put( MARK_MATOMO, template.getHtml( ) );
131         }
132         else
133         {
134             rootModel.put( MARK_MATOMO, "" );
135         }
136     }
137 
138 }