You are here

nodeviewcount.module in Node view count 7.3

Same filename and directory in other branches
  1. 8 nodeviewcount.module
  2. 7 nodeviewcount.module
  3. 7.2 nodeviewcount.module

Defines necessery hooks.

File

nodeviewcount.module
View source
<?php

/**
 * @file
 * Defines necessery hooks.
 */
define('NODEVIEWCOUNT_PHP_WAY_COUNT_VIEWS', 0);
define('NODEVIEWCOUNT_JS_WAY_COUNT_VIEWS', 1);
require_once 'includes/nodeviewcount.api.inc';
require_once 'model/nodeviewcount.db.inc';

/**
 * Implements hook_menu().
 */
function nodeviewcount_menu() {
  $items = array();
  $items['admin/config/content/nodeviewcount'] = array(
    'title' => 'Node view count settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'nodeviewcount_admin_settings_form',
    ),
    'access arguments' => array(
      'administer modules',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'includes/pages/nodeviewcount.admin.inc',
  );
  $items['nodeviewcount/%nodeviewcount_node/%nodeviewcount_user/%/%'] = array(
    'page callback' => 'nodeviewcount_count_node_view_ajax',
    'page arguments' => array(
      1,
      2,
      3,
      4,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'includes/pages/nodeviewcount.pages.inc',
  );
  return $items;
}

/**
 * Menu argument loader: Current node.
 *
 * @param int $nid
 *   Node id.
 *
 * @return bool|mixed
 *   Node object or FALSE.
 */
function nodeviewcount_node_load($nid) {
  if (is_numeric($nid)) {
    $node = node_load($nid);
    if ($node) {
      return $node;
    }
  }
  return FALSE;
}

/**
 * Menu argument loader: Current user.
 *
 * @param int $uid
 *   User id.
 *
 * @return bool|mixed
 *   User object or FALSE.
 */
function nodeviewcount_user_load($uid) {
  if (is_numeric($uid)) {
    $user = user_load($uid);
    if ($user) {
      return $user;
    }
  }
  return FALSE;
}

/**
 * Implements hook_node_view().
 */
function nodeviewcount_node_view($node, $view_mode, $langcode) {
  if (!nodeviewcount_get_node_view_modes_count()) {

    // If nothing is checked then check the node view mode to the full mode.
    if ($view_mode != 'full') {
      return FALSE;
    }
  }
  elseif (!nodeviewcount_is_node_view_mode_count($view_mode)) {
    return FALSE;
  }
  if (!nodeviewcount_is_node_type_count($node)) {
    return FALSE;
  }
  global $user;
  if (!nodeviewcount_is_user_role_count($user)) {
    return FALSE;
  }
  $context = array(
    'node' => $node,
    'view_mode' => $view_mode,
    'langcode' => $langcode,
  );
  $result = module_invoke_all('nodeviewcount_insert', $context);
  if (in_array(FALSE, $result, TRUE)) {
    return FALSE;
  }
  $nid = $node->nid;
  $way_counting = variable_get('nodeviewcount_way_counting', NODEVIEWCOUNT_PHP_WAY_COUNT_VIEWS);
  $dateTime = new DateTime('NOW');
  $dateTime
    ->setTimeZone(new DateTimeZone(date_default_timezone_get()));
  $dateTime
    ->format(DEFAULT_TIME_FORMAT);
  $sessionTimeLimit = _nodeviewcount_get_session_time_limit($nid);
  $uip = ip_address();

  // Check the time limit given by the session, FALSE if it is first view.
  if ($sessionTimeLimit === FALSE || $dateTime >= $sessionTimeLimit) {
    $_SESSION['nodeviewcount_views_limit'][$nid] = $dateTime;
    if ($way_counting == NODEVIEWCOUNT_PHP_WAY_COUNT_VIEWS) {
      nodeviewcount_insert_node_view($node->nid, $user->uid, $uip);
    }
    else {
      $timestamp = $dateTime
        ->getTimestamp();
      $token_data = array(
        $node->nid,
        $user->uid,
        $timestamp,
        $uip,
      );
      $token = _nodeviewcount_create_token($token_data);
      $js_setting = array(
        'nodeviewcount' => array(
          'nodeviewcount_path' => 'nodeviewcount',
          'nodeviewcount_nid' => $node->nid,
          'nodeviewcount_uid' => $user->uid,
          'nodeviewcount_timestamp' => $timestamp,
          'nodeviewcount_uip' => $uip,
          'nodeviewcount_token' => $token,
        ),
      );
      drupal_add_js($js_setting, 'setting');
      drupal_add_js(drupal_get_path('module', 'nodeviewcount') . '/theme/js/nodeviewcount.js');
    }
  }
}

/**
 * Implements hook_cron().
 */
function nodeviewcount_cron() {
  $flush_log_timer = variable_get('nodeviewcount_flush_log_timer', 0);
  if ($flush_log_timer > 0) {
    db_delete('nodeviewcount')
      ->condition('datetime', format_date(REQUEST_TIME - $flush_log_timer, 'custom', DEFAULT_TIME_FORMAT, date_default_timezone_get()), '<')
      ->execute();
  }
}

/**
 * Implements hook_views_api().
 */
function nodeviewcount_views_api() {
  return array(
    'api' => 3.0,
    'path' => drupal_get_path('module', 'nodeviewcount') . '/includes',
  );
}

/**
 * Implements hook_entity_info().
 */
function nodeviewcount_entity_info() {
  $info['nodeviewcount'] = array(
    'label' => t('Node view count'),
    'base table' => 'nodeviewcount',
    'entity keys' => array(
      'id' => 'id',
    ),
    'module' => 'nodeviewcount',
    'entity class' => 'Entity',
    'controller class' => 'EntityAPIController',
  );
  return $info;
}

/**
 * Create the token used to verify an URL.
 *
 * @param array $data
 *   URL components (strings) that need to be verified.
 *
 * @return string
 *   The token.
 */
function _nodeviewcount_create_token(array $data) {
  $key = variable_get('nodeviewcount_secret_key', 'SECRET_KEY');
  $data = implode(':', $data);
  return drupal_hmac_base64($data, $key);
}

/**
 * Get the time limit to count a new view for the node by the current user.
 *
 * @param int $nid
 *   The id of the node viewed.
 * @param bool $js
 *   TRUE if the info is required by the menu page callback requested by AJAX,
 *   FALSE otherwise.
 *
 * @return mixed
 *   The time limit (DateTime) or FALSE if the used session variable is not set
 *   yet.
 */
function _nodeviewcount_get_session_time_limit($nid, $js = FALSE) {
  $viewsLimit = variable_get('nodeviewcount_views_limit', 0);
  $session_key = $js ? 'nodeviewcount_views_limit_js' : 'nodeviewcount_views_limit';
  if (!isset($_SESSION[$session_key][$nid])) {
    return FALSE;
  }
  $viewsLimitInterval = new DateInterval('PT' . $viewsLimit . 'S');
  $sessionTimeLimit = clone $_SESSION[$session_key][$nid];
  $sessionTimeLimit
    ->add($viewsLimitInterval);
  return $sessionTimeLimit;
}

Functions

Namesort descending Description
nodeviewcount_cron Implements hook_cron().
nodeviewcount_entity_info Implements hook_entity_info().
nodeviewcount_menu Implements hook_menu().
nodeviewcount_node_load Menu argument loader: Current node.
nodeviewcount_node_view Implements hook_node_view().
nodeviewcount_user_load Menu argument loader: Current user.
nodeviewcount_views_api Implements hook_views_api().
_nodeviewcount_create_token Create the token used to verify an URL.
_nodeviewcount_get_session_time_limit Get the time limit to count a new view for the node by the current user.

Constants

Namesort descending Description
NODEVIEWCOUNT_JS_WAY_COUNT_VIEWS
NODEVIEWCOUNT_PHP_WAY_COUNT_VIEWS @file Defines necessery hooks.