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';
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;
}
function nodeviewcount_node_load($nid) {
if (is_numeric($nid)) {
$node = node_load($nid);
if ($node) {
return $node;
}
}
return FALSE;
}
function nodeviewcount_user_load($uid) {
if (is_numeric($uid)) {
$user = user_load($uid);
if ($user) {
return $user;
}
}
return FALSE;
}
function nodeviewcount_node_view($node, $view_mode, $langcode) {
if (!nodeviewcount_get_node_view_modes_count()) {
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');
}
}
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();
}
}
function nodeviewcount_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'nodeviewcount') . '/includes',
);
}