PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/php/PHPReports/input/PHPReportInputCrosstab.php

http://tracmor.googlecode.com/
PHP | 92 lines | 62 code | 15 blank | 15 comment | 8 complexity | d6c93b268b49e74c77b902937aa71c58 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. require_once("PHPReportInputObject.php");
  3. class PHPReportInputCrosstab extends PHPReportInputObject {
  4. function run(){
  5. // default values for aggregation functions
  6. $this->_default["SUM"] = 0;
  7. $this->_default["COUNT"] = "null";
  8. $this->_default["MIN"] = "null";
  9. $this->_default["MAX"] = "null";
  10. // first step - find what columns we have, let's make a query that returns nothing but the columns
  11. $stmt = PHPReportsDBI::db_query($this->_con,"select * from (".$this->_sql.") crosstab_table where 1=2");
  12. $cols = Array();
  13. for($i=1; $i<=PHPReportsDBI::db_colnum($stmt); $i++)
  14. array_push($cols,PHPReportsDBI::db_columnName($stmt,$i));
  15. PHPReportsDBI::db_free($stmt);
  16. // find a delimiter
  17. $deli = isNumericType($cols[$this->_group_key]) ? "" : "'";
  18. // now we find the colums to work with the aggregated values - those are the values we'll create the columns
  19. $cagr = array_diff(array_values($cols),array_merge($this->_group_desc,Array($this->_group_key)));
  20. // ok, now we know that columns to work with, we need to know the values of the group key
  21. $stmt = PHPReportsDBI::db_query($this->_con,"select distinct ".$this->_group_key." from (".$this->_sql.") crosstab_table order by ".$this->_group_key);
  22. $keys = Array();
  23. while($row=PHPReportsDBI::db_fetch($stmt))
  24. array_push($keys,$row[$this->_group_key]);
  25. PHPReportsDBI::db_free($stmt);
  26. // create the sql query
  27. // check if there is another default operation other than SUM
  28. $oper = "SUM";
  29. if($this->_options["DEFAULT_OPERATION"])
  30. $oper = strtoupper($this->_options["DEFAULT_OPERATION"]);
  31. // check if there is an order
  32. $order = "";
  33. if($this->_options["ORDER"])
  34. $order = "order by ".$this->_options["ORDER"];
  35. // first the description columns
  36. $sql = "";
  37. foreach($this->_group_desc as $col)
  38. $sql .= $col.",";
  39. $group = substr($sql,0,strlen($str)-1);
  40. $sql = "select ".$sql;
  41. $coln = Array(); // store the used column names
  42. $apcn = $this->_options["APPEND_COLUMNS_NAMES"];
  43. // then the aggregated values
  44. foreach($keys as $key){ // here the key to compare
  45. foreach($cagr as $col){ // here the column to manipulate
  46. $op = $oper; // default operation
  47. // if there is a customized function for this column ...
  48. if($this->_options["COLUMNS_FUNCTIONS"][$col])
  49. $op = $this->_options["COLUMNS_FUNCTIONS"][$col];
  50. // check the default value of the aggregation function - convert to uppercase because they're uppercase there
  51. if(!array_key_exists(strtoupper($op),$this->_default)){
  52. print "THERE IS NO DEFAULT VALUE FOR $op!";
  53. return;
  54. }
  55. $defv = $this->_default[strtoupper($op)];
  56. // check if there is some alias to the function - some translation, for example
  57. $alias= $this->_options["FUNCTIONS_ALIASES"][$op] ? $this->_options["FUNCTIONS_ALIASES"][$op] : $op;
  58. // create the column name
  59. $name = strtoupper($alias)."_".strtoupper($key).($apcn?"_$col":"");
  60. // check if there is already a column name like this, if so create a new
  61. // one based on how many times it was repeated.
  62. if($coln[$name]){
  63. $coln[$name] = $coln[$name]+1;
  64. $name = strtoupper($alias)."_".$coln[$name]."_".strtoupper($key).($apcn?"_$col":"");
  65. }else
  66. $coln[$name] = 1;
  67. $sql .= "$op(case when ".$this->_group_key."=$deli$key$deli then $col else $defv end) as $name,";
  68. }
  69. }
  70. $sql = substr($sql,0,strlen($sql)-1)." from (".$this->_sql.") crosstab_table group by $group $order";
  71. if($this->_options["SHOW_SQL"])
  72. print $sql;
  73. return $sql;
  74. }
  75. }
  76. ?>