BxDolChartGrowth.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. $aMarkers = array (
  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 = bx_replace_markers($sQuery, $aMarkers);
  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. $aMarkers['where_inteval'] = "AND `" . $this->_aObject['table'] . "`.`" . $sFieldDate . "` < :from";
  41. $aDataInit = $this->_oDb->getAll(bx_replace_markers($sQuery, $aMarkers), array(
  42. 'from' => $aBindings['from']
  43. ));
  44. $iCountInit = 0;
  45. foreach($aDataInit as $aDataItem)
  46. $iCountInit += $aDataItem[$this->_aObject['object']];
  47. // fill in missed days and convert values to numbers
  48. if ($this->_aObject['column_date'] >= 0) {
  49. $iCountCurrent = 0;
  50. $aDataSlice = array_slice($aData, 0, 1);
  51. $iColumnsNum = count(array_pop($aDataSlice));
  52. for ($i = $iFrom ; $i <= ($iTo + 24*3600 - 1); $i += 24*60*60) {
  53. $sDate = $this->_getDate($i);
  54. $aRow = array ();
  55. for ($j = 0 ; $j < $iColumnsNum ; ++$j) {
  56. if($this->_aObject['column_date'] != $j) {
  57. $iCountCurrent += isset($aData[$sDate]) ? (int)$aData[$sDate][$j] : 0;
  58. $aRow[$j] = $iCountInit + $iCountCurrent;
  59. }
  60. else
  61. $aRow[$j] = $sDate;
  62. }
  63. $aData[$sDate] = $aRow;
  64. }
  65. }
  66. else
  67. foreach ($aData as $k => $v)
  68. foreach ($aData[$k] as $kk => $vv)
  69. if ($kk > 0)
  70. $aData[$k][$kk] = (int)$aData[$k][$kk];
  71. // return values only
  72. ksort($aData);
  73. return array_values($aData);
  74. }
  75. }
  76. /** @} */