You are here

function nodeviewcount_node_view in Node view count 7.3

Same name and namespace in other branches
  1. 8 nodeviewcount.module \nodeviewcount_node_view()
  2. 7 nodeviewcount.module \nodeviewcount_node_view()
  3. 7.2 nodeviewcount.module \nodeviewcount_node_view()

Implements hook_node_view().

File

./nodeviewcount.module, line 79
Defines necessery hooks.

Code

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');
    }
  }
}