You are here

counter.install in Counter 7

Install the module with a new database table

File

counter.install
View source
<?php

/**
 * @file
 * Install the module with a new database table
 */

/**
 * Implements hook_install().
 */
function counter_install() {
  counter_update_7101();
  counter_update_7102();
  counter_update_7103();
  counter_update_7104();
}

/**
 * Implements hook_schema().
 */
function counter_schema() {
  $schema['counter'] = array(
    'fields' => array(
      'counter_id' => array(
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'ip' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'url' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'counter_id',
    ),
  );
  $schema['counter_data'] = array(
    'fields' => array(
      'counter_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 0,
      ),
      'counter_value' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'counter_name',
    ),
  );
  return $schema;
}

/**
 * Update: Rename field name and add index.
 */
function counter_update_7101() {
  if (db_field_exists('counter', 'counter_ip')) {
    db_change_field('counter', 'counter_ip', 'ip', array(
      'type' => 'varchar',
      'length' => 32,
      'not null' => TRUE,
      'default' => '',
    ));
  }
  if (db_field_exists('counter', 'counter_date')) {
    db_drop_field('counter', 'counter_date');
    db_add_field('counter', 'created', array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ));
  }
  if (db_field_exists('counter', 'counter_page')) {
    db_change_field('counter', 'counter_page', 'url', array(
      'type' => 'varchar',
      'length' => 256,
      'not null' => TRUE,
      'default' => '',
    ));
  }
  if (db_index_exists('counter', 'counter_date')) {
    db_drop_index('counter', 'counter_date');
  }
  if (db_index_exists('counter', 'counter_ip')) {
    db_drop_index('counter', 'counter_ip');
  }
  db_add_index('counter', 'created', array(
    'created',
  ));
  db_add_index('counter', 'ip', array(
    'ip',
  ));
}

/**
 * Update: Updating Counter Data table.
 */
function counter_update_7102() {
  $ret = array();

  // site_counter
  $sql = " SELECT count(*) as total FROM {counter} c ";
  $counter_total = db_query($sql)
    ->fetchField();
  $sql = " SELECT count(*) as total FROM {counter_data} WHERE counter_name= 'site_counter' ";
  $counter_name_exist = db_query($sql)
    ->fetchField();
  if (!$counter_name_exist) {
    $sql = " INSERT INTO {counter_data} (counter_name, counter_value) VALUES ('site_counter', '{$counter_total}')";
    db_query($sql);
  }

  // unique_visitor
  $sql = " SELECT count(*) as total FROM (SELECT ip FROM {counter} GROUP BY ip) c";
  $counter_total = db_query($sql)
    ->fetchField();
  $sql = " SELECT count(*) as total FROM {counter_data} WHERE counter_name= 'unique_visitor' ";
  $counter_name_exist = db_query($sql)
    ->fetchField();
  if (!$counter_name_exist) {
    $sql = " INSERT INTO {counter_data} (counter_name,counter_value) VALUES ('unique_visitor','{$counter_total}')";
    db_query($sql);
  }

  // registered_user
  $sql = " SELECT count(*) as total FROM {users} WHERE access<>0 and uid<>0";
  $counter_total = db_query($sql)
    ->fetchField();
  $sql = " SELECT count(*) as total FROM {counter_data} WHERE counter_name= 'registered_user' ";
  $counter_name_exist = db_query($sql)
    ->fetchField();
  if (!$counter_name_exist) {
    $sql = " INSERT INTO {counter_data} (counter_name,counter_value) VALUES ('registered_user', '{$counter_total}')";
    db_query($sql);
  }

  // unregistered_user
  $sql = " SELECT count(*) as total FROM {users} WHERE access=0 and uid<>0";
  $counter_total = db_query($sql)
    ->fetchField();
  $sql = " SELECT count(*) as total FROM {counter_data} WHERE counter_name= 'unregistered_user' ";
  $counter_name_exist = db_query($sql)
    ->fetchField();
  if (!$counter_name_exist) {
    $sql = " INSERT INTO {counter_data} (counter_name,counter_value) VALUES ('unregistered_user', '{$counter_total}')";
    db_query($sql);
  }

  // published_node
  $sql = " SELECT count(*) as total FROM {node} WHERE status=1";
  $counter_total = db_query($sql)
    ->fetchField();
  $sql = " SELECT count(*) as total FROM {counter_data} WHERE counter_name= 'published_node' ";
  $counter_name_exist = db_query($sql)
    ->fetchField();
  if (!$counter_name_exist) {
    $sql = " INSERT INTO {counter_data} (counter_name,counter_value) VALUES ('published_node', '{$counter_total}')";
    db_query($sql);
  }

  // unpublished_node
  $sql = " SELECT count(*) as total FROM {node} WHERE status=0";
  $counter_total = db_query($sql)
    ->fetchField();
  $sql = " SELECT count(*) as total FROM {counter_data} WHERE counter_name= 'unpublished_node' ";
  $counter_name_exist = db_query($sql)
    ->fetchField();
  if (!$counter_name_exist) {
    $sql = " INSERT INTO {counter_data} (counter_name,counter_value) VALUES ('unpublished_node','{$counter_total}')";
    db_query($sql);
  }

  // blocked_user
  $sql = " SELECT count(*) as total FROM {users} WHERE status=0 and uid<>0";
  $counter_total = db_query($sql)
    ->fetchField();
  $sql = " SELECT count(*) as total FROM {counter_data} WHERE counter_name= 'blocked_user' ";
  $counter_name_exist = db_query($sql)
    ->fetchField();
  if (!$counter_name_exist) {
    $sql = " INSERT INTO {counter_data} (counter_name,counter_value) VALUES ('blocked_user','{$counter_total}')";
    db_query($sql);
  }
  return $ret;
}

/**
 * Add field: uid
 */
function counter_update_7103() {
  if (!db_field_exists('counter', 'uid')) {
    db_add_field('counter', 'uid', array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ));
    db_add_index('counter', 'uid', array(
      'uid',
    ));
  }
}

/**
 * Add fields: nid, type, browser name, browser version, platform
 */
function counter_update_7104() {
  if (!db_field_exists('counter', 'nid')) {
    db_add_field('counter', 'nid', array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ));
    db_add_index('counter', 'nid', array(
      'nid',
    ));
  }
  if (!db_field_exists('counter', 'type')) {
    db_add_field('counter', 'type', array(
      'type' => 'varchar',
      'length' => 256,
      'not null' => TRUE,
      'default' => '',
    ));
  }
  if (!db_field_exists('counter', 'browser_name')) {
    db_add_field('counter', 'browser_name', array(
      'type' => 'varchar',
      'length' => 64,
      'not null' => TRUE,
      'default' => '',
    ));
  }
  if (!db_field_exists('counter', 'browser_version')) {
    db_add_field('counter', 'browser_version', array(
      'type' => 'varchar',
      'length' => 64,
      'not null' => TRUE,
      'default' => '',
    ));
  }
  if (!db_field_exists('counter', 'platform')) {
    db_add_field('counter', 'platform', array(
      'type' => 'varchar',
      'length' => 64,
      'not null' => TRUE,
      'default' => '',
    ));
  }
}

Functions

Namesort descending Description
counter_install Implements hook_install().
counter_schema Implements hook_schema().
counter_update_7101 Update: Rename field name and add index.
counter_update_7102 Update: Updating Counter Data table.
counter_update_7103 Add field: uid
counter_update_7104 Add fields: nid, type, browser name, browser version, platform