You are here

nodeviewcount.module in Node view count 7

File

nodeviewcount.module
View source
<?php

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,
    ),
    '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;
  }
  $way_counting = variable_get('nodeviewcount_way_counting', NODEVIEWCOUNT_PHP_WAY_COUNT_VIEWS);
  if ($way_counting == NODEVIEWCOUNT_PHP_WAY_COUNT_VIEWS) {
    nodeviewcount_insert_node_view($node->nid, $user->uid);
  }
  else {
    $js_setting = array(
      'nodeviewcount' => array(
        'nodeviewcount_path' => 'nodeviewcount',
        'nodeviewcount_nid' => $node->nid,
        'nodeviewcount_uid' => $user->uid,
      ),
    );
    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('timestamp', REQUEST_TIME - $flush_log_timer, '<')
      ->execute();
  }
}

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

Functions

Namesort descending Description
nodeviewcount_cron Implements hook_cron().
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().

Constants