You are here

function piwik_reports_block_view in Piwik Reports 7.3

Same name and namespace in other branches
  1. 7.4 piwik_reports.module \piwik_reports_block_view()

Implements hook_block_view().

File

./piwik_reports.module, line 283
Defines features and functions common to Piwik Reports.

Code

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;
}