PageRenderTime 26ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/example.report/api/App.php

https://github.com/sluther/Cerb5-Example-Plugins
PHP | 132 lines | 96 code | 21 blank | 15 comment | 8 complexity | 0129e496cd915c8aa68f2c4263881653 MD5 | raw file
  1. <?php
  2. class ExReportGroup extends Extension_ReportGroup {
  3. function __construct($manifest) {
  4. parent::__construct($manifest);
  5. }
  6. };
  7. class ExReport extends Extension_Report {
  8. function __construct($manifest) {
  9. parent::__construct($manifest);
  10. }
  11. function render() {
  12. $db = DevblocksPlatform::getDatabaseService();
  13. $tpl = DevblocksPlatform::getTemplateService();
  14. @$filter_group_ids = DevblocksPlatform::importGPC($_REQUEST['group_id'],'array',array());
  15. $tpl->assign('filter_group_ids', $filter_group_ids);
  16. @$start = DevblocksPlatform::importGPC($_REQUEST['start'],'string','-15 days');
  17. @$end = DevblocksPlatform::importGPC($_REQUEST['end'],'string','now');
  18. // Start + End
  19. @$start_time = strtotime($start);
  20. @$end_time = strtotime($end);
  21. $tpl->assign('start', $start);
  22. $tpl->assign('end', $end);
  23. $tpl->assign('age_dur', abs(floor(($start_time - $end_time)/86400)));
  24. // Calculate the # of ticks between the dates (and the scale -- day, month, etc)
  25. $range = $end_time - $start_time;
  26. $range_days = $range/86400;
  27. $plots = $range/15;
  28. $ticks = array();
  29. @$report_date_grouping = DevblocksPlatform::importGPC($_REQUEST['report_date_grouping'],'string','');
  30. $date_group = '';
  31. $date_increment = '';
  32. // Did the user choose a specific grouping?
  33. switch($report_date_grouping) {
  34. case 'year':
  35. $date_group = '%Y';
  36. $date_increment = 'year';
  37. break;
  38. case 'month':
  39. $date_group = '%Y-%m';
  40. $date_increment = 'month';
  41. break;
  42. case 'day':
  43. $date_group = '%Y-%m-%d';
  44. $date_increment = 'day';
  45. break;
  46. }
  47. // Fallback to automatic grouping
  48. if(empty($date_group) || empty($date_increment)) {
  49. if($range_days > 365) {
  50. $date_group = '%Y';
  51. $date_increment = 'year';
  52. } elseif($range_days > 32) {
  53. $date_group = '%Y-%m';
  54. $date_increment = 'month';
  55. } elseif($range_days > 1) {
  56. $date_group = '%Y-%m-%d';
  57. $date_increment = 'day';
  58. } else {
  59. $date_group = '%Y-%m-%d %H';
  60. $date_increment = 'hour';
  61. }
  62. }
  63. $tpl->assign('report_date_grouping', $date_increment);
  64. // Find unique values
  65. $time = strtotime(sprintf("-1 %s", $date_increment), $start_time);
  66. while($time < $end_time) {
  67. $time = strtotime(sprintf("+1 %s", $date_increment), $time);
  68. if($time <= $end_time)
  69. $ticks[strftime($date_group, $time)] = 0;
  70. }
  71. // you can run any custom sql queries you like to get the data you wish to display
  72. // however, it is important to make sure that the "date_plot" is kept intact, as this
  73. // is used to determine which dates have information to display in the graph
  74. // Chart
  75. $sql = sprintf("SELECT t.team_id AS group_id, DATE_FORMAT(FROM_UNIXTIME(t.created_date),'%s') AS date_plot, ".
  76. "COUNT(*) AS hits ".
  77. "FROM ticket t ".
  78. "WHERE t.created_date >= %d ".
  79. "AND t.created_date <= %d ".
  80. "%s ".
  81. "AND t.is_deleted = 0 ".
  82. "AND t.spam_score < 0.9000 ".
  83. "AND t.spam_training != 'S' ".
  84. "GROUP BY group_id, date_plot ",
  85. $date_group,
  86. $start_time,
  87. $end_time,
  88. (is_array($filter_group_ids) && !empty($filter_group_ids) ? sprintf("AND t.team_id IN (%s)", implode(',', $filter_group_ids)) : "")
  89. );
  90. $rs = $db->Execute($sql);
  91. $data = array();
  92. // $group_id is the numeric id of the group
  93. // $date_plot is the date in YYYY-MM-DD format
  94. // $ticks is an array containing all of the date_plots for this timespan
  95. // "hits" is the number of results for the current date_plot
  96. while($row = mysql_fetch_assoc($rs)) {
  97. $group_id = intval($row['group_id']);
  98. $date_plot = $row['date_plot'];
  99. if(!isset($data[$group_id]))
  100. $data[$group_id] = $ticks;
  101. // associate the date for each group with the number of results
  102. $data[$group_id][$date_plot] = intval($row['hits']);
  103. }
  104. // Sort the data in descending order
  105. uasort($data, array('ChReportSorters','sortDataDesc'));
  106. $tpl->assign('xaxis_ticks', array_keys($ticks));
  107. $tpl->assign('data', $data);
  108. mysql_free_result($rs);
  109. $tpl->display('devblocks:example.report::index.tpl');
  110. }
  111. };