Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @file
  5. * Tracks visited pages.
  6. */
  7.  
  8. /**
  9. * Implements hook_init().
  10. */
  11. function tracks_init() {
  12. // Grab the tracking history from a variable.
  13. $track = variable_get('tracks_history', array());
  14.  
  15. // Add current page to track.
  16. $track[] = array(
  17. 'title' => strip_tags(drupal_get_title()),
  18. 'path' => $_GET['q'],
  19. 'timestamp' => REQUEST_TIME,
  20. );
  21.  
  22. // Save track as a variable.
  23. variable_set('tracks_history', $track);
  24. }
  25.  
  26. /**
  27. * Implements hook_permission().
  28. */
  29. function tracks_permission() {
  30. return array(
  31. 'administer tracks' => array(
  32. 'title' => t('Administer Tracks module'),
  33. 'description' => t('Perform admin tasks for Tracks module.'),
  34. ),
  35. 'access tracks blocks' => array(
  36. 'title' => t('Access Tracks blocks'),
  37. 'description' => t('View blocks generated by Tracks module.'),
  38. ),
  39. );
  40. }
  41.  
  42. /**
  43. * Implements hook_menu().
  44. */
  45. function tracks_menu() {
  46. // Module settings.
  47. $items['admin/config/tracks'] = array(
  48. 'title' => 'Tracks',
  49. 'description' => 'tracks configuration',
  50. 'page callback' => 'drupal_get_form',
  51. 'page arguments' => array('tracks_admin_settings'),
  52. 'access arguments' => array('administer tracks'),
  53. 'file' => 'tracks.admin.inc',
  54. 'file path' => drupal_get_path('module', 'tracks'),
  55. );
  56. return $items;
  57. }
  58.  
  59. /**
  60. * Implements hook_cron().
  61. */
  62. function tracks_cron() {
  63. // Get track array.
  64. $track = variable_get('tracks_history', array());
  65.  
  66. // Get offset for array_slice, save last five items.
  67. $count_minus_5 = count($track) - 5;
  68.  
  69. // Eliminate everything except the last five.
  70. $short_track = array_slice($track, $count_minus_5);
  71.  
  72. // Save the shorter list.
  73. variable_set('tracks_history', $short_track);
  74. }
  75.  
  76. /**
  77. * Implements hook_block_info().
  78. */
  79. function tracks_block_info() {
  80. $blocks['history'] = array(
  81. 'info' => t('History'),
  82. 'cache' => DRUPAL_NO_CACHE,
  83. );
  84. return $blocks;
  85. }
  86.  
  87. /**
  88. * Implements hook_block_configure().
  89. */
  90. function tracks_block_configure($delta = '') {
  91. // Get max allowed from config.
  92. $max_to_display = variable_get('tracks_block_max', 50);
  93.  
  94. // Add a select box of numbers form 1 to $max_to_display.
  95. $form['tracks_block_num'] = array(
  96. '#type' => 'select',
  97. '#title' => t('Number of items to show'),
  98. '#default_value' => variable_get('tracks_block_num', '5'),
  99. '#options' => drupal_map_assoc(range(1, $max_to_display)),
  100. );
  101. return $form;
  102. }
  103.  
  104. /**
  105. * Implements hook_block_save().
  106. */
  107. function tracks_block_save($delta = '', $edit = array()) {
  108. variable_set('tracks_block_num', $edit['tracks_block_num']);
  109. }
  110.  
  111. /**
  112. * Implements hook_block_view().
  113. */
  114. function tracks_block_view($delta = '') {
  115. if (user_access('access tracks blocks')) {
  116. $block = array();
  117. $type = $delta;
  118. // list($type, $id) = explode('-', $delta); // Unnecessary with one block.
  119. switch ($type) {
  120. case 'history':
  121. // Create list of previous paths.
  122. // Get history from variable.
  123. $track = variable_get('tracks_history', array());
  124.  
  125. // Flip saved array to show newest pages first.
  126. $reverse_track = array_reverse($track);
  127.  
  128. // Get number of items to display.
  129. $num_items = variable_get('tracks_block_num', '5');
  130.  
  131. // Output latest items as list.
  132. // Initialize variable.
  133. $output = '';
  134. for ($i = 0; $i < $num_items; $i++) {
  135. if ($item = $reverse_track[$i]) {
  136. $output .= '<li>'
  137. . l($item['title'], $item['path'])
  138. . ' - '
  139. . format_interval(REQUEST_TIME - $item['timestamp'])
  140. . ' '
  141. . t('ago')
  142. . '</li>';
  143. }
  144. }
  145. if (isset($output)) {
  146. $output = '
  147. <p>' . t('Last @num pages you visited.', array('@num' => $num_items)) . '</p>
  148. <ul>' . $output . '</ul>
  149. ';
  150. }
  151.  
  152. // Return the $block variable with subject (title) and content (output).
  153. $block['subject'] = 'History';
  154. $block['content'] = $output;
  155. break;
  156. }
  157.  
  158. return $block;
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement