BxDolChartGrowth.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolChartGrowth extends BxDolChart
  10. {
  11. protected function __construct($aObject)
  12. {
  13. parent::__construct($aObject);
  14. }
  15. protected function _getDataByInterval($iFrom, $iTo)
  16. {
  17. $sFieldDate = $this->_aObject['field_date_dt'] ? $this->_aObject['field_date_dt'] : $this->_aObject['field_date_ts'];
  18. $this->addMarkers([
  19. 'field_date' => "`" . $sFieldDate . "`",
  20. 'field_date_formatted' => "DATE_FORMAT(" . ($this->_aObject['field_date_dt'] ? "`{$this->_aObject['field_date_dt']}`" : "FROM_UNIXTIME(`{$this->_aObject['field_date_ts']}`)") . ", '%Y-%m-%d')",
  21. 'object' => $this->_aObject['object'],
  22. 'table' => "`{$this->_aObject['table']}`",
  23. 'where_inteval' => "AND `" . $this->_aObject['table'] . "`.`" . $sFieldDate . "` >= :from AND `" . $this->_aObject['table'] . "`.`" . $sFieldDate . "` <= :to"
  24. ]);
  25. // build query
  26. $sQuery = $this->_getQuery();
  27. $sQueryInterval = $this->replaceMarkers($sQuery);
  28. $aBindings = array(
  29. 'from' => $this->_aObject['field_date_dt'] ? $this->_getDate($iFrom) . ' 00:00:00' : $iFrom,
  30. 'to' => $this->_aObject['field_date_dt'] ? $this->_getDate($iTo) . ' 23:59:59' : $iTo + 24*3600 - 1,
  31. );
  32. // get data
  33. if ($this->_aObject['column_date'] >= 0)
  34. $aData = $this->_oDb->getAllWithKey($sQueryInterval, $this->_aObject['column_date'], $aBindings, PDO::FETCH_NUM);
  35. else
  36. $aData = $this->_oDb->getAll($sQueryInterval, array(), $aBindings, PDO::FETCH_NUM);
  37. if (!$aData)
  38. return false;
  39. // get interval first value
  40. $this->addMarkers([
  41. 'where_inteval' => "AND `" . $this->_aObject['table'] . "`.`" . $sFieldDate . "` < :from"
  42. ]);
  43. $aDataInit = $this->_oDb->getAll($this->replaceMarkers($sQuery), array(
  44. 'from' => $aBindings['from']
  45. ));
  46. $iCountInit = 0;
  47. foreach($aDataInit as $aDataItem)
  48. $iCountInit += $aDataItem[$this->_aObject['object']];
  49. // fill in missed days and convert values to numbers
  50. if ($this->_aObject['column_date'] >= 0) {
  51. $iCountCurrent = 0;
  52. $aDataSlice = array_slice($aData, 0, 1);
  53. $iColumnsNum = count(array_pop($aDataSlice));
  54. for ($i = $iFrom ; $i <= ($iTo + 24*3600 - 1); $i += 24*60*60) {
  55. $sDate = $this->_getDate($i);
  56. $aRow = array ();
  57. for ($j = 0 ; $j < $iColumnsNum ; ++$j) {
  58. if($this->_aObject['column_date'] != $j) {
  59. $iCountCurrent += isset($aData[$sDate]) ? (int)$aData[$sDate][$j] : 0;
  60. $aRow[$j] = $iCountInit + $iCountCurrent;
  61. }
  62. else
  63. $aRow[$j] = $sDate;
  64. }
  65. $aData[$sDate] = $aRow;
  66. }
  67. }
  68. else
  69. foreach ($aData as $k => $v)
  70. foreach ($aData[$k] as $kk => $vv)
  71. if ($kk > 0)
  72. $aData[$k][$kk] = (int)$aData[$k][$kk];
  73. // return values only
  74. ksort($aData);
  75. return array_values($aData);
  76. }
  77. }
  78. /** @} */