| 1 |
<?php
|
| 2 |
// $Id: statistics.tokens.inc,v 1.1 2009/08/19 20:19:36 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Builds placeholder replacement tokens for node visitor statistics.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implement hook_token_info().
|
| 11 |
*/
|
| 12 |
function statistics_token_info() {
|
| 13 |
$node['views'] = array(
|
| 14 |
'name' => t("Number of views"),
|
| 15 |
'description' => t("The number of visitors who have read the node."),
|
| 16 |
);
|
| 17 |
$node['day-views'] = array(
|
| 18 |
'name' => t("Views today"),
|
| 19 |
'description' => t("The number of visitors who have read the node today."),
|
| 20 |
);
|
| 21 |
$node['last-view'] = array(
|
| 22 |
'name' => t("Last view"),
|
| 23 |
'description' => t("The date on which a visitor last read the node."),
|
| 24 |
'type' => 'date',
|
| 25 |
);
|
| 26 |
|
| 27 |
return array(
|
| 28 |
'tokens' => array('node' => $node),
|
| 29 |
);
|
| 30 |
}
|
| 31 |
|
| 32 |
/**
|
| 33 |
* Implement hook_tokens().
|
| 34 |
*/
|
| 35 |
function statistics_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
| 36 |
$url_options = array('absolute' => TRUE);
|
| 37 |
$replacements = array();
|
| 38 |
|
| 39 |
if ($type == 'node' & !empty($data['node'])) {
|
| 40 |
$node = $data['node'];
|
| 41 |
|
| 42 |
foreach ($tokens as $name => $original) {
|
| 43 |
if ($name == 'views') {
|
| 44 |
$statistics = statistics_get($node->nid);
|
| 45 |
$replacements[$original] = $statistics['totalviews'];
|
| 46 |
}
|
| 47 |
elseif ($name == 'views-today') {
|
| 48 |
$statistics = statistics_get($node->nid);
|
| 49 |
$replacements[$original] = $statistics['dayviews'];
|
| 50 |
}
|
| 51 |
elseif ($name == 'last-view') {
|
| 52 |
$statistics = statistics_get($node->nid);
|
| 53 |
$replacements[$original] = format_date($statistics['timestamp']);
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
if ($created_tokens = token_find_with_prefix($tokens, 'last-view')) {
|
| 58 |
$statistics = statistics_get($node->nid);
|
| 59 |
$replacements += token_generate('date', $created_tokens, array('date' => $statistics['timestamp']), $options);
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
return $replacements;
|
| 64 |
}
|