You are here

piwik_reports.module in Piwik Reports 7.4

Defines features and functions common to Piwik Reports.

File

piwik_reports.module
View source
<?php

/**
 * @file
 * Defines features and functions common to Piwik Reports.
 */

/**
 * Implements hook_help().
 */
function piwik_reports_help($path, $arg) {
  global $user;
  switch ($path) {
    case arg(0) == 'admin' && arg(1) == 'reports' && arg(2) == 'piwik_reports':
      if ($user->uid && empty($user->data['piwik_reports_token_auth']) && variable_get('piwik_reports_token_auth', '') == '') {
        drupal_set_message(t('You need to configure your <em>Piwik authentication string</em> on the <a href="@user-edit">My account</a> page.', array(
          '@user-edit' => url('user/' . $user->uid . '/edit'),
        )), 'warning', FALSE);
      }
  }
}

/**
 * Implements hook_permission().
 */
function piwik_reports_permission() {
  return array(
    'access piwik reports' => array(
      'title' => t('Piwik reports access'),
      'description' => t('Allow access to Piwik reports.'),
    ),
    'administer piwik reports' => array(
      'title' => t('Administer Piwik reports'),
      'description' => t('Perform basic configuration tasks for Piwik reports.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function piwik_reports_menu() {

  // Reports:
  $items['admin/reports/piwik_reports'] = array(
    'title' => 'Piwik Web Analytics',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'visitors_overview',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'description' => 'Reports on your website visitors, your popular pages, the search engines keywords they used... and so much more.',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/reports/piwik_reports/visitors'] = array(
    'title' => 'Visitors',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'visitors_overview',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 1,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/visitors/overview'] = array(
    'title' => 'Overview',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'visitors_overview',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 1,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/visitors/settings'] = array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'visitors_settings',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/visitors/times'] = array(
    'title' => 'Times',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'visitors_times',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/visitors/locations'] = array(
    'title' => 'Locations Provider',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'visitors_locations',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 4,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/visitors/variables'] = array(
    'title' => 'Custom Variables',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'custom_variables',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 5,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/actions'] = array(
    'title' => 'Actions',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'actions_pageurls',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/actions/pages'] = array(
    'title' => 'Pages',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'actions_pageurls',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 1,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/actions/entrypages'] = array(
    'title' => 'Entry Pages',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'actions_entryurls',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/actions/exitpages'] = array(
    'title' => 'Exit Pages',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'actions_exiturls',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/actions/sitesearch'] = array(
    'title' => 'Site Search',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'actions_sitesearch',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/actions/outlinks'] = array(
    'title' => 'Outlinks',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'actions_outlinks',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/actions/downloads'] = array(
    'title' => 'Downloads',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'actions_downloads',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 4,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/referrers'] = array(
    'title' => 'Referrers',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'referrers_overview',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/referrers/overview'] = array(
    'title' => 'Overview',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'referrers_overview',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 1,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/referrers/search'] = array(
    'title' => 'Search Engines & Keywords',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'referrers_search',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/referrers/websites'] = array(
    'title' => 'Websites',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'referrers_websites',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/goals'] = array(
    'title' => 'Goals',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'goals',
    ),
    'access arguments' => array(
      'access piwik reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 4,
    'type' => MENU_LOCAL_TASK,
  );

  // If piwik is installed, group with it, otherwise not.
  if (module_exists('piwik')) {
    $items['admin/config/system/piwik/tracking'] = array(
      'title' => 'Tracking',
      'type' => MENU_NORMAL_ITEM,
      'description' => 'Configure the settings used to generate your Piwik tracking code.',
      'page callback' => 'drupal_get_form',
      'access arguments' => array(
        'administer piwik',
      ),
      'page arguments' => array(
        'piwik_admin_settings_form',
      ),
      'file path' => drupal_get_path('module', 'piwik'),
      'file' => 'piwik.admin.inc',
    );
    $items['admin/config/system/piwik/reports'] = array(
      'title' => 'Reports',
      'description' => 'Configure the settings for your Piwik reports.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'piwik_reports_admin_settings_form',
      ),
      'access arguments' => array(
        'administer piwik reports',
      ),
      'type' => MENU_NORMAL_ITEM,
      'weight' => 10,
    );
  }
  else {
    $items['admin/config/system/piwik_reports'] = array(
      'title' => 'Piwik Reports',
      'description' => 'Configure the settings for your Piwik reports.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'piwik_reports_admin_settings_form',
      ),
      'access arguments' => array(
        'administer piwik reports',
      ),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function piwik_reports_menu_alter(&$items) {
  if (module_exists('piwik')) {
    $items['admin/config/system/piwik'] = array(
      'title' => 'Piwik',
      'description' => 'Configure the settings for Piwik.',
      'access arguments' => array(
        array(
          'administer piwik',
          'administer piwik reports',
        ),
      ),
      'access callback' => 'piwik_reports_any_access',
      'type' => MENU_NORMAL_ITEM,
    );
  }
}

/**
 * Implements hook_block_info().
 */
function piwik_reports_block_info() {
  $blocks['piwik_page_report'] = array(
    'info' => t('Piwik Page Statistics'),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function piwik_reports_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'piwik_page_report':
      $block['subject'] = t('Piwik Page Statistics');
      if (!module_exists('piwik')) {
        $block['content'] = t('To use this block you need to install <a href="!url">Piwik</a> module', array(
          '!url' => 'http://www.drupal.org/project/piwik',
        ));
        return $block;
      }
      if ($router_item = menu_get_item()) {
        if ($router_item['access'] != TRUE) {
          return;
        }
      }
      if (!user_access('access piwik reports')) {
        return;
      }

      // Build the data URL with all params.
      global $user;
      $piwik_auth = check_plain(variable_get('piwik_reports_token_auth', '') ? variable_get('piwik_reports_token_auth', '') : (isset($user->data['piwik_reports_token_auth']) ? $user->data['piwik_reports_token_auth'] : ''));
      $piwik_url = variable_get('piwik_reports_url_http', '');
      $data_params = array();
      $data_params['idSite'] = variable_get('piwik_site_id', '');
      $data_params['date'] = 'today';
      $data_params['period'] = 'year';
      $data_params['module'] = 'API';
      $data_params['method'] = 'Actions.getPageUrl';
      $data_params['pageUrl'] = urldecode($_SERVER['REQUEST_URI']);
      $data_params['format'] = 'JSON';
      if (!empty($piwik_auth)) {
        $data_params['token_auth'] = $piwik_auth;
      }
      $data_url = $piwik_url . 'index.php?' . drupal_http_build_query($data_params) . '&jsoncallback=?';
      $block['content'] = array(
        '#markup' => '<div id="piwikpageviews"></div>',
        '#attached' => array(
          'js' => array(
            array(
              'type' => 'setting',
              'data' => array(
                'piwik_reports' => array(
                  'url' => $data_url,
                ),
              ),
            ),
            array(
              'data' => drupal_get_path('module', 'piwik_reports') . '/piwik_reports.js',
              'type' => 'file',
            ),
          ),
        ),
      );
      break;
  }
  return $block;
}

/**
 * Implements hook_theme().
 */
function piwik_reports_theme() {
  return array(
    'piwik_reports' => array(
      'template' => 'piwik-reports',
      'variables' => array(
        'data_url' => NULL,
        'period' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_form_alter().
 */
function piwik_reports_form_alter(&$form, $form_state, $form_id) {

  // Extend the form in "user/{userid}/edit/account".
  if ($form_id == 'user_profile_form' && $form['#user_category'] == 'account' && user_access('access piwik reports') && variable_get('piwik_reports_token_auth', '') == '') {
    $account = $form['#user'];
    $form['piwik_reports'] = array(
      '#type' => 'fieldset',
      '#title' => t('Piwik reports configuration'),
      '#weight' => 3,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['piwik_reports']['piwik_reports_token_auth'] = array(
      '#type' => 'textfield',
      '#title' => t('Piwik authentication string'),
      '#default_value' => !empty($account->data['piwik_reports_token_auth']) ? $account->data['piwik_reports_token_auth'] : '',
      '#size' => 40,
      '#maxlength' => 40,
      '#description' => t('Click the <strong>Settings</strong> link in your Piwik account, then the <strong>Users</strong> tab and copy and paste your personal <strong>token_auth</strong> value into this field. If anonymous users have view permissions in Piwik, you can set this value to <strong>anonymous</strong> or ask the Piwik server administrator for a token value.'),
    );
  }
  elseif ($form_id == 'piwik_admin_settings_form') {
    drupal_set_title(t('Piwik tracking'));
  }
}

/**
 * Determine if user has any piwik reports permissions.
 *
 * @param array $arguments
 *   Array of permissions.
 *
 * @return bool
 *   True if user has any piwik reports permissions.
 */
function piwik_reports_any_access($arguments) {
  foreach ($arguments as $perm) {
    if (user_access($perm)) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Implements hook_user_presave().
 */
function piwik_reports_user_presave(&$edit, $account, $category) {
  if (isset($edit['piwik_reports_token_auth'])) {
    $edit['data']['piwik_reports_token_auth'] = $edit['piwik_reports_token_auth'];
  }
}

/**
 * Return the $form that handles piwik reports config page.
 *
 * @param array $form_state
 *   See form API.
 *
 * @return array
 *   Piwik form fields.
 */
function piwik_reports_admin_settings_form($form_state) {
  drupal_set_title(t('Piwik reports'));
  $piwik_installed = module_exists('piwik');
  $form['piwik_reports_server'] = array(
    '#type' => 'fieldset',
    '#title' => t('Piwik report server'),
    '#weight' => 1,
    '#collapsible' => TRUE,
    '#collapsed' => $piwik_installed,
  );
  if ($piwik_installed) {
    $form['piwik_reports_server']['#description'] = t('Leave blank to use the same URL set in Piwik tracking.');
  }
  $form['piwik_reports_server']['piwik_reports_url_http'] = array(
    '#type' => 'textfield',
    '#title' => t('Piwik Server URL'),
    '#default_value' => variable_get('piwik_reports_url_http', ''),
    '#size' => 80,
    '#maxlength' => 255,
    '#required' => !$piwik_installed,
    '#weight' => 1,
    '#description' => t('The URL to your Piwik base directory. Example: "http://www.example.com/piwik/".'),
  );
  $form['piwik_reports_token'] = array(
    '#type' => 'fieldset',
    '#title' => t('Token auth'),
    '#weight' => 5,
    '#description' => t('To see piwik reports in Drupal you need a <strong>token_auth</strong> value. You can find it in the  <strong>Users</strong> tab under the <strong>Settings</strong> link in your Piwik account or ask your Piwik server administrator.'),
  );
  $form['piwik_reports_token']['piwik_reports_token_auth'] = array(
    '#type' => 'textfield',
    '#title' => t('Piwik authentication string'),
    '#default_value' => check_plain(variable_get('piwik_reports_token_auth', '')),
    '#size' => 40,
    '#maxlength' => 40,
    '#description' => t('Leave blank if you prefer each user setting his own, or paste it here to have a global <strong>token_auth</strong>. If anonymous users have view permissions in Piwik you can set this value to <strong>anonymous</strong>. Users still need "Access Piwik reports" permission to see the reports in Drupal.'),
  );
  $form['piwik_reports_sites'] = array(
    '#type' => 'fieldset',
    '#title' => t('Allowed sites'),
    '#weight' => 10,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('List sites you want restrict your users access to.'),
  );
  $sites = piwik_reports_get_sites(check_plain(variable_get('piwik_reports_token_auth', '')));
  $form['piwik_reports_sites']['piwik_reports_allowed_sites'] = array(
    '#type' => 'textfield',
    '#weight' => 10,
    '#collapsible' => TRUE,
    '#default_value' => variable_get('piwik_reports_allowed_sites', ''),
    '#collapsed' => TRUE,
    '#description' => t('List accessible sites id separated by a comma. Example: "1,4,12". Leave blank to let users see all sites accessible on piwik server with current token auth (highly recommended in case of per user token auth).'),
  );
  if (is_array($sites) && count($sites)) {
    if (variable_get('piwik_reports_token_auth', '') != '') {
      $form['piwik_reports_sites']['piwik_reports_allowed_sites']['#description'] .= ' ' . t('Sites currently accessible with global token_auth are:');
    }
    else {
      $form['piwik_reports_sites']['piwik_reports_allowed_sites']['#description'] .= ' ' . t('Sites currently accessible as anonymous are:');
    }
    foreach ($sites as $site) {
      $form['piwik_reports_sites']['piwik_reports_allowed_sites']['#description'] .= '<br />' . (int) $site['idsite'] . ' - ' . check_plain($site['name']);
    }
  }
  else {
    $form['piwik_reports_sites']['piwik_reports_allowed_sites']['#description'] .= ' ' . t('No accessible sites are available with current global token auth. Please check your token auth is correct and that it has view permission on Piwik server.');
  }
  return system_settings_form($form);
}

/*
 * Implements hook_form_validate().
 *
 * Validate the server url.
 */
function piwik_reports_admin_settings_form_validate($form, &$form_state) {
  $piwik_url = $form_state['values']['piwik_reports_url_http'];
  if ('/' != drupal_substr($piwik_url, -1, 1)) {
    $piwik_url = $piwik_url . '/';
    $form_state['values']['piwik_reports_url_http'] = $piwik_url;
  }
  $url = $piwik_url . 'piwik.php';
  $result = drupal_http_request($url);
  if ($result->code != 200) {
    form_set_error('piwik_reports_url_http', t('The validation of "@url" failed with error "@error" (HTTP code @code).', array(
      '@url' => check_url($url),
      '@error' => $result->error,
      '@code' => $result->code,
    )));
  }
}

/**
 * Return a list of sites which statistic are accessible on piwik server.
 *
 * @param string $token_auth
 *   Piwik server token auth.
 *
 * @return array
 *   Array of sites returned from Piwik reports API.
 */
function piwik_reports_get_sites($token_auth) {

  // TODO: shall I cache this?
  $result = drupal_http_request(piwik_reports_get_url() . 'index.php?module=API&method=SitesManager.getSitesWithAtLeastViewAccess&format=JSON&token_auth=' . $token_auth);
  if (empty($result->data)) {
    $sites = array();
  }
  else {
    $sites = drupal_json_decode($result->data);
  }
  return $sites;
}

/**
 * Return a list of goals active on selected site.
 *
 * @param string $token_auth
 *   Piwik server token auth.
 * @param string $site
 *   Selected site id.
 *
 * @return array
 *   Goals returned from Piwik reports API.
 */
function piwik_reports_get_goals($token_auth, $site) {

  // TODO: shall I cache this?
  $result = drupal_http_request(piwik_reports_get_url() . 'index.php?module=API&method=Goals.getGoals&idSite=' . (int) $site . '&format=JSON&token_auth=' . $token_auth);
  if (empty($result->data)) {
    $goals = array();
  }
  else {
    $goals = drupal_json_decode($result->data);
  }
  return $goals;
}

/**
 * Return Piwik server url.
 *
 * @return string
 *   Stored value of Piwik server URL.
 */
function piwik_reports_get_url() {
  $url = variable_get('piwik_reports_url_http', '');
  if ($url == '') {
    if (module_exists('piwik')) {
      $url = variable_get('piwik_url_http', '');
    }
  }
  if ($url == '') {
    drupal_set_message(t('Piwik server url is missing or wrong. Please ask your administrator to check Piwik Reports configuration.'), 'warning', FALSE);
  }
  return $url;
}

Functions

Namesort descending Description
piwik_reports_admin_settings_form Return the $form that handles piwik reports config page.
piwik_reports_admin_settings_form_validate
piwik_reports_any_access Determine if user has any piwik reports permissions.
piwik_reports_block_info Implements hook_block_info().
piwik_reports_block_view Implements hook_block_view().
piwik_reports_form_alter Implements hook_form_alter().
piwik_reports_get_goals Return a list of goals active on selected site.
piwik_reports_get_sites Return a list of sites which statistic are accessible on piwik server.
piwik_reports_get_url Return Piwik server url.
piwik_reports_help Implements hook_help().
piwik_reports_menu Implements hook_menu().
piwik_reports_menu_alter Implements hook_menu_alter().
piwik_reports_permission Implements hook_permission().
piwik_reports_theme Implements hook_theme().
piwik_reports_user_presave Implements hook_user_presave().