blocks.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. define('TOTAL_COUNTS_BLOCK', 'total_counts');
  3. /**
  4. * Implements hook_block_info().
  5. */
  6. function custom_maps_block_info() {
  7. $blocks[TOTAL_COUNTS_BLOCK] = array(
  8. 'info' => t('Display totals info'),
  9. 'cache' => DRUPAL_NO_CACHE,
  10. );
  11. return $blocks;
  12. }
  13. /**
  14. * Implements hook_block_view().
  15. */
  16. function custom_maps_block_view($delta = '') {
  17. $block = array();
  18. switch ($delta) {
  19. case TOTAL_COUNTS_BLOCK:
  20. $block = _block_total_counts_content();
  21. // $block['content'] = drupal_render($content);
  22. break;
  23. }
  24. return $block;
  25. }
  26. /**
  27. * Implements hook_theme().
  28. */
  29. function custom_maps_theme($existing, $type, $theme, $path) {
  30. $themes[TOTAL_COUNTS_BLOCK] = array( // replace module--delta with your block template suggestions
  31. 'variables' => array(),
  32. 'template' => TOTAL_COUNTS_BLOCK, // the name fo your .tpl.php file.
  33. 'path' => $path . '/templates', // path to the template in your module
  34. );
  35. return $themes;
  36. }
  37. /**
  38. * Helper function to call theme
  39. */
  40. function _my_module_block1() {
  41. $block = array(
  42. 'subject' => '',
  43. 'content' => theme(MY_MODULE_BLOCK1, array()),
  44. );
  45. return $block;
  46. }
  47. function _block_total_counts_content() {
  48. $total_activities = 0;
  49. $latest_distance = 0;
  50. $latest_time = 0;
  51. $latest_date = 'mm/dd/yyyy hh.mm';
  52. $month_distance = 0;
  53. $month_time = 0;
  54. $content = array(
  55. 'total' => __count_global_totals(),
  56. 'latest' => __count_global_totals(FALSE, TRUE),
  57. 'month' => __count_global_totals(TRUE),
  58. );
  59. $block = array(
  60. 'subject' => '',
  61. 'content' => theme(TOTAL_COUNTS_BLOCK, $content),
  62. );
  63. return $block;
  64. }
  65. function __count_global_totals($month = FALSE, $latest = FALSE) {
  66. global $user;
  67. $total_time = 0;
  68. $total_distance = 0;
  69. $count = 0;
  70. $date = 0;
  71. $query = new EntityFieldQuery();
  72. $query->entityCondition('entity_type', 'node')
  73. ->entityCondition('bundle', 'activity')
  74. ->propertyCondition('status', NODE_PUBLISHED);
  75. if ( $user->uid == 1) {
  76. // Run the query as user 1.
  77. $query->addMetaData('account', user_load(1));
  78. } else {
  79. $query->propertyCondition('uid', $user->uid);
  80. }
  81. if ( $month ) {
  82. $query->fieldCondition('field_date', 'value', strtotime(date('01-m-Y')), '>');
  83. }
  84. $query->fieldOrderBy('field_date', 'value', 'DESC');
  85. $result = $query->execute();
  86. if (isset($result['node'])) {
  87. foreach ($result['node'] as $index => $node) {
  88. $count++;
  89. $node = node_load($node->nid);
  90. $total_time += $node->field_total_time[LANGUAGE_NONE][0]['value'];
  91. $total_distance += $node->field_distance[LANGUAGE_NONE][0]['value'];
  92. $date = $node->field_date[LANGUAGE_NONE][0]['value'];
  93. if ( $latest ) break;
  94. }
  95. }
  96. return array('total_activities' => $count,
  97. 'distance' => __number_format($total_distance / 1000, 2),
  98. 'time' => __format_seconds($total_time),
  99. 'date' => date('d/m/Y H:i:s', $date));
  100. }