BxBaseChartServices.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Services for chart objects functionality
  11. * @see BxDolChart
  12. */
  13. class BxBaseChartServices extends BxDol
  14. {
  15. public function serviceCheckAllowedView($isPerformAction = false)
  16. {
  17. $iProfileId = bx_get_logged_profile_id();
  18. $aCheck = checkActionModule($iProfileId, 'chart view', 'system', $isPerformAction);
  19. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  20. return $aCheck[CHECK_ACTION_MESSAGE];
  21. return CHECK_ACTION_RESULT_ALLOWED;
  22. }
  23. /**
  24. * @page service Service Calls
  25. * @section bx_system_general System Services
  26. * @subsection bx_system_general-chart Chart
  27. * @subsubsection bx_system_general-get_chart_growth get_chart_growth
  28. *
  29. * @code bx_srv('system', 'get_chart_growth', [], 'TemplChartServices'); @endcode
  30. * @code {{~system:get_chart_growth:TemplChartServices~}} @endcode
  31. *
  32. * Get growth charts.
  33. *
  34. * @see BxBaseChartServices::serviceProfileMembership
  35. */
  36. /**
  37. * @ref bx_system_general-get_chart_growth "get_chart_growth"
  38. */
  39. public function serviceGetChartGrowth()
  40. {
  41. $mixedResult = BxDolService::call('system', 'check_allowed_view', array(), 'TemplChartServices');
  42. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  43. return '';
  44. $sDateFrom = date('Y-m-d', time() - 30*24*60*60);
  45. $sDateTo = date('Y-m-d', time());
  46. $aForm = array(
  47. 'form_attrs' => array(
  48. 'id' => 'bx_chart_controls',
  49. 'action' => ''
  50. ),
  51. 'inputs' => array (
  52. 'object' => array(
  53. 'type' => 'select',
  54. 'name' => 'object',
  55. 'caption' => _t('_sys_chart_growth_object'),
  56. 'info' => '',
  57. 'value' => '',
  58. 'values' => array(),
  59. 'required' => '0',
  60. 'attrs' => array(
  61. 'id' => 'bx_chart_growth_objects',
  62. 'onchange' => 'oBxDolChartGrowth.loadData()'
  63. ),
  64. ),
  65. 'date_from' => array(
  66. 'type' => 'datepicker',
  67. 'name' => 'date_from',
  68. 'caption' => _t('_sys_chart_growth_date_from'),
  69. 'info' => '',
  70. 'value' => $sDateFrom,
  71. 'values' => array(),
  72. 'required' => '0',
  73. 'attrs' => array(
  74. 'id' => 'bx_chart_growth_date_from',
  75. 'onchange' => 'oBxDolChartGrowth.loadData()'
  76. ),
  77. ),
  78. 'date_to' => array(
  79. 'type' => 'datepicker',
  80. 'name' => 'date_to',
  81. 'caption' => _t('_sys_chart_growth_date_to'),
  82. 'info' => '',
  83. 'value' => $sDateTo,
  84. 'values' => array(),
  85. 'required' => '0',
  86. 'attrs' => array(
  87. 'id' => 'bx_chart_growth_date_to',
  88. 'onchange' => 'oBxDolChartGrowth.loadData()'
  89. ),
  90. )
  91. )
  92. );
  93. $aObjects = BxDolChartQuery::getChartObjects();
  94. foreach($aObjects as $aObject)
  95. $aForm['inputs']['object']['values'][] = array('key' => $aObject['object'], 'value' => _t($aObject['title']));
  96. $oForm = new BxTemplFormView($aForm);
  97. $oTemplate = BxDolTemplate::getInstance();
  98. $oTemplate->addJs(array('chart.min.js', 'BxDolChartGrowth.js'));
  99. $oTemplate->addCss(array('chart.css'));
  100. return $oTemplate->parseHtmlByName('chart_growth.html', [
  101. 'date_from' => $sDateFrom,
  102. 'date_to' => $sDateTo,
  103. 'request_params' => json_encode([]),
  104. 'controls' => $oForm->getCode()
  105. ]);
  106. }
  107. /**
  108. * @page service Service Calls
  109. * @section bx_system_general System Services
  110. * @subsection bx_system_general-chart Chart
  111. * @subsubsection bx_system_general-get_chart_stats get_chart_stats
  112. *
  113. * @code bx_srv('system', 'get_chart_stats', [], 'TemplChartServices'); @endcode
  114. * @code {{~system:get_chart_stats:TemplChartServices~}} @endcode
  115. *
  116. * Get statistics chart.
  117. *
  118. * @see BxBaseChartServices::serviceGetChartStats
  119. */
  120. /**
  121. * @ref bx_system_general-get_chart_stats "get_chart_stats"
  122. */
  123. public function serviceGetChartStats()
  124. {
  125. $mixedResult = BxDolService::call('system', 'check_allowed_view', array(), 'TemplChartServices');
  126. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  127. return '';
  128. $aTmplVarsItems = array();
  129. $aTmplVarsDataLabels = $aTmplVarsDataSet = array();
  130. $oDb = BxDolDb::getInstance();
  131. $sQuery = "SELECT * FROM `sys_statistics` WHERE 1 ORDER BY `order`";
  132. $aItems = $oDb->getAll($sQuery);
  133. foreach($aItems as $aItem) {
  134. $sTitleAttr = bx_html_attribute(_t($aItem['title']));
  135. $iValue = 0;
  136. if(!empty($aItem['query']))
  137. $iValue = (int)$oDb->getOne($aItem['query']);
  138. else if(BxDolRequest::serviceExists($aItem['module'], 'get_query_statistics'))
  139. $iValue = (int)BxDolService::call($aItem['module'], 'get_query_statistics', array($aItem));
  140. $aTmplVarsDataLabels[] = $sTitleAttr . ' - ' . $iValue;
  141. $aTmplVarsDataSet['data'][] = $iValue;
  142. $aTmplVarsDataSet['backgroundColor'][] = '#' . dechex(rand(0x000000, 0xFFFFFF));
  143. }
  144. $oTemplate = BxDolTemplate::getInstance();
  145. $oTemplate->addJs(array('chart.min.js'));
  146. $oTemplate->addCss(array('chart.css'));
  147. return $oTemplate->parseHtmlByName('chart_stats.html', array(
  148. 'chart_id' => 'bx_chart_stats_graph',
  149. 'chart_data' => json_encode(array(
  150. 'labels' => $aTmplVarsDataLabels,
  151. 'datasets' => array($aTmplVarsDataSet)
  152. ))
  153. ));
  154. }
  155. public function serviceGetDataByInterval($sObject, $iFrom, $iTo, $iContentId = false)
  156. {
  157. $oChart = BxDolChart::getObjectInstance($sObject);
  158. if($oChart === false)
  159. return [];
  160. if($iContentId){
  161. $oChart->addMarkers([
  162. 'content_id' => $iContentId
  163. ]);
  164. }
  165. return $oChart->getDataByInterval($oChart->getTimestamp($iFrom), $oChart->getTimestamp($iTo));
  166. }
  167. }
  168. /** @} */