You are here

nodeviewcount.install in Node view count 7.3

Creates table for views counting.

File

nodeviewcount.install
View source
<?php

/**
 * @file
 * Creates table for views counting.
 */

/**
 * Implements hook_schema().
 */
function nodeviewcount_schema() {
  $schema = array();
  $schema['nodeviewcount'] = array(
    'description' => 'The count views of node for every user.',
    'fields' => array(
      'id' => array(
        'description' => 'The unique ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'nid' => array(
        'description' => 'The node ID.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'The user ID who view of node.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'datetime' => array(
        'description' => 'The date when the node was view.',
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'default' => 0,
      ),
      'uip' => array(
        'description' => 'The user IP who view of node.',
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'indexes' => array(
      'nid_uid' => array(
        'uid',
        'nid',
      ),
      'datetime' => array(
        'datetime',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function nodeviewcount_install() {
  variable_set('nodeviewcount_node_types', array());
  variable_set('nodeviewcount_user_roles', array());
  variable_set('nodeviewcount_way_counting', NODEVIEWCOUNT_PHP_WAY_COUNT_VIEWS);
  variable_set('nodeviewcount_flush_log_timer', 0);
  nodeviewcount_update_7212();
}

/**
 * Implements hook_uninstall().
 */
function nodeviewcount_uninstall() {
  variable_del('nodeviewcount_node_types');
  variable_del('nodeviewcount_user_roles');
  variable_del('nodeviewcount_way_counting');
  variable_del('nodeviewcount_flush_log_timer');
  variable_del('nodeviewcount_secret_key');
}

/**
 * Add a new nid index in DB to the table nodeviewcount.
 */
function nodeviewcount_update_7211() {
  $table = 'nodeviewcount';
  $index_name = 'nid';
  if (db_table_exists($table)) {
    if (!db_index_exists($table, $index_name)) {
      db_add_index($table, $index_name, array(
        'nid',
      ));
      return t('New index added');
    }
    else {
      return t('Index already exist');
    }
  }
}

/**
 * Create initial secret key, used to generate validation URL tokens.
 */
function nodeviewcount_update_7212() {
  variable_set('nodeviewcount_secret_key', drupal_random_key());
}

/**
 * Create field in database, for storage user IP.
 */
function nodeviewcount_update_7300() {
  $field = array(
    'description' => 'The user IP who view of node.',
    'type' => 'varchar',
    'length' => 100,
    'not null' => TRUE,
    'default' => 0,
  );
  db_add_field('nodeviewcount', 'uip', $field);
}

Functions

Namesort descending Description
nodeviewcount_install Implements hook_install().
nodeviewcount_schema Implements hook_schema().
nodeviewcount_uninstall Implements hook_uninstall().
nodeviewcount_update_7211 Add a new nid index in DB to the table nodeviewcount.
nodeviewcount_update_7212 Create initial secret key, used to generate validation URL tokens.
nodeviewcount_update_7300 Create field in database, for storage user IP.