You are here

piwik_reports.module in Piwik Reports 7.2

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']['piwik_token_auth']) && variable_get('piwik_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_menu().
 */
function piwik_reports_menu() {

  // Reports
  $items['admin/reports/piwik_reports'] = array(
    'title' => 'Piwik web analytics',
    'page callback' => 'piwik_reports_menu_block_page',
    'access arguments' => array(
      'access site 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 site reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 1,
    'type' => MENU_NORMAL_ITEM,
  );
  $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 site 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 site 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 site 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 site reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 4,
    '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 site reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 2,
    'type' => MENU_NORMAL_ITEM,
  );
  $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 site reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 1,
    'type' => MENU_DEFAULT_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 site reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 2,
    '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 site reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/referers'] = array(
    'title' => 'Referers',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'referers_evolution',
    ),
    'access arguments' => array(
      'access site reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 3,
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/reports/piwik_reports/referers/evolution'] = array(
    'title' => 'Evolution',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'referers_evolution',
    ),
    'access arguments' => array(
      'access site reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 1,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/referers/search'] = array(
    'title' => 'Search engines & keywords',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'referers_search',
    ),
    'access arguments' => array(
      'access site reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/reports/piwik_reports/referers/websites'] = array(
    'title' => 'Websites',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'piwik_reports_reports',
      'referers_websites',
    ),
    'access arguments' => array(
      'access site reports',
    ),
    'file' => 'piwik_reports.pages.inc',
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function piwik_reports_theme() {
  return array(
    'visitors_overview' => array(
      'template' => 'visitors-overview',
      'variables' => array(
        'piwik_url' => NULL,
        'data1_url' => NULL,
        'period' => NULL,
      ),
    ),
    'visitors_settings' => array(
      'template' => 'visitors-settings',
      'variables' => array(
        'piwik_url' => NULL,
        'data1_url' => NULL,
        'data2_url' => NULL,
        'data3_url' => NULL,
        'data4_url' => NULL,
      ),
    ),
    'visitors_times' => array(
      'template' => 'visitors-times',
      'variables' => array(
        'piwik_url' => NULL,
        'data1_url' => NULL,
      ),
    ),
    'visitors_locations' => array(
      'template' => 'visitors-locations',
      'variables' => array(
        'piwik_url' => NULL,
        'data1_url' => NULL,
      ),
    ),
    'referers_evolution' => array(
      'template' => 'referers-evolution',
      'variables' => array(
        'piwik_url' => NULL,
        'data1_url' => NULL,
      ),
    ),
    'referers_search' => array(
      'template' => 'referers-search',
      'variables' => array(
        'piwik_url' => NULL,
        'data1_url' => 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 site reports') && variable_get('piwik_token_auth', '') == '') {
    global $user;

    // Make sure the fieldset is also visible if user do not have permission
    // on the form field "Enable user tracking" in the main piwik module.
    // Additional the fieldset shouldn't be overriden by this hook if still active.
    if (empty($form['piwik'])) {
      $form['piwik'] = array(
        '#type' => 'fieldset',
        '#title' => t('Piwik configuration'),
        '#weight' => 3,
        '#collapsible' => TRUE,
        '#tree' => TRUE,
      );
    }
    $form['piwik']['piwik_token_auth'] = array(
      '#type' => 'textfield',
      '#title' => t('Piwik authentication string'),
      '#default_value' => !empty($user->data['piwik']['piwik_token_auth']) ? $user->data['piwik']['piwik_token_auth'] : 'anonymous',
      '#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>.'),
    );
  }
  elseif ($form_id == 'piwik_admin_settings_form') {
    $form['piwik_reports'] = array(
      '#type' => 'fieldset',
      '#title' => t('Piwik reports token_auth'),
      '#weight' => 0,
      '#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.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['piwik_reports']['piwik_token_auth'] = array(
      '#type' => 'textfield',
      '#title' => t('Piwik authentication string'),
      '#default_value' => variable_get('piwik_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>. In any case users still need adequate permissions in Drupal to see the reports.'),
    );
  }
}

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