You are here

function counter_block in Counter 6

Same name and namespace in other branches
  1. 5 counter.module \counter_block()

Implementation of hook_block().

File

./counter.module, line 205
The counter module used for displaying Site Counter.

Code

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 .= "," . $counter_page . arg(1);
        }
        if (!arg(2)) {
          $counter_page .= "," . $counter_page . arg(2);
        }
        $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;
  }
}