You are here

counter.module in Counter 5

Same filename and directory in other branches
  1. 8 counter.module
  2. 6.2 counter.module
  3. 6 counter.module
  4. 7 counter.module

The counter module used for displaying Site Counter.

File

counter.module
View source
<?php

/**
 * @file
 * The counter module used for displaying Site Counter.
 */

/**
 * Implementation of hook_help().
 */
function counter_help($section) {
  switch ($section) {
    case 'admin/help#Counter':
      $output = "The counter module used for displaying Site Counter.";
      return $output;
    case 'admin/modules#description':
      return 'The counter module used for displaying Site Counter';
  }
}

/**
 * Implementation of hook_perm
 */
function counter_perm() {
  return array(
    'access counter',
    'administer counter',
  );
}

/**
 * Menu callback. Prints a listing of active nodes on the site.
 */
function counter_menu() {
  $items = array();
  $items['admin/settings/counter'] = array(
    'title' => 'Counter settings',
    'description' => 'Show Site Counter, Client IP, and Unique Visitor.',
    'access arguments' => array(
      'administer counter',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'counter_admin_settings',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['counter/report'] = array(
    'title' => 'Counter Report',
    'description' => 'View Counter Report',
    'access arguments' => array(
      'administer counter',
    ),
    'page callback' => 'counter_report',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}
function counter_admin_settings() {
  $form = array();

  // only administrators can access this function
  // Generate the form - settings applying to all patterns first
  $form['counter_settings'] = array(
    '#type' => 'fieldset',
    '#weight' => -20,
    '#title' => t('Basic settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#description' => l(t('Click here to access: Site Counter Report'), "counter/report"),
  );
  $form['counter_settings']['counter_show_site_counter'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Site Counter'),
    '#default_value' => variable_get('counter_show_site_counter', 1),
    '#description' => t('Show Site Counter'),
  );
  $form['counter_settings']['counter_show_unique_visitor'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Unique Visitors'),
    '#default_value' => variable_get('counter_show_unique_visitor', 1),
    '#description' => t('Show Unique Visitors'),
  );
  $form['counter_settings']['counter_registered_user'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Registered Users'),
    '#default_value' => variable_get('counter_registered_user', 1),
    '#description' => t('Show Registered Users'),
  );
  $form['counter_settings']['counter_unregistered_user'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Unregistered Nodes'),
    '#default_value' => variable_get('counter_unregistered_user', 1),
    '#description' => t('Show Unregistered Users'),
  );
  $form['counter_settings']['counter_published_node'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Published Nodes'),
    '#default_value' => variable_get('counter_published_node', 1),
    '#description' => t('Show Published Nodes'),
  );
  $form['counter_settings']['counter_unpublished_node'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Unpublished Users'),
    '#default_value' => variable_get('counter_unpublished_node', 1),
    '#description' => t('Show Unpublished Users'),
  );
  $form['counter_settings']['counter_show_ip'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Client IP'),
    '#default_value' => variable_get('counter_show_ip', 1),
    '#description' => t('Show Client IP'),
  );
  $form['counter_settings']['counter_show_counter_since'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Sice Counter Since'),
    '#default_value' => variable_get('counter_show_counter_since', 1),
    '#description' => t('Show the first entry date in the Site Counter'),
  );
  $form['counter_initial'] = array(
    '#type' => 'fieldset',
    '#weight' => -20,
    '#title' => t('Initial Values'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#description' => t("Set initial values for Site Counter."),
  );
  $form['counter_initial']['counter_initial_counter'] = array(
    '#type' => 'textfield',
    '#title' => t('Initial value of Site Counter'),
    '#default_value' => variable_get('counter_initial_counter', 0),
    '#description' => t('Initial value of Site Counter'),
  );
  $form['counter_initial']['counter_initial_unique_visitor'] = array(
    '#type' => 'textfield',
    '#title' => t('Initial value of Unique Visitor'),
    '#default_value' => variable_get('counter_initial_unique_visitor', 0),
    '#description' => t('Initial value of Unique Visitor'),
  );
  $form['counter_initial']['counter_initial_since'] = array(
    '#type' => 'textfield',
    '#title' => t("Replace 'Since' value with this string"),
    '#default_value' => variable_get('counter_initial_since', ''),
    '#description' => t("If you leave this field blank than Counter module will use the first date of Counter record. This field type is textfield, so you can enter: '2008-08-12 or 12 August 2008 06:39'."),
  );
  return system_settings_form($form);
}
function counter_report() {
  $items_per_page = variable_get('default_nodes_main', 10);
  $sql = "SELECT * FROM {counter} ORDER BY counter_id DESC";
  $sql_count = db_rewrite_sql('SELECT COUNT(*) FROM {counter}');
  $results = pager_query($sql, $items_per_page, 0, $sql_count);
  $rows = array();
  $page = isset($_GET['page']) ? $_GET['page'] : '';
  $i = 1 + $page * $items_per_page;
  $header = array(
    t('#'),
    t('ID'),
    t('IP Address'),
    t('Created Date'),
    t('Access page'),
  );
  while ($data = db_fetch_object($results)) {
    $rows[] = array(
      $i++,
      $data->counter_id,
      $data->counter_ip,
      $data->counter_date,
      $data->counter_page,
    );
  }
  $output = "";
  $output .= theme('table', $header, $rows);
  $output .= theme('pager', NULL, $items_per_page, 0);
  return $output;
}

/**
 * Implementation of hook_block().
 *
 */
function counter_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = 'Site Counter';
    return $blocks;
  }
  if ($op == 'view') {
    $counter_show_site_counter = variable_get('counter_show_site_counter', 1);
    $counter_show_unique_visitor = variable_get('counter_show_unique_visitor', 1);
    $counter_registered_user = variable_get('counter_registered_user', 1);
    $counter_unregistered_user = variable_get('counter_unregistered_user', 1);
    $counter_published_node = variable_get('counter_published_node', 1);
    $counter_unpublished_node = variable_get('counter_unpublished_node', 1);
    $counter_show_ip = variable_get('counter_show_ip', 1);
    $counter_show_counter_since = variable_get('counter_show_counter_since', 1);
    $counter_initial_counter = variable_get('counter_initial_counter', 0);
    $counter_initial_unique_visitor = variable_get('counter_initial_unique_visitor', 0);
    $counter_initial_since = variable_get('counter_initial_since', '');
    switch ($delta) {
      case 0:
        $block['subject'] = 'Site Counter';
        $output = '';
        $counter_ip = $_SERVER['REMOTE_ADDR'];
        $counter_page = arg(0);
        if (arg(1) != '') {
          $counter_page .= "," . arg(1);
        }
        if (arg(2) != '') {
          $counter_page .= "," . arg(2);
        }
        if (arg(3) != '') {
          $counter_page .= "," . arg(3);
        }
        $counter_date = date('Y-m-d');

        //Check database
        $sql = " SELECT count(*) AS total FROM {counter}" . " WHERE counter_ip='{$counter_ip}' AND counter_date='{$counter_date}' AND counter_page='{$counter_page}'";
        $results = db_query($sql);
        $data = db_fetch_object($results);
        $counter_check = $data->total;
        if (!$counter_check) {
          $sql = " INSERT IGNORE INTO {counter}  " . " (counter_ip, counter_date, counter_page) VALUES " . " ('{$counter_ip}', '{$counter_date}', '{$counter_page}') ";
          $results = db_query($sql);
        }
        $output .= '<ul>';
        if ($counter_show_site_counter) {
          $sql = " SELECT count(*) as total FROM {counter} c ";
          $results = db_query($sql);
          $data = db_fetch_object($results);
          $counter_total = $data->total;
          $output .= '<li/>' . t('Site Counter: ') . ($counter_initial_counter + $counter_total);
        }
        if ($counter_show_unique_visitor) {
          $sql = " SELECT count(*) as total FROM (SELECT counter_ip FROM {counter} GROUP BY counter_ip) c";
          $results = db_query($sql);
          $data = db_fetch_object($results);
          $counter_unique = $data->total;
          $output .= '<li/>' . t('Unique Visitor: ') . ($counter_initial_unique_visitor + $counter_unique);
        }
        if ($counter_registered_user) {
          $sql = " SELECT count(*) as total FROM {users} WHERE status=1 and uid<>0";
          $results = db_query($sql);
          $data = db_fetch_object($results);
          $total = $data->total;
          $output .= '<li/>' . t('Registered Users: ') . $total;
        }
        if ($counter_unregistered_user) {
          $sql = " SELECT count(*) as total FROM {users} WHERE status=0 and uid<>0";
          $results = db_query($sql);
          $data = db_fetch_object($results);
          $total = $data->total;
          $output .= '<li/>' . t('Unregistered Users: ') . $total;
        }
        if ($counter_published_node) {
          $sql = " SELECT count(*) as total FROM {node} WHERE status=1";
          $results = db_query($sql);
          $data = db_fetch_object($results);
          $total = $data->total;
          $output .= '<li/>' . t('Published Nodes: ') . $total;
        }
        if ($counter_unpublished_node) {
          $sql = " SELECT count(*) as total FROM {node} WHERE status=0";
          $results = db_query($sql);
          $data = db_fetch_object($results);
          $total = $data->total;
          $output .= '<li/>' . t('Unpublished Nodes: ') . $total;
        }
        if ($counter_show_ip) {
          $output .= '<li/>' . t("Your IP: ") . $counter_ip;
        }
        if ($counter_show_counter_since) {
          $sql = " SELECT counter_date FROM {counter} order by counter_date ASC LIMIT 1";
          $results = db_query($sql);
          $data = db_fetch_object($results);
          $counter_since = $data->counter_date;
          if ($counter_initial_since == "") {
            $output .= '<li/>' . t("Since: ") . $counter_since;
          }
          else {
            $output .= '<li/>' . t("Since: ") . $counter_initial_since;
          }
        }
        $output .= '</ul>';
        $block['content'] = $output;
        break;
    }
    return $block;
  }
}

Functions

Namesort descending Description
counter_admin_settings
counter_block Implementation of hook_block().
counter_help Implementation of hook_help().
counter_menu Menu callback. Prints a listing of active nodes on the site.
counter_perm Implementation of hook_perm
counter_report